cuetake 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/package.json +1 -1
- package/src/cli.js +9 -3
- package/src/cursor.js +3 -32
- package/src/edit.js +37 -44
- package/src/look.js +72 -58
- package/src/pipeline.js +28 -24
- package/src/render-page.js +325 -192
- package/src/render.js +26 -11
- package/src/stage.js +28 -44
- package/src/styles/arrow.js +13 -0
- package/src/styles/browser.js +24 -0
- package/src/styles/dot.js +11 -0
- package/src/styles/fluted.glsl +35 -0
- package/src/styles/macos.js +12 -0
- package/src/styles/shadow.glsl +10 -0
- package/src/styles/spark.js +19 -0
- package/src/styles.js +105 -0
package/src/render.js
CHANGED
|
@@ -3,11 +3,13 @@ import { createRequire } from 'node:module';
|
|
|
3
3
|
import { dirname, join } from 'node:path';
|
|
4
4
|
import { fileURLToPath } from 'node:url';
|
|
5
5
|
import { chromium } from 'playwright';
|
|
6
|
+
import { contentType } from './styles.js';
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* Render runs in a headless Chromium page (ADR 0001). Files reach the page through intercepted
|
|
9
10
|
* requests on a private origin, so no port is opened: the Source is served with byte ranges and
|
|
10
|
-
* the Final arrives as positioned chunks written straight to disk.
|
|
11
|
+
* the Final arrives as positioned chunks written straight to disk. The page has no network: it runs
|
|
12
|
+
* agent-written code (ADR 0005), and anything that code tries to fetch from elsewhere is refused.
|
|
11
13
|
*/
|
|
12
14
|
const ORIGIN = 'https://render.cuetake.invalid';
|
|
13
15
|
// The bundled build is a single file the page can import; the package's own entry is many modules.
|
|
@@ -30,19 +32,23 @@ async function serveRange(route, path) {
|
|
|
30
32
|
try {
|
|
31
33
|
const body = Buffer.alloc(end - start + 1);
|
|
32
34
|
await handle.read(body, 0, body.length, start);
|
|
33
|
-
await route.fulfill({ status: match ? 206 : 200, body, headers: { 'content-type':
|
|
35
|
+
await route.fulfill({ status: match ? 206 : 200, body, headers: { 'content-type': contentType(path), 'accept-ranges': 'bytes', 'content-range': `bytes ${start}-${end}/${size}` } });
|
|
34
36
|
} finally { await handle.close(); }
|
|
35
37
|
}
|
|
36
38
|
|
|
37
39
|
/** Opens the render page with `files` ({ name: path }) readable and `writable` ({ name: handle }) writable. */
|
|
38
40
|
async function withRenderPage({ files = {}, writable = {} }, task) {
|
|
39
|
-
|
|
41
|
+
// An unreachable proxy stops what request interception does not see, such as WebSockets.
|
|
42
|
+
const browser = await chromium.launch({ headless: true, channel: 'chromium', proxy: { server: 'http://127.0.0.1:9' }, args: ['--force-webrtc-ip-handling-policy=disable_non_proxied_udp'] });
|
|
43
|
+
const blocked = [];
|
|
40
44
|
try {
|
|
41
45
|
const page = await browser.newPage();
|
|
42
46
|
const errors = [];
|
|
43
47
|
page.on('pageerror', error => errors.push(error.message));
|
|
44
|
-
await page.route(
|
|
45
|
-
const
|
|
48
|
+
await page.route('**/*', async route => {
|
|
49
|
+
const url = new URL(route.request().url());
|
|
50
|
+
if (url.origin !== ORIGIN) { blocked.push(url.href); return route.abort('blockedbyclient'); }
|
|
51
|
+
const { pathname, searchParams } = url;
|
|
46
52
|
const name = pathname.slice('/files/'.length);
|
|
47
53
|
if (route.request().method() === 'POST' && writable[name]) {
|
|
48
54
|
const body = route.request().postDataBuffer();
|
|
@@ -56,15 +62,24 @@ async function withRenderPage({ files = {}, writable = {} }, task) {
|
|
|
56
62
|
});
|
|
57
63
|
await page.goto(`${ORIGIN}/render.html`);
|
|
58
64
|
await page.waitForFunction(() => window.cuetakeRender, null, { timeout: 15_000 }).catch(() => { throw new Error(`Render page failed to load: ${errors.join('; ')}`); });
|
|
59
|
-
|
|
65
|
+
const result = await task(page).catch(error => { throw offline(error, blocked); });
|
|
66
|
+
if (blocked.length) throw offline(null, blocked);
|
|
67
|
+
return result;
|
|
60
68
|
} finally { await browser.close(); }
|
|
61
69
|
}
|
|
62
70
|
|
|
63
|
-
/**
|
|
64
|
-
|
|
71
|
+
/** Names what styling code tried to fetch, which the render page cannot. */
|
|
72
|
+
function offline(error, blocked) {
|
|
73
|
+
if (!blocked.length) return error;
|
|
74
|
+
const note = `The render page has no network and refused ${[...new Set(blocked)].join(', ')}; download it next to the Look or Take and name it by its relative path in \`files\``;
|
|
75
|
+
return new Error(error ? `${error.message}\n${note}` : note);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Composes the Final from a Source, Timeline and Edit; `files` serves the Edit's styling files (styles.js forRender). */
|
|
79
|
+
export async function renderFinal({ sourcePath, finalPath, timeline, edit, files = {} }) {
|
|
65
80
|
const handle = await fs.open(finalPath, 'w');
|
|
66
81
|
try {
|
|
67
|
-
return await withRenderPage({ files: { source: sourcePath }, writable: { final: handle } }, page => page.evaluate(options => window.cuetakeRender(options), {
|
|
82
|
+
return await withRenderPage({ files: { ...files, source: sourcePath }, writable: { final: handle } }, page => page.evaluate(options => window.cuetakeRender(options), {
|
|
68
83
|
sourceUrl: `${ORIGIN}/files/source`, finalUrl: `${ORIGIN}/files/final`, duration: timeline.duration,
|
|
69
84
|
viewport: timeline.viewport, pointer: timeline.pointer ?? [], events: timeline.events ?? [], edit,
|
|
70
85
|
}));
|
|
@@ -72,8 +87,8 @@ export async function renderFinal({ sourcePath, finalPath, timeline, edit }) {
|
|
|
72
87
|
}
|
|
73
88
|
|
|
74
89
|
/** A Preview PNG of `tiles` ([{ name, edit }]) at `at` seconds, or at the end with the camera wide. No Final is touched. */
|
|
75
|
-
export async function composePreview({ sourcePath, timeline, tiles, at }) {
|
|
76
|
-
const png = await withRenderPage({ files: { source: sourcePath } }, page => page.evaluate(options => window.cuetakePreview(options), {
|
|
90
|
+
export async function composePreview({ sourcePath, timeline, tiles, at, files = {} }) {
|
|
91
|
+
const png = await withRenderPage({ files: { ...files, source: sourcePath } }, page => page.evaluate(options => window.cuetakePreview(options), {
|
|
77
92
|
sourceUrl: `${ORIGIN}/files/source`, duration: timeline.duration, viewport: timeline.viewport,
|
|
78
93
|
pointer: timeline.pointer ?? [], events: timeline.events ?? [], tiles, at: at ?? null,
|
|
79
94
|
}));
|
package/src/stage.js
CHANGED
|
@@ -1,47 +1,40 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Stage layout shared by Node and the render page. Pure functions, no imports.
|
|
3
3
|
*
|
|
4
|
-
* The stage is the Final's pixel grid
|
|
5
|
-
* bar above it)
|
|
4
|
+
* The stage is the Final's pixel grid at camera zoom 1: the page sits in a window (with an optional
|
|
5
|
+
* chrome bar above it) inset by padding. The Background stays in output pixels whatever the camera does.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
export const
|
|
10
|
-
/** Chrome bar heights in page CSS pixels. */
|
|
11
|
-
export const CHROME_HEIGHT = { none: 0, macos: 28, browser: 44 };
|
|
8
|
+
/** The Final's pixel size. */
|
|
9
|
+
export const outputSize = edit => ({ width: edit.output.width, height: edit.output.height });
|
|
12
10
|
|
|
13
|
-
/** The
|
|
14
|
-
export function
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
? { width: even(short * viewport.width / viewport.height), height: short }
|
|
18
|
-
: { width: short, height: even(short * viewport.height / viewport.width) };
|
|
19
|
-
const long = even(short * 16 / 9);
|
|
20
|
-
return { '16:9': { width: long, height: short }, '9:16': { width: short, height: long }, '1:1': { width: short, height: short } }[edit.output.aspect];
|
|
11
|
+
/** The Edit with its output scaled so the short side is `short` pixels; everything else scales with it. */
|
|
12
|
+
export function atShortSide(edit, short) {
|
|
13
|
+
const { width, height } = edit.output, k = short / Math.min(width, height), even = n => Math.round(n * k / 2) * 2;
|
|
14
|
+
return { ...edit, output: { ...edit.output, width: even(width), height: even(height) } };
|
|
21
15
|
}
|
|
22
16
|
|
|
23
|
-
/**
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
const
|
|
31
|
-
const
|
|
32
|
-
const scale = Math.min((width -
|
|
33
|
-
const w = viewport.width * scale, h = (viewport.height +
|
|
34
|
-
const window = { x: (width - w) / 2, y: (height - h) / 2, w, h };
|
|
35
|
-
const chrome = chromeCss * scale;
|
|
17
|
+
/**
|
|
18
|
+
* Where the window, its chrome and the page sit on the stage, in stage pixels. `chrome` is the chrome
|
|
19
|
+
* bar's height in CSS pixels; `scale` is stage px per CSS px. The window is as large as fits inside the
|
|
20
|
+
* padding and centered in it.
|
|
21
|
+
*/
|
|
22
|
+
export function layout(edit, viewport, chrome = 0) {
|
|
23
|
+
const { width, height } = outputSize(edit), short = Math.min(width, height);
|
|
24
|
+
const padding = [edit.frame.padding ?? 0].flat();
|
|
25
|
+
const [top, right, bottom, left] = (padding.length === 4 ? padding : [padding[0], padding[0], padding[0], padding[0]]).map(p => p * short);
|
|
26
|
+
const scale = Math.min((width - left - right) / viewport.width, (height - top - bottom) / (viewport.height + chrome));
|
|
27
|
+
const w = viewport.width * scale, h = (viewport.height + chrome) * scale;
|
|
28
|
+
const window = { x: left + (width - left - right - w) / 2, y: top + (height - top - bottom - h) / 2, w, h };
|
|
36
29
|
return {
|
|
37
|
-
width, height, scale, chrome, radius: edit.frame.radius *
|
|
38
|
-
window, page: { x: window.x, y: window.y + chrome, w, h: h - chrome },
|
|
30
|
+
width, height, scale, chrome: chrome * scale, radius: (edit.frame.radius ?? 0) * short / 1080,
|
|
31
|
+
window, page: { x: window.x, y: window.y + chrome * scale, w, h: h - chrome * scale },
|
|
39
32
|
};
|
|
40
33
|
}
|
|
41
34
|
|
|
42
35
|
/**
|
|
43
36
|
* The camera for a viewport crop: the stage point at the Final's center and the zoom. Zoom 1 shows
|
|
44
|
-
* the whole stage; zooming keeps the view inside the stage, so
|
|
37
|
+
* the whole stage; zooming keeps the view inside the stage, so the Background shows beside the page near its edges.
|
|
45
38
|
*/
|
|
46
39
|
export function cameraView(crop, stage, viewport) {
|
|
47
40
|
const zoom = viewport.width / crop.w;
|
|
@@ -54,24 +47,15 @@ export function cameraView(crop, stage, viewport) {
|
|
|
54
47
|
};
|
|
55
48
|
}
|
|
56
49
|
|
|
57
|
-
/** Mesh backgrounds: a base color under soft radial blobs [x, y, radius] as fractions of the stage. */
|
|
58
|
-
export const BACKGROUNDS = {
|
|
59
|
-
aurora: { base: '#1e1b4b', blobs: [[0.15, 0.2, 0.7, '#7c3aed'], [0.85, 0.15, 0.6, '#db2777'], [0.7, 0.9, 0.75, '#2563eb'], [0.2, 0.85, 0.55, '#0ea5e9']] },
|
|
60
|
-
sunset: { base: '#431407', blobs: [[0.1, 0.15, 0.7, '#f97316'], [0.9, 0.25, 0.65, '#e11d48'], [0.6, 0.95, 0.7, '#a21caf'], [0.25, 0.8, 0.45, '#facc15']] },
|
|
61
|
-
ocean: { base: '#082f49', blobs: [[0.2, 0.1, 0.65, '#14b8a6'], [0.85, 0.3, 0.7, '#0284c7'], [0.4, 0.95, 0.75, '#1e3a8a'], [0.95, 0.9, 0.4, '#22d3ee']] },
|
|
62
|
-
graphite: { base: '#0a0a0a', blobs: [[0.3, 0.2, 0.7, '#3f3f46'], [0.8, 0.8, 0.7, '#27272a']] },
|
|
63
|
-
paper: { base: '#f4f4f5', blobs: [[0.5, 0, 0.8, '#ffffff'], [0.5, 1.1, 0.7, '#e4e4e7']] },
|
|
64
|
-
};
|
|
65
|
-
|
|
66
50
|
const hsl = (h, s, l) => {
|
|
67
51
|
const k = n => (n + h / 30) % 12, a = s * Math.min(l, 1 - l);
|
|
68
|
-
|
|
69
|
-
return '#' + [f(0), f(8), f(4)].map(v => Math.round(v * 255).toString(16).padStart(2, '0')).join('');
|
|
52
|
+
return [0, 8, 4].map(n => l - a * Math.max(-1, Math.min(k(n) - 3, 9 - k(n), 1)));
|
|
70
53
|
};
|
|
71
54
|
|
|
72
|
-
/**
|
|
73
|
-
export function
|
|
74
|
-
|
|
55
|
+
/** The brand color shaders receive, as RGB from 0 to 1: the page's brand hue, or a neutral gray for a colorless page. */
|
|
56
|
+
export function brandColor(rgba) {
|
|
57
|
+
const hue = brandHue(rgba);
|
|
58
|
+
return hue === null ? [0.55, 0.56, 0.6] : hsl(hue, 0.75, 0.55);
|
|
75
59
|
}
|
|
76
60
|
|
|
77
61
|
/** The hue of the most frequent saturated color in RGBA pixels, or null for a colorless page. */
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// The arrow cursor: filled with `color`, outlined in dark gray, with a soft shadow.
|
|
2
|
+
// Drawn in a 28×36 box whose tip at (2, 2) is the hotspot; CueTake scales the box to the cursor's size.
|
|
3
|
+
export const box = { width: 28, height: 36, hotspot: [2, 2] };
|
|
4
|
+
const ARROW = new Path2D('M2 2 L2 27 L8.5 21 L14 33 L19 30.5 L13.5 19 L23 19 Z');
|
|
5
|
+
|
|
6
|
+
export function draw(ctx, { params }) {
|
|
7
|
+
// Shadow sizes are in device pixels, so scale them by the box's current size on screen.
|
|
8
|
+
const pixels = ctx.getTransform().a;
|
|
9
|
+
ctx.shadowColor = '#00000055'; ctx.shadowBlur = 2 * pixels; ctx.shadowOffsetY = pixels;
|
|
10
|
+
ctx.fillStyle = params.color ?? '#ffffff'; ctx.fill(ARROW);
|
|
11
|
+
ctx.shadowColor = 'transparent';
|
|
12
|
+
ctx.lineJoin = 'round'; ctx.lineWidth = 1.7; ctx.strokeStyle = '#171717'; ctx.stroke(ARROW);
|
|
13
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// A browser window's top bar: traffic lights and a centered address field showing the url.
|
|
2
|
+
// Parameters: dark (a dark bar). Drawn in CSS pixels; CueTake clips it to the window's rounded top.
|
|
3
|
+
export const height = 44;
|
|
4
|
+
|
|
5
|
+
export function draw(ctx, { width, height, url, params }) {
|
|
6
|
+
const t = params.dark
|
|
7
|
+
? { bar: '#2a2a2e', line: '#111113', field: '#3a3a40', fieldLine: '#4a4a52', text: '#d4d4d8', lock: '#a1a1aa' }
|
|
8
|
+
: { bar: '#f3f3f5', line: '#d4d4d8', field: '#ffffff', fieldLine: '#e4e4e7', text: '#52525b', lock: '#71717a' };
|
|
9
|
+
ctx.fillStyle = t.bar; ctx.fillRect(0, 0, width, height);
|
|
10
|
+
ctx.fillStyle = t.line; ctx.fillRect(0, height - 1, width, 1);
|
|
11
|
+
['#ff5f57', '#febc2e', '#28c840'].forEach((light, i) => {
|
|
12
|
+
ctx.beginPath(); ctx.arc(20 + i * 20, height / 2, 6, 0, 2 * Math.PI);
|
|
13
|
+
ctx.fillStyle = light; ctx.fill(); ctx.strokeStyle = '#0000001f'; ctx.lineWidth = 0.5; ctx.stroke();
|
|
14
|
+
});
|
|
15
|
+
const w = 420, h = 28, x = (width - w) / 2, y = (height - h) / 2;
|
|
16
|
+
ctx.beginPath(); ctx.roundRect(x, y, w, h, h / 2);
|
|
17
|
+
ctx.fillStyle = t.field; ctx.fill(); ctx.strokeStyle = t.fieldLine; ctx.lineWidth = 0.8; ctx.stroke();
|
|
18
|
+
ctx.font = '13px system-ui, -apple-system, sans-serif'; ctx.textBaseline = 'middle';
|
|
19
|
+
const left = width / 2 - (ctx.measureText(url).width + 16) / 2;
|
|
20
|
+
ctx.fillStyle = t.lock; // padlock
|
|
21
|
+
ctx.beginPath(); ctx.roundRect(left, y + 13, 9, 7, 1.5); ctx.fill();
|
|
22
|
+
ctx.strokeStyle = t.lock; ctx.lineWidth = 1.3; ctx.beginPath(); ctx.arc(left + 4.5, y + 13, 2.8, Math.PI, 0); ctx.stroke();
|
|
23
|
+
ctx.fillStyle = t.text; ctx.fillText(url, left + 16, height / 2 + 0.5);
|
|
24
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// A round dot cursor filled with `color` and outlined in dark gray; its center is the hotspot.
|
|
2
|
+
export const box = { width: 24, height: 24, hotspot: [12, 12] };
|
|
3
|
+
|
|
4
|
+
export function draw(ctx, { params }) {
|
|
5
|
+
const pixels = ctx.getTransform().a;
|
|
6
|
+
ctx.shadowColor = '#00000055'; ctx.shadowBlur = 2 * pixels; ctx.shadowOffsetY = pixels;
|
|
7
|
+
ctx.beginPath(); ctx.arc(12, 12, 12, 0, 2 * Math.PI);
|
|
8
|
+
ctx.fillStyle = params.color ?? '#ffffff'; ctx.fill();
|
|
9
|
+
ctx.shadowColor = 'transparent';
|
|
10
|
+
ctx.lineWidth = 1.5; ctx.strokeStyle = '#171717'; ctx.beginPath(); ctx.arc(12, 12, 11.25, 0, 2 * Math.PI); ctx.stroke();
|
|
11
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Fluted glass: soft light in the page's brand color seen through glass ribs, over near black.
|
|
2
|
+
// Parameters: count (ribs across the short side), strength (how far a rib bends the light, as a
|
|
3
|
+
// fraction of the short side), angle (rib tilt in degrees). Set uBrand to light it in another color.
|
|
4
|
+
|
|
5
|
+
vec3 hueShift(vec3 c, float turn) {
|
|
6
|
+
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
|
|
7
|
+
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
|
|
8
|
+
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
|
|
9
|
+
float d = q.x - min(q.w, q.y);
|
|
10
|
+
vec3 hsv = vec3(fract(abs(q.z + (q.w - q.y) / (6.0 * d + 1e-10)) + turn), d / (q.x + 1e-10), q.x);
|
|
11
|
+
return hsv.z * mix(vec3(1.0), clamp(abs(mod(hsv.x * 6.0 + vec3(0.0, 4.0, 2.0), 6.0) - 3.0) - 1.0, 0.0, 1.0), hsv.y);
|
|
12
|
+
}
|
|
13
|
+
float blob(vec2 uv, vec2 c, float r) { return exp(-dot(uv - c, uv - c) / (r * r)); }
|
|
14
|
+
// Light placed where the margins around the window show; uv is 0–1 across the short side, centered.
|
|
15
|
+
vec3 light(vec2 uv) {
|
|
16
|
+
return uBrand * 0.06
|
|
17
|
+
+ uBrand * blob(uv, vec2(0.15, 0.95), 0.42)
|
|
18
|
+
+ hueShift(uBrand, 0.1) * blob(uv, vec2(0.92, 0.08), 0.38) * 0.9
|
|
19
|
+
+ hueShift(uBrand, -0.11) * blob(uv, vec2(0.85, 0.9), 0.3) * 0.7
|
|
20
|
+
+ hueShift(uBrand, 0.04) * blob(uv, vec2(0.05, 0.15), 0.25) * 0.5;
|
|
21
|
+
}
|
|
22
|
+
float hash(vec2 p) { return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453); }
|
|
23
|
+
|
|
24
|
+
void main() {
|
|
25
|
+
vec2 px = pixel(), center = uResolution * 0.5;
|
|
26
|
+
float side = min(uResolution.x, uResolution.y), a = radians(angle);
|
|
27
|
+
vec2 across = vec2(cos(a), sin(a));
|
|
28
|
+
float w = side / count, u = fract(dot(px - center, across) / w);
|
|
29
|
+
vec2 bent = px + across * (u - 0.5) * strength * side;
|
|
30
|
+
vec3 c = light((bent - center) / side + 0.5);
|
|
31
|
+
c *= 0.78 + 0.34 * pow(u, 1.6); // each rib darkens toward one edge
|
|
32
|
+
c += 0.05 * smoothstep(0.9, 1.0, u); // and catches a highlight on the other
|
|
33
|
+
c += (hash(px) - 0.5) * 0.05; // grain keeps H.264 from banding the light
|
|
34
|
+
color = vec4(c, 1.0);
|
|
35
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// A macOS app window's title bar: traffic lights only. Parameters: dark (a dark bar).
|
|
2
|
+
// Drawn in CSS pixels; CueTake clips it to the window's rounded top.
|
|
3
|
+
export const height = 28;
|
|
4
|
+
|
|
5
|
+
export function draw(ctx, { width, height, params }) {
|
|
6
|
+
ctx.fillStyle = params.dark ? '#2a2a2e' : '#ececef'; ctx.fillRect(0, 0, width, height);
|
|
7
|
+
ctx.fillStyle = params.dark ? '#111113' : '#d4d4d8'; ctx.fillRect(0, height - 1, width, 1);
|
|
8
|
+
['#ff5f57', '#febc2e', '#28c840'].forEach((light, i) => {
|
|
9
|
+
ctx.beginPath(); ctx.arc(16 + i * 20, height / 2, 6, 0, 2 * Math.PI);
|
|
10
|
+
ctx.fillStyle = light; ctx.fill(); ctx.strokeStyle = '#0000001f'; ctx.lineWidth = 0.5; ctx.stroke();
|
|
11
|
+
});
|
|
12
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// A soft drop shadow under the window and a faint light rim around it. It follows the window as the
|
|
2
|
+
// camera zooms and moves it. Parameters: blur and offset (1080p pixels at zoom 1), opacity and rim (0–1).
|
|
3
|
+
|
|
4
|
+
void main() {
|
|
5
|
+
vec2 px = pixel();
|
|
6
|
+
float k = uUnit * uZoom;
|
|
7
|
+
float shadow = opacity * (1.0 - smoothstep(-blur * k, blur * k, windowDistance(px - vec2(0.0, offset * k))));
|
|
8
|
+
float edge = rim * (1.0 - smoothstep(0.0, 1.5, abs(windowDistance(px))));
|
|
9
|
+
color = vec4(vec3(edge), edge + shadow * (1.0 - edge));
|
|
10
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// A spark: `count` rays burst out from the click to `radius` CSS px, shrinking from `length` and fading.
|
|
2
|
+
// Rays are the cursor's color unless `color` is set, with a dark edge so they read on any page.
|
|
3
|
+
export const reach = ({ radius, length }) => radius + length + 8;
|
|
4
|
+
|
|
5
|
+
export function draw(ctx, { progress, params, cursor }) {
|
|
6
|
+
const { radius, length: rayLength, count } = params, color = params.color ?? cursor.color ?? '#ffffff';
|
|
7
|
+
const eased = 1 - (1 - progress) ** 3;
|
|
8
|
+
const distance = 5 + eased * radius, length = rayLength * (1 - eased);
|
|
9
|
+
ctx.globalAlpha = 1 - progress;
|
|
10
|
+
ctx.lineCap = 'round';
|
|
11
|
+
for (let i = 0; i < count; i++) {
|
|
12
|
+
const angle = 2 * Math.PI * i / count + Math.PI / 8;
|
|
13
|
+
ctx.beginPath();
|
|
14
|
+
ctx.moveTo(distance * Math.cos(angle), distance * Math.sin(angle));
|
|
15
|
+
ctx.lineTo((distance + length) * Math.cos(angle), (distance + length) * Math.sin(angle));
|
|
16
|
+
ctx.strokeStyle = '#171717'; ctx.lineWidth = 4.5; ctx.stroke();
|
|
17
|
+
ctx.strokeStyle = color; ctx.lineWidth = 2.5; ctx.stroke();
|
|
18
|
+
}
|
|
19
|
+
}
|
package/src/styles.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import { basename, extname, isAbsolute, join, relative, resolve } from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The files an Edit or Look draws with (ADR 0005). A ref with an extension is a file, relative to the
|
|
7
|
+
* Take, Look or Scenario that names it; a ref without one is a built-in from src/styles, with the
|
|
8
|
+
* parameters it starts from. Built-ins are ordinary files agents can read and copy.
|
|
9
|
+
*/
|
|
10
|
+
export const BUILTIN_DIR = fileURLToPath(new URL('./styles/', import.meta.url));
|
|
11
|
+
export const BUILTINS = {
|
|
12
|
+
fluted: { file: 'fluted.glsl', params: { count: 36, strength: 0.09, angle: 0 } },
|
|
13
|
+
shadow: { file: 'shadow.glsl', params: { blur: 60, offset: 24, opacity: 0.5, rim: 0.12 } },
|
|
14
|
+
browser: { file: 'browser.js', params: { dark: false } },
|
|
15
|
+
'browser-dark': { file: 'browser.js', params: { dark: true } },
|
|
16
|
+
macos: { file: 'macos.js', params: { dark: false } },
|
|
17
|
+
'macos-dark': { file: 'macos.js', params: { dark: true } },
|
|
18
|
+
arrow: { file: 'arrow.js', params: {} },
|
|
19
|
+
dot: { file: 'dot.js', params: {} },
|
|
20
|
+
spark: { file: 'spark.js', params: { radius: 32, length: 16, count: 8 } },
|
|
21
|
+
};
|
|
22
|
+
export const KINDS = ['shader', 'draw', 'image'];
|
|
23
|
+
export const isFile = ref => typeof ref === 'string' && extname(ref) !== '';
|
|
24
|
+
|
|
25
|
+
/** Every drawn item an Edit or (partial) Look holds. */
|
|
26
|
+
function items(style) {
|
|
27
|
+
const { frame, cursor, clickEffect } = style ?? {};
|
|
28
|
+
return [...(Array.isArray(frame?.background) ? frame.background : []), ...(Array.isArray(frame?.effects) ? frame.effects : []), frame?.chrome, cursor, clickEffect]
|
|
29
|
+
.filter(item => item && typeof item === 'object');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** A copy of `style` with every file ref (an item's drawing and its `files`) replaced by `map(ref)`. */
|
|
33
|
+
export function mapFiles(style, map) {
|
|
34
|
+
const copy = structuredClone(style);
|
|
35
|
+
for (const item of items(copy)) {
|
|
36
|
+
for (const key of KINDS) if (isFile(item[key])) item[key] = map(item[key]);
|
|
37
|
+
if (item.files) item.files = Object.fromEntries(Object.entries(item.files).map(([name, ref]) => [name, isFile(ref) ? map(ref) : ref]));
|
|
38
|
+
}
|
|
39
|
+
return copy;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The file refs `style` names, in order, without repeats. */
|
|
43
|
+
export function fileRefs(style) {
|
|
44
|
+
const refs = new Set();
|
|
45
|
+
mapFiles(style, ref => refs.add(ref));
|
|
46
|
+
return [...refs];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The shader and drawing-code files `style` runs, for an agent to review before using a Look. */
|
|
50
|
+
export function codeRefs(style) {
|
|
51
|
+
return [...new Set(items(style).flatMap(item => ['shader', 'draw'].map(key => item[key]).filter(isFile)))];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Refs made absolute against `dir`, so styling from several places can be merged. */
|
|
55
|
+
export const absolute = (style, dir) => mapFiles(style, ref => resolve(dir, ref));
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Copies every file `style` names (resolved against `fromDir`) into `toDir` and points the refs at the
|
|
59
|
+
* copies as `prefix + name`. Files already inside `toDir` stay; a different file with a taken name gets a suffix.
|
|
60
|
+
*/
|
|
61
|
+
export async function bundle(style, fromDir, toDir, prefix = '') {
|
|
62
|
+
const copies = new Map();
|
|
63
|
+
for (const ref of fileRefs(style)) {
|
|
64
|
+
const source = resolve(fromDir, ref);
|
|
65
|
+
if (!relative(toDir, source).startsWith('..') && !isAbsolute(relative(toDir, source))) { copies.set(ref, prefix + relative(toDir, source)); continue; }
|
|
66
|
+
const bytes = await fs.readFile(source);
|
|
67
|
+
await fs.mkdir(toDir, { recursive: true });
|
|
68
|
+
const ext = extname(source), stem = basename(source, ext);
|
|
69
|
+
for (let n = 1; ; n++) {
|
|
70
|
+
const name = n === 1 ? stem + ext : `${stem}-${n}${ext}`, target = join(toDir, name);
|
|
71
|
+
const existing = await fs.readFile(target).catch(error => { if (error.code === 'ENOENT') return null; throw error; });
|
|
72
|
+
if (existing && !existing.equals(bytes)) continue;
|
|
73
|
+
if (!existing) await fs.writeFile(target, bytes);
|
|
74
|
+
copies.set(ref, prefix + name);
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return mapFiles(style, ref => copies.get(ref));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const PAGE_TYPES = { '.js': 'text/javascript', '.mjs': 'text/javascript', '.glsl': 'text/plain', '.frag': 'text/plain', '.svg': 'image/svg+xml', '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.webp': 'image/webp', '.woff2': 'font/woff2', '.woff': 'font/woff', '.ttf': 'font/ttf', '.otf': 'font/otf' };
|
|
82
|
+
export const contentType = path => PAGE_TYPES[extname(path).toLowerCase()] ?? 'application/octet-stream';
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The Edit as the render page reads it: each drawing becomes a URL under `/files/` (with its built-in
|
|
86
|
+
* parameters underneath the item's own). Refs resolve against `dir`; `files` collects the served
|
|
87
|
+
* names and paths, and may be shared by several Edits.
|
|
88
|
+
*/
|
|
89
|
+
export function forRender(edit, dir, files = {}) {
|
|
90
|
+
const serve = path => {
|
|
91
|
+
let name = Object.keys(files).find(key => files[key] === path);
|
|
92
|
+
if (!name) { name = `style-${Object.keys(files).length}${extname(path)}`; files[name] = path; }
|
|
93
|
+
return `/files/${name}`;
|
|
94
|
+
};
|
|
95
|
+
const copy = structuredClone(edit);
|
|
96
|
+
for (const item of items(copy)) {
|
|
97
|
+
const kind = KINDS.find(key => item[key] !== undefined);
|
|
98
|
+
if (!kind) continue;
|
|
99
|
+
const ref = item[kind], builtin = !isFile(ref) && BUILTINS[ref];
|
|
100
|
+
if (!isFile(ref) && !builtin) throw new Error(`No built-in named '${ref}'; built-ins are ${Object.keys(BUILTINS).join(', ')}, or name a file with its extension`);
|
|
101
|
+
Object.assign(item, { ...(builtin ? builtin.params : {}), ...item, ref, kind, url: serve(builtin ? join(BUILTIN_DIR, builtin.file) : resolve(dir, ref)) });
|
|
102
|
+
if (item.files) item.files = Object.fromEntries(Object.entries(item.files).map(([name, file]) => [name, serve(resolve(dir, file))]));
|
|
103
|
+
}
|
|
104
|
+
return { edit: copy, files };
|
|
105
|
+
}
|