Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions crates/processing_wasm/examples/background_image.js
Original file line number Diff line number Diff line change
@@ -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);
}
64 changes: 18 additions & 46 deletions crates/processing_wasm/examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,28 @@
<meta charset="utf-8">
<title>Processing</title>
<style>
body {
margin: 0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: #1a1a1a;
}
canvas {
border: 1px solid #333;
}
body { margin: 0; min-height: 100vh; background: #1a1a1a; color: #ccc;
display: flex; flex-direction: column; align-items: center; gap: 12px; padding-top: 12px; }
canvas { border: 1px solid #333; }
</style>
</head>
<body>
<canvas id="canvas" width="600" height="600"></canvas>
<select id="example">
<option>background_image</option>
<option>translate</option>
<option>mouse_2d</option>
</select>
<canvas></canvas>
<script type="module">
import init, * as p from '../../../target/wasm/processing_wasm.js';

async function main() {
await init();
await p.init();
const surface = p.createSurface('canvas', 600, 600);

const bgImage = await p.loadImage('images/logo.png');

let frame = 0;

function draw() {
try {
p.beginDraw(surface);
p.backgroundImage(surface, 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.endDraw(surface);

frame++;
} catch (e) {
console.error('Draw error:', e);
}

requestAnimationFrame(draw);
}

draw();
}

main().catch(console.error);
const select = document.getElementById('example');
select.value = new URLSearchParams(location.search).get('example') ?? select.options[0].value;
select.onchange = () => (location.search = `?example=${select.value}`);

// the sketch is a plain script that defines setup() and draw()
const sketch = document.createElement('script');
sketch.src = `${select.value}.js`;
sketch.onload = () => import('./processing.js');
document.head.append(sketch);
</script>
</body>
</html>
18 changes: 18 additions & 0 deletions crates/processing_wasm/examples/mouse_2d.js
Original file line number Diff line number Diff line change
@@ -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);
}
97 changes: 97 additions & 0 deletions crates/processing_wasm/examples/processing.js
Original file line number Diff line number Diff line change
@@ -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();
34 changes: 34 additions & 0 deletions crates/processing_wasm/examples/translate.js
Original file line number Diff line number Diff line change
@@ -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);
}
Loading