Hi
friends!
I am pretty new to functional programming so please be kind ❤️
I've been playing around with p5.js and the Javascript interop that Gleam provides with @external and I've managed to get some pretty cool things running with an API that looks (probably incorrectly) very similar to the p5.js API:
pub fn gleam_draw(sketch: p5.P5) -> p5.P5 {
let y = 205
let setup_sketch = fn(sketch: p5.P5) -> p5.P5 {
p5.create_canvas(sketch, 700, 410)
p5.framerate(sketch, 10.0)
}
let draw_sketch = fn(sketch: p5.P5) -> p5.P5 {
sketch
|> p5.background(150)
|> p5.ellipse(500, y, 50, 50)
}
sketch
|> p5.setup(setup_sketch)
|> p5.draw(draw_sketch)
}
However, I'm really having a hard time when it comes to animations. In p5.js you would typically use mutable values for things such as y like in the example above. As an example you might have something that looks like:
pub fn gleam_draw(sketch: p5.P5) -> p5.P5 {
let y = 205
...
let draw_sketch = fn(sketch: p5.P5) -> p5.P5 {
y += 1 // invalid, because no mutability
sketch
|> p5.background(150)
|> p5.ellipse(500, y, 50, 50)
}
...
}
Where I am really struggling is trying to figure out how I can get p5.js to call the draw_sketch function as it would per frame. While keeping track of a changing value (such as y). Since I don't control the actual "loop" (because it comes from the js lib) is there a way I could implement this in pure Gleam?
Many thanks, ❤️