Skip to content

Commit

Permalink
docs and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Acepie committed Mar 21, 2024
1 parent 1b844de commit 9e2d921
Show file tree
Hide file tree
Showing 26 changed files with 2,270 additions and 243 deletions.
Binary file added .DS_Store
Binary file not shown.
70 changes: 53 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,77 @@
# p5js_gleam

A demo showcasing how to create and use bindings for [p5js](https://p5js.org/)
A simple game library providing [p5js](https://p5js.org/) bindings for Gleam in a functional style to make basic games and animations. Heavily inspired by the Racket library 2htdp/universe.

This project is made up of
This project is primarily made up of

- A [bun](https://bun.sh/) script that is used to generate bindings for p5js class methods
- Script is based on script created for [glare](https://github.com/Enderchief/glare/tree/master/scripts)
- An html page that shows how to use those bindings to make a basic animation
- A wrapper game engine that provides a functional interface to p5js

## Generating FFI Bindings
## Using the library

To generate the FFI bindings you will need bun installed. Once you have bun installed run
Add the library to your project by running the following command

```bash
bun run ./scripts/generate_p5.ts
gleam add p5js_gleam
```

### Bundling the app
Then add a call to the `start_sketch` function in your main function.

This example uses [esgleam](https://hexdocs.pm/esgleam/) to bundle the main gleam module for use in a static site.
```gleam
import p5js_gleam.{type P5, SketchConfig}
import p5js_gleam/bindings as p5
import gleam/option
To install esbuild run the following command. You should only need to run it once.
fn setup(p: P5) -> String {
p5.create_canvas(p, 800.0, 600.0)
"Hello, world!"
}
```bash
gleam run -m esgleam/bundle
fn draw(p: P5, state: String) -> Nil {
p5.background(p, "#ffffff")
p5.fill(p, "#000000")
p5.text(p, state, 400.0, 300.0)
Nil
}
pub fn main() {
p5.start_sketch(SketchConfig(
init: setup,
draw: draw,
on_tick: option.None,
on_key: option.None,
on_mouse: option.None,
))
}
```

To bundle the app run the following command after making code changes.
Afterwards you will need to build your project and include it in an html file. The html file must import p5js as a module before the generated javascript file. The easiest way to do this is to load the p5js library from a CDN.

```bash
gleam run -m esgleam/bundle
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Ball Spawner</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/p5.js"></script>
<script type="module">
import { main } from "./ball_spawner.js";
main();
</script>
</head>
<body></body>
</html>
```

### Serving the app
To see the library in action you can serve one of the examples in the `examples` directory.

You can use any static site server to host the app but for development you can continue to use esgleam by running
## Generating FFI Bindings

To generate the FFI bindings you will need bun installed. Once you have bun installed run

```bash
gleam run -m esgleam/serve
bun run ./scripts/generate_p5.ts
```
4 changes: 4 additions & 0 deletions examples/ball_spawner/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.beam
*.ez
/build
erl_crash.dump
27 changes: 27 additions & 0 deletions examples/ball_spawner/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# ball_spawner

Simple example of using the library to create a ball spawner. Balls are spawned on click at the clicked location with a random direction and color.

### Bundling the app

This example uses [esgleam](https://hexdocs.pm/esgleam/) to bundle the main gleam module for use in a static site.

To install esbuild run the following command. You should only need to run it once.

```bash
gleam run -m esgleam/bundle
```

To bundle the app run the following command after making code changes.

```bash
gleam run -m esgleam/bundle
```

### Serving the app

You can use any static site server to host the app but for development you can continue to use esgleam by running

```bash
gleam run -m esgleam/serve
```
Loading

0 comments on commit 9e2d921

Please sign in to comment.