oddlyalive 0.2.0-alpha.2
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/CHANGELOG.md +45 -0
- package/CONTRIBUTING.md +62 -0
- package/LICENSE +21 -0
- package/README.md +158 -0
- package/SECURITY.md +29 -0
- package/assets/photoreal/PROVENANCE.md +19 -0
- package/assets/photoreal/baseball.png +0 -0
- package/assets/photoreal/basketball.png +0 -0
- package/assets/photoreal/crystal.png +0 -0
- package/assets/photoreal/kicking-cleat.png +0 -0
- package/assets/photoreal/letter-charm-square.png +0 -0
- package/assets/photoreal/letter-charm.png +0 -0
- package/assets/photoreal/sneaker.png +0 -0
- package/assets/photoreal/soccer-ball.png +0 -0
- package/bin/oddlyalive.js +283 -0
- package/docs/ARCHITECTURE.md +55 -0
- package/docs/DEMO-VIDEOS.md +122 -0
- package/docs/QUICKSTART.md +115 -0
- package/docs/ROADMAP.md +33 -0
- package/examples/ball-lab/PROVENANCE.md +4 -0
- package/examples/ball-lab/app.js +33 -0
- package/examples/ball-lab/index.html +56 -0
- package/examples/ball-lab/scene.json +68 -0
- package/examples/crystal-mobile/PROVENANCE.md +4 -0
- package/examples/crystal-mobile/app.js +26 -0
- package/examples/crystal-mobile/index.html +56 -0
- package/examples/crystal-mobile/scene.json +69 -0
- package/examples/football-kick/PROVENANCE.md +4 -0
- package/examples/football-kick/app.js +33 -0
- package/examples/football-kick/index.html +56 -0
- package/examples/football-kick/scene.json +44 -0
- package/examples/gallery.css +379 -0
- package/examples/index.html +109 -0
- package/examples/shared/example.css +320 -0
- package/examples/shared/player.js +85 -0
- package/examples/shoe-splash/PROVENANCE.md +4 -0
- package/examples/shoe-splash/app.js +26 -0
- package/examples/shoe-splash/index.html +56 -0
- package/examples/shoe-splash/scene.json +38 -0
- package/examples/string-touch/PROVENANCE.md +11 -0
- package/examples/string-touch/app.js +79 -0
- package/examples/string-touch/index.html +70 -0
- package/examples/string-touch/scene.json +73 -0
- package/examples/string-touch/styles.css +273 -0
- package/package.json +82 -0
- package/schemas/scene.schema.json +336 -0
- package/scripts/render-demo-videos.sh +77 -0
- package/scripts/serve.js +88 -0
- package/scripts/update-visual-fixtures.mjs +60 -0
- package/scripts/verify-demo-videos.mjs +138 -0
- package/skills/oddlyalive/SKILL.md +68 -0
- package/skills/oddlyalive/agents/openai.yaml +4 -0
- package/skills/oddlyalive/references/object-models.md +25 -0
- package/skills/oddlyalive/references/recipes.md +27 -0
- package/skills/oddlyalive/references/scene-schema.md +84 -0
- package/src/crystal-renderer.js +338 -0
- package/src/gesture.js +58 -0
- package/src/index.js +44 -0
- package/src/rigid-balls.js +304 -0
- package/src/rigid-renderer.js +760 -0
- package/src/rigid-scene.js +223 -0
- package/src/scene-registry.js +17 -0
- package/src/scene.js +206 -0
- package/src/state-hash.js +17 -0
- package/src/strands.js +773 -0
- package/src/surface-wave.js +209 -0
- package/src/svg-renderer.js +348 -0
- package/src/wave-renderer.js +463 -0
- package/src/wave-scene.js +163 -0
- package/tests/cli.test.js +101 -0
- package/tests/determinism.test.js +57 -0
- package/tests/fixtures/visual-baselines.json +27 -0
- package/tests/rigid-balls.test.js +77 -0
- package/tests/scene.test.js +78 -0
- package/tests/surface-wave.test.js +39 -0
- package/tests/visual-fixtures.test.js +43 -0
- package/videos/oddlyalive-demos/BRIEF.md +40 -0
- package/videos/oddlyalive-demos/DEMO-PLAN.md +13 -0
- package/videos/oddlyalive-demos/README.md +45 -0
- package/videos/oddlyalive-demos/app.js +212 -0
- package/videos/oddlyalive-demos/hyperframes.json +9 -0
- package/videos/oddlyalive-demos/index.html +246 -0
- package/videos/oddlyalive-demos/meta.json +5 -0
- package/videos/oddlyalive-demos/package-lock.json +506 -0
- package/videos/oddlyalive-demos/package.json +19 -0
- package/videos/oddlyalive-demos/prepare-assets.mjs +16 -0
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { createStateHasher } from "./state-hash.js";
|
|
2
|
+
import { validateSurfaceWaveScene } from "./wave-scene.js";
|
|
3
|
+
|
|
4
|
+
export function simulateSurfaceWave(inputScene, options = {}) {
|
|
5
|
+
const scene = validateSurfaceWaveScene(inputScene);
|
|
6
|
+
const { fps, substeps, duration } = scene.timing;
|
|
7
|
+
const dt = 1 / (fps * substeps);
|
|
8
|
+
const frameCount = Math.round(duration * fps) + 1;
|
|
9
|
+
const sampleCount = scene.surface.samples;
|
|
10
|
+
const dx = scene.surface.width / (sampleCount - 1);
|
|
11
|
+
const includeFrames = options.includeFrames !== false;
|
|
12
|
+
|
|
13
|
+
const displacement = new Float64Array(sampleCount);
|
|
14
|
+
const velocity = new Float64Array(sampleCount);
|
|
15
|
+
const acceleration = new Float64Array(sampleCount);
|
|
16
|
+
const frames = includeFrames ? new Array(frameCount) : null;
|
|
17
|
+
const hasher = createStateHasher();
|
|
18
|
+
const diagnostics = {
|
|
19
|
+
waveEnergy: new Float32Array(frameCount),
|
|
20
|
+
firstImpactTime: null,
|
|
21
|
+
impactCount: 0,
|
|
22
|
+
maxWaveHeight: 0,
|
|
23
|
+
maxSubmersion: 0,
|
|
24
|
+
maxBodySpeed: 0
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
let bodyX = scene.body.x;
|
|
28
|
+
let bodyY = scene.body.y;
|
|
29
|
+
let bodyVX = scene.body.vx;
|
|
30
|
+
let bodyVY = scene.body.vy;
|
|
31
|
+
let bodyAngle = scene.body.angle;
|
|
32
|
+
let bodyAngularVelocity = scene.body.angularVelocity;
|
|
33
|
+
let touchingWater = false;
|
|
34
|
+
|
|
35
|
+
function sampleIndexAt(x) {
|
|
36
|
+
const normalized =
|
|
37
|
+
(x - scene.surface.x) / Math.max(1, scene.surface.width);
|
|
38
|
+
return Math.max(
|
|
39
|
+
1,
|
|
40
|
+
Math.min(sampleCount - 2, Math.round(normalized * (sampleCount - 1)))
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function addWaveImpulse(centerIndex, impulse) {
|
|
45
|
+
const radius = Math.max(3, Math.round(sampleCount * 0.035));
|
|
46
|
+
for (
|
|
47
|
+
let index = Math.max(1, centerIndex - radius);
|
|
48
|
+
index <= Math.min(sampleCount - 2, centerIndex + radius);
|
|
49
|
+
index += 1
|
|
50
|
+
) {
|
|
51
|
+
const distance = (index - centerIndex) / radius;
|
|
52
|
+
const weight = Math.exp(-distance * distance * 3.2);
|
|
53
|
+
velocity[index] += impulse * weight;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function solveSurface() {
|
|
58
|
+
const waveCoefficient =
|
|
59
|
+
(scene.surface.waveSpeed * scene.surface.waveSpeed) / (dx * dx);
|
|
60
|
+
for (let index = 1; index < sampleCount - 1; index += 1) {
|
|
61
|
+
const laplacian =
|
|
62
|
+
displacement[index - 1] -
|
|
63
|
+
2 * displacement[index] +
|
|
64
|
+
displacement[index + 1];
|
|
65
|
+
acceleration[index] =
|
|
66
|
+
waveCoefficient * laplacian -
|
|
67
|
+
scene.surface.damping * velocity[index];
|
|
68
|
+
}
|
|
69
|
+
for (let index = 1; index < sampleCount - 1; index += 1) {
|
|
70
|
+
velocity[index] += acceleration[index] * dt;
|
|
71
|
+
displacement[index] += velocity[index] * dt;
|
|
72
|
+
}
|
|
73
|
+
displacement[0] = 0;
|
|
74
|
+
displacement[sampleCount - 1] = 0;
|
|
75
|
+
velocity[0] = 0;
|
|
76
|
+
velocity[sampleCount - 1] = 0;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function solveBody(time) {
|
|
80
|
+
const airDecay = Math.exp(-scene.world.airDrag * dt);
|
|
81
|
+
bodyVY += scene.world.gravity * dt;
|
|
82
|
+
bodyVX *= airDecay;
|
|
83
|
+
bodyVY *= airDecay;
|
|
84
|
+
bodyAngularVelocity *= Math.exp(-scene.world.airDrag * 0.3 * dt);
|
|
85
|
+
bodyX += bodyVX * dt;
|
|
86
|
+
bodyY += bodyVY * dt;
|
|
87
|
+
bodyAngle += bodyAngularVelocity * dt * (180 / Math.PI);
|
|
88
|
+
|
|
89
|
+
const centerIndex = sampleIndexAt(bodyX);
|
|
90
|
+
const localSurfaceY =
|
|
91
|
+
scene.surface.restY + displacement[centerIndex];
|
|
92
|
+
const bottom = bodyY + scene.body.height * 0.5;
|
|
93
|
+
const submersion = Math.max(0, bottom - localSurfaceY);
|
|
94
|
+
diagnostics.maxSubmersion = Math.max(
|
|
95
|
+
diagnostics.maxSubmersion,
|
|
96
|
+
submersion
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
if (submersion > 0) {
|
|
100
|
+
if (!touchingWater) {
|
|
101
|
+
const impactSpeed = Math.max(0, bodyVY);
|
|
102
|
+
addWaveImpulse(
|
|
103
|
+
centerIndex,
|
|
104
|
+
impactSpeed * scene.surface.coupling
|
|
105
|
+
);
|
|
106
|
+
diagnostics.impactCount += 1;
|
|
107
|
+
if (diagnostics.firstImpactTime === null) {
|
|
108
|
+
diagnostics.firstImpactTime = time;
|
|
109
|
+
}
|
|
110
|
+
bodyVY = -impactSpeed * scene.body.restitution;
|
|
111
|
+
bodyAngularVelocity +=
|
|
112
|
+
(bodyVX / Math.max(1, scene.body.width)) * 0.45;
|
|
113
|
+
}
|
|
114
|
+
touchingWater = true;
|
|
115
|
+
const submergedRatio = Math.min(
|
|
116
|
+
1.5,
|
|
117
|
+
submersion / Math.max(1, scene.body.height)
|
|
118
|
+
);
|
|
119
|
+
bodyVY -= scene.world.buoyancy * submersion * dt;
|
|
120
|
+
const waterDecay = Math.exp(
|
|
121
|
+
-scene.world.waterDrag * submergedRatio * dt
|
|
122
|
+
);
|
|
123
|
+
bodyVX *= waterDecay;
|
|
124
|
+
bodyVY *= waterDecay;
|
|
125
|
+
bodyAngularVelocity *= Math.exp(
|
|
126
|
+
-scene.world.waterDrag * 0.75 * submergedRatio * dt
|
|
127
|
+
);
|
|
128
|
+
bodyAngularVelocity +=
|
|
129
|
+
((-bodyAngle * Math.PI) / 180) *
|
|
130
|
+
2.4 *
|
|
131
|
+
submergedRatio *
|
|
132
|
+
dt;
|
|
133
|
+
|
|
134
|
+
const pressure = Math.max(0, bodyVY) * 0.003 + submersion * 0.012;
|
|
135
|
+
velocity[centerIndex] += pressure;
|
|
136
|
+
if (submersion > scene.body.height * 0.82) {
|
|
137
|
+
bodyY -=
|
|
138
|
+
(submersion - scene.body.height * 0.82) * 0.18;
|
|
139
|
+
}
|
|
140
|
+
} else {
|
|
141
|
+
touchingWater = false;
|
|
142
|
+
}
|
|
143
|
+
diagnostics.maxBodySpeed = Math.max(
|
|
144
|
+
diagnostics.maxBodySpeed,
|
|
145
|
+
Math.sqrt(bodyVX * bodyVX + bodyVY * bodyVY)
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function saveFrame(frameIndex) {
|
|
150
|
+
let energy = 0;
|
|
151
|
+
for (let index = 0; index < sampleCount; index += 1) {
|
|
152
|
+
energy +=
|
|
153
|
+
0.5 * velocity[index] ** 2 +
|
|
154
|
+
0.5 * displacement[index] ** 2;
|
|
155
|
+
diagnostics.maxWaveHeight = Math.max(
|
|
156
|
+
diagnostics.maxWaveHeight,
|
|
157
|
+
Math.abs(displacement[index])
|
|
158
|
+
);
|
|
159
|
+
if (frameIndex % 6 === 0) {
|
|
160
|
+
hasher.add(displacement[index]);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
diagnostics.waveEnergy[frameIndex] = energy;
|
|
164
|
+
if (frameIndex % 6 === 0) {
|
|
165
|
+
hasher.add(bodyX);
|
|
166
|
+
hasher.add(bodyY);
|
|
167
|
+
hasher.add(bodyAngle);
|
|
168
|
+
}
|
|
169
|
+
if (frames) {
|
|
170
|
+
frames[frameIndex] = {
|
|
171
|
+
surface: Float32Array.from(displacement),
|
|
172
|
+
body: {
|
|
173
|
+
x: bodyX,
|
|
174
|
+
y: bodyY,
|
|
175
|
+
angle: bodyAngle
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const preRollSteps = Math.round(scene.timing.preRoll / dt);
|
|
182
|
+
for (let index = 0; index < preRollSteps; index += 1) {
|
|
183
|
+
const time = (index - preRollSteps) * dt;
|
|
184
|
+
solveSurface();
|
|
185
|
+
solveBody(time);
|
|
186
|
+
}
|
|
187
|
+
diagnostics.firstImpactTime = null;
|
|
188
|
+
diagnostics.impactCount = 0;
|
|
189
|
+
diagnostics.maxWaveHeight = 0;
|
|
190
|
+
diagnostics.maxSubmersion = 0;
|
|
191
|
+
diagnostics.maxBodySpeed = 0;
|
|
192
|
+
|
|
193
|
+
for (let frame = 0; frame < frameCount; frame += 1) {
|
|
194
|
+
saveFrame(frame);
|
|
195
|
+
if (frame === frameCount - 1) break;
|
|
196
|
+
for (let substep = 0; substep < substeps; substep += 1) {
|
|
197
|
+
const time = (frame * substeps + substep + 1) * dt;
|
|
198
|
+
solveSurface();
|
|
199
|
+
solveBody(time);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return {
|
|
204
|
+
scene,
|
|
205
|
+
frames,
|
|
206
|
+
diagnostics,
|
|
207
|
+
fingerprint: hasher.digest()
|
|
208
|
+
};
|
|
209
|
+
}
|
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
const SVG_NS = "http://www.w3.org/2000/svg";
|
|
2
|
+
|
|
3
|
+
function svgNode(name, attributes = {}) {
|
|
4
|
+
const node = document.createElementNS(SVG_NS, name);
|
|
5
|
+
for (const [key, value] of Object.entries(attributes)) {
|
|
6
|
+
node.setAttribute(key, String(value));
|
|
7
|
+
}
|
|
8
|
+
return node;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function strandPath(points) {
|
|
12
|
+
if (points.length === 0) return "";
|
|
13
|
+
if (points.length === 1) return `M ${points[0].x} ${points[0].y}`;
|
|
14
|
+
let path = `M ${points[0].x.toFixed(2)} ${points[0].y.toFixed(2)}`;
|
|
15
|
+
for (let index = 1; index < points.length - 1; index += 1) {
|
|
16
|
+
const current = points[index];
|
|
17
|
+
const next = points[index + 1];
|
|
18
|
+
const middleX = (current.x + next.x) * 0.5;
|
|
19
|
+
const middleY = (current.y + next.y) * 0.5;
|
|
20
|
+
path += ` Q ${current.x.toFixed(2)} ${current.y.toFixed(2)} ${middleX.toFixed(2)} ${middleY.toFixed(2)}`;
|
|
21
|
+
}
|
|
22
|
+
const previous = points[points.length - 2];
|
|
23
|
+
const last = points[points.length - 1];
|
|
24
|
+
path += ` Q ${previous.x.toFixed(2)} ${previous.y.toFixed(2)} ${last.x.toFixed(2)} ${last.y.toFixed(2)}`;
|
|
25
|
+
return path;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function createSvgRenderer(svg, simulation, options = {}) {
|
|
29
|
+
const { scene, base, frames } = simulation;
|
|
30
|
+
if (!frames) {
|
|
31
|
+
throw new TypeError("The SVG renderer requires simulation frames.");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const { columns, rows } = scene.field;
|
|
35
|
+
const glyphText = Array.from(scene.payload.text);
|
|
36
|
+
const root = svgNode("g", { class: "oa-scene" });
|
|
37
|
+
const defs = svgNode("defs");
|
|
38
|
+
const railGradient = svgNode("linearGradient", {
|
|
39
|
+
id: "oa-rail-metal",
|
|
40
|
+
x1: "0",
|
|
41
|
+
y1: "0",
|
|
42
|
+
x2: "0",
|
|
43
|
+
y2: "1"
|
|
44
|
+
});
|
|
45
|
+
railGradient.append(
|
|
46
|
+
svgNode("stop", { offset: "0", "stop-color": "#6a6255" }),
|
|
47
|
+
svgNode("stop", { offset: "0.24", "stop-color": "#d9cdb9" }),
|
|
48
|
+
svgNode("stop", { offset: "0.42", "stop-color": "#736b5f" }),
|
|
49
|
+
svgNode("stop", { offset: "0.68", "stop-color": "#2b2a27" }),
|
|
50
|
+
svgNode("stop", { offset: "1", "stop-color": "#161614" })
|
|
51
|
+
);
|
|
52
|
+
const brassGradient = svgNode("radialGradient", {
|
|
53
|
+
id: "oa-eyelet-brass",
|
|
54
|
+
cx: "34%",
|
|
55
|
+
cy: "26%",
|
|
56
|
+
r: "74%"
|
|
57
|
+
});
|
|
58
|
+
brassGradient.append(
|
|
59
|
+
svgNode("stop", { offset: "0", "stop-color": "#fff2b2" }),
|
|
60
|
+
svgNode("stop", { offset: "0.25", "stop-color": "#d6a53c" }),
|
|
61
|
+
svgNode("stop", { offset: "0.66", "stop-color": "#8b5b18" }),
|
|
62
|
+
svgNode("stop", { offset: "1", "stop-color": "#493014" })
|
|
63
|
+
);
|
|
64
|
+
defs.append(railGradient, brassGradient);
|
|
65
|
+
const shadow = svgNode("filter", {
|
|
66
|
+
id: "oa-soft-shadow",
|
|
67
|
+
x: "-30%",
|
|
68
|
+
y: "-30%",
|
|
69
|
+
width: "160%",
|
|
70
|
+
height: "180%"
|
|
71
|
+
});
|
|
72
|
+
shadow.appendChild(
|
|
73
|
+
svgNode("feDropShadow", {
|
|
74
|
+
dx: "1.6",
|
|
75
|
+
dy: "5",
|
|
76
|
+
stdDeviation: "3.4",
|
|
77
|
+
"flood-color": "#10100e",
|
|
78
|
+
"flood-opacity": "0.2"
|
|
79
|
+
})
|
|
80
|
+
);
|
|
81
|
+
defs.appendChild(shadow);
|
|
82
|
+
root.appendChild(defs);
|
|
83
|
+
|
|
84
|
+
const railY = base.anchorY[0] - 12;
|
|
85
|
+
const railStart = base.anchorX[0] - 35;
|
|
86
|
+
const railEnd = base.anchorX[columns - 1] + 35;
|
|
87
|
+
const railGroup = svgNode("g", {
|
|
88
|
+
class: "oa-rail",
|
|
89
|
+
filter: "url(#oa-soft-shadow)"
|
|
90
|
+
});
|
|
91
|
+
railGroup.append(
|
|
92
|
+
svgNode("rect", {
|
|
93
|
+
x: railStart,
|
|
94
|
+
y: railY - 7,
|
|
95
|
+
width: railEnd - railStart,
|
|
96
|
+
height: 14,
|
|
97
|
+
rx: 6.5,
|
|
98
|
+
fill: "url(#oa-rail-metal)",
|
|
99
|
+
stroke: "#171714",
|
|
100
|
+
"stroke-width": "1.25"
|
|
101
|
+
}),
|
|
102
|
+
svgNode("path", {
|
|
103
|
+
d: `M ${railStart + 8} ${railY - 3.8} L ${railEnd - 8} ${railY - 3.8}`,
|
|
104
|
+
fill: "none",
|
|
105
|
+
stroke: "rgba(255,255,255,0.5)",
|
|
106
|
+
"stroke-width": "1.2",
|
|
107
|
+
"stroke-linecap": "round"
|
|
108
|
+
}),
|
|
109
|
+
svgNode("rect", {
|
|
110
|
+
x: railStart - 8,
|
|
111
|
+
y: railY - 12,
|
|
112
|
+
width: 16,
|
|
113
|
+
height: 24,
|
|
114
|
+
rx: 3,
|
|
115
|
+
fill: "#292824",
|
|
116
|
+
stroke: "#11110f",
|
|
117
|
+
"stroke-width": "1.2"
|
|
118
|
+
}),
|
|
119
|
+
svgNode("rect", {
|
|
120
|
+
x: railEnd - 8,
|
|
121
|
+
y: railY - 12,
|
|
122
|
+
width: 16,
|
|
123
|
+
height: 24,
|
|
124
|
+
rx: 3,
|
|
125
|
+
fill: "#292824",
|
|
126
|
+
stroke: "#11110f",
|
|
127
|
+
"stroke-width": "1.2"
|
|
128
|
+
})
|
|
129
|
+
);
|
|
130
|
+
for (const x of [railStart, railEnd]) {
|
|
131
|
+
railGroup.append(
|
|
132
|
+
svgNode("circle", {
|
|
133
|
+
cx: x,
|
|
134
|
+
cy: railY,
|
|
135
|
+
r: 3.4,
|
|
136
|
+
fill: "#bdb3a2",
|
|
137
|
+
stroke: "#11110f",
|
|
138
|
+
"stroke-width": "1"
|
|
139
|
+
}),
|
|
140
|
+
svgNode("path", {
|
|
141
|
+
d: `M ${x - 1.9} ${railY + 1.4} L ${x + 1.9} ${railY - 1.4}`,
|
|
142
|
+
stroke: "#4d493f",
|
|
143
|
+
"stroke-width": "0.9",
|
|
144
|
+
"stroke-linecap": "round"
|
|
145
|
+
})
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
root.appendChild(railGroup);
|
|
149
|
+
|
|
150
|
+
const cordGroup = svgNode("g", {
|
|
151
|
+
class: "oa-cords",
|
|
152
|
+
fill: "none",
|
|
153
|
+
"stroke-linecap": "round",
|
|
154
|
+
"stroke-linejoin": "round"
|
|
155
|
+
});
|
|
156
|
+
const knotGroup = svgNode("g", {
|
|
157
|
+
class: "oa-knots",
|
|
158
|
+
fill: "none",
|
|
159
|
+
"stroke-linecap": "round",
|
|
160
|
+
"stroke-linejoin": "round"
|
|
161
|
+
});
|
|
162
|
+
const charmGroup = svgNode("g", {
|
|
163
|
+
class: "oa-glyph-charms",
|
|
164
|
+
filter: "url(#oa-soft-shadow)"
|
|
165
|
+
});
|
|
166
|
+
const glyphGroup = svgNode("g", {
|
|
167
|
+
class: "oa-glyphs",
|
|
168
|
+
"font-family": scene.payload.fontFamily,
|
|
169
|
+
"font-size": scene.payload.fontSize,
|
|
170
|
+
"text-anchor": "middle",
|
|
171
|
+
"dominant-baseline": "central",
|
|
172
|
+
filter: "url(#oa-soft-shadow)"
|
|
173
|
+
});
|
|
174
|
+
const hardwareGroup = svgNode("g", { class: "oa-hardware" });
|
|
175
|
+
root.append(cordGroup, charmGroup, glyphGroup, knotGroup, hardwareGroup);
|
|
176
|
+
svg.appendChild(root);
|
|
177
|
+
|
|
178
|
+
const cordPaths = [];
|
|
179
|
+
const cordHighlights = [];
|
|
180
|
+
const knotTails = [];
|
|
181
|
+
for (let column = 0; column < columns; column += 1) {
|
|
182
|
+
const path = svgNode("path", {
|
|
183
|
+
stroke: "rgba(34, 29, 23, 0.74)",
|
|
184
|
+
"stroke-width": column % 4 === 0 ? "1.65" : "1.42"
|
|
185
|
+
});
|
|
186
|
+
const highlight = svgNode("path", {
|
|
187
|
+
stroke: "rgba(244, 226, 192, 0.58)",
|
|
188
|
+
"stroke-width": "0.52",
|
|
189
|
+
"stroke-dasharray": "2.1 2.35",
|
|
190
|
+
"stroke-dashoffset": String((column % 5) * 0.47)
|
|
191
|
+
});
|
|
192
|
+
cordGroup.appendChild(path);
|
|
193
|
+
cordGroup.appendChild(highlight);
|
|
194
|
+
cordPaths.push(path);
|
|
195
|
+
cordHighlights.push(highlight);
|
|
196
|
+
|
|
197
|
+
const tail = svgNode("path", {
|
|
198
|
+
stroke: "rgba(41, 33, 24, 0.75)",
|
|
199
|
+
"stroke-width": "1.2"
|
|
200
|
+
});
|
|
201
|
+
knotGroup.appendChild(tail);
|
|
202
|
+
knotTails.push(tail);
|
|
203
|
+
|
|
204
|
+
hardwareGroup.append(
|
|
205
|
+
svgNode("path", {
|
|
206
|
+
d: `M ${base.anchorX[column] - 4} ${railY + 4} Q ${base.anchorX[column]} ${railY + 10} ${base.anchorX[column] + 4} ${railY + 4}`,
|
|
207
|
+
fill: "none",
|
|
208
|
+
stroke: "#191815",
|
|
209
|
+
"stroke-width": "1.7",
|
|
210
|
+
"stroke-linecap": "round"
|
|
211
|
+
}),
|
|
212
|
+
svgNode("circle", {
|
|
213
|
+
cx: base.anchorX[column],
|
|
214
|
+
cy: base.anchorY[column] - 2.2,
|
|
215
|
+
r: column % 3 === 0 ? 3.9 : 3.35,
|
|
216
|
+
fill: "url(#oa-eyelet-brass)",
|
|
217
|
+
stroke: "#4a3013",
|
|
218
|
+
"stroke-width": "1"
|
|
219
|
+
}),
|
|
220
|
+
svgNode("circle", {
|
|
221
|
+
cx: base.anchorX[column],
|
|
222
|
+
cy: base.anchorY[column] - 2.2,
|
|
223
|
+
r: column % 3 === 0 ? 1.65 : 1.35,
|
|
224
|
+
fill: "#181713",
|
|
225
|
+
stroke: "#f2d77f",
|
|
226
|
+
"stroke-opacity": "0.56",
|
|
227
|
+
"stroke-width": "0.55"
|
|
228
|
+
}),
|
|
229
|
+
svgNode("path", {
|
|
230
|
+
d: `M ${base.anchorX[column] - 1.4} ${base.anchorY[column] + 1} C ${base.anchorX[column] - 4.4} ${base.anchorY[column] + 7}, ${base.anchorX[column] + 4.1} ${base.anchorY[column] + 7.4}, ${base.anchorX[column] + 1.1} ${base.anchorY[column] + 11}`,
|
|
231
|
+
fill: "none",
|
|
232
|
+
stroke: "rgba(42,33,23,0.8)",
|
|
233
|
+
"stroke-width": "1.15",
|
|
234
|
+
"stroke-linecap": "round"
|
|
235
|
+
})
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const glyphs = [];
|
|
240
|
+
const charmNodes = [];
|
|
241
|
+
for (let index = 0; index < columns * rows; index += 1) {
|
|
242
|
+
if (options.glyphCharmHref) {
|
|
243
|
+
const charm = svgNode("image", {
|
|
244
|
+
href: options.glyphCharmHref,
|
|
245
|
+
x: "-9.4",
|
|
246
|
+
y: "-9.4",
|
|
247
|
+
width: "18.8",
|
|
248
|
+
height: "18.8",
|
|
249
|
+
preserveAspectRatio: "xMidYMid meet",
|
|
250
|
+
decoding: "sync",
|
|
251
|
+
opacity: index % 7 === 0 ? "0.82" : "0.96",
|
|
252
|
+
style: `filter:hue-rotate(${(index % 5) * 2 - 4}deg) brightness(${0.95 + (index % 3) * 0.025})`
|
|
253
|
+
});
|
|
254
|
+
charmGroup.appendChild(charm);
|
|
255
|
+
charmNodes.push(charm);
|
|
256
|
+
} else {
|
|
257
|
+
charmNodes.push(null);
|
|
258
|
+
}
|
|
259
|
+
const text = svgNode("text", {
|
|
260
|
+
fill:
|
|
261
|
+
index % 29 === 0 || index % 47 === 0
|
|
262
|
+
? scene.render.accent
|
|
263
|
+
: scene.render.ink,
|
|
264
|
+
opacity: index % 7 === 0 ? 0.76 : 0.92
|
|
265
|
+
});
|
|
266
|
+
text.textContent = glyphText[index % glyphText.length];
|
|
267
|
+
text.setAttribute(
|
|
268
|
+
"style",
|
|
269
|
+
`paint-order:stroke;stroke:${scene.render.background};stroke-width:0.7px;stroke-opacity:0.48`
|
|
270
|
+
);
|
|
271
|
+
glyphGroup.appendChild(text);
|
|
272
|
+
glyphs.push(text);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const debugGroup = svgNode("g", {
|
|
276
|
+
class: "oa-debug",
|
|
277
|
+
opacity: options.debug ? 1 : 0,
|
|
278
|
+
"pointer-events": "none"
|
|
279
|
+
});
|
|
280
|
+
const debugCapsule = svgNode("ellipse", {
|
|
281
|
+
fill: "rgba(236, 91, 54, 0.08)",
|
|
282
|
+
stroke: scene.render.accent,
|
|
283
|
+
"stroke-width": "1.5",
|
|
284
|
+
"stroke-dasharray": "5 6"
|
|
285
|
+
});
|
|
286
|
+
debugGroup.appendChild(debugCapsule);
|
|
287
|
+
svg.appendChild(debugGroup);
|
|
288
|
+
|
|
289
|
+
function renderFrame(frameIndex, force) {
|
|
290
|
+
const frame = frames[Math.max(0, Math.min(frames.length - 1, frameIndex))];
|
|
291
|
+
for (let column = 0; column < columns; column += 1) {
|
|
292
|
+
const points = [
|
|
293
|
+
{ x: base.anchorX[column], y: base.anchorY[column] }
|
|
294
|
+
];
|
|
295
|
+
for (let row = 0; row < rows; row += 1) {
|
|
296
|
+
const index = row * columns + column;
|
|
297
|
+
const x = base.x[index] + frame.x[index];
|
|
298
|
+
const y = base.y[index] + frame.y[index];
|
|
299
|
+
points.push({ x, y });
|
|
300
|
+
glyphs[index].setAttribute(
|
|
301
|
+
"transform",
|
|
302
|
+
`translate(${x.toFixed(2)} ${y.toFixed(2)}) rotate(${frame.rotation[index].toFixed(2)})`
|
|
303
|
+
);
|
|
304
|
+
if (charmNodes[index]) {
|
|
305
|
+
charmNodes[index].setAttribute(
|
|
306
|
+
"transform",
|
|
307
|
+
`translate(${x.toFixed(2)} ${y.toFixed(2)}) rotate(${frame.rotation[index].toFixed(2)})`
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
const path = strandPath(points);
|
|
312
|
+
cordPaths[column].setAttribute("d", path);
|
|
313
|
+
cordHighlights[column].setAttribute("d", path);
|
|
314
|
+
const terminal = points[points.length - 1];
|
|
315
|
+
const beforeTerminal = points[points.length - 2];
|
|
316
|
+
const angle = Math.atan2(
|
|
317
|
+
terminal.y - beforeTerminal.y,
|
|
318
|
+
terminal.x - beforeTerminal.x
|
|
319
|
+
);
|
|
320
|
+
const normalX = Math.cos(angle + Math.PI * 0.5);
|
|
321
|
+
const normalY = Math.sin(angle + Math.PI * 0.5);
|
|
322
|
+
const tailX = terminal.x + Math.cos(angle) * 10 + normalX * (column % 2 ? 3 : -3);
|
|
323
|
+
const tailY = terminal.y + Math.sin(angle) * 10 + normalY * (column % 2 ? 3 : -3);
|
|
324
|
+
knotTails[column].setAttribute(
|
|
325
|
+
"d",
|
|
326
|
+
`M ${(terminal.x - normalX * 2.2).toFixed(2)} ${(terminal.y - normalY * 2.2).toFixed(2)} Q ${(terminal.x + normalX * 4.4).toFixed(2)} ${(terminal.y + normalY * 4.4).toFixed(2)} ${tailX.toFixed(2)} ${tailY.toFixed(2)}`
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
if (force) {
|
|
331
|
+
debugCapsule.setAttribute("cx", force.x);
|
|
332
|
+
debugCapsule.setAttribute("cy", force.y);
|
|
333
|
+
debugCapsule.setAttribute("rx", scene.contact.radiusX);
|
|
334
|
+
debugCapsule.setAttribute("ry", scene.contact.radiusY);
|
|
335
|
+
debugCapsule.setAttribute(
|
|
336
|
+
"opacity",
|
|
337
|
+
Math.max(0.12, force.pressure).toFixed(2)
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
return {
|
|
343
|
+
renderFrame,
|
|
344
|
+
setDebug(enabled) {
|
|
345
|
+
debugGroup.setAttribute("opacity", enabled ? "1" : "0");
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
}
|