agentcut 0.2.4 → 0.2.6
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/dist/studio/workspace.js +4 -0
- package/dist/studio/workspace.js.map +1 -1
- package/package.json +1 -1
- package/template/components/CATALOG.md +52 -0
- package/template/components/README.md +70 -61
- package/template/components/STYLES.md +2 -0
- package/template/components/catalog.json +477 -0
- package/template/components/catalog.ts +4 -0
- package/template/components/cinematic/Cinematic.tsx +1093 -0
- package/template/components/cinematic/Device3D.tsx +324 -0
- package/template/components/cinematic/devices3d/display-model.ts +184 -0
- package/template/components/cinematic/devices3d/iphone-model.ts +643 -0
- package/template/components/cinematic/devices3d/macbook-model.ts +604 -0
- package/template/components/cinematic/devices3d/models.ts +657 -0
- package/template/components/cinematic/devices3d/runtime.ts +267 -0
- package/template/components/cinematic/math.ts +100 -0
- package/template/components/dot-trace-title/DotTraceTitle.tsx +181 -0
- package/template/components/dot-trace-title/meta.json +51 -0
- package/template/components/generate/GenerateReveal.tsx +381 -0
- package/template/components/index.ts +36 -0
- package/template/components/lib.ts +18 -6
- package/template/components/loops/LoopCanvas.tsx +132 -0
- package/template/components/loops/engine.ts +184 -0
- package/template/components/loops/library.ts +667 -0
- package/template/components/loops/library2.ts +777 -0
- package/template/components/motion-loop/MotionLoop.tsx +37 -0
- package/template/components/motion-loop/meta.json +312 -0
- package/template/skill/SKILL.md +16 -1
- package/template/skill/references/browser-export.md +61 -61
- package/template/skill/references/cinematic.md +184 -0
- package/template/skill/references/loops.md +151 -0
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
// three.js side of <Device3D>: one renderer per device, lit like ScreenSlick's device stage, drawn
|
|
2
|
+
// through the cinematic Stage's camera. Loaded on demand so pages without 3D devices don't pay for three.
|
|
3
|
+
import * as THREE from "three";
|
|
4
|
+
import { RoomEnvironment } from "three/examples/jsm/environments/RoomEnvironment.js";
|
|
5
|
+
import {
|
|
6
|
+
disposeVideoDeviceModel,
|
|
7
|
+
loadVideoDeviceModel,
|
|
8
|
+
type LoadedVideoDeviceModel,
|
|
9
|
+
type VideoDeviceModelId,
|
|
10
|
+
} from "./models";
|
|
11
|
+
import { setMacbookLidAngle } from "./macbook-model";
|
|
12
|
+
|
|
13
|
+
const SCREEN_VERTEX = /* glsl */ `
|
|
14
|
+
varying vec2 vUv;
|
|
15
|
+
varying vec3 vViewNormal;
|
|
16
|
+
varying vec3 vViewDir;
|
|
17
|
+
void main() {
|
|
18
|
+
vUv = uv;
|
|
19
|
+
vec4 mvPosition = modelViewMatrix * vec4(position, 1.0);
|
|
20
|
+
vViewNormal = normalize(normalMatrix * normal);
|
|
21
|
+
vViewDir = -mvPosition.xyz;
|
|
22
|
+
gl_Position = projectionMatrix * mvPosition;
|
|
23
|
+
}
|
|
24
|
+
`;
|
|
25
|
+
|
|
26
|
+
const SCREEN_FRAGMENT = /* glsl */ `
|
|
27
|
+
uniform sampler2D map;
|
|
28
|
+
uniform mat3 uvTransform;
|
|
29
|
+
uniform float brightness;
|
|
30
|
+
uniform float hasMap;
|
|
31
|
+
uniform vec3 offColor;
|
|
32
|
+
varying vec2 vUv;
|
|
33
|
+
varying vec3 vViewNormal;
|
|
34
|
+
varying vec3 vViewDir;
|
|
35
|
+
void main() {
|
|
36
|
+
vec2 uv = (uvTransform * vec3(vUv, 1.0)).xy;
|
|
37
|
+
float inside = step(0.0, uv.x) * step(uv.x, 1.0) * step(0.0, uv.y) * step(uv.y, 1.0);
|
|
38
|
+
vec3 color = offColor;
|
|
39
|
+
if (hasMap > 0.5) color = texture2D(map, clamp(uv, 0.0, 1.0)).rgb * inside;
|
|
40
|
+
// Real displays go dark edge-on.
|
|
41
|
+
float facing = abs(dot(normalize(vViewNormal), normalize(vViewDir)));
|
|
42
|
+
float grazing = smoothstep(0.06, 0.32, facing);
|
|
43
|
+
gl_FragColor = vec4(mix(offColor, color, brightness * grazing), 1.0);
|
|
44
|
+
#include <colorspace_fragment>
|
|
45
|
+
}
|
|
46
|
+
`;
|
|
47
|
+
|
|
48
|
+
export interface DeviceFrameParams {
|
|
49
|
+
width: number;
|
|
50
|
+
height: number;
|
|
51
|
+
/** Viewer distance of the Stage projection (px). */
|
|
52
|
+
D: number;
|
|
53
|
+
fov: number;
|
|
54
|
+
/** Screen-space shift of the Stage's target (px). */
|
|
55
|
+
shiftX: number;
|
|
56
|
+
shiftY: number;
|
|
57
|
+
/** Stage camera-space placement (row-major 4×4, CSS axes, px): view × model. */
|
|
58
|
+
matrix: number[];
|
|
59
|
+
/** Largest dimension of the device in camera-space px at scale 1. */
|
|
60
|
+
size: number;
|
|
61
|
+
exposure: number;
|
|
62
|
+
lidAngle: number;
|
|
63
|
+
/** Where the screenshot sits when it's taller than the screen: 0 top … 1 bottom. */
|
|
64
|
+
scroll: number;
|
|
65
|
+
pixelRatio: number;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface DeviceRenderer {
|
|
69
|
+
readonly screenAspect: number;
|
|
70
|
+
readonly targetSize: number;
|
|
71
|
+
/**
|
|
72
|
+
* Where a point of the screenshot (fractions, y down) sits on the device, in model units (y up),
|
|
73
|
+
* relative to the device center, for the given scroll and lid angle. Lifts start from these points.
|
|
74
|
+
*/
|
|
75
|
+
imagePoint(x: number, y: number, scroll: number, lidAngle: number): [number, number, number];
|
|
76
|
+
setImage(image: HTMLImageElement | null): void;
|
|
77
|
+
render(params: DeviceFrameParams): void;
|
|
78
|
+
dispose(): void;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export async function createDeviceRenderer(
|
|
82
|
+
canvas: HTMLCanvasElement,
|
|
83
|
+
id: VideoDeviceModelId,
|
|
84
|
+
finishColor?: string,
|
|
85
|
+
/** A GLB the user brings (in place of the bundled model, or required for external models). */
|
|
86
|
+
modelSrc?: string,
|
|
87
|
+
): Promise<DeviceRenderer> {
|
|
88
|
+
const renderer = new THREE.WebGLRenderer({ canvas, alpha: true, antialias: true, preserveDrawingBuffer: true });
|
|
89
|
+
renderer.setPixelRatio(1);
|
|
90
|
+
renderer.outputColorSpace = THREE.SRGBColorSpace;
|
|
91
|
+
renderer.toneMapping = THREE.ACESFilmicToneMapping;
|
|
92
|
+
renderer.toneMappingExposure = 1.12;
|
|
93
|
+
renderer.setClearColor(0x000000, 0);
|
|
94
|
+
|
|
95
|
+
const scene = new THREE.Scene();
|
|
96
|
+
const pmrem = new THREE.PMREMGenerator(renderer);
|
|
97
|
+
const room = new RoomEnvironment();
|
|
98
|
+
const environment = pmrem.fromScene(room, 0.04).texture;
|
|
99
|
+
room.traverse((object) => {
|
|
100
|
+
if (object instanceof THREE.Mesh) {
|
|
101
|
+
object.geometry.dispose();
|
|
102
|
+
(object.material as THREE.Material).dispose();
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
pmrem.dispose();
|
|
106
|
+
scene.environment = environment;
|
|
107
|
+
// Laptops and displays are big flat aluminium and black keys: a softer, darker studio keeps the
|
|
108
|
+
// anodised finish and the keyboard from washing out. Phones keep the brighter rig.
|
|
109
|
+
const studio = id === "procedural-macbook" || id === "macbook-pro-14" || id === "procedural-display";
|
|
110
|
+
scene.environmentIntensity = studio ? 0.6 : 1;
|
|
111
|
+
const baseExposure = studio ? 0.98 : 1.12;
|
|
112
|
+
|
|
113
|
+
const camera = new THREE.PerspectiveCamera(24, 16 / 9, 1, 100000);
|
|
114
|
+
scene.add(camera);
|
|
115
|
+
scene.add(new THREE.HemisphereLight(0xf1f5ff, 0x14161b, studio ? 0.35 : 1.4));
|
|
116
|
+
// Lights ride with the camera so every angle stays product-lit (ScreenSlick's rig).
|
|
117
|
+
for (const [color, intensity, position] of [
|
|
118
|
+
[0xfff4e7, studio ? 3.4 : 5.2, [-2.8, 3.4, 4.4]],
|
|
119
|
+
[0x86aaff, studio ? 2.4 : 3.1, [3.5, 1.5, -3.2]],
|
|
120
|
+
[0xffffff, studio ? 0.6 : 1.4, [1.8, -1.4, 3.2]],
|
|
121
|
+
] as const) {
|
|
122
|
+
const light = new THREE.DirectionalLight(color, intensity);
|
|
123
|
+
light.position.set(position[0], position[1], position[2]);
|
|
124
|
+
camera.add(light);
|
|
125
|
+
camera.add(light.target);
|
|
126
|
+
light.target.position.set(0, 0, -3);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const screenMaterial = new THREE.ShaderMaterial({
|
|
130
|
+
uniforms: {
|
|
131
|
+
map: { value: null },
|
|
132
|
+
uvTransform: { value: new THREE.Matrix3() },
|
|
133
|
+
brightness: { value: 1 },
|
|
134
|
+
hasMap: { value: 0 },
|
|
135
|
+
offColor: { value: new THREE.Color(0x07080b) },
|
|
136
|
+
},
|
|
137
|
+
vertexShader: SCREEN_VERTEX,
|
|
138
|
+
fragmentShader: SCREEN_FRAGMENT,
|
|
139
|
+
side: THREE.DoubleSide,
|
|
140
|
+
toneMapped: false,
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
let model: LoadedVideoDeviceModel | null = null;
|
|
144
|
+
try {
|
|
145
|
+
model = await loadVideoDeviceModel(id, screenMaterial, finishColor, modelSrc);
|
|
146
|
+
if (!model) throw new Error(`Couldn't load the ${id} device model.`);
|
|
147
|
+
} catch (error) {
|
|
148
|
+
// Free the GPU side before giving up: a failed load must not keep a WebGL context alive.
|
|
149
|
+
screenMaterial.dispose();
|
|
150
|
+
environment.dispose();
|
|
151
|
+
renderer.dispose();
|
|
152
|
+
renderer.forceContextLoss();
|
|
153
|
+
throw error;
|
|
154
|
+
}
|
|
155
|
+
const holder = new THREE.Group();
|
|
156
|
+
holder.matrixAutoUpdate = false;
|
|
157
|
+
holder.add(model.root);
|
|
158
|
+
scene.add(holder);
|
|
159
|
+
|
|
160
|
+
let texture: THREE.Texture | null = null;
|
|
161
|
+
let imageAspect = model.config.screenAspect;
|
|
162
|
+
let lastScroll = -1;
|
|
163
|
+
|
|
164
|
+
// Cover the screen; a taller screenshot shows its top (or scrolls), a wider one is centered.
|
|
165
|
+
// Screen uv → image uv: (tx + sx·u, ty + sy·v).
|
|
166
|
+
const uvFit = (scroll: number) => {
|
|
167
|
+
const screenAspect = model.config.screenAspect;
|
|
168
|
+
let repeatX = 1;
|
|
169
|
+
let repeatY = 1;
|
|
170
|
+
if (imageAspect > screenAspect) repeatX = screenAspect / imageAspect;
|
|
171
|
+
else repeatY = imageAspect / screenAspect;
|
|
172
|
+
const offsetX = (1 - repeatX) / 2;
|
|
173
|
+
const s = Math.min(1, Math.max(0, scroll));
|
|
174
|
+
// flipY puts the image's top at v = 1; without it (glTF UVs) the top is at v = 0.
|
|
175
|
+
const offsetY = (1 - repeatY) * (model.config.textureFlipY ? 1 - s : s);
|
|
176
|
+
const mirror = model.config.textureMirrorX;
|
|
177
|
+
return { tx: mirror ? 1 - offsetX : offsetX, ty: offsetY, sx: mirror ? -repeatX : repeatX, sy: repeatY };
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
const fitTexture = (scroll: number) => {
|
|
181
|
+
const f = uvFit(scroll);
|
|
182
|
+
(screenMaterial.uniforms.uvTransform.value as THREE.Matrix3).setUvTransform(f.tx, f.ty, f.sx, f.sy, 0, 0, 0);
|
|
183
|
+
lastScroll = scroll;
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
const relative = new THREE.Matrix4();
|
|
187
|
+
const point = new THREE.Vector3();
|
|
188
|
+
|
|
189
|
+
// CSS camera space (y down) ↔ three (y up): conjugate by a y flip.
|
|
190
|
+
const flip = new THREE.Matrix4().makeScale(1, -1, 1);
|
|
191
|
+
const placement = new THREE.Matrix4();
|
|
192
|
+
const scale = new THREE.Matrix4();
|
|
193
|
+
|
|
194
|
+
return {
|
|
195
|
+
screenAspect: model.config.screenAspect,
|
|
196
|
+
targetSize: model.config.targetSize,
|
|
197
|
+
imagePoint(x, y, scroll, lidAngle) {
|
|
198
|
+
if (id === "procedural-macbook") setMacbookLidAngle(model.root, lidAngle);
|
|
199
|
+
model.root.updateMatrixWorld(true);
|
|
200
|
+
holder.updateMatrixWorld(true);
|
|
201
|
+
relative.copy(holder.matrixWorld).invert().multiply(model.screen.matrixWorld);
|
|
202
|
+
const f = uvFit(scroll);
|
|
203
|
+
// With flipY image v runs up (the top of the screenshot is v = 1); without it, v runs down.
|
|
204
|
+
const u = (x - f.tx) / f.sx;
|
|
205
|
+
const v = ((model.config.textureFlipY ? 1 - y : y) - f.ty) / f.sy;
|
|
206
|
+
const frame = model.screenFrame;
|
|
207
|
+
point.copy(frame.origin).addScaledVector(frame.du, u).addScaledVector(frame.dv, v).applyMatrix4(relative);
|
|
208
|
+
return [point.x, point.y, point.z];
|
|
209
|
+
},
|
|
210
|
+
setImage(image) {
|
|
211
|
+
texture?.dispose();
|
|
212
|
+
texture = null;
|
|
213
|
+
if (!image) {
|
|
214
|
+
screenMaterial.uniforms.map.value = null;
|
|
215
|
+
screenMaterial.uniforms.hasMap.value = 0;
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
texture = new THREE.Texture(image);
|
|
219
|
+
texture.colorSpace = THREE.SRGBColorSpace;
|
|
220
|
+
texture.flipY = model.config.textureFlipY;
|
|
221
|
+
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
|
|
222
|
+
texture.needsUpdate = true;
|
|
223
|
+
imageAspect = image.naturalWidth / Math.max(1, image.naturalHeight);
|
|
224
|
+
screenMaterial.uniforms.map.value = texture;
|
|
225
|
+
screenMaterial.uniforms.hasMap.value = 1;
|
|
226
|
+
lastScroll = -1;
|
|
227
|
+
},
|
|
228
|
+
render(p) {
|
|
229
|
+
if (canvas.width !== Math.round(p.width * p.pixelRatio) || canvas.height !== Math.round(p.height * p.pixelRatio)) {
|
|
230
|
+
renderer.setPixelRatio(p.pixelRatio);
|
|
231
|
+
renderer.setSize(p.width, p.height, false);
|
|
232
|
+
}
|
|
233
|
+
renderer.toneMappingExposure = baseExposure * p.exposure;
|
|
234
|
+
if (p.scroll !== lastScroll) fitTexture(p.scroll);
|
|
235
|
+
if (id === "procedural-macbook") setMacbookLidAngle(model.root, p.lidAngle);
|
|
236
|
+
|
|
237
|
+
camera.fov = p.fov;
|
|
238
|
+
camera.aspect = p.width / p.height;
|
|
239
|
+
camera.near = Math.max(1, p.D * 0.02);
|
|
240
|
+
camera.far = p.D * 40;
|
|
241
|
+
camera.position.set(0, 0, p.D);
|
|
242
|
+
camera.lookAt(0, 0, 0);
|
|
243
|
+
camera.setViewOffset(p.width, p.height, -p.shiftX, -p.shiftY, p.width, p.height);
|
|
244
|
+
camera.updateProjectionMatrix();
|
|
245
|
+
|
|
246
|
+
// Row-major (CSS) → three's column-major set().
|
|
247
|
+
const m = p.matrix;
|
|
248
|
+
placement.set(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8], m[9], m[10], m[11], m[12], m[13], m[14], m[15]);
|
|
249
|
+
const k = p.size / model.config.targetSize;
|
|
250
|
+
scale.makeScale(k, k, k);
|
|
251
|
+
holder.matrix.copy(flip).multiply(placement).multiply(flip).multiply(scale);
|
|
252
|
+
holder.matrixWorldNeedsUpdate = true;
|
|
253
|
+
renderer.render(scene, camera);
|
|
254
|
+
},
|
|
255
|
+
dispose() {
|
|
256
|
+
texture?.dispose();
|
|
257
|
+
disposeVideoDeviceModel(model);
|
|
258
|
+
screenMaterial.dispose();
|
|
259
|
+
environment.dispose();
|
|
260
|
+
renderer.dispose();
|
|
261
|
+
// Browsers keep only ~16 WebGL contexts; release this one now instead of at garbage collection.
|
|
262
|
+
renderer.forceContextLoss();
|
|
263
|
+
},
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export type { VideoDeviceModelId };
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// 4×4 matrix math for the cinematic camera (row-major, column vectors, CSS axes: x right, y down,
|
|
2
|
+
// z toward the viewer). No DOM, no React: the stage computes one matrix3d() per plane.
|
|
3
|
+
import { Easing } from "remotion";
|
|
4
|
+
|
|
5
|
+
export type M4 = number[];
|
|
6
|
+
|
|
7
|
+
export const identity = (): M4 => [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
|
|
8
|
+
|
|
9
|
+
export function mul(...ms: M4[]): M4 {
|
|
10
|
+
return ms.reduce((a, b) => {
|
|
11
|
+
const o = new Array<number>(16).fill(0);
|
|
12
|
+
for (let i = 0; i < 4; i++)
|
|
13
|
+
for (let j = 0; j < 4; j++)
|
|
14
|
+
for (let k = 0; k < 4; k++) o[i * 4 + j] += a[i * 4 + k] * b[k * 4 + j];
|
|
15
|
+
return o;
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const translate = (x: number, y: number, z = 0): M4 => [1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z, 0, 0, 0, 1];
|
|
20
|
+
export const scale = (x: number, y = x, z = x): M4 => [x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1];
|
|
21
|
+
|
|
22
|
+
const rad = (deg: number) => (deg * Math.PI) / 180;
|
|
23
|
+
|
|
24
|
+
/** Positive tilts the top edge away from the viewer. */
|
|
25
|
+
export function rotateX(deg: number): M4 {
|
|
26
|
+
const c = Math.cos(rad(deg));
|
|
27
|
+
const s = Math.sin(rad(deg));
|
|
28
|
+
return [1, 0, 0, 0, 0, c, -s, 0, 0, s, c, 0, 0, 0, 0, 1];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Positive turns the right edge away from the viewer. */
|
|
32
|
+
export function rotateY(deg: number): M4 {
|
|
33
|
+
const c = Math.cos(rad(deg));
|
|
34
|
+
const s = Math.sin(rad(deg));
|
|
35
|
+
return [c, 0, s, 0, 0, 1, 0, 0, -s, 0, c, 0, 0, 0, 0, 1];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Positive rolls clockwise on screen. */
|
|
39
|
+
export function rotateZ(deg: number): M4 {
|
|
40
|
+
const c = Math.cos(rad(deg));
|
|
41
|
+
const s = Math.sin(rad(deg));
|
|
42
|
+
return [c, -s, 0, 0, s, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** CSS perspective(d): the viewer sits at z = d. */
|
|
46
|
+
export const perspective = (d: number): M4 => [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, -1 / d, 1];
|
|
47
|
+
|
|
48
|
+
export function apply(m: M4, x: number, y: number, z = 0): [number, number, number, number] {
|
|
49
|
+
return [
|
|
50
|
+
m[0] * x + m[1] * y + m[2] * z + m[3],
|
|
51
|
+
m[4] * x + m[5] * y + m[6] * z + m[7],
|
|
52
|
+
m[8] * x + m[9] * y + m[10] * z + m[11],
|
|
53
|
+
m[12] * x + m[13] * y + m[14] * z + m[15],
|
|
54
|
+
];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** CSS matrix3d() takes columns. */
|
|
58
|
+
export function toCss(m: M4) {
|
|
59
|
+
const cols: number[] = [];
|
|
60
|
+
for (let j = 0; j < 4; j++) for (let i = 0; i < 4; i++) cols.push(m[i * 4 + j]);
|
|
61
|
+
return `matrix3d(${cols.map((v) => (Math.abs(v) < 1e-12 ? 0 : +v.toPrecision(10))).join(",")})`;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Object placement shared by planes and groups. */
|
|
65
|
+
export interface Placement {
|
|
66
|
+
/** World px from the frame center (y down, z toward the viewer). */
|
|
67
|
+
x?: number;
|
|
68
|
+
y?: number;
|
|
69
|
+
z?: number;
|
|
70
|
+
/** Degrees. tilt: top edge leans back; turn: right edge goes back; roll: clockwise. */
|
|
71
|
+
tilt?: number;
|
|
72
|
+
turn?: number;
|
|
73
|
+
roll?: number;
|
|
74
|
+
scale?: number;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function placementMatrix(p: Placement): M4 {
|
|
78
|
+
return mul(
|
|
79
|
+
translate(p.x ?? 0, p.y ?? 0, p.z ?? 0),
|
|
80
|
+
rotateZ(p.roll ?? 0),
|
|
81
|
+
rotateX(p.tilt ?? 0),
|
|
82
|
+
rotateY(p.turn ?? 0),
|
|
83
|
+
scale(p.scale ?? 1),
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export const EASES = {
|
|
88
|
+
linear: (t: number) => t,
|
|
89
|
+
in: Easing.bezier(0.55, 0, 0.75, 0.25),
|
|
90
|
+
out: Easing.bezier(0.22, 1, 0.36, 1),
|
|
91
|
+
inOut: Easing.bezier(0.65, 0, 0.35, 1),
|
|
92
|
+
/** Fast start, very long landing: the premium glide. */
|
|
93
|
+
expo: Easing.bezier(0.16, 1, 0.3, 1),
|
|
94
|
+
/** Overshoots a little, then settles. */
|
|
95
|
+
back: Easing.bezier(0.34, 1.4, 0.64, 1),
|
|
96
|
+
/** Slow out, very fast middle, slow in: whip pans between subjects. */
|
|
97
|
+
whip: Easing.bezier(0.8, 0, 0.2, 1),
|
|
98
|
+
} as const;
|
|
99
|
+
|
|
100
|
+
export type EaseName = keyof typeof EASES;
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { useLayoutEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
import { AbsoluteFill, useCurrentFrame, useDelayRender } from "remotion";
|
|
3
|
+
import { alpha, exitOut, fill, FONTS, loadMotionFonts, ramp, unit } from "../lib";
|
|
4
|
+
import type { MotionBaseProps } from "../types";
|
|
5
|
+
|
|
6
|
+
type Props = MotionBaseProps & {
|
|
7
|
+
lines: string[];
|
|
8
|
+
font: string;
|
|
9
|
+
weight: string;
|
|
10
|
+
size: number;
|
|
11
|
+
color: string;
|
|
12
|
+
accent: string;
|
|
13
|
+
background: string;
|
|
14
|
+
dot: number;
|
|
15
|
+
build: number;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* "Generated by *multi word* agents" → plain and highlighted runs, spaces kept. A starred run may span
|
|
20
|
+
* words; every star is dropped, so the type and the traced dots spell the same text.
|
|
21
|
+
*/
|
|
22
|
+
function highlightRuns(line: string) {
|
|
23
|
+
return line
|
|
24
|
+
.split(/(\*[^*]+\*)/)
|
|
25
|
+
.filter(Boolean)
|
|
26
|
+
.map((part) => ({ text: part.replace(/\*/g, ""), highlighted: /^\*[^*]+\*$/.test(part) }))
|
|
27
|
+
.filter((run) => run.text);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Dots (x, y in grid cells, coverage 0–1) where the headline has ink. */
|
|
31
|
+
interface DotSet {
|
|
32
|
+
cols: number;
|
|
33
|
+
rows: number;
|
|
34
|
+
dots: Float32Array;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A headline that an agent seems to generate: a scan line traces its letters in dots, the dots swell
|
|
39
|
+
* into the glyphs and the real type resolves out of a blur.
|
|
40
|
+
*/
|
|
41
|
+
export function DotTraceTitle(props: Props) {
|
|
42
|
+
const frame = useCurrentFrame();
|
|
43
|
+
const { width, height, fps, durationInFrames: d } = props;
|
|
44
|
+
const canvas = useRef<HTMLCanvasElement>(null);
|
|
45
|
+
const { delayRender, continueRender } = useDelayRender();
|
|
46
|
+
const u = unit(width, height);
|
|
47
|
+
const fontSize = Math.round((props.size / 100) * Math.min(width, height) * 0.9);
|
|
48
|
+
const family = FONTS[props.font] ?? FONTS.display;
|
|
49
|
+
const font = `${props.weight} ${fontSize}px ${family}`;
|
|
50
|
+
// Spaces collapsed like the DOM does, so the canvas traces exactly the type it resolves into.
|
|
51
|
+
const lines = props.lines
|
|
52
|
+
.map((line) => line.replace(/\s+/g, " ").trim())
|
|
53
|
+
.filter(Boolean)
|
|
54
|
+
.slice(0, 3);
|
|
55
|
+
const text = lines.map((line) => line.replace(/\*/g, ""));
|
|
56
|
+
const textKey = text.join("\n");
|
|
57
|
+
const lineHeight = fontSize * 1.02;
|
|
58
|
+
const top = height / 2 - (lines.length * lineHeight) / 2;
|
|
59
|
+
const pitch = Math.max(4, props.dot * u);
|
|
60
|
+
const buildFrames = Math.max(12, Math.round(props.build * fps));
|
|
61
|
+
const trace = ramp(frame, 0, buildFrames * 0.6);
|
|
62
|
+
const resolve = ramp(frame, buildFrames * 0.45, buildFrames * 0.55);
|
|
63
|
+
const out = exitOut(frame, d, 12);
|
|
64
|
+
|
|
65
|
+
// The dots are traced from the real font, never a fallback: hold the render until this exact face is
|
|
66
|
+
// in (from the first commit, in a layout effect), and release it once a frame is drawn with it, or
|
|
67
|
+
// in cleanup (unmount, StrictMode's second mount, a new font).
|
|
68
|
+
const [fontReady, setFontReady] = useState<string | null>(null);
|
|
69
|
+
const releaseFont = useRef<(() => void) | null>(null);
|
|
70
|
+
useLayoutEffect(() => {
|
|
71
|
+
let alive = true;
|
|
72
|
+
let released = false;
|
|
73
|
+
const handle = delayRender(`Loading ${font} for DotTraceTitle`);
|
|
74
|
+
const release = () => {
|
|
75
|
+
if (released) return;
|
|
76
|
+
released = true;
|
|
77
|
+
continueRender(handle);
|
|
78
|
+
};
|
|
79
|
+
releaseFont.current = release;
|
|
80
|
+
// loadMotionFonts resolves even when the network fails (the type falls back), so this always ends.
|
|
81
|
+
loadMotionFonts()
|
|
82
|
+
.then(() => document.fonts.load(font, textKey || "A"))
|
|
83
|
+
.catch(() => undefined)
|
|
84
|
+
.then(() => {
|
|
85
|
+
if (alive) setFontReady(font);
|
|
86
|
+
});
|
|
87
|
+
return () => {
|
|
88
|
+
alive = false;
|
|
89
|
+
if (releaseFont.current === release) releaseFont.current = null;
|
|
90
|
+
release();
|
|
91
|
+
};
|
|
92
|
+
}, [font, textKey, delayRender, continueRender]);
|
|
93
|
+
const ready = fontReady === font;
|
|
94
|
+
|
|
95
|
+
// Sample the headline's own pixels on a dot grid offscreen, once per text, font and grid.
|
|
96
|
+
const dotSet = useMemo<DotSet | null>(() => {
|
|
97
|
+
if (!ready || typeof document === "undefined") return null;
|
|
98
|
+
const off = document.createElement("canvas");
|
|
99
|
+
off.width = Math.ceil(width / pitch);
|
|
100
|
+
off.height = Math.ceil(height / pitch);
|
|
101
|
+
const o = off.getContext("2d");
|
|
102
|
+
if (!o) return null;
|
|
103
|
+
o.scale(1 / pitch, 1 / pitch);
|
|
104
|
+
o.fillStyle = "#fff";
|
|
105
|
+
o.textAlign = "center";
|
|
106
|
+
o.textBaseline = "middle";
|
|
107
|
+
o.font = font;
|
|
108
|
+
textKey.split("\n").forEach((line, i) => o.fillText(line, width / 2, top + lineHeight * (i + 0.5)));
|
|
109
|
+
const data = o.getImageData(0, 0, off.width, off.height).data;
|
|
110
|
+
const found: number[] = [];
|
|
111
|
+
for (let y = 0; y < off.height; y++) {
|
|
112
|
+
for (let x = 0; x < off.width; x++) {
|
|
113
|
+
const a = data[(y * off.width + x) * 4 + 3] / 255;
|
|
114
|
+
if (a >= 0.2) found.push(x, y, a);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return { cols: off.width, rows: off.height, dots: Float32Array.from(found) };
|
|
118
|
+
}, [ready, font, textKey, width, height, pitch, top, lineHeight]);
|
|
119
|
+
|
|
120
|
+
// Draw the dots the scan has passed.
|
|
121
|
+
useLayoutEffect(() => {
|
|
122
|
+
const c = canvas.current;
|
|
123
|
+
const ctx = c?.getContext("2d");
|
|
124
|
+
if (c && ctx) {
|
|
125
|
+
ctx.clearRect(0, 0, width, height);
|
|
126
|
+
if (dotSet && resolve < 1) {
|
|
127
|
+
const scanX = trace * (dotSet.cols + 6);
|
|
128
|
+
const { dots } = dotSet;
|
|
129
|
+
for (let i = 0; i < dots.length; i += 3) {
|
|
130
|
+
const x = dots[i];
|
|
131
|
+
const y = dots[i + 1];
|
|
132
|
+
const a = dots[i + 2];
|
|
133
|
+
const behind = scanX - x - (y / dotSet.rows) * 4;
|
|
134
|
+
if (behind < 0) continue;
|
|
135
|
+
const grow = Math.min(1, behind / 6);
|
|
136
|
+
ctx.fillStyle = behind < 2 ? props.accent : alpha(props.color, 0.85);
|
|
137
|
+
ctx.globalAlpha = (1 - resolve) * a;
|
|
138
|
+
ctx.beginPath();
|
|
139
|
+
ctx.arc((x + 0.5) * pitch, (y + 0.5) * pitch, pitch * 0.42 * grow * (0.6 + 0.4 * a), 0, Math.PI * 2);
|
|
140
|
+
ctx.fill();
|
|
141
|
+
}
|
|
142
|
+
// The scan line itself.
|
|
143
|
+
if (trace < 1) {
|
|
144
|
+
ctx.globalAlpha = 0.7 * (1 - resolve);
|
|
145
|
+
ctx.fillStyle = props.accent;
|
|
146
|
+
ctx.fillRect(scanX * pitch, top - fontSize * 0.2, Math.max(2, u * 3), lines.length * lineHeight + fontSize * 0.4);
|
|
147
|
+
}
|
|
148
|
+
ctx.globalAlpha = 1;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (ready) releaseFont.current?.();
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
return (
|
|
155
|
+
<AbsoluteFill style={{ background: fill(props.background), opacity: out }}>
|
|
156
|
+
<AbsoluteFill
|
|
157
|
+
style={{
|
|
158
|
+
alignItems: "center",
|
|
159
|
+
justifyContent: "center",
|
|
160
|
+
flexDirection: "column",
|
|
161
|
+
opacity: resolve,
|
|
162
|
+
filter: resolve < 1 ? `blur(${((1 - resolve) * 14 * u).toFixed(2)}px)` : undefined,
|
|
163
|
+
}}
|
|
164
|
+
>
|
|
165
|
+
{lines.map((line, i) => (
|
|
166
|
+
<div
|
|
167
|
+
key={i}
|
|
168
|
+
style={{ font, lineHeight: `${lineHeight}px`, color: props.color, textAlign: "center", whiteSpace: "nowrap" }}
|
|
169
|
+
>
|
|
170
|
+
{highlightRuns(line).map((run, j) => (
|
|
171
|
+
<span key={j} style={{ color: run.highlighted ? props.accent : undefined }}>
|
|
172
|
+
{run.text}
|
|
173
|
+
</span>
|
|
174
|
+
))}
|
|
175
|
+
</div>
|
|
176
|
+
))}
|
|
177
|
+
</AbsoluteFill>
|
|
178
|
+
<canvas ref={canvas} width={width} height={height} style={{ position: "absolute", inset: 0, width: "100%", height: "100%" }} />
|
|
179
|
+
</AbsoluteFill>
|
|
180
|
+
);
|
|
181
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "dot-trace-title",
|
|
3
|
+
"name": "Dot Trace Title",
|
|
4
|
+
"category": "Text & Titles",
|
|
5
|
+
"description": "A headline that looks generated by an agent: a scan line traces the letters in dots, the dots swell into the glyphs, and the real type resolves out of a blur.",
|
|
6
|
+
"useWhen": "AI products, agents, 'built by AI', launches of generative features, the moment a product creates something; also a techy title card for dev tools. Two or three short words per line. Mark the accent word with *stars*.",
|
|
7
|
+
"durationInFrames": 90,
|
|
8
|
+
"layer": "overlay",
|
|
9
|
+
"stillAt": 0.75,
|
|
10
|
+
"tags": ["ai", "generate", "dots", "scan", "title", "agent", "tech", "halftone"],
|
|
11
|
+
"props": {
|
|
12
|
+
"lines": { "type": "lines", "label": "Headline", "default": ["Generated", "by *agents*"], "max": 3, "help": "One line per row. *Starred* words (or a *starred phrase*) use the accent." },
|
|
13
|
+
"font": {
|
|
14
|
+
"type": "select",
|
|
15
|
+
"style": "font",
|
|
16
|
+
"label": "Font",
|
|
17
|
+
"default": "display",
|
|
18
|
+
"options": [
|
|
19
|
+
{ "value": "display", "label": "Bricolage" },
|
|
20
|
+
{ "value": "sans", "label": "Inter" },
|
|
21
|
+
{ "value": "grotesk", "label": "Space Grotesk" },
|
|
22
|
+
{ "value": "wide", "label": "Unbounded" },
|
|
23
|
+
{ "value": "serif", "label": "Instrument Serif" },
|
|
24
|
+
{ "value": "mono", "label": "Geist Mono" },
|
|
25
|
+
{ "value": "condensed", "label": "Anton" }
|
|
26
|
+
]
|
|
27
|
+
},
|
|
28
|
+
"weight": {
|
|
29
|
+
"type": "select",
|
|
30
|
+
"style": "weight",
|
|
31
|
+
"label": "Weight",
|
|
32
|
+
"default": "800",
|
|
33
|
+
"options": [
|
|
34
|
+
{ "value": "500", "label": "Medium" },
|
|
35
|
+
{ "value": "700", "label": "Bold" },
|
|
36
|
+
{ "value": "800", "label": "Extra bold" }
|
|
37
|
+
]
|
|
38
|
+
},
|
|
39
|
+
"size": { "type": "number", "label": "Size", "default": 18, "min": 6, "max": 34, "step": 0.5, "unit": "% of frame", "help": "Type size: this share of the frame's short side, times 0.9 (18 is about 16% of the short side)." },
|
|
40
|
+
"color": { "type": "color", "style": "text", "label": "Text", "default": "#ffffff" },
|
|
41
|
+
"accent": { "type": "color", "style": "accent", "label": "Scan and accent", "default": "#7c5cff" },
|
|
42
|
+
"background": { "type": "color", "label": "Background", "default": "transparent" },
|
|
43
|
+
"dot": { "type": "number", "label": "Dot size", "default": 9, "min": 4, "max": 24, "step": 1, "unit": "px", "help": "Dot spacing in px at 1080p on the short side; it scales with the frame size." },
|
|
44
|
+
"build": { "type": "number", "label": "Build time", "default": 1.8, "min": 0.6, "max": 5, "step": 0.1, "unit": "s" }
|
|
45
|
+
},
|
|
46
|
+
"examples": [
|
|
47
|
+
{ "name": "Generated", "props": {} },
|
|
48
|
+
{ "name": "On paper", "props": { "lines": ["Your site,", "*built*"], "color": "#111111", "accent": "#f97316", "background": "#f5f3ee", "font": "grotesk" } },
|
|
49
|
+
{ "name": "Mono", "props": { "lines": ["npx agentcut"], "font": "mono", "weight": "700", "accent": "#22d3ee", "size": 12 } }
|
|
50
|
+
]
|
|
51
|
+
}
|