From 682866a7506a783e44b99908ca729841f388d5cc Mon Sep 17 00:00:00 2001 From: Nitish C <86357181+Nitish-bot@users.noreply.github.com> Date: Tue, 17 Nov 2026 02:45:58 +0530 Subject: [PATCH 1/8] fix wasm example --- crates/processing_wasm/examples/index.html | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/crates/processing_wasm/examples/index.html b/crates/processing_wasm/examples/index.html index 20bbd703..62aed17b 100644 --- a/crates/processing_wasm/examples/index.html +++ b/crates/processing_wasm/examples/index.html @@ -25,22 +25,23 @@ async function main() { await init(); await p.init(); - const surface = p.createSurface('canvas', 600, 600); + const surface = p.surfaceCreateFromCanvas('canvas', 600, 600); + const graphics = p.graphicsCreate(surface, 600, 600); - const bgImage = await p.loadImage('images/logo.png'); + const bgImage = await p.imageLoad('images/logo.png'); let frame = 0; function draw() { try { - p.beginDraw(surface); - p.backgroundImage(surface, bgImage); + p.beginDraw(graphics); + p.backgroundImage(graphics, bgImage); const x = 250 + Math.sin(frame * 0.02) * 200; - p.fill(surface, 0.9, 0.1, 0.3, 1.0); - p.rect(surface, x, 50, 100, 100, 20, 20, 20, 20); + p.setFill(graphics, 0.9, 0.1, 0.3, 1.0, 0 /* sRGB */); + p.rect(graphics, x, 50, 100, 100, 20, 20, 20, 20); - p.endDraw(surface); + p.endDraw(graphics); frame++; } catch (e) { From adfeffdb37dc16b9130e3528b9acb7b28531c184 Mon Sep 17 00:00:00 2001 From: Nitish C <86357181+Nitish-bot@users.noreply.github.com> Date: Tue, 17 Nov 2026 02:49:32 +0530 Subject: [PATCH 2/8] delete comment --- crates/processing_wasm/examples/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/processing_wasm/examples/index.html b/crates/processing_wasm/examples/index.html index 62aed17b..1a879e66 100644 --- a/crates/processing_wasm/examples/index.html +++ b/crates/processing_wasm/examples/index.html @@ -38,7 +38,7 @@ p.backgroundImage(graphics, bgImage); const x = 250 + Math.sin(frame * 0.02) * 200; - p.setFill(graphics, 0.9, 0.1, 0.3, 1.0, 0 /* sRGB */); + p.setFill(graphics, 0.9, 0.1, 0.3, 1.0, 0); p.rect(graphics, x, 50, 100, 100, 20, 20, 20, 20); p.endDraw(graphics); From 539243fdaeb9a3b19b8f70267f3be0c6fa13bcb0 Mon Sep 17 00:00:00 2001 From: Nitish C <86357181+Nitish-bot@users.noreply.github.com> Date: Tue, 17 Nov 2026 15:33:25 +0530 Subject: [PATCH 3/8] add examples --- .../examples/background_image.js | 18 ++++ crates/processing_wasm/examples/index.html | 65 ++++--------- crates/processing_wasm/examples/mouse_2d.js | 18 ++++ crates/processing_wasm/examples/processing.js | 97 +++++++++++++++++++ crates/processing_wasm/examples/translate.js | 34 +++++++ 5 files changed, 185 insertions(+), 47 deletions(-) create mode 100644 crates/processing_wasm/examples/background_image.js create mode 100644 crates/processing_wasm/examples/mouse_2d.js create mode 100644 crates/processing_wasm/examples/processing.js create mode 100644 crates/processing_wasm/examples/translate.js diff --git a/crates/processing_wasm/examples/background_image.js b/crates/processing_wasm/examples/background_image.js new file mode 100644 index 00000000..dc233f21 --- /dev/null +++ b/crates/processing_wasm/examples/background_image.js @@ -0,0 +1,18 @@ +// Background Image. +// +// Loads an image, draws it as the background and animates a rounded +// rectangle over it. +let logo; + +async function setup() { + size(600, 600); + logo = await loadImage('images/logo.png'); +} + +function draw() { + background(logo); + + const x = 250 + Math.sin(frameCount * 0.02) * 200; + fill(0.9, 0.1, 0.3); + rect(x, 50, 100, 100, 20); +} diff --git a/crates/processing_wasm/examples/index.html b/crates/processing_wasm/examples/index.html index 1a879e66..a5ff5bf9 100644 --- a/crates/processing_wasm/examples/index.html +++ b/crates/processing_wasm/examples/index.html @@ -4,57 +4,28 @@ Processing - + + diff --git a/crates/processing_wasm/examples/mouse_2d.js b/crates/processing_wasm/examples/mouse_2d.js new file mode 100644 index 00000000..983a4bcd --- /dev/null +++ b/crates/processing_wasm/examples/mouse_2d.js @@ -0,0 +1,18 @@ +// Mouse 2D. +// +// Moving the mouse changes the position and size of each box. +function setup() { + size(640, 360); + noStroke(); + rectMode(CENTER); +} + +function draw() { + background(0.2); + fill(1.0, 0.8); + rect(mouseX, height / 2, mouseY / 2 + 10, mouseY / 2 + 10); + fill(1.0, 0.8); + const inverseX = width - mouseX; + const inverseY = height - mouseY; + rect(inverseX, height / 2, inverseY / 2 + 10, inverseY / 2 + 10); +} diff --git a/crates/processing_wasm/examples/processing.js b/crates/processing_wasm/examples/processing.js new file mode 100644 index 00000000..6cf5b08b --- /dev/null +++ b/crates/processing_wasm/examples/processing.js @@ -0,0 +1,97 @@ +// A thin Processing-style layer over the raw wasm bindings: global drawing +// functions that act on a default canvas, and a setup()/draw() loop. The +// surface and graphics ids stay internal. +import initWasm, * as p from '../../../target/wasm/processing_wasm.js'; + +let surface, graphics; +let frameCount = 0; +let maxes = [1, 1, 1, 1]; // channel maximums, mirrors colorMode() +const SRGB = 0; + +// color args -> [c1, c2, c3, a], following Processing's 1/2/3/4 arg forms +function color(args) { + const [a, b, c, d] = args; + switch (args.length) { + case 1: return [a, a, a, maxes[3]]; + case 2: return [a, a, a, b]; + case 3: return [a, b, c, maxes[3]]; + case 4: return [a, b, c, d]; + default: throw new TypeError('expected 1 to 4 color arguments'); + } +} + +const api = { + size(width, height) { + const canvas = document.querySelector('canvas'); + canvas.id ||= 'processing-canvas'; + canvas.width = width; + canvas.height = height; + surface = p.surfaceCreateFromCanvas(canvas.id, width, height); + graphics = p.graphicsCreate(surface, width, height); + canvas.addEventListener('mousemove', (e) => p.inputMouseMove(surface, e.offsetX, e.offsetY)); + canvas.addEventListener('mousedown', (e) => p.inputMouseButton(surface, e.button, true)); + canvas.addEventListener('mouseup', (e) => p.inputMouseButton(surface, e.button, false)); + Object.defineProperties(globalThis, { + width: { value: width, configurable: true }, + height: { value: height, configurable: true }, + }); + }, + loadImage: (path) => p.imageLoad(path), + + colorMode(space, ...max) { + maxes = [max[0] ?? 1, max[1] ?? max[0] ?? 1, max[2] ?? max[0] ?? 1, max[3] ?? max[0] ?? 1]; + p.colorMode(graphics, space, ...maxes); + }, + // background(image) or background(...color) + background: (...args) => + typeof args[0] === 'bigint' + ? p.backgroundImage(graphics, args[0]) + : p.backgroundColor(graphics, ...color(args), SRGB), + fill: (...args) => p.setFill(graphics, ...color(args), SRGB), + stroke: (...args) => p.setStrokeColor(graphics, ...color(args), SRGB), + noFill: () => p.noFill(graphics), + noStroke: () => p.noStroke(graphics), + strokeWeight: (w) => p.setStrokeWeight(graphics, w), + rectMode: (mode) => p.rectMode(graphics, mode), + + push: () => p.pushMatrix(graphics), + pop: () => p.popMatrix(graphics), + translate: (x, y) => p.translate(graphics, x, y), + rotate: (angle) => p.rotate(graphics, angle), + scale: (x, y = x) => p.scale(graphics, x, y), + + // rect(x, y, w, h), rect(x, y, w, h, r) or rect(x, y, w, h, tl, tr, br, bl) + rect(x, y, w, h, ...r) { + const radii = r.length === 1 ? [r[0], r[0], r[0], r[0]] : r.length === 4 ? r : [0, 0, 0, 0]; + p.rect(graphics, x, y, w, h, ...radii); + }, + ellipse: (x, y, w, h) => p.ellipse(graphics, x, y, w, h), + circle: (x, y, d) => p.circle(graphics, x, y, d), + line: (x1, y1, x2, y2) => p.line(graphics, x1, y1, x2, y2), + triangle: (...v) => p.triangle(graphics, ...v), + point: (x, y) => p.point(graphics, x, y), +}; + +Object.assign(globalThis, api, { CORNER: 0, CORNERS: 1, CENTER: 2, RADIUS: 3 }); +Object.defineProperties(globalThis, { + frameCount: { get: () => frameCount, configurable: true }, + mouseX: { get: () => p.mouseX(surface), configurable: true }, + mouseY: { get: () => p.mouseY(surface), configurable: true }, +}); + +function frame() { + p.inputFlush(); + p.beginDraw(graphics); + try { + globalThis.draw?.(); + } finally { + p.endDraw(graphics); + } + frameCount++; + requestAnimationFrame(frame); +} + +await initWasm(); +await p.init(); +await globalThis.setup?.(); +frame(); diff --git a/crates/processing_wasm/examples/translate.js b/crates/processing_wasm/examples/translate.js new file mode 100644 index 00000000..3589d0a9 --- /dev/null +++ b/crates/processing_wasm/examples/translate.js @@ -0,0 +1,34 @@ +// Translate. +// +// The translate() function allows objects to be moved +// to any location within the window. The first parameter +// sets the x-axis offset and the second parameter sets the +// y-axis offset. +let x = 0; +const dim = 80.0; + +function setup() { + size(640, 360); + noStroke(); +} + +function draw() { + background(0.4); + + x = x + 0.8; + + if (x > width + dim) { + x = -dim; + } + + translate(x, height / 2 - dim / 2); + fill(1.0); + rect(-dim / 2, -dim / 2, dim, dim); + + // Transforms accumulate. Notice how this rect moves + // twice as fast as the other, but it has the same + // parameter for the x-axis value + translate(x, dim); + fill(0.0); + rect(-dim / 2, -dim / 2, dim, dim); +} From 1f167f57fdd5ded03c49bd0f20d23cac7946f840 Mon Sep 17 00:00:00 2001 From: Nitish C <86357181+Nitish-bot@users.noreply.github.com> Date: Wed, 18 Nov 2026 23:27:56 +0530 Subject: [PATCH 4/8] add tick, step and looping --- crates/processing_wasm/examples/processing.js | 36 +++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/crates/processing_wasm/examples/processing.js b/crates/processing_wasm/examples/processing.js index 6cf5b08b..bcc719c6 100644 --- a/crates/processing_wasm/examples/processing.js +++ b/crates/processing_wasm/examples/processing.js @@ -79,7 +79,17 @@ Object.defineProperties(globalThis, { mouseY: { get: () => p.mouseY(surface), configurable: true }, }); -function frame() { +// The wasm side has no loop of its own, so we drive frames from JS. Loop state +// mirrors the pyo3 host: noLoop()/loop()/redraw(), plus frameRate() since +// requestAnimationFrame follows the display (60, 120, 144 Hz...). +let looping = true; +let redrawRequested = true; // draw once even if setup() calls noLoop() +let frameInterval = 1000 / 60; +let lastFrame = -Infinity; +let handle; + +// one frame: flush input, then begin -> draw -> end +function step() { p.inputFlush(); p.beginDraw(graphics); try { @@ -88,10 +98,30 @@ function frame() { p.endDraw(graphics); } frameCount++; - requestAnimationFrame(frame); } +function tick(now) { + // schedule first so nothing below can silently end the loop + handle = requestAnimationFrame(tick); + if (!(looping || redrawRequested) || now - lastFrame < frameInterval - 1) return; + lastFrame = now; + redrawRequested = false; + try { + step(); + } catch (e) { + cancelAnimationFrame(handle); // stop on error, like Processing + throw e; + } +} + +Object.assign(globalThis, { + noLoop: () => { looping = false; }, + loop: () => { looping = true; }, + redraw: () => { redrawRequested = true; }, + frameRate: (fps) => { frameInterval = 1000 / fps; }, +}); + await initWasm(); await p.init(); await globalThis.setup?.(); -frame(); +handle = requestAnimationFrame(tick); From 2d6937e5dd59f6dd5866a802777c0db2808a5625 Mon Sep 17 00:00:00 2001 From: Nitish C <86357181+Nitish-bot@users.noreply.github.com> Date: Wed, 18 Nov 2026 23:43:03 +0530 Subject: [PATCH 5/8] tighten the api layer --- crates/processing_wasm/examples/index.html | 7 ++- crates/processing_wasm/examples/processing.js | 61 +++++++++++++++---- crates/processing_wasm/src/lib.rs | 18 ++++++ 3 files changed, 74 insertions(+), 12 deletions(-) diff --git a/crates/processing_wasm/examples/index.html b/crates/processing_wasm/examples/index.html index a5ff5bf9..3fe56c35 100644 --- a/crates/processing_wasm/examples/index.html +++ b/crates/processing_wasm/examples/index.html @@ -18,13 +18,18 @@ diff --git a/crates/processing_wasm/examples/processing.js b/crates/processing_wasm/examples/processing.js index bcc719c6..2c88fd07 100644 --- a/crates/processing_wasm/examples/processing.js +++ b/crates/processing_wasm/examples/processing.js @@ -20,8 +20,17 @@ function color(args) { } } +// color args -> the trailing (c1, c2, c3, a, space) params of the color bindings +const rgba = (args) => [...color(args), SRGB]; + +// the DOM also reports back/forward (3, 4), which Processing has no names for +const isProcessingButton = (button) => button <= 2; + const api = { size(width, height) { + // Processing only allows one size() call; a second would leak the old + // surface and double up the mouse listeners + if (surface) throw new Error('size() can only be called once'); const canvas = document.querySelector('canvas'); canvas.id ||= 'processing-canvas'; canvas.width = width; @@ -29,8 +38,12 @@ const api = { surface = p.surfaceCreateFromCanvas(canvas.id, width, height); graphics = p.graphicsCreate(surface, width, height); canvas.addEventListener('mousemove', (e) => p.inputMouseMove(surface, e.offsetX, e.offsetY)); - canvas.addEventListener('mousedown', (e) => p.inputMouseButton(surface, e.button, true)); - canvas.addEventListener('mouseup', (e) => p.inputMouseButton(surface, e.button, false)); + canvas.addEventListener('mousedown', (e) => { + if (isProcessingButton(e.button)) p.inputMouseButton(surface, e.button, true); + }); + canvas.addEventListener('mouseup', (e) => { + if (isProcessingButton(e.button)) p.inputMouseButton(surface, e.button, false); + }); Object.defineProperties(globalThis, { width: { value: width, configurable: true }, height: { value: height, configurable: true }, @@ -38,24 +51,42 @@ const api = { }, loadImage: (path) => p.imageLoad(path), + // colorMode(space) keeps the current maximums + // colorMode(space, max) sets all four + // colorMode(space, m1, m2, m3) sets the color channels, alpha max unchanged + // colorMode(space, m1, m2, m3, mA) colorMode(space, ...max) { - maxes = [max[0] ?? 1, max[1] ?? max[0] ?? 1, max[2] ?? max[0] ?? 1, max[3] ?? max[0] ?? 1]; + switch (max.length) { + case 0: break; + case 1: maxes = [max[0], max[0], max[0], max[0]]; break; + case 3: maxes = [max[0], max[1], max[2], maxes[3]]; break; + case 4: maxes = [...max]; break; + default: throw new TypeError('colorMode() takes 0, 1, 3 or 4 maximums'); + } p.colorMode(graphics, space, ...maxes); }, // background(image) or background(...color) background: (...args) => typeof args[0] === 'bigint' ? p.backgroundImage(graphics, args[0]) - : p.backgroundColor(graphics, ...color(args), SRGB), - fill: (...args) => p.setFill(graphics, ...color(args), SRGB), - stroke: (...args) => p.setStrokeColor(graphics, ...color(args), SRGB), + : p.backgroundColor(graphics, ...rgba(args)), + fill: (...args) => p.setFill(graphics, ...rgba(args)), + stroke: (...args) => p.setStrokeColor(graphics, ...rgba(args)), noFill: () => p.noFill(graphics), noStroke: () => p.noStroke(graphics), strokeWeight: (w) => p.setStrokeWeight(graphics, w), rectMode: (mode) => p.rectMode(graphics, mode), - push: () => p.pushMatrix(graphics), - pop: () => p.popMatrix(graphics), + // like Processing, push()/pop() save and restore style (fill, stroke, ...) + // as well as the transform + push() { + p.pushStyle(graphics); + p.pushMatrix(graphics); + }, + pop() { + p.popStyle(graphics); + p.popMatrix(graphics); + }, translate: (x, y) => p.translate(graphics, x, y), rotate: (angle) => p.rotate(graphics, angle), scale: (x, y = x) => p.scale(graphics, x, y), @@ -75,8 +106,9 @@ const api = { Object.assign(globalThis, api, { CORNER: 0, CORNERS: 1, CENTER: 2, RADIUS: 3 }); Object.defineProperties(globalThis, { frameCount: { get: () => frameCount, configurable: true }, - mouseX: { get: () => p.mouseX(surface), configurable: true }, - mouseY: { get: () => p.mouseY(surface), configurable: true }, + // 0 until size() has created the surface + mouseX: { get: () => (surface ? p.mouseX(surface) : 0), configurable: true }, + mouseY: { get: () => (surface ? p.mouseY(surface) : 0), configurable: true }, }); // The wasm side has no loop of its own, so we drive frames from JS. Loop state @@ -104,7 +136,13 @@ function tick(now) { // schedule first so nothing below can silently end the loop handle = requestAnimationFrame(tick); if (!(looping || redrawRequested) || now - lastFrame < frameInterval - 1) return; - lastFrame = now; + // Advance by exactly one interval rather than snapping to `now`: frames land + // a little late on displays that aren't a multiple of the target rate + // (60 fps on 144 Hz), and snapping would throw that lateness away, settling + // at 48 fps. Resync only if we've fallen a whole interval behind (hidden + // tab, slow frame) so we don't burst to catch up. + lastFrame += frameInterval; + if (now - lastFrame > frameInterval) lastFrame = now; redrawRequested = false; try { step(); @@ -124,4 +162,5 @@ Object.assign(globalThis, { await initWasm(); await p.init(); await globalThis.setup?.(); +if (!graphics) api.size(100, 100); // Processing's default when setup() skips size() handle = requestAnimationFrame(tick); diff --git a/crates/processing_wasm/src/lib.rs b/crates/processing_wasm/src/lib.rs index 3153b783..dfa80d6d 100644 --- a/crates/processing_wasm/src/lib.rs +++ b/crates/processing_wasm/src/lib.rs @@ -282,6 +282,24 @@ pub fn js_no_stroke(graphics_id: u64) -> Result<(), JsValue> { )) } +#[wasm_bindgen(js_name = "pushStyle")] +pub fn js_push_style(graphics_id: u64) -> Result<(), JsValue> { + let graphics_entity = Entity::from_bits(graphics_id); + check(graphics_record_command( + graphics_entity, + DrawCommand::PushStyle, + )) +} + +#[wasm_bindgen(js_name = "popStyle")] +pub fn js_pop_style(graphics_id: u64) -> Result<(), JsValue> { + let graphics_entity = Entity::from_bits(graphics_id); + check(graphics_record_command( + graphics_entity, + DrawCommand::PopStyle, + )) +} + #[wasm_bindgen(js_name = "pushMatrix")] pub fn js_push_matrix(graphics_id: u64) -> Result<(), JsValue> { let graphics_entity = Entity::from_bits(graphics_id); From f21e27797eeea9d64b2ea2715b7badce7daa4fbd Mon Sep 17 00:00:00 2001 From: Nitish C <86357181+Nitish-bot@users.noreply.github.com> Date: Thu, 19 Nov 2026 00:03:00 +0530 Subject: [PATCH 6/8] cleanup comments --- crates/processing_wasm/examples/index.html | 5 +- crates/processing_wasm/examples/processing.js | 46 ++++++------------- 2 files changed, 14 insertions(+), 37 deletions(-) diff --git a/crates/processing_wasm/examples/index.html b/crates/processing_wasm/examples/index.html index 3fe56c35..559baa8a 100644 --- a/crates/processing_wasm/examples/index.html +++ b/crates/processing_wasm/examples/index.html @@ -18,14 +18,11 @@ diff --git a/crates/processing_wasm/examples/mouse_2d.js b/crates/processing_wasm/examples/mouse_2d.js deleted file mode 100644 index 983a4bcd..00000000 --- a/crates/processing_wasm/examples/mouse_2d.js +++ /dev/null @@ -1,18 +0,0 @@ -// Mouse 2D. -// -// Moving the mouse changes the position and size of each box. -function setup() { - size(640, 360); - noStroke(); - rectMode(CENTER); -} - -function draw() { - background(0.2); - fill(1.0, 0.8); - rect(mouseX, height / 2, mouseY / 2 + 10, mouseY / 2 + 10); - fill(1.0, 0.8); - const inverseX = width - mouseX; - const inverseY = height - mouseY; - rect(inverseX, height / 2, inverseY / 2 + 10, inverseY / 2 + 10); -} diff --git a/crates/processing_wasm/examples/processing.js b/crates/processing_wasm/examples/processing.js deleted file mode 100644 index 33996eca..00000000 --- a/crates/processing_wasm/examples/processing.js +++ /dev/null @@ -1,147 +0,0 @@ -// Processing-style globals over the wasm bindings. Sketches call rect(), -// fill() etc. directly and never see the surface or graphics ids. -import initWasm, * as p from '../../../target/wasm/processing_wasm.js'; - -let surface, graphics; -let frameCount = 0; -let maxes = [1, 1, 1, 1]; -const SRGB = 0; - -function color(args) { - const [a, b, c, d] = args; - switch (args.length) { - case 1: return [a, a, a, maxes[3]]; - case 2: return [a, a, a, b]; - case 3: return [a, b, c, maxes[3]]; - case 4: return [a, b, c, d]; - default: throw new TypeError('expected 1 to 4 color arguments'); - } -} - -const rgba = (args) => [...color(args), SRGB]; - -// browsers also report back/forward buttons, which libprocessing doesn't know -const isProcessingButton = (button) => button <= 2; - -const api = { - size(width, height) { - // a second call would leak the surface and double up the mouse listeners - if (surface) throw new Error('size() can only be called once'); - const canvas = document.querySelector('canvas'); - canvas.id ||= 'processing-canvas'; - canvas.width = width; - canvas.height = height; - surface = p.surfaceCreateFromCanvas(canvas.id, width, height); - graphics = p.graphicsCreate(surface, width, height); - canvas.addEventListener('mousemove', (e) => p.inputMouseMove(surface, e.offsetX, e.offsetY)); - canvas.addEventListener('mousedown', (e) => { - if (isProcessingButton(e.button)) p.inputMouseButton(surface, e.button, true); - }); - canvas.addEventListener('mouseup', (e) => { - if (isProcessingButton(e.button)) p.inputMouseButton(surface, e.button, false); - }); - Object.defineProperties(globalThis, { - width: { value: width, configurable: true }, - height: { value: height, configurable: true }, - }); - }, - loadImage: (path) => p.imageLoad(path), - - colorMode(space, ...max) { - switch (max.length) { - case 0: break; - case 1: maxes = [max[0], max[0], max[0], max[0]]; break; - case 3: maxes = [max[0], max[1], max[2], maxes[3]]; break; - case 4: maxes = [...max]; break; - default: throw new TypeError('colorMode() takes 0, 1, 3 or 4 maximums'); - } - p.colorMode(graphics, space, ...maxes); - }, - - // the user should ideally be exposed to an image object that wraps the bigint - background: (...args) => - typeof args[0] === 'bigint' - ? p.backgroundImage(graphics, args[0]) - : p.backgroundColor(graphics, ...rgba(args)), - fill: (...args) => p.setFill(graphics, ...rgba(args)), - stroke: (...args) => p.setStrokeColor(graphics, ...rgba(args)), - noFill: () => p.noFill(graphics), - noStroke: () => p.noStroke(graphics), - strokeWeight: (w) => p.setStrokeWeight(graphics, w), - rectMode: (mode) => p.rectMode(graphics, mode), - - push() { - p.pushStyle(graphics); - p.pushMatrix(graphics); - }, - pop() { - p.popStyle(graphics); - p.popMatrix(graphics); - }, - translate: (x, y) => p.translate(graphics, x, y), - rotate: (angle) => p.rotate(graphics, angle), - scale: (x, y = x) => p.scale(graphics, x, y), - - rect(x, y, w, h, ...r) { - const radii = r.length === 1 ? [r[0], r[0], r[0], r[0]] : r.length === 4 ? r : [0, 0, 0, 0]; - p.rect(graphics, x, y, w, h, ...radii); - }, - ellipse: (x, y, w, h) => p.ellipse(graphics, x, y, w, h), - circle: (x, y, d) => p.circle(graphics, x, y, d), - line: (x1, y1, x2, y2) => p.line(graphics, x1, y1, x2, y2), - triangle: (...v) => p.triangle(graphics, ...v), - point: (x, y) => p.point(graphics, x, y), -}; - -Object.assign(globalThis, api, { CORNER: 0, CORNERS: 1, CENTER: 2, RADIUS: 3 }); -Object.defineProperties(globalThis, { - frameCount: { get: () => frameCount, configurable: true }, - mouseX: { get: () => (surface ? p.mouseX(surface) : 0), configurable: true }, - mouseY: { get: () => (surface ? p.mouseY(surface) : 0), configurable: true }, -}); - -// The wasm side has no loop of its own, so we drive frames from JS. -// requestAnimationFrame follows the display's refresh rate, so frameRate() skips ticks. -let looping = true; -let redrawRequested = true; // so we still draw once if setup() calls noLoop() -let frameInterval = 1000 / 60; -let lastFrame = -Infinity; -let handle; - -function step() { - p.inputFlush(); - p.beginDraw(graphics); - try { - globalThis.draw?.(); - } finally { - p.endDraw(graphics); - } - frameCount++; -} - -function tick(now) { - handle = requestAnimationFrame(tick); - if (!(looping || redrawRequested) || now - lastFrame < frameInterval - 1) return; - lastFrame += frameInterval; - if (now - lastFrame > frameInterval) lastFrame = now; // if we're too far behind - redrawRequested = false; - try { - step(); - } catch (e) { - cancelAnimationFrame(handle); - throw e; - } -} - -Object.assign(globalThis, { - noLoop: () => { looping = false; }, - loop: () => { looping = true; }, - redraw: () => { redrawRequested = true; }, - frameRate: (fps) => { frameInterval = 1000 / fps; }, -}); - -await initWasm(); -await p.init(); -await globalThis.setup?.(); -if (!graphics) api.size(100, 100); -handle = requestAnimationFrame(tick); diff --git a/crates/processing_wasm/examples/translate.js b/crates/processing_wasm/examples/translate.js deleted file mode 100644 index 3589d0a9..00000000 --- a/crates/processing_wasm/examples/translate.js +++ /dev/null @@ -1,34 +0,0 @@ -// Translate. -// -// The translate() function allows objects to be moved -// to any location within the window. The first parameter -// sets the x-axis offset and the second parameter sets the -// y-axis offset. -let x = 0; -const dim = 80.0; - -function setup() { - size(640, 360); - noStroke(); -} - -function draw() { - background(0.4); - - x = x + 0.8; - - if (x > width + dim) { - x = -dim; - } - - translate(x, height / 2 - dim / 2); - fill(1.0); - rect(-dim / 2, -dim / 2, dim, dim); - - // Transforms accumulate. Notice how this rect moves - // twice as fast as the other, but it has the same - // parameter for the x-axis value - translate(x, dim); - fill(0.0); - rect(-dim / 2, -dim / 2, dim, dim); -}