Ridge still: paint the scrim into the canvas under the planes, from the scrim's one gradient
All checks were successful
Build pull request / Build pull request (pull_request) Successful in 4m26s
All checks were successful
Build pull request / Build pull request (pull_request) Successful in 4m26s
24e1a1c3 put a DOM scrim over the canvas after the planes were painted into it,
so the settled still dimmed the top of the ridge (0.35 fill to 55% height, planes
from about 30%) and a full-tier scene darkened when it settled; a second settle
would also have re-painted the planes over themselves. The still now paints sky
and stars, then the scrim, then the planes into the canvas: the rig's order. Both
the DOM layer and the canvas fill read scrimStops, one spelling of the gradient.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
24e1a1c3e2
commit
0134355cb0
2 changed files with 68 additions and 22 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { createRidgeScene, SCRIM_ALPHA, scrimBackground } from './Ridge';
|
||||
import { createRidgeScene, SCRIM_ALPHA, scrimBackground, scrimStops } from './Ridge';
|
||||
import { valueNoise, hash2 } from './noise';
|
||||
import { FrameSampler, frameStats, SLOW_P95_MS, WARMUP_FRAMES, WINDOW_MS } from './probe';
|
||||
import type { SkyColours } from './skyColour';
|
||||
|
|
@ -140,11 +140,17 @@ const draws: string[] = [];
|
|||
const images: unknown[][] = [];
|
||||
/** Every RGBA grid the host canvas was painted from (the small canvas's putImageData). */
|
||||
const painted: Uint8ClampedArray[] = [];
|
||||
/** Every fillRect whose fill was a linear gradient: its stops, and how many images had been drawn before it (paint order). */
|
||||
const fills: { stops: [number, string][]; imagesBefore: number }[] = [];
|
||||
function ctx() {
|
||||
const c: any = {
|
||||
createLinearGradient: () => ({ addColorStop: vi.fn() }),
|
||||
createLinearGradient: () => {
|
||||
const stops: [number, string][] = [];
|
||||
return { stops, addColorStop: (o: number, col: string) => stops.push([o, col]) };
|
||||
},
|
||||
fillRect() {
|
||||
draws.push('sky');
|
||||
if (c.fillGradient?.stops) fills.push({ stops: c.fillGradient.stops, imagesBefore: images.length });
|
||||
},
|
||||
save() {},
|
||||
restore() {},
|
||||
|
|
@ -182,7 +188,9 @@ function ctx() {
|
|||
},
|
||||
globalAlpha: 1,
|
||||
globalCompositeOperation: 'source-over',
|
||||
set fillStyle(_v: unknown) {},
|
||||
set fillStyle(v: any) {
|
||||
c.fillGradient = v;
|
||||
},
|
||||
};
|
||||
return c;
|
||||
}
|
||||
|
|
@ -197,6 +205,7 @@ beforeEach(() => {
|
|||
draws.length = 0;
|
||||
images.length = 0;
|
||||
painted.length = 0;
|
||||
fills.length = 0;
|
||||
search = '';
|
||||
motion = { matches: false, addEventListener: vi.fn(), removeEventListener: vi.fn() };
|
||||
doc = Object.assign(new EventTarget(), {
|
||||
|
|
@ -413,9 +422,7 @@ describe('the starfield in the scene', () => {
|
|||
const { parent, canvas } = host();
|
||||
createRidgeScene({ canvas, tier: 'still', sky: night });
|
||||
expect(count('star')).toBe(N);
|
||||
expect(
|
||||
parent.children.filter((c: any) => c !== canvas).map((c: any) => c.style.pointerEvents)
|
||||
).toEqual(['none']); // only the scrim
|
||||
expect(parent.children).toEqual([canvas]); // the scrim is in the canvas
|
||||
expect(rafQueue).toHaveLength(0);
|
||||
});
|
||||
|
||||
|
|
@ -469,7 +476,7 @@ describe('the starfield in the scene', () => {
|
|||
const { parent, canvas } = host();
|
||||
const s = createRidgeScene({ canvas, tier: 'full', sky: night })!;
|
||||
runFrames(100, 100);
|
||||
expect(parent.children.filter((c: any) => c !== canvas)).toHaveLength(1); // the scrim stays
|
||||
expect(parent.children).toEqual([canvas]); // the scrim moves into the canvas
|
||||
expect(count('star')).toBe(N);
|
||||
expect(rafQueue).toHaveLength(0);
|
||||
s.dispose();
|
||||
|
|
@ -588,22 +595,37 @@ describe('the ridge planes in the scene', () => {
|
|||
expect(SCRIM_ALPHA.flat).toBeGreaterThan(SCRIM_ALPHA.planes);
|
||||
});
|
||||
|
||||
it('the settled still keeps the scrim over the canvas: gradient with planes, flat without', () => {
|
||||
for (const [backdrop, flat] of [
|
||||
[NOON, false],
|
||||
[undefined, true],
|
||||
it('the settled still paints the scrim into the canvas under the planes, once: gradient with planes, flat without', () => {
|
||||
for (const [backdrop, hasPlanes] of [
|
||||
[NOON, true],
|
||||
[undefined, false],
|
||||
] as const) {
|
||||
fills.length = 0;
|
||||
images.length = 0;
|
||||
const { parent, canvas } = host();
|
||||
const s = createRidgeScene({ canvas, tier: 'still', sky: night, backdrop })!;
|
||||
const scrims = parent.children.filter((c: any) => c.style.background);
|
||||
expect(parent.children).toEqual([canvas]); // nothing over the canvas dims the planes
|
||||
const scrims = fills.filter(
|
||||
(f) => JSON.stringify(f.stops) === JSON.stringify(scrimStops(night, hasPlanes))
|
||||
);
|
||||
expect(scrims).toHaveLength(1);
|
||||
expect(scrims[0].style.background).toBe(scrimBackground(night, !flat));
|
||||
expect(scrims[0].style.pointerEvents).toBe('none');
|
||||
const planeImages = images.filter((a) =>
|
||||
hasPlanes ? Object.values(NOON.a).includes(a[0] as HTMLImageElement) : false
|
||||
);
|
||||
expect(planeImages).toHaveLength(hasPlanes ? 3 : 0);
|
||||
if (hasPlanes) expect(scrims[0].imagesBefore).toBe(images.indexOf(planeImages[0])); // the scrim, then the planes
|
||||
s.dispose();
|
||||
expect(parent.children).toEqual([canvas]);
|
||||
}
|
||||
});
|
||||
|
||||
it('the DOM scrim and the still scrim are one gradient', () => {
|
||||
for (const hasPlanes of [true, false]) {
|
||||
const css = scrimBackground(night, hasPlanes);
|
||||
scrimStops(night, hasPlanes).forEach(([, c]) => expect(css).toContain(c));
|
||||
}
|
||||
expect(scrimBackground(night, true)).toContain(' 55%, ');
|
||||
});
|
||||
|
||||
it('a day sky has no scrim', () => {
|
||||
const { parent, canvas } = host();
|
||||
createRidgeScene({ canvas, tier: 'full', sky: DAY, backdrop: NOON });
|
||||
|
|
|
|||
|
|
@ -32,13 +32,27 @@ export type RidgeOptions = Pick<SceneOptions, 'canvas' | 'tier'> & {
|
|||
|
||||
/** The fill behind nav, header and room-list rows: the sky's top colour over the stars, under the planes. With planes it fades out toward the ridge; without them (they failed to load, or none was given) it is flat and heavier, so rows stay legible over the sky alone. Never changes layout: it is one pointer-transparent layer. */
|
||||
export const SCRIM_ALPHA = { planes: 0.35, flat: 0.55 } as const;
|
||||
export const scrimBackground = (sky: SkyColours, hasPlanes: boolean): string => {
|
||||
/** The scrim's gradient, top to bottom, as [offset 0..1, colour]: the one spelling the DOM layer (`scrimBackground`) and the still's canvas fill both read. */
|
||||
export const scrimStops = (sky: SkyColours, hasPlanes: boolean): [number, string][] => {
|
||||
const [r, g, b] = sky.top;
|
||||
const a = hasPlanes ? SCRIM_ALPHA.planes : SCRIM_ALPHA.flat;
|
||||
const c = (k: number) => `rgba(${r},${g},${b},${a * k})`;
|
||||
const stops: [number, number][] = hasPlanes
|
||||
? [
|
||||
[0, 1],
|
||||
[0.55, 1],
|
||||
[0.85, 0],
|
||||
]
|
||||
: [
|
||||
[0, 1],
|
||||
[1, 1],
|
||||
];
|
||||
return stops.map(([o, k]) => [o, `rgba(${r},${g},${b},${a * k})`]);
|
||||
};
|
||||
export const scrimBackground = (sky: SkyColours, hasPlanes: boolean): string => {
|
||||
const s = scrimStops(sky, hasPlanes);
|
||||
return hasPlanes
|
||||
? `linear-gradient(to bottom, ${c(1)} 0%, ${c(1)} 55%, ${c(0)} 85%)`
|
||||
: `linear-gradient(${c(1)}, ${c(1)})`;
|
||||
? `linear-gradient(to bottom, ${s.map(([o, c]) => `${c} ${Math.round(o * 100)}%`).join(', ')})`
|
||||
: `linear-gradient(${s[0][1]}, ${s[1][1]})`;
|
||||
};
|
||||
|
||||
const css = (c: Srgb): string => `rgb(${c[0]},${c[1]},${c[2]})`;
|
||||
|
|
@ -350,6 +364,17 @@ export function createRidgeScene(opts: RidgeOptions): Scene | null {
|
|||
return scrim;
|
||||
};
|
||||
|
||||
/** The still's scrim, painted into the host canvas over the sky and stars and under the planes: the rig's order, in one image. */
|
||||
const paintScrim = (hasPlanes: boolean) => {
|
||||
if (!isNight(sky)) return;
|
||||
const ctx = context2d(canvas);
|
||||
if (!ctx) return;
|
||||
const g = ctx.createLinearGradient(0, 0, 0, canvas.height);
|
||||
scrimStops(sky, hasPlanes).forEach(([o, c]) => g.addColorStop(o, c));
|
||||
ctx.fillStyle = g;
|
||||
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
||||
};
|
||||
|
||||
/** Paint the sky, the stars and the rain. Returns false when a 2D context is missing or there is no area yet. */
|
||||
const build = (): boolean => {
|
||||
teardown();
|
||||
|
|
@ -437,10 +462,9 @@ export function createRidgeScene(opts: RidgeOptions): Scene | null {
|
|||
teardown();
|
||||
cancelAnimationFrame(raf);
|
||||
raf = 0;
|
||||
// One order in every path: sky and stars, then the scrim, then the planes (the rig sits above the DOM scrim the same way).
|
||||
paintScrim(!!opts.backdrop);
|
||||
if (opts.backdrop) paintPlanes(opts.backdrop);
|
||||
// The planes are in the canvas now: the scrim, which fades out toward them, goes over the canvas.
|
||||
const scrim = makeScrim();
|
||||
built = scrim ? { scrim } : undefined;
|
||||
report(true);
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue