-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added more bindings, on mouse, and on key
- Loading branch information
Showing
5 changed files
with
269 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,82 @@ | ||
import gleam/option.{type Option} | ||
|
||
pub type P5 | ||
|
||
pub type SketchConfig(model) { | ||
SketchConfig( | ||
init: fn(P5) -> model, | ||
draw: fn(P5, model) -> Nil, | ||
on_tick: Option(fn(model) -> model), | ||
on_key: Option(fn(String, model) -> model), | ||
on_mouse: Option(fn(Float, Float, model) -> model), | ||
) | ||
} | ||
|
||
@external(javascript, "../p5js_ffi.mjs", "startSketch") | ||
pub fn start_sketch( | ||
init: fn(P5) -> model, | ||
draw: fn(P5, model) -> Nil, | ||
on_tick: fn(model) -> model, | ||
) -> Nil | ||
pub fn start_sketch(config: SketchConfig(model)) -> Nil | ||
|
||
@external(javascript, "../p5js_ffi.mjs", "createCanvas") | ||
pub fn create_canvas(p: P5, width: Int, height: Int) -> Nil | ||
pub fn create_canvas(p: P5, width: Float, height: Float) -> P5 | ||
|
||
@external(javascript, "../p5js_ffi.mjs", "background") | ||
pub fn background(p: P5, gray_scale: Int) -> Nil | ||
pub fn background(p: P5, gray_scale: Int) -> P5 | ||
|
||
@external(javascript, "../p5js_ffi.mjs", "ellipse") | ||
pub fn ellipse( | ||
p: P5, | ||
x_center: Int, | ||
y_center: Int, | ||
width: Int, | ||
height: Int, | ||
) -> Nil | ||
x_center: Float, | ||
y_center: Float, | ||
width: Float, | ||
height: Float, | ||
) -> P5 | ||
|
||
@external(javascript, "../p5js_ffi.mjs", "circle") | ||
pub fn circle(p: P5, x_center: Float, y_center: Float, radius: Float) -> P5 | ||
|
||
@external(javascript, "../p5js_ffi.mjs", "rectangle") | ||
pub fn rectangle( | ||
p: P5, | ||
top_left_x: Float, | ||
top_left_y: Float, | ||
width: Float, | ||
height: Float, | ||
) -> P5 | ||
|
||
@external(javascript, "../p5js_ffi.mjs", "square") | ||
pub fn square( | ||
p: P5, | ||
top_left_x: Float, | ||
top_left_y: Float, | ||
side_length: Float, | ||
) -> P5 | ||
|
||
@external(javascript, "../p5js_ffi.mjs", "line") | ||
pub fn line( | ||
p: P5, | ||
point1_x: Float, | ||
point1_y: Float, | ||
point2_x: Float, | ||
point2_y: Float, | ||
) -> P5 | ||
|
||
@external(javascript, "../p5js_ffi.mjs", "quad") | ||
pub fn quad( | ||
p: P5, | ||
p1_x: Float, | ||
p1_y: Float, | ||
p2_x: Float, | ||
p2_y: Float, | ||
p3_x: Float, | ||
p3_y: Float, | ||
p4_x: Float, | ||
p4_y: Float, | ||
) -> P5 | ||
|
||
@external(javascript, "../p5js_ffi.mjs", "fill") | ||
pub fn fill(p: P5, color_hex: String) -> P5 | ||
|
||
@external(javascript, "../p5js_ffi.mjs", "stroke") | ||
pub fn stroke(p: P5, color_hex: String) -> P5 | ||
|
||
@external(javascript, "../p5js_ffi.mjs", "strokeWeight") | ||
pub fn stroke_weight(p: P5, weight: Int) -> P5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,85 @@ | ||
export const startSketch = (setup, draw, tick) => { | ||
import { is_some, unwrap } from "../gleam_stdlib/gleam/option.mjs"; | ||
|
||
export const startSketch = (config) => { | ||
let model; | ||
new p5(function (p) { | ||
p.setup = function () { | ||
model = setup(p); | ||
model = config.init(p); | ||
}; | ||
|
||
p.draw = function () { | ||
draw(p, model); | ||
model = tick(model); | ||
config.draw(p, model); | ||
if (is_some(config.on_tick)) { | ||
model = unwrap(config.on_tick)(model); | ||
} | ||
}; | ||
|
||
if (is_some(config.on_key)) { | ||
p.keyPressed = function () { | ||
model = unwrap(config.on_key)(p.key, model); | ||
} | ||
} | ||
|
||
if (is_some(config.on_mouse)) { | ||
p.mouseClicked = function () { | ||
model = unwrap(config.on_mouse)(p.pmouseX, p.pmouseY, model); | ||
} | ||
} | ||
}); | ||
}; | ||
|
||
export function createCanvas(p, ...args) { | ||
return p.createCanvas(...args); | ||
p.createCanvas(...args); | ||
return p; | ||
} | ||
|
||
export function background(p, ...args) { | ||
return p.background(...args); | ||
p.background(...args); | ||
return p; | ||
} | ||
|
||
export function ellipse(p, ...args) { | ||
return p.ellipse(...args); | ||
p.ellipse(...args); | ||
return p; | ||
} | ||
|
||
export function circle(p, ...args) { | ||
p.circle(...args); | ||
return p; | ||
} | ||
|
||
export function rectangle(p, ...args) { | ||
p.rectangle(...args); | ||
return p; | ||
} | ||
|
||
export function square(p, ...args) { | ||
p.square(...args); | ||
return p; | ||
} | ||
|
||
export function line(p, ...args) { | ||
p.line(...args); | ||
return p; | ||
} | ||
|
||
export function quad(p, ...args) { | ||
p.quad(...args); | ||
return p; | ||
} | ||
|
||
export function fill(p, ...args) { | ||
p.fill(...args); | ||
return p; | ||
} | ||
|
||
export function stroke(p, ...args) { | ||
p.stroke(...args); | ||
return p; | ||
} | ||
|
||
export function strokeWeight(p, ...args) { | ||
p.strokeWeight(...args); | ||
return p; | ||
} | ||
|
Oops, something went wrong.