@codaloop/runtime
v0.1.3
Published
A simplified game programming API for kids — Canvas drawing, sprites, sound, and a 60fps game loop. Think Processing/p5.js but simpler.
Maintainers
Readme
@codaloop/runtime
A simplified game programming API for kids — Canvas drawing, sprites, sound, and a 60fps game loop. Think Processing/p5.js but simpler.
Built for Codaloop, a zero-friction browser-based game programming environment for classrooms.
Install
# npm
npm install @codaloop/runtime
# Deno / JSR
deno add jsr:@codaloop/runtimeQuick Start
import { createRuntime } from "@codaloop/runtime";
const runtime = createRuntime();
// Game loop
runtime.start({
setup() {
size(400, 400);
},
draw() {
background("skyblue");
fill("yellow");
circle(mouseX, mouseY, 30);
},
keyPressed() {
if (key === " ") playSound("jump");
},
});Browser (IIFE)
The browser bundle reads user code from a __CODALOOP_CODE__ global, which is
useful for sandboxed iframe environments:
<script src="codaloop.js"></script>
<script>
window.__CODALOOP_CODE__ = `
function setup() { size(400, 400); }
function draw() {
background("white");
fill("red");
circle(mouseX, mouseY, 30);
}
`;
</script>API Overview
Lifecycle
size(w, h)— Set canvas sizebackground(color)— Clear canvas with a colornoLoop()/loop()— Stop/restart the draw loop
Shapes
rect(x, y, w, h)— Rectanglecircle(x, y, r)— Circleellipse(x, y, w, h)— Ellipseline(x1, y1, x2, y2)— Linetriangle(x1, y1, x2, y2, x3, y3)— Trianglearc(x, y, r, startAngle, stopAngle)— Arc (degrees)text(str, x, y)— Text
Style
fill(color)/noFill()— Fill colorstroke(color)/noStroke()— Outline colorstrokeWeight(n)— Outline thicknesstextSize(n)/textAlign(align)— Text stylingcolor(r, g, b, a?)— Create RGBA colorpush()/pop()— Save/restore drawing state + transformstranslate(x, y)/rotate(angle)— Transform (degrees)
Input
mouseX,mouseY— Mouse positionmouseIsPressed— Is mouse down?key,keyIsPressed— Last key pressedkeyIsDown(k)— Is a specific key held?keyPressed()/keyReleased()— Callbacks (define as globals)
Sprites
createSprite(x, y, w, h)— Create a spritedrawSprite(s)/moveSprite(s)— Draw/movecollides(a, b)— AABB collision between two spritesoverlap(s, x, y)— Check if a point is inside a spriteapplyGravity(s, amount?)/bounceEdges(s)— Physics helpers
Pixel Art
createImage(rows, colorMap)— Create from string art (.or space = transparent)drawImage(img, x, y, scale?)— Draw pixel imagegetImage(name, colors?)— Get built-in sprite
const ship = createImage(
[
"..R..",
".RRR.",
"RRRRR",
],
{ R: "red" }
);
drawImage(ship, 100, 100, 3);Sound
playSound(name)—"beep","coin","jump","hit","explosion","powerup","laser","pop"playNote(note, duration, volume?)— Play a musical note ("C4","F#5", etc.)playDrum(name)—"kick","snare","hihat"
Math
random(max)/random(min, max)— Random floatrandomInt(max)/randomInt(min, max)— Random integerdist(x1, y1, x2, y2)— Distanceconstrain(val, lo, hi)— Clampmap(val, fromLo, fromHi, toLo, toHi)— Remaplerp(a, b, t)— Linear interpolation
Developer Tools
gridHelper(opts?)— Overlay a coordinate grid with a pinnable crosshair
Building
deno task build # IIFE browser bundle → codaloop.js
deno task build:npm # npm package → npm/
deno task publish:jsr # publish to JSRLicense
MIT
