catapultDemo = {
const H = 400; // fixed height in px
const M = 20; // background overscan so screen shake never shows edges
const TAU = Math.PI * 2;
const G = 900; // gravity, px/s^2
const L = 88; // arm length (pivot to cup)
const A_REST = -95 * Math.PI / 180; // arm angle at rest (points up)
const PHI_MAX = 130 * Math.PI / 180; // max wind-up (radians back from rest)
const PHI_REL = 30 * Math.PI / 180; // fruit leaves the cup this far before the stop
const PHI_MIN = 0.07; // pulls smaller than this just snap back
const TR = 56; // target radius
const rnd = Math.random;
const clamp = (v, a, b) => Math.max(a, Math.min(b, v));
let W = 900;
let dpr = window.devicePixelRatio || 1;
// ---------- DOM ----------
const container = document.createElement("div");
container.style.cssText = `position:relative;width:100%;height:${H}px;border-radius:10px;overflow:hidden;
background:#cfe9ff;user-select:none;-webkit-user-select:none;`;
const canvas = document.createElement("canvas");
canvas.style.cssText = `width:100%;height:${H}px;display:block;touch-action:none;cursor:default;`;
container.append(canvas);
const ctx = canvas.getContext("2d");
const cleanBtn = document.createElement("button");
cleanBtn.textContent = "Clean up";
cleanBtn.style.cssText = `position:absolute;top:10px;right:10px;z-index:2;padding:5px 12px;font-size:13px;
border:1px solid #7b4f26;background:#fff8ee;color:#5a4328;border-radius:6px;cursor:pointer;`;
container.append(cleanBtn);
// ---------- Layout (set in resize) ----------
let groundY = H - 46, gl = groundY + 8;
let px = 180, py = gl - 132, bx = 55;
let tx = 700, ty = groundY - 100;
let x0 = 0, y0 = 0; // nominal launch point (for calibration)
let VMAX = 800, K = 19; // max launch speed, spring constant
let bgSky = null, bgFront = null, decals = null, dc = null;
// ---------- Fruit ----------
const FRUITS = [
{ name: "apple", r: 17, c0: "#ff7468", c1: "#a31515", juice: "#f6edc0", rim: "#d8c68a", pulp: "#fffbe6", skin: "#d32f2f", seed: "#4e2a14" },
{ name: "orange", r: 18, c0: "#ffc766", c1: "#e67700", juice: "#ffb22e", rim: "#e08200", pulp: "#ffd885", skin: "#f08c00", seed: "#fff3c9" },
{ name: "watermelon", r: 26, c0: "#69c46b", c1: "#2b7a35", juice: "#ff5577", rim: "#d42a52", pulp: "#ff8fa3", skin: "#2e8b3d", seed: "#1b1b1b" },
{ name: "lemon", r: 15, c0: "#fff59d", c1: "#f2b705", juice: "#fbea4a", rim: "#d7c11a", pulp: "#fff8b0", skin: "#f0d020", seed: "#fffbd0" },
{ name: "plum", r: 15, c0: "#b36ad0", c1: "#4a148c", juice: "#a63cc8", rim: "#6c1f8b", pulp: "#e3a8d9", skin: "#5b2a86", seed: "#3a2616" },
{ name: "strawberry", r: 14, c0: "#ff6b7f", c1: "#c3112f", juice: "#ff4a68", rim: "#c61c43", pulp: "#ffa0b0", skin: "#e0243b", seed: "#ffe38a" },
{ name: "kiwi", r: 16, c0: "#b58a5a", c1: "#6d4a28", juice: "#9ccf3a", rim: "#6a9b1c", pulp: "#cdea7c", skin: "#8a6a3c", seed: "#202020" }
];
const randFruit = () => FRUITS[(rnd() * FRUITS.length) | 0];
// ---------- State ----------
const arm = { phi: 0, w: 0, released: false, phiRel: 0, impact: 0 };
let phase = "idle"; // idle | drag | swing | reload
let dragging = false;
let cupFruit = randFruit(); // fruit sitting in the cup (or null)
let pile = [randFruit(), randFruit(), randFruit()];
let pop2 = 1;
let hop = null, reloadDelay = 0;
let flying = [], bits = [], drips = [], popups = [];
let clouds = [];
let score = 0, shots = 0, shake = 0, hint = 1, hintDone = false, pwHold = 0, pulse = 0;
// ---------- Arm physics (shared by animation and trajectory preview) ----------
const tipOf = phi => {
const a = A_REST - phi;
return { x: px + L * Math.cos(a), y: py + L * Math.sin(a), a };
};
function armStep(s, dt) {
let launch = null;
const c = s.released ? 3.0 : 0;
s.w += (-K * s.phi - c * s.w) * dt;
s.phi += s.w * dt;
if (!s.released && s.phi <= s.phiRel) {
s.released = true;
const a = A_REST - s.phi;
const wa = -s.w; // angular velocity of the arm's direction angle
launch = {
tip: { x: px + L * Math.cos(a), y: py + L * Math.sin(a) },
vx: -L * wa * Math.sin(a),
vy: L * wa * Math.cos(a)
};
}
if (s.phi < 0) { // hit the stop
s.phi = 0;
if (s.w < 0) { s.impact = -s.w; s.w = -s.w * 0.3; }
}
return launch;
}
function predict(phi0) {
if (phi0 < PHI_MIN) return null;
const s = { phi: phi0, w: 0, released: false, phiRel: Math.min(PHI_REL, 0.4 * phi0), impact: 0 };
for (let i = 0; i < 3000; i++) {
const l = armStep(s, 1 / 240);
if (l) return l;
}
return null;
}
// ---------- Splat drawing (into the persistent decal layer) ----------
function blob(c, x, y, r, n, j) {
const pts = [];
for (let i = 0; i < n; i++) {
const a = (i / n) * TAU;
const rr = r * (1 - j + rnd() * j * 2);
pts.push([x + Math.cos(a) * rr, y + Math.sin(a) * rr]);
}
c.beginPath();
c.moveTo((pts[n - 1][0] + pts[0][0]) / 2, (pts[n - 1][1] + pts[0][1]) / 2);
for (let i = 0; i < n; i++) {
const p = pts[i], q = pts[(i + 1) % n];
c.quadraticCurveTo(p[0], p[1], (p[0] + q[0]) / 2, (p[1] + q[1]) / 2);
}
c.closePath();
}
function splat(F, x, y, mode, power) {
const c = dc;
const base = F.r * (1.35 + rnd() * 0.7) * (0.75 + 0.5 * power);
const fy = mode === "ground" ? 0.38 : 1;
c.save();
if (mode === "disc") { c.beginPath(); c.arc(tx, ty, TR, 0, TAU); c.clip(); }
c.translate(x, y);
c.scale(1, fy);
// tapered streaks with droplets at the tips
const n = 10 + ((rnd() * 8) | 0);
for (let i = 0; i < n; i++) {
const a = rnd() * TAU;
const len = base * (1.15 + rnd() * 1.6);
const d = 0.08 + rnd() * 0.12;
c.fillStyle = i % 3 === 0 ? F.rim : F.juice;
c.beginPath();
c.moveTo(Math.cos(a - d) * base * 0.5, Math.sin(a - d) * base * 0.5);
c.lineTo(Math.cos(a) * len, Math.sin(a) * len);
c.lineTo(Math.cos(a + d) * base * 0.5, Math.sin(a + d) * base * 0.5);
c.closePath();
c.fill();
c.beginPath();
c.arc(Math.cos(a) * len * 1.06, Math.sin(a) * len * 1.06, 1.5 + rnd() * 3.2, 0, TAU);
c.fill();
}
// satellite droplets
for (let i = 0; i < 14; i++) {
const a = rnd() * TAU, dist = base * (1.2 + rnd() * 2.2);
c.fillStyle = rnd() < 0.6 ? F.juice : F.rim;
c.beginPath();
c.arc(Math.cos(a) * dist, Math.sin(a) * dist, 1 + rnd() * 2.5, 0, TAU);
c.fill();
}
// outer rim, juice and pulp blobs
c.fillStyle = F.rim;
blob(c, 0, 0, base * 1.06, 14, 0.22); c.fill();
c.fillStyle = F.juice;
blob(c, 0, 0, base * 0.95, 14, 0.25); c.fill();
c.fillStyle = F.pulp;
blob(c, (rnd() - 0.5) * base * 0.25, (rnd() - 0.5) * base * 0.25, base * 0.5, 9, 0.3); c.fill();
// skin bits and seeds
c.fillStyle = F.skin;
for (let i = 0; i < 5; i++) {
blob(c, (rnd() - 0.5) * base * 1.3, (rnd() - 0.5) * base * 1.3, 2 + rnd() * 3.5, 6, 0.3);
c.fill();
}
c.fillStyle = F.seed;
for (let i = 0; i < 6; i++) {
c.beginPath();
c.ellipse((rnd() - 0.5) * base * 1.2, (rnd() - 0.5) * base * 1.2, 1.6, 2.6, rnd() * TAU, 0, TAU);
c.fill();
}
// wet sheen
c.fillStyle = "rgba(255,255,255,0.28)";
c.beginPath();
c.ellipse(-base * 0.3, -base * 0.3, base * 0.25, base * 0.12, -0.5, 0, TAU);
c.fill();
c.restore();
}
function burst(F, x, y, mode, power, vx0) {
const nd = 24 + ((22 * power) | 0);
for (let i = 0; i < nd; i++) {
let a, sp;
if (mode === "ground") {
a = -Math.PI * (0.08 + rnd() * 0.84);
sp = (120 + rnd() * 420) * (0.45 + 0.55 * power);
} else {
a = rnd() * TAU;
sp = (60 + rnd() * 320) * (0.45 + 0.55 * power);
}
bits.push({
type: "drop", x, y,
vx: Math.cos(a) * sp + vx0 * 0.15,
vy: Math.sin(a) * sp - (mode === "disc" ? 40 : 0),
r: 1.2 + rnd() * 2.8, col: rnd() < 0.6 ? F.juice : F.rim,
landY: mode === "ground"
? clamp(y + (rnd() - 0.5) * 18, groundY + 3, H - 6)
: groundY + 3 + rnd() * (H - groundY - 9),
rim: F.rim
});
}
const nc = 6 + ((6 * power) | 0);
const cols = [F.pulp, F.skin, F.seed, F.pulp];
for (let i = 0; i < nc; i++) {
const a = mode === "ground" ? -Math.PI * (0.1 + rnd() * 0.8) : rnd() * TAU;
const sp = (90 + rnd() * 300) * (0.45 + 0.55 * power);
bits.push({
type: "chunk", x, y,
vx: Math.cos(a) * sp + vx0 * 0.1, vy: Math.sin(a) * sp,
r: 2.5 + rnd() * 3.5, rot: rnd() * TAU, vr: (rnd() - 0.5) * 16,
col: cols[(rnd() * cols.length) | 0],
landY: mode === "ground"
? clamp(y + (rnd() - 0.5) * 18, groundY + 3, H - 6)
: groundY + 3 + rnd() * (H - groundY - 9),
rim: F.rim
});
}
}
function stampBit(b) {
if (b.type === "drop") {
dc.fillStyle = b.col;
dc.beginPath();
dc.ellipse(b.x, b.landY, b.r * 1.5, b.r * 0.6, 0, 0, TAU);
dc.fill();
} else {
dc.fillStyle = b.rim;
dc.beginPath();
dc.ellipse(b.x, b.landY + 1, b.r * 1.9, b.r * 0.8, 0, 0, TAU);
dc.fill();
dc.fillStyle = b.col;
dc.beginPath();
for (let i = 0; i < 5; i++) {
const a = (i / 5) * TAU + b.rot;
const rr = b.r * (0.7 + 0.5 * ((i * 7) % 3) / 3);
const X = b.x + Math.cos(a) * rr * 1.3, Y = b.landY + Math.sin(a) * rr * 0.55;
if (i === 0) dc.moveTo(X, Y); else dc.lineTo(X, Y);
}
dc.closePath();
dc.fill();
}
}
const WORDS = ["SPLAT!", "SQUISH!", "SPLOOSH!", "SPLORT!", "KERSPLAT!"];
function popup(x, y, text, col, size = 22) {
popups.push({ x, y, text, col, size, life: 1.2 });
}
function impactGround(f) {
const power = clamp(Math.hypot(f.vx, f.vy) / VMAX, 0.2, 1);
const sx = f.x, sy = groundY + 4 + rnd() * 26;
splat(f.F, sx, sy, "ground", power);
burst(f.F, sx, sy, "ground", power, f.vx);
shake = Math.max(shake, 3 + 5 * power);
popup(sx, groundY - 14, WORDS[(rnd() * WORDS.length) | 0], f.F.juice);
}
function impactTarget(f) {
const power = clamp(Math.hypot(f.vx, f.vy) / VMAX, 0.2, 1);
const dx = f.x - tx, dy = f.y - ty;
const d = Math.hypot(dx, dy) || 1;
const k = d > TR * 0.9 ? TR * 0.9 / d : 1;
const sx = tx + dx * k, sy = ty + dy * k;
const dd = Math.hypot(sx - tx, sy - ty);
const pts = dd < 13 ? 100 : dd < 24 ? 50 : dd < 35 ? 30 : dd < 46 ? 20 : 10;
score += pts;
splat(f.F, sx, sy, "disc", power);
burst(f.F, sx, sy, "disc", power, f.vx);
const nd = 3 + ((rnd() * 3) | 0);
for (let i = 0; i < nd; i++) {
drips.push({
x: sx + (rnd() - 0.5) * f.F.r * 1.6, y: sy + f.F.r * 0.3,
v: 22 + rnd() * 20, len: 18 + rnd() * 45 * (f.F.r / 18),
w: 2.5 + rnd() * 2.5, col: f.F.juice
});
}
shake = Math.max(shake, 4 + 5 * power);
popup(tx, ty - TR - 8, pts === 100 ? "BULLSEYE! +100" : `+${pts}`, pts === 100 ? "#ffe14a" : "#ffffff", pts === 100 ? 26 : 22);
}
// ---------- Launch / reload ----------
function doLaunch(l) {
const F = cupFruit;
flying.push({
F, r: F.r, x: l.tip.x, y: l.tip.y - F.r + 9, vx: l.vx, vy: l.vy,
rot: 0, vr: (rnd() < 0.5 ? -1 : 1) * (4 + rnd() * 6), trail: []
});
cupFruit = null;
shots++;
hintDone = true;
}
function startReload() {
phase = "reload";
reloadDelay = 0.3;
hop = null;
}
// ---------- Simulation step ----------
function simStep(dt) {
if (phase === "swing") {
const l = armStep(arm, dt);
if (l) doLaunch(l);
if (arm.impact) { shake = Math.max(shake, Math.min(8, arm.impact * 0.6)); arm.impact = 0; }
if (arm.released && arm.phi < 0.012 && Math.abs(arm.w) < 0.35) {
arm.phi = 0; arm.w = 0;
startReload();
}
}
for (let i = flying.length - 1; i >= 0; i--) {
const f = flying[i];
f.x += f.vx * dt;
f.y += f.vy * dt + 0.5 * G * dt * dt;
f.vy += G * dt;
f.rot += f.vr * dt;
if (Math.hypot(f.x - tx, f.y - ty) <= TR + f.r * 0.2) { impactTarget(f); flying.splice(i, 1); continue; }
if (f.y + f.r >= groundY) { impactGround(f); flying.splice(i, 1); continue; }
if (f.x - f.r > W + 40) { popup(W - 70, 120, "Too far!", "#ffffff", 20); flying.splice(i, 1); }
}
for (let i = bits.length - 1; i >= 0; i--) {
const b = bits[i];
b.x += b.vx * dt;
b.y += b.vy * dt + 0.5 * G * dt * dt;
b.vy += G * dt;
if (b.type === "chunk") b.rot += b.vr * dt;
if (b.vy > 0 && b.y >= b.landY) { stampBit(b); bits.splice(i, 1); continue; }
if (b.x < -50 || b.x > W + 50) bits.splice(i, 1);
}
for (let i = drips.length - 1; i >= 0; i--) {
const d = drips[i];
const dy = d.v * dt;
const ny = d.y + dy;
if (Math.hypot(d.x - tx, ny - ty) > TR - 1) { drips.splice(i, 1); continue; }
dc.fillStyle = d.col;
dc.fillRect(d.x - d.w / 2, d.y, d.w, dy + 0.6);
d.y = ny;
d.len -= dy;
d.v *= 1 - 0.8 * dt;
if (d.len <= 0 || d.v < 1.5) {
dc.beginPath();
dc.arc(d.x, d.y, d.w * 0.85, 0, TAU);
dc.fill();
drips.splice(i, 1);
}
}
}
function frameUpdate(dtF) {
shake *= Math.exp(-dtF * 9);
if (shake < 0.05) shake = 0;
pulse += dtF;
pwHold = dragging ? arm.phi / PHI_MAX : pwHold * Math.exp(-dtF * 2.5);
if (hintDone) hint = Math.max(0, hint - dtF * 1.2);
if (pop2 < 1) pop2 = Math.min(1, pop2 + dtF * 3);
for (const c of clouds) {
c.x += c.v * dtF;
if (c.x - 90 * c.s > W + M) c.x = -90 * c.s - M;
}
for (let i = popups.length - 1; i >= 0; i--) {
const p = popups[i];
p.life -= dtF;
p.y -= 38 * dtF;
if (p.life <= 0) popups.splice(i, 1);
}
for (const f of flying) {
f.trail.push({ x: f.x, y: f.y });
if (f.trail.length > 14) f.trail.shift();
}
if (phase === "reload") {
if (!hop) {
reloadDelay -= dtF;
if (reloadDelay <= 0) {
const F = pile[2];
pile[2] = randFruit();
pop2 = 0;
hop = { F, t: 0 };
}
} else {
hop.t += dtF / 0.5;
if (hop.t >= 1) {
cupFruit = hop.F;
hop = null;
phase = "idle";
}
}
}
}
// ---------- Rendering helpers ----------
function drawFruit(F, x, y, rot, s = 1) {
const r = F.r * s;
ctx.save();
ctx.translate(x, y);
ctx.rotate(rot);
ctx.lineWidth = 1;
ctx.strokeStyle = "rgba(0,0,0,0.28)";
const grad = () => {
const g = ctx.createRadialGradient(-r * 0.3, -r * 0.3, r * 0.1, 0, 0, r * 1.1);
g.addColorStop(0, F.c0);
g.addColorStop(1, F.c1);
return g;
};
switch (F.name) {
case "apple": {
ctx.fillStyle = grad();
for (const dx of [-0.26, 0.26]) {
ctx.beginPath(); ctx.arc(r * dx, r * 0.04, r * 0.78, 0, TAU); ctx.fill();
}
ctx.beginPath(); ctx.arc(0, 0, r * 0.5, 0, TAU); ctx.fill();
ctx.strokeStyle = "#5b3a1e";
ctx.lineWidth = 2 * s;
ctx.beginPath();
ctx.moveTo(0, -r * 0.62);
ctx.quadraticCurveTo(r * 0.05, -r * 0.95, r * 0.2, -r * 1.1);
ctx.stroke();
ctx.fillStyle = "#4caf50";
ctx.beginPath(); ctx.ellipse(r * 0.45, -r * 0.88, r * 0.32, r * 0.14, -0.5, 0, TAU); ctx.fill();
break;
}
case "orange": {
ctx.fillStyle = grad();
ctx.beginPath(); ctx.arc(0, 0, r, 0, TAU); ctx.fill(); ctx.stroke();
ctx.fillStyle = "rgba(150,70,0,0.3)";
for (let i = 0; i < 14; i++) {
const a = i * 2.4, d = r * (0.25 + ((i * 37) % 10) / 14);
ctx.beginPath(); ctx.arc(Math.cos(a) * d, Math.sin(a) * d, r * 0.05, 0, TAU); ctx.fill();
}
ctx.fillStyle = "#3f8f2f";
ctx.beginPath(); ctx.arc(0, -r * 0.82, r * 0.12, 0, TAU); ctx.fill();
break;
}
case "watermelon": {
ctx.fillStyle = grad();
ctx.beginPath(); ctx.arc(0, 0, r, 0, TAU); ctx.fill();
ctx.save();
ctx.beginPath(); ctx.arc(0, 0, r, 0, TAU); ctx.clip();
ctx.fillStyle = "rgba(20,80,30,0.65)";
for (let i = -3; i <= 3; i++) {
ctx.beginPath(); ctx.ellipse(i * r * 0.3, 0, r * 0.09, r * 1.05, 0, 0, TAU); ctx.fill();
}
ctx.restore();
ctx.beginPath(); ctx.arc(0, 0, r, 0, TAU); ctx.stroke();
break;
}
case "lemon": {
ctx.fillStyle = grad();
ctx.beginPath(); ctx.ellipse(0, 0, r * 1.15, r * 0.82, 0, 0, TAU); ctx.fill(); ctx.stroke();
ctx.fillStyle = F.c1;
ctx.beginPath(); ctx.arc(-r * 1.15, 0, r * 0.14, 0, TAU); ctx.fill();
ctx.beginPath(); ctx.arc(r * 1.15, 0, r * 0.14, 0, TAU); ctx.fill();
break;
}
case "plum": {
ctx.fillStyle = grad();
ctx.beginPath(); ctx.arc(0, 0, r, 0, TAU); ctx.fill(); ctx.stroke();
ctx.strokeStyle = "rgba(0,0,0,0.25)";
ctx.lineWidth = 1.5;
ctx.beginPath(); ctx.arc(-r * 1.0, 0, r * 1.0, -0.9, 0.9); ctx.stroke();
break;
}
case "strawberry": {
ctx.beginPath();
ctx.moveTo(0, r * 1.15);
ctx.bezierCurveTo(r * 0.6, r * 0.9, r * 1.15, r * 0.2, r * 0.95, -r * 0.35);
ctx.bezierCurveTo(r * 0.85, -r * 0.85, r * 0.2, -r * 0.85, 0, -r * 0.6);
ctx.bezierCurveTo(-r * 0.2, -r * 0.85, -r * 0.85, -r * 0.85, -r * 0.95, -r * 0.35);
ctx.bezierCurveTo(-r * 1.15, r * 0.2, -r * 0.6, r * 0.9, 0, r * 1.15);
ctx.closePath();
ctx.fillStyle = grad();
ctx.fill(); ctx.stroke();
ctx.fillStyle = F.seed;
for (const [sx, sy] of [[-0.4, -0.2], [0.4, -0.2], [0, 0], [-0.55, 0.3], [0.55, 0.3], [-0.2, 0.5], [0.2, 0.5], [0, 0.85], [-0.28, -0.45], [0.28, -0.45]]) {
ctx.beginPath(); ctx.ellipse(sx * r, sy * r, r * 0.06, r * 0.1, 0, 0, TAU); ctx.fill();
}
ctx.fillStyle = "#43a047";
for (let i = 0; i < 5; i++) {
ctx.save();
ctx.translate(0, -r * 0.62);
ctx.rotate(i * TAU / 5);
ctx.beginPath(); ctx.ellipse(0, -r * 0.2, r * 0.1, r * 0.24, 0, 0, TAU); ctx.fill();
ctx.restore();
}
break;
}
case "kiwi": {
ctx.fillStyle = grad();
ctx.beginPath(); ctx.arc(0, 0, r, 0, TAU); ctx.fill();
ctx.strokeStyle = "rgba(60,35,15,0.5)";
ctx.lineWidth = 1;
for (let i = 0; i < 26; i++) {
const a = i * 0.9, d = r * (0.25 + ((i * 7) % 10) / 14);
const X = Math.cos(a) * d, Y = Math.sin(a) * d;
ctx.beginPath(); ctx.moveTo(X, Y); ctx.lineTo(X + 2.5, Y + 1.5); ctx.stroke();
}
ctx.strokeStyle = "rgba(0,0,0,0.28)";
ctx.beginPath(); ctx.arc(0, 0, r, 0, TAU); ctx.stroke();
break;
}
}
ctx.restore();
if (F.name !== "kiwi") {
ctx.fillStyle = "rgba(255,255,255,0.32)";
ctx.beginPath();
ctx.ellipse(x - r * 0.35, y - r * 0.4, r * 0.2, r * 0.11, -0.7, 0, TAU);
ctx.fill();
}
}
const WOOD = "#b07a42", WOOD_D = "#7b4f26", WOOD_L = "#cf9a5e";
function line(a, b, c, d) {
ctx.beginPath(); ctx.moveTo(a, b); ctx.lineTo(c, d); ctx.stroke();
}
function wheel(x, y, r) {
ctx.fillStyle = WOOD_D;
ctx.beginPath(); ctx.arc(x, y, r, 0, TAU); ctx.fill();
ctx.fillStyle = WOOD;
ctx.beginPath(); ctx.arc(x, y, r - 3, 0, TAU); ctx.fill();
ctx.strokeStyle = WOOD_D;
ctx.lineWidth = 2;
for (let i = 0; i < 6; i++) {
const a = i * TAU / 6 + 0.3;
line(x, y, x + Math.cos(a) * (r - 3), y + Math.sin(a) * (r - 3));
}
ctx.fillStyle = "#444";
ctx.beginPath(); ctx.arc(x, y, 3, 0, TAU); ctx.fill();
}
const tenColor = t => `hsl(${120 - 120 * clamp(t, 0, 1)},85%,50%)`;
function drawCatapult() {
ctx.lineCap = "round";
ctx.lineJoin = "round";
// shadow
ctx.fillStyle = "rgba(0,0,0,0.2)";
ctx.beginPath(); ctx.ellipse(px + 4, gl + 5, 98, 7, 0, 0, TAU); ctx.fill();
// A-frame legs
ctx.strokeStyle = WOOD_D; ctx.lineWidth = 10;
line(px - 54, gl - 26, px, py); line(px + 56, gl - 26, px, py);
ctx.strokeStyle = WOOD; ctx.lineWidth = 6;
line(px - 54, gl - 26, px, py); line(px + 56, gl - 26, px, py);
// stop post and pad
ctx.fillStyle = WOOD_D; ctx.fillRect(px, py - 70, 9, 80);
ctx.fillStyle = WOOD; ctx.fillRect(px + 1.5, py - 70, 4.5, 80);
ctx.fillStyle = "#3b2a1a"; ctx.fillRect(px - 2, py - 70, 5, 18);
// base beam and wheels
ctx.fillStyle = WOOD_D; ctx.fillRect(px - 74, gl - 30, 156, 11);
ctx.fillStyle = WOOD_L; ctx.fillRect(px - 73, gl - 29, 154, 4);
wheel(px - 50, gl - 14, 14);
wheel(px + 56, gl - 14, 14);
// arm
const tip = tipOf(arm.phi);
const ten = arm.phi / PHI_MAX;
ctx.save();
ctx.translate(px, py);
ctx.rotate(tip.a);
ctx.fillStyle = WOOD;
ctx.strokeStyle = WOOD_D;
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(-0.42 * L, -5.5); ctx.lineTo(L, -4); ctx.lineTo(L, 4); ctx.lineTo(-0.42 * L, 5.5);
ctx.closePath();
ctx.fill(); ctx.stroke();
ctx.strokeStyle = "rgba(90,55,25,0.45)";
ctx.lineWidth = 1;
line(-0.35 * L, -2, L - 4, -1.4);
line(-0.35 * L, 2, L - 4, 1.4);
// iron bands
ctx.fillStyle = "#4a4a50";
ctx.fillRect(0.34 * L, -6, 5, 12);
ctx.fillRect(0.86 * L, -5, 5, 10);
// counterweight
ctx.fillStyle = "#55555c";
ctx.fillRect(-0.42 * L - 8, -10, 22, 20);
ctx.fillStyle = "rgba(255,255,255,0.18)";
ctx.fillRect(-0.42 * L - 8, -10, 22, 5);
ctx.restore();
// tension arc and twisted-rope hub
if (arm.phi > 0.02) {
ctx.beginPath();
ctx.arc(px, py, 34, tip.a, A_REST, false);
ctx.strokeStyle = tenColor(ten);
ctx.lineWidth = 5;
ctx.stroke();
}
ctx.beginPath();
ctx.arc(px, py, 12, 0, TAU);
ctx.strokeStyle = `rgb(${(190 + 10 * ten) | 0},${(160 - 90 * ten) | 0},${(100 - 60 * ten) | 0})`;
ctx.lineWidth = 7;
ctx.setLineDash([5, 3]);
ctx.lineDashOffset = -tip.a * 10;
ctx.stroke();
ctx.setLineDash([]);
ctx.fillStyle = "#3d3d42";
ctx.beginPath(); ctx.arc(px, py, 5, 0, TAU); ctx.fill();
// fruit in cup, then the cup in front of it
if (cupFruit) drawFruit(cupFruit, tip.x, tip.y - cupFruit.r + 9, 0);
ctx.fillStyle = "#8a5a2c";
ctx.strokeStyle = "#4a2e14";
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(tip.x - 24, tip.y - 3);
ctx.quadraticCurveTo(tip.x - 20, tip.y + 16, tip.x, tip.y + 17);
ctx.quadraticCurveTo(tip.x + 20, tip.y + 16, tip.x + 24, tip.y - 3);
ctx.closePath();
ctx.fill(); ctx.stroke();
ctx.strokeStyle = WOOD_L;
ctx.lineWidth = 3;
line(tip.x - 24, tip.y - 3, tip.x + 24, tip.y - 3);
ctx.fillStyle = "#3d3d42";
ctx.beginPath(); ctx.arc(tip.x, tip.y + 4, 4, 0, TAU); ctx.fill();
}
function drawBasket() {
const x = bx, y = gl;
const slots = [[-13, -33], [13, -33], [0, -46]];
for (let i = 0; i < 3; i++) {
const sc = (i === 2 ? pop2 : 1) * 0.62;
if (sc > 0.02) drawFruit(pile[i], x + slots[i][0], y + slots[i][1], 0.4 * i, sc);
}
ctx.fillStyle = "#c99a5b";
ctx.strokeStyle = "#7b5126";
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(x - 32, y - 30); ctx.lineTo(x + 32, y - 30); ctx.lineTo(x + 25, y); ctx.lineTo(x - 25, y);
ctx.closePath();
ctx.fill(); ctx.stroke();
ctx.strokeStyle = "rgba(90,55,25,0.5)";
ctx.lineWidth = 1.5;
for (const dy of [-22, -14, -6]) line(x - 30 + (dy + 30) * -0.2 * -1, y + dy, x + 30 - (dy + 30) * 0.2, y + dy);
ctx.fillStyle = "#a8763a";
ctx.fillRect(x - 34, y - 33, 68, 6);
}
function drawTarget() {
// pole
ctx.fillStyle = "rgba(0,0,0,0.18)";
ctx.beginPath(); ctx.ellipse(tx, groundY + 12, 30, 5, 0, 0, TAU); ctx.fill();
ctx.fillStyle = WOOD_D; ctx.fillRect(tx - 5, ty + TR - 8, 10, groundY + 12 - (ty + TR - 8));
ctx.fillStyle = WOOD; ctx.fillRect(tx - 3, ty + TR - 8, 4, groundY + 12 - (ty + TR - 8));
// board
ctx.fillStyle = "#8d5a2b";
ctx.beginPath(); ctx.arc(tx, ty, TR + 5, 0, TAU); ctx.fill();
const rings = [[56, "#d94141"], [46, "#fbf6e9"], [35, "#d94141"], [24, "#fbf6e9"], [13, "#d94141"]];
for (const [rad, col] of rings) {
ctx.fillStyle = col;
ctx.beginPath(); ctx.arc(tx, ty, rad, 0, TAU); ctx.fill();
}
}
function drawClouds() {
ctx.fillStyle = "rgba(255,255,255,0.85)";
for (const c of clouds) {
const s = c.s;
ctx.beginPath();
ctx.arc(c.x, c.y, 18 * s, 0, TAU);
ctx.arc(c.x + 22 * s, c.y - 8 * s, 22 * s, 0, TAU);
ctx.arc(c.x + 48 * s, c.y, 18 * s, 0, TAU);
ctx.arc(c.x + 24 * s, c.y + 6 * s, 18 * s, 0, TAU);
ctx.fill();
}
}
function drawPreview() {
if (!dragging || !cupFruit) return;
const l = predict(arm.phi);
if (!l) return;
const sx = l.tip.x, sy = l.tip.y - cupFruit.r + 9;
for (let i = 1; i <= 70; i++) {
const t = i * 0.04;
const x = sx + l.vx * t;
const y = sy + l.vy * t + 0.5 * G * t * t;
if (y > groundY - cupFruit.r || x > W + 10) break;
if (Math.hypot(x - tx, y - ty) < TR) break;
ctx.fillStyle = `rgba(255,255,255,${0.85 * (1 - i / 80)})`;
ctx.beginPath(); ctx.arc(x, y, 2.4, 0, TAU); ctx.fill();
}
}
function drawChunk(b) {
ctx.save();
ctx.translate(b.x, b.y);
ctx.rotate(b.rot);
ctx.fillStyle = b.col;
ctx.fillRect(-b.r, -b.r * 0.7, b.r * 2, b.r * 1.4);
ctx.restore();
}
function drawHUD() {
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.fillStyle = "rgba(0,0,0,0.38)";
ctx.fillRect(10, 10, 196, 74);
ctx.fillStyle = "#fff";
ctx.textAlign = "left";
ctx.font = "bold 15px system-ui, sans-serif";
ctx.fillText(`Score ${score}`, 20, 31);
ctx.font = "13px system-ui, sans-serif";
ctx.fillText(`Shots ${shots}`, 130, 31);
ctx.fillStyle = "rgba(255,255,255,0.22)";
ctx.fillRect(20, 44, 176, 10);
const pw = clamp(pwHold, 0, 1);
const g = ctx.createLinearGradient(20, 0, 196, 0);
g.addColorStop(0, "#5fd35f"); g.addColorStop(0.55, "#ffd93a"); g.addColorStop(1, "#ff3d2e");
ctx.fillStyle = g;
ctx.fillRect(20, 44, 176 * pw, 10);
ctx.fillStyle = "rgba(255,255,255,0.85)";
ctx.font = "11px system-ui, sans-serif";
ctx.fillText("POWER", 20, 72);
ctx.textAlign = "right";
ctx.fillText(`${Math.round(pw * 100)}%`, 196, 72);
if (hint > 0) {
ctx.globalAlpha = hint * 0.9;
ctx.textAlign = "center";
ctx.font = "15px system-ui, sans-serif";
const msg = "Drag the catapult arm down and back, then let go to fling the fruit!";
const tw = ctx.measureText(msg).width;
ctx.fillStyle = "rgba(0,0,0,0.4)";
ctx.fillRect(W / 2 - tw / 2 - 12, 16, tw + 24, 28);
ctx.fillStyle = "#fff";
ctx.fillText(msg, W / 2, 35);
ctx.globalAlpha = 1;
}
}
function draw() {
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.clearRect(0, 0, W, H);
const sx = (rnd() - 0.5) * 2 * shake, sy = (rnd() - 0.5) * 2 * shake * 0.8;
ctx.setTransform(dpr, 0, 0, dpr, sx * dpr, sy * dpr);
if (bgSky) ctx.drawImage(bgSky, -M, -M, W + 2 * M, H + 2 * M);
drawClouds();
if (bgFront) ctx.drawImage(bgFront, -M, -M, W + 2 * M, H + 2 * M);
drawTarget();
if (decals) ctx.drawImage(decals, 0, 0, W, H);
drawBasket();
drawCatapult();
// pulsing ring prompting the first pull
if (phase === "idle" && cupFruit && shots === 0 && !dragging) {
const t = tipOf(0);
const a = 0.5 + 0.5 * Math.sin(pulse * 4);
ctx.strokeStyle = `rgba(255,255,255,${0.35 + 0.45 * a})`;
ctx.lineWidth = 3;
ctx.beginPath(); ctx.arc(t.x, t.y - 6, 34 + 4 * a, 0, TAU); ctx.stroke();
}
// flying fruit with trails
for (const f of flying) {
for (let i = 0; i < f.trail.length; i++) {
const p = f.trail[i];
ctx.fillStyle = `rgba(255,255,255,${(i / f.trail.length) * 0.35})`;
ctx.beginPath(); ctx.arc(p.x, p.y, f.r * (0.25 + 0.4 * i / f.trail.length), 0, TAU); ctx.fill();
}
drawFruit(f.F, f.x, f.y, f.rot);
}
// reload hop
if (hop) {
const t = hop.t;
const end = tipOf(0);
const sx0 = bx, sy0 = gl - 46;
const ex = end.x, ey = end.y - hop.F.r + 9;
drawFruit(hop.F, sx0 + (ex - sx0) * t, sy0 + (ey - sy0) * t - Math.sin(Math.PI * t) * 70, t * 6, 1);
}
// juice drops and chunks in the air
for (const b of bits) {
if (b.type === "drop") {
ctx.fillStyle = b.col;
ctx.beginPath(); ctx.arc(b.x, b.y, b.r, 0, TAU); ctx.fill();
} else {
drawChunk(b);
}
}
drawPreview();
// popups
ctx.textAlign = "center";
for (const p of popups) {
ctx.globalAlpha = clamp(p.life / 0.5, 0, 1);
ctx.font = `bold ${p.size}px system-ui, sans-serif`;
ctx.lineWidth = 5;
ctx.lineJoin = "round";
ctx.strokeStyle = "rgba(40,20,10,0.85)";
ctx.strokeText(p.text, p.x, p.y);
ctx.fillStyle = p.col;
ctx.fillText(p.text, p.x, p.y);
}
ctx.globalAlpha = 1;
drawHUD();
}
// ---------- Input ----------
const pos = e => {
const r = canvas.getBoundingClientRect();
return { x: e.clientX - r.left, y: e.clientY - r.top };
};
function distSeg(x, y, ax, ay, bx2, by2) {
const dx = bx2 - ax, dy = by2 - ay;
const t = clamp(((x - ax) * dx + (y - ay) * dy) / (dx * dx + dy * dy), 0, 1);
return Math.hypot(x - (ax + t * dx), y - (ay + t * dy));
}
function hitArm(p) {
const a = A_REST - arm.phi, ux = Math.cos(a), uy = Math.sin(a);
const ex = px + L * ux, ey = py + L * uy;
return distSeg(p.x, p.y, px - 0.42 * L * ux, py - 0.42 * L * uy, ex, ey) < 22 ||
Math.hypot(p.x - ex, p.y - (ey - 5)) < 38;
}
function phiFromPointer(p) {
const dx = p.x - px, dy = p.y - py;
if (dx * dx + dy * dy < 400) return arm.phi;
let phi = A_REST - Math.atan2(dy, dx);
const prev = arm.phi;
while (phi - prev > Math.PI) phi -= TAU;
while (phi - prev < -Math.PI) phi += TAU;
return clamp(phi, 0, PHI_MAX);
}
function endDrag() {
dragging = false;
const phi0 = arm.phi;
if (phi0 < PHI_MIN) {
arm.phi = 0; arm.w = 0;
phase = "idle";
return;
}
arm.released = false;
arm.w = 0;
arm.phiRel = Math.min(PHI_REL, 0.4 * phi0);
arm.impact = 0;
phase = "swing";
}
canvas.addEventListener("pointerdown", e => {
const p = pos(e);
if (phase === "idle" && cupFruit && hitArm(p)) {
dragging = true;
phase = "drag";
hintDone = true;
canvas.setPointerCapture(e.pointerId);
arm.phi = phiFromPointer(p);
canvas.style.cursor = "grabbing";
e.preventDefault();
}
});
canvas.addEventListener("pointermove", e => {
const p = pos(e);
if (dragging) {
arm.phi = phiFromPointer(p);
} else {
canvas.style.cursor = phase === "idle" && cupFruit && hitArm(p) ? "grab" : "default";
}
});
const onUp = e => {
if (!dragging) return;
if (canvas.hasPointerCapture && canvas.hasPointerCapture(e.pointerId)) canvas.releasePointerCapture(e.pointerId);
canvas.style.cursor = "default";
endDrag();
};
canvas.addEventListener("pointerup", onUp);
canvas.addEventListener("pointercancel", onUp);
cleanBtn.addEventListener("click", () => {
if (dc) dc.clearRect(0, 0, W, H);
drips = [];
bits = [];
});
// ---------- Layers ----------
function makeLayer(w, h) {
const c = document.createElement("canvas");
c.width = Math.round(w * dpr);
c.height = Math.round(h * dpr);
const g = c.getContext("2d");
g.scale(dpr, dpr);
return { c, g };
}
function mulberry32(a) {
return () => {
a |= 0; a = a + 0x6D2B79F5 | 0;
let t = Math.imul(a ^ a >>> 15, 1 | a);
t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
return ((t ^ t >>> 14) >>> 0) / 4294967296;
};
}
function paintSky(g) {
const sg = g.createLinearGradient(0, -M, 0, groundY);
sg.addColorStop(0, "#6bbcf2");
sg.addColorStop(1, "#e6f5ff");
g.fillStyle = sg;
g.fillRect(-M, -M, W + 2 * M, H + 2 * M);
const sunX = W * 0.82, sunY = 70;
const gg = g.createRadialGradient(sunX, sunY, 6, sunX, sunY, 90);
gg.addColorStop(0, "rgba(255,248,200,0.95)");
gg.addColorStop(0.25, "rgba(255,240,170,0.5)");
gg.addColorStop(1, "rgba(255,240,170,0)");
g.fillStyle = gg;
g.fillRect(sunX - 90, sunY - 90, 180, 180);
g.fillStyle = "#fff6c2";
g.beginPath(); g.arc(sunX, sunY, 24, 0, TAU); g.fill();
}
function paintFront(g) {
const hills = (col, base, a1, a2, f1, f2, ph) => {
g.fillStyle = col;
g.beginPath();
g.moveTo(-M, H + M);
for (let x = -M; x <= W + M; x += 8) {
g.lineTo(x, groundY - base - a1 * Math.sin(x * f1 + ph) - a2 * Math.sin(x * f2 + ph * 2));
}
g.lineTo(W + M, H + M);
g.closePath();
g.fill();
};
hills("#a7d6a5", 64, 24, 12, 0.004, 0.011, 1.0);
hills("#86c47a", 34, 16, 8, 0.006, 0.017, 3.1);
const gr = g.createLinearGradient(0, groundY, 0, H + M);
gr.addColorStop(0, "#7cc357");
gr.addColorStop(1, "#4f8a35");
g.fillStyle = gr;
g.fillRect(-M, groundY, W + 2 * M, H - groundY + M);
g.fillStyle = "#97d56f";
g.fillRect(-M, groundY, W + 2 * M, 3);
const rng = mulberry32(11);
g.strokeStyle = "rgba(40,90,30,0.35)";
g.lineWidth = 1.5;
for (let i = 0; i < W / 6; i++) {
const x = rng() * W, y = groundY + 6 + rng() * (H - groundY - 8);
g.beginPath(); g.moveTo(x, y); g.lineTo(x - 2, y - 5); g.moveTo(x, y); g.lineTo(x + 2, y - 5); g.stroke();
}
const cols = ["#ffffff", "#ffe066", "#ff9ec4"];
for (let i = 0; i < W / 70; i++) {
const x = rng() * W, y = groundY + 8 + rng() * (H - groundY - 14);
g.fillStyle = cols[(rng() * 3) | 0];
g.beginPath(); g.arc(x, y, 1.8, 0, TAU); g.fill();
}
}
// ---------- Calibration and resize ----------
function calibrate() {
const aRel = A_REST - PHI_REL;
x0 = px + L * Math.cos(aRel);
y0 = py + L * Math.sin(aRel) - 18 + 9;
const c = -Math.sin(aRel), s = Math.cos(aRel); // launch direction (tangent)
const range = v => {
const vx = v * c, vy = v * s;
const dy = (groundY - 18) - y0;
const t = (-vy + Math.sqrt(vy * vy + 2 * G * dy)) / G;
return vx * t;
};
let D = Math.max((W - 80) - x0, 260);
const want = 1.15 * D;
let lo = 380, hi = 900;
if (range(hi) <= want) {
VMAX = hi;
D = range(hi) / 1.15;
} else {
for (let i = 0; i < 30; i++) {
const mid = (lo + hi) / 2;
if (range(mid) < want) lo = mid; else hi = mid;
}
VMAX = (lo + hi) / 2;
}
tx = Math.min(x0 + D, W - 60);
K = (VMAX / L) ** 2 / (PHI_MAX ** 2 - PHI_REL ** 2);
}
function resize() {
const w = Math.floor(container.clientWidth);
if (w <= 0) return;
W = w;
dpr = window.devicePixelRatio || 1;
canvas.width = Math.round(W * dpr);
canvas.height = Math.round(H * dpr);
groundY = H - 46;
gl = groundY + 8;
px = clamp(W * 0.14, 150, 260);
py = gl - 132;
bx = Math.max(36, px - 125);
ty = groundY - 100;
calibrate();
const sky = makeLayer(W + 2 * M, H + 2 * M);
sky.g.translate(M, M);
paintSky(sky.g);
bgSky = sky.c;
const front = makeLayer(W + 2 * M, H + 2 * M);
front.g.translate(M, M);
paintFront(front.g);
bgFront = front.c;
const d = makeLayer(W, H);
decals = d.c;
dc = d.g;
clouds = [];
const rng = mulberry32(5);
const nc = Math.max(3, Math.round(W / 280));
for (let i = 0; i < nc; i++) {
clouds.push({ x: rng() * W, y: 30 + rng() * 90, s: 0.7 + rng() * 0.7, v: 5 + rng() * 9 });
}
// reset the action
flying = []; bits = []; drips = []; popups = [];
arm.phi = 0; arm.w = 0; arm.released = false; arm.impact = 0;
dragging = false;
hop = null;
if (!cupFruit) cupFruit = randFruit();
phase = "idle";
}
const ro = new ResizeObserver(resize);
ro.observe(container);
// ---------- Loop ----------
let last = performance.now(), acc = 0, raf;
const DT = 1 / 240;
function frame(now) {
const dtF = Math.min(0.1, (now - last) / 1000);
last = now;
acc += dtF;
while (acc >= DT) { if (dc) simStep(DT); acc -= DT; }
frameUpdate(dtF);
if (bgSky) draw();
raf = requestAnimationFrame(frame);
}
raf = requestAnimationFrame(frame);
invalidation.then(() => {
cancelAnimationFrame(raf);
ro.disconnect();
});
return container;
}AP Physics ⚛️
Greetings, Scientist!
AP Physics 1 is all about mechanics and motion. How do forces shape our world? How does energy transfer from one form to another? These and other questions will be answered as you learn to solve challenging problems.
Please, click and drag the catapult arm to yeet the fruits.