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 20bbd703..a5ff5bf9 100644 --- a/crates/processing_wasm/examples/index.html +++ b/crates/processing_wasm/examples/index.html @@ -4,56 +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); +}