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
20 changes: 20 additions & 0 deletions windows/src/hub/frames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@ export function shouldAnimate(live: number, pageVisibility: string, windowShown:
return live > 0 && windowShown && pageVisibility === "visible";
}

/** Frames a second the canvases are drawn at. They are slow drifts and walks:
* 30 look the same as 60 while the window has the focus, and 10 are plenty
* behind other windows. On Linux, WebKitGTK paints and composites each
* changed canvas in software: at 60 the open window took about half a CPU
* core, at 30/10 about a third and a tenth of one (#792). */
export const FPS_FOCUSED = 30;
export const FPS_BACKGROUND = 10;

/** Milliseconds between two drawn frames. */
export function frameInterval(focused: boolean): number {
return 1000 / (focused ? FPS_FOCUSED : FPS_BACKGROUND);
}

/** Whether the animation frame at `now` is drawn: one `interval` after the
* last drawn one, give or take a quarter of a 60 Hz frame, so that a 60 Hz
* screen draws every other frame and not one in three when they jitter. */
export function frameDue(now: number, lastDrawn: number, interval: number): boolean {
return now - lastDrawn >= interval - 4;
}

/** Whether a canvas at `box` shows at least one pixel of a `width`×`height`
* viewport: one scrolled out of the page, or not laid out, is not drawn. */
export function onScreen(box: Box, width: number, height: number): boolean {
Expand Down
23 changes: 19 additions & 4 deletions windows/src/hub/hero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// scrolled out of sight.

import { BotEngine, hexToRGB } from "../mochi/engine";
import { onScreen, shouldAnimate } from "./frames";
import { frameDue, frameInterval, onScreen, shouldAnimate } from "./frames";
import type { BotEmoteName, BotStateName } from "../core/layout";
import type { Outfit } from "../mochi/wardrobe";

Expand All @@ -28,6 +28,8 @@ let fallback: number | null = null;
/** No animation frame came after the last ask: the timer drives the loop. */
let starved = false;
let last = performance.now();
/** When the last frame was drawn: frames in between are skipped (`frameDue`). */
let lastDrawn = -Infinity;
const start = performance.now();
/** The Coucou window is on screen, as Rust last said (`setWindowShown`). */
let windowShown = true;
Expand All @@ -45,7 +47,13 @@ function loop(now: number) {
window.clearTimeout(fallback);
fallback = null;
}
const dt = Math.min(0.05, (now - last) / 1000);
if (!frameDue(now, lastDrawn, frameInterval(document.hasFocus?.() ?? true))) {
if (running()) ask();
return;
}
lastDrawn = now;
// Up to 0.12 s, so the walks and the drift keep their speed at 10 frames a second.
const dt = Math.min(0.12, (now - last) / 1000);
last = now;
const t = (now - start) / 1000;
stepVoice(dt, now);
Expand Down Expand Up @@ -126,6 +134,13 @@ function stepVoice(dt: number, now: number) {
if (voice < 0.001) voice = 0;
}

/** Mochi's springs (the mouth's ω₀ is 2π/0.25) need steps under 0.08 s: a
* frame drawn behind other windows (10 a second) is stepped in pieces. */
function stepEngine(engine: BotEngine, dt: number) {
const MAX_STEP = 1 / 30;
for (let left = dt; left > 1e-6; left -= MAX_STEP) engine.update(Math.min(left, MAX_STEP));
}

function add(a: Animated) {
live.add(a);
wake();
Expand Down Expand Up @@ -381,7 +396,7 @@ export function troupe(canvas: HTMLCanvasElement, cast: MochiSpec[], width: numb
w.engine.lookX = w.dir * 0.5;
w.engine.lookY = 0.1 + 0.1 * Math.sin(t + w.spec.start * 7);
}
w.engine.update(dt);
stepEngine(w.engine, dt);
// `size` is the drawing's square, as on the Mac; the body is about 0.68 of it.
const box = s;
const floor = ch - 8;
Expand Down Expand Up @@ -419,7 +434,7 @@ export function liveMochi(canvas: HTMLCanvasElement, color = "#F5F6F8", state: B
engine.lookX = Math.max(-1, Math.min(1, mouse.x / 300));
engine.lookY = Math.max(-1, Math.min(1, mouse.y / 300));
}
engine.update(dt);
stepEngine(engine, dt);
ctx.clearRect(0, 0, w, h);
engine.draw(ctx, w, h);
},
Expand Down
13 changes: 11 additions & 2 deletions windows/src/mochi/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ function starPath(x: CanvasRenderingContext2D, ro: number, ri: number) {
}

const FONT = `system-ui, "Segoe UI Variable Text", "Segoe UI", sans-serif`;
/** The sleeping z's font, drawn at this size and scaled (see `drawParticles`). */
const Z_FONT_PX = 32;
const Z_FONT = `700 ${Z_FONT_PX}px ${FONT}`;

// ── Dance (BotEngine.applyDance, macOS) ──────────────────────────────────────

Expand Down Expand Up @@ -1327,13 +1330,19 @@ export class BotEngine {
x.quadraticCurveTo(-sz * 0.8, sz * 0.2, 0, -sz);
x.fill();
break;
case "z":
case "z": {
// One font size, scaled to the z's: a z grows as it rises, and a new
// size at every frame was a new font for WebKitGTK to resolve and
// cache, up to 70 ms a frame for one sleeping Mochi (#792).
const scale = (sz * 1.9) / Z_FONT_PX;
x.fillStyle = "rgb(209,219,235)";
x.font = `700 ${sz * 1.9}px ${FONT}`;
x.font = Z_FONT;
x.textAlign = "center";
x.textBaseline = "middle";
x.scale(scale, scale);
x.fillText("z", 0, 0);
break;
}
}
x.restore();
}
Expand Down
42 changes: 41 additions & 1 deletion windows/tests/hub-frames.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import { test } from "node:test";
import assert from "node:assert/strict";
import { onScreen, shouldAnimate } from "../src/hub/frames.ts";
import { frameDue, frameInterval, onScreen, shouldAnimate } from "../src/hub/frames.ts";

test("the loop runs only with something to draw, the window shown and the page visible", () => {
assert.equal(shouldAnimate(3, "visible", true), true);
Expand All @@ -31,3 +31,43 @@ test("a canvas scrolled out of sight, or not laid out, is skipped", () => {
assert.equal(onScreen(box(0, 1100, 300, 200), 1100, 720), false);
assert.equal(onScreen(box(100, 240, 0, 0), 1100, 720), false);
});

// The canvases are drawn at 30 frames a second with the focus, 10 without:
// at 60 a WebKitWebProcess kept a CPU core busy on Linux while the window was open (#792).
test("frames are drawn at 30 a second with the focus, 10 behind other windows", () => {
assert.equal(Math.round(frameInterval(true)), 33);
assert.equal(Math.round(frameInterval(false)), 100);
});

test("a 60 Hz screen draws every other frame with the focus", () => {
const interval = frameInterval(true);
let lastDrawn = -Infinity;
let drawn = 0;
// One second of 60 Hz frames, with a little jitter.
for (let i = 0; i < 60; i++) {
const now = i * (1000 / 60) + (i % 2 ? 0.7 : -0.7);
if (frameDue(now, lastDrawn, interval)) {
drawn++;
lastDrawn = now;
}
}
assert.equal(drawn, 30);
});

test("behind other windows, a 60 Hz screen draws one frame in six", () => {
const interval = frameInterval(false);
let lastDrawn = -Infinity;
let drawn = 0;
for (let i = 0; i < 60; i++) {
const now = i * (1000 / 60);
if (frameDue(now, lastDrawn, interval)) {
drawn++;
lastDrawn = now;
}
}
assert.equal(drawn, 10);
});

test("the first frame is always drawn", () => {
assert.equal(frameDue(0, -Infinity, frameInterval(true)), true);
});