@runtypelabs/terminal-animations 0.1.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/package.json +48 -0
- package/src/components.js +110 -0
- package/src/icon.js +95 -0
- package/src/index.d.ts +24 -0
- package/src/index.js +3 -0
- package/src/loading-animation.js +123 -0
- package/src/runner-scenes.js +464 -0
- package/src/variants.js +330 -0
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@runtypelabs/terminal-animations",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Terminal loading animations for Runtype CLI — AnimatedVariant, LoadingAnimation, and variant library.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"types": "./src/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./src/index.d.ts",
|
|
11
|
+
"import": "./src/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"src"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "echo 'No build - source consumed directly'"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"ink": "6.7.0",
|
|
22
|
+
"react": "^19.2.4"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/react": "^19.2.14"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"author": "Runtype Labs",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "https://github.com/runtypelabs/core",
|
|
35
|
+
"directory": "packages/terminal-animations"
|
|
36
|
+
},
|
|
37
|
+
"engines": {
|
|
38
|
+
"node": ">=18.0.0"
|
|
39
|
+
},
|
|
40
|
+
"keywords": [
|
|
41
|
+
"runtype",
|
|
42
|
+
"ink",
|
|
43
|
+
"cli",
|
|
44
|
+
"terminal",
|
|
45
|
+
"animation",
|
|
46
|
+
"loading"
|
|
47
|
+
]
|
|
48
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import React, {useEffect, useMemo, useState} from "react";
|
|
2
|
+
import {Box, Text} from "ink";
|
|
3
|
+
import {getVariant, variantNames} from "./variants.js";
|
|
4
|
+
|
|
5
|
+
const createVariantComponent = variantId => {
|
|
6
|
+
const Component = props =>
|
|
7
|
+
React.createElement(AnimatedVariant, {
|
|
8
|
+
variantId,
|
|
9
|
+
...props
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
Component.displayName = variantId
|
|
13
|
+
.split("-")
|
|
14
|
+
.map(part => part.charAt(0).toUpperCase() + part.slice(1))
|
|
15
|
+
.join("");
|
|
16
|
+
|
|
17
|
+
return Component;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const animationIds = variantNames;
|
|
21
|
+
|
|
22
|
+
export const AnimationGrid = ({grid}) =>
|
|
23
|
+
React.createElement(
|
|
24
|
+
Box,
|
|
25
|
+
{flexDirection: "column"},
|
|
26
|
+
...grid.map((row, rowIndex) =>
|
|
27
|
+
React.createElement(
|
|
28
|
+
Box,
|
|
29
|
+
{key: `row-${rowIndex}`},
|
|
30
|
+
...row.map((cell, columnIndex) =>
|
|
31
|
+
React.createElement(
|
|
32
|
+
Text,
|
|
33
|
+
{
|
|
34
|
+
key: `cell-${rowIndex}-${columnIndex}`,
|
|
35
|
+
color: cell.color,
|
|
36
|
+
backgroundColor: cell.backgroundColor,
|
|
37
|
+
dimColor: cell.dimColor,
|
|
38
|
+
bold: cell.bold
|
|
39
|
+
},
|
|
40
|
+
cell.text
|
|
41
|
+
)
|
|
42
|
+
)
|
|
43
|
+
)
|
|
44
|
+
)
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
export const AnimatedVariant = ({
|
|
48
|
+
variantId,
|
|
49
|
+
frame: controlledFrame,
|
|
50
|
+
initialFrame = 0,
|
|
51
|
+
speed,
|
|
52
|
+
autoplay = true
|
|
53
|
+
}) => {
|
|
54
|
+
const variant = useMemo(() => getVariant(variantId), [variantId]);
|
|
55
|
+
const [localFrame, setLocalFrame] = useState(initialFrame);
|
|
56
|
+
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
setLocalFrame(initialFrame);
|
|
59
|
+
}, [initialFrame, variantId]);
|
|
60
|
+
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
if (!variant || controlledFrame !== undefined || !autoplay) {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const delay = Number.isFinite(speed) && speed > 0 ? speed : variant.speed;
|
|
67
|
+
const timer = globalThis.setInterval(() => {
|
|
68
|
+
setLocalFrame(current => current + 1);
|
|
69
|
+
}, delay);
|
|
70
|
+
|
|
71
|
+
return () => globalThis.clearInterval(timer);
|
|
72
|
+
}, [autoplay, controlledFrame, speed, variant]);
|
|
73
|
+
|
|
74
|
+
if (!variant) {
|
|
75
|
+
return React.createElement(
|
|
76
|
+
Text,
|
|
77
|
+
{color: "redBright"},
|
|
78
|
+
`Unknown animation variant: ${variantId}`
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const frame = controlledFrame ?? localFrame;
|
|
83
|
+
const grid = variant.painter(frame);
|
|
84
|
+
|
|
85
|
+
return React.createElement(AnimationGrid, {grid});
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
export const NeonPulse = createVariantComponent("neon-pulse");
|
|
89
|
+
export const ScanlineSweep = createVariantComponent("scanline-sweep");
|
|
90
|
+
export const GlitchShift = createVariantComponent("glitch-shift");
|
|
91
|
+
export const MatrixDrip = createVariantComponent("matrix-drip");
|
|
92
|
+
export const PacketTrace = createVariantComponent("packet-trace");
|
|
93
|
+
export const FirewallLock = createVariantComponent("firewall-lock");
|
|
94
|
+
export const QuantumLag = createVariantComponent("quantum-lag");
|
|
95
|
+
export const SignalBloom = createVariantComponent("signal-bloom");
|
|
96
|
+
export const GhostShell = createVariantComponent("ghost-shell");
|
|
97
|
+
export const CoreBreach = createVariantComponent("core-breach");
|
|
98
|
+
|
|
99
|
+
export const animationComponents = {
|
|
100
|
+
NeonPulse,
|
|
101
|
+
ScanlineSweep,
|
|
102
|
+
GlitchShift,
|
|
103
|
+
MatrixDrip,
|
|
104
|
+
PacketTrace,
|
|
105
|
+
FirewallLock,
|
|
106
|
+
QuantumLag,
|
|
107
|
+
SignalBloom,
|
|
108
|
+
GhostShell,
|
|
109
|
+
CoreBreach
|
|
110
|
+
};
|
package/src/icon.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
export const GRID_SIZE = 8;
|
|
2
|
+
|
|
3
|
+
export const ICON_CELLS = [
|
|
4
|
+
[3, 3],
|
|
5
|
+
[3, 5],
|
|
6
|
+
[3, 6],
|
|
7
|
+
[4, 3],
|
|
8
|
+
[4, 4],
|
|
9
|
+
[4, 7],
|
|
10
|
+
[5, 3],
|
|
11
|
+
[6, 3],
|
|
12
|
+
[7, 3]
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
export const ICON_CELL_SET = new Set(
|
|
16
|
+
ICON_CELLS.map(([row, column]) => `${row},${column}`)
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
export const R_CELLS = [
|
|
20
|
+
[1, 2],
|
|
21
|
+
[2, 2],
|
|
22
|
+
[3, 2],
|
|
23
|
+
[4, 2],
|
|
24
|
+
[5, 2],
|
|
25
|
+
[6, 2],
|
|
26
|
+
[1, 3],
|
|
27
|
+
[1, 4],
|
|
28
|
+
[2, 5],
|
|
29
|
+
[3, 3],
|
|
30
|
+
[3, 4],
|
|
31
|
+
[4, 5],
|
|
32
|
+
[5, 6],
|
|
33
|
+
[6, 7]
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
export const R_CELL_SET = new Set(
|
|
37
|
+
R_CELLS.map(([row, column]) => `${row},${column}`)
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
export const SCENE_CELLS = [
|
|
41
|
+
[2, 4],
|
|
42
|
+
[2, 5],
|
|
43
|
+
[2, 6],
|
|
44
|
+
[3, 3],
|
|
45
|
+
[3, 7],
|
|
46
|
+
[4, 2],
|
|
47
|
+
[4, 3],
|
|
48
|
+
[4, 4],
|
|
49
|
+
[4, 5],
|
|
50
|
+
[4, 6],
|
|
51
|
+
[4, 7],
|
|
52
|
+
[4, 8],
|
|
53
|
+
[5, 3],
|
|
54
|
+
[5, 7],
|
|
55
|
+
[6, 4],
|
|
56
|
+
[6, 5],
|
|
57
|
+
[6, 6],
|
|
58
|
+
[8, 8],
|
|
59
|
+
[8, 9],
|
|
60
|
+
[8, 10],
|
|
61
|
+
[9, 7],
|
|
62
|
+
[9, 11],
|
|
63
|
+
[10, 7],
|
|
64
|
+
[10, 11],
|
|
65
|
+
[11, 8],
|
|
66
|
+
[11, 9],
|
|
67
|
+
[11, 10],
|
|
68
|
+
[12, 2],
|
|
69
|
+
[12, 3],
|
|
70
|
+
[12, 4],
|
|
71
|
+
[13, 1],
|
|
72
|
+
[13, 5],
|
|
73
|
+
[14, 1],
|
|
74
|
+
[14, 5],
|
|
75
|
+
[15, 2],
|
|
76
|
+
[15, 3],
|
|
77
|
+
[15, 4]
|
|
78
|
+
];
|
|
79
|
+
|
|
80
|
+
export const SCENE_CELL_SET = new Set(
|
|
81
|
+
SCENE_CELLS.map(([row, column]) => `${row},${column}`)
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
export const VARIANT_ORDER = [
|
|
85
|
+
"neon-pulse",
|
|
86
|
+
"scanline-sweep",
|
|
87
|
+
"glitch-shift",
|
|
88
|
+
"matrix-drip",
|
|
89
|
+
"packet-trace",
|
|
90
|
+
"firewall-lock",
|
|
91
|
+
"quantum-lag",
|
|
92
|
+
"signal-bloom",
|
|
93
|
+
"ghost-shell",
|
|
94
|
+
"core-breach"
|
|
95
|
+
];
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ReactNode } from 'react'
|
|
2
|
+
|
|
3
|
+
export const LOADING_VARIANTS: readonly string[]
|
|
4
|
+
|
|
5
|
+
export function LoadingAnimation(props?: {
|
|
6
|
+
width?: number
|
|
7
|
+
height?: number
|
|
8
|
+
}): ReactNode
|
|
9
|
+
|
|
10
|
+
export function AnimatedVariant(props: {
|
|
11
|
+
variantId: string
|
|
12
|
+
frame?: number
|
|
13
|
+
initialFrame?: number
|
|
14
|
+
speed?: number
|
|
15
|
+
autoplay?: boolean
|
|
16
|
+
}): ReactNode
|
|
17
|
+
|
|
18
|
+
export function AnimationGrid(props: { grid: unknown[][] }): ReactNode
|
|
19
|
+
|
|
20
|
+
export const animationIds: readonly string[]
|
|
21
|
+
|
|
22
|
+
export function getVariant(name: string): unknown
|
|
23
|
+
export const variantNames: readonly string[]
|
|
24
|
+
export function getAllVariants(): Array<{ name: string; [key: string]: unknown }>
|
package/src/index.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import React, { useEffect, useMemo, useState } from 'react'
|
|
2
|
+
import { Box } from 'ink'
|
|
3
|
+
import { AnimatedVariant } from './components.js'
|
|
4
|
+
|
|
5
|
+
/** Variants available for the loading animation (randomly selected on mount) */
|
|
6
|
+
export const LOADING_VARIANTS = [
|
|
7
|
+
'neon-pulse',
|
|
8
|
+
'scanline-sweep',
|
|
9
|
+
'glitch-shift',
|
|
10
|
+
'matrix-drip',
|
|
11
|
+
'packet-trace',
|
|
12
|
+
'firewall-lock',
|
|
13
|
+
'quantum-lag',
|
|
14
|
+
'signal-bloom',
|
|
15
|
+
'ghost-shell',
|
|
16
|
+
'core-breach',
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
const LOGO_WIDTH = 16
|
|
20
|
+
const LOGO_HEIGHT = 8
|
|
21
|
+
const POSITION_TICK_MS = 80
|
|
22
|
+
|
|
23
|
+
function clamp(value, min, max) {
|
|
24
|
+
return Math.min(max, Math.max(min, value))
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function createInitialMotion(width, height) {
|
|
28
|
+
const maxX = Math.max(0, width - LOGO_WIDTH)
|
|
29
|
+
const maxY = Math.max(0, height - LOGO_HEIGHT)
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
x: Math.floor(maxX / 2),
|
|
33
|
+
y: Math.floor(maxY / 2),
|
|
34
|
+
dx: maxX > 0 ? (Math.random() < 0.5 ? 1 : -1) : 0,
|
|
35
|
+
dy: maxY > 0 ? (Math.random() < 0.5 ? 1 : -1) : 0,
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Short loading animation that randomly selects a variant on mount and
|
|
41
|
+
* bounces around the available area like a DVD screensaver logo.
|
|
42
|
+
*/
|
|
43
|
+
export function LoadingAnimation({
|
|
44
|
+
width = 48,
|
|
45
|
+
height = 16,
|
|
46
|
+
} = {}) {
|
|
47
|
+
const variantId = useMemo(
|
|
48
|
+
() => LOADING_VARIANTS[Math.floor(Math.random() * LOADING_VARIANTS.length)],
|
|
49
|
+
[]
|
|
50
|
+
)
|
|
51
|
+
const boundedWidth = Math.max(LOGO_WIDTH, Math.floor(width))
|
|
52
|
+
const boundedHeight = Math.max(LOGO_HEIGHT, Math.floor(height))
|
|
53
|
+
const [motion, setMotion] = useState(() =>
|
|
54
|
+
createInitialMotion(boundedWidth, boundedHeight)
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
setMotion((current) => {
|
|
59
|
+
const maxX = Math.max(0, boundedWidth - LOGO_WIDTH)
|
|
60
|
+
const maxY = Math.max(0, boundedHeight - LOGO_HEIGHT)
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
x: clamp(current.x, 0, maxX),
|
|
64
|
+
y: clamp(current.y, 0, maxY),
|
|
65
|
+
dx: maxX > 0 ? current.dx || 1 : 0,
|
|
66
|
+
dy: maxY > 0 ? current.dy || 1 : 0,
|
|
67
|
+
}
|
|
68
|
+
})
|
|
69
|
+
}, [boundedHeight, boundedWidth])
|
|
70
|
+
|
|
71
|
+
useEffect(() => {
|
|
72
|
+
const timer = globalThis.setInterval(() => {
|
|
73
|
+
setMotion((current) => {
|
|
74
|
+
const maxX = Math.max(0, boundedWidth - LOGO_WIDTH)
|
|
75
|
+
const maxY = Math.max(0, boundedHeight - LOGO_HEIGHT)
|
|
76
|
+
let nextX = current.x + current.dx
|
|
77
|
+
let nextY = current.y + current.dy
|
|
78
|
+
let nextDx = current.dx
|
|
79
|
+
let nextDy = current.dy
|
|
80
|
+
|
|
81
|
+
if (nextX <= 0 || nextX >= maxX) {
|
|
82
|
+
nextDx = -nextDx || 0
|
|
83
|
+
nextX = clamp(nextX, 0, maxX)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (nextY <= 0 || nextY >= maxY) {
|
|
87
|
+
nextDy = -nextDy || 0
|
|
88
|
+
nextY = clamp(nextY, 0, maxY)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
x: nextX,
|
|
93
|
+
y: nextY,
|
|
94
|
+
dx: nextDx,
|
|
95
|
+
dy: nextDy,
|
|
96
|
+
}
|
|
97
|
+
})
|
|
98
|
+
}, POSITION_TICK_MS)
|
|
99
|
+
|
|
100
|
+
return () => globalThis.clearInterval(timer)
|
|
101
|
+
}, [boundedHeight, boundedWidth])
|
|
102
|
+
|
|
103
|
+
return React.createElement(
|
|
104
|
+
Box,
|
|
105
|
+
{
|
|
106
|
+
width: boundedWidth,
|
|
107
|
+
height: boundedHeight,
|
|
108
|
+
overflow: 'hidden',
|
|
109
|
+
},
|
|
110
|
+
React.createElement(
|
|
111
|
+
Box,
|
|
112
|
+
{
|
|
113
|
+
flexDirection: 'column',
|
|
114
|
+
marginTop: motion.y,
|
|
115
|
+
marginLeft: motion.x,
|
|
116
|
+
},
|
|
117
|
+
React.createElement(AnimatedVariant, {
|
|
118
|
+
variantId,
|
|
119
|
+
autoplay: true,
|
|
120
|
+
})
|
|
121
|
+
)
|
|
122
|
+
)
|
|
123
|
+
}
|
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
const VIEW_WIDTH = 38;
|
|
2
|
+
const VIEW_HEIGHT = 30;
|
|
3
|
+
const BASE_HORIZON = 7;
|
|
4
|
+
const CAMERA_HEIGHT = 1.15;
|
|
5
|
+
const FOCAL_LENGTH = 16;
|
|
6
|
+
const MAX_OBJECT_DISTANCE = 52;
|
|
7
|
+
const DEFAULT_LOOP_FRAMES = 110;
|
|
8
|
+
const SOLID_BLOCK = "██";
|
|
9
|
+
const HEAVY_BLOCK = "▓▓";
|
|
10
|
+
const MID_BLOCK = "▒▒";
|
|
11
|
+
const LIGHT_BLOCK = "░░";
|
|
12
|
+
const EMPTY_CELL = {text: " ", color: "#000000", backgroundColor: "#000000", dimColor: true, bold: false};
|
|
13
|
+
|
|
14
|
+
const clamp = (value, min, max) => Math.min(max, Math.max(min, value));
|
|
15
|
+
const mod = (value, divisor) => {
|
|
16
|
+
const result = value % divisor;
|
|
17
|
+
return result < 0 ? result + divisor : result;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const lerp = (start, end, amount) => start + (end - start) * amount;
|
|
21
|
+
|
|
22
|
+
const smoothstep = value => value * value * (3 - 2 * value);
|
|
23
|
+
|
|
24
|
+
const makeCell = (text, color, dimColor = false, bold = false, backgroundColor = color) => ({
|
|
25
|
+
text,
|
|
26
|
+
color,
|
|
27
|
+
backgroundColor,
|
|
28
|
+
dimColor,
|
|
29
|
+
bold
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const hash01 = value => {
|
|
33
|
+
const result = Math.sin(value * 12.9898) * 43758.5453123;
|
|
34
|
+
return result - Math.floor(result);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const noise1D = (value, seed) => {
|
|
38
|
+
const left = Math.floor(value);
|
|
39
|
+
const offset = value - left;
|
|
40
|
+
const eased = smoothstep(offset);
|
|
41
|
+
const a = hash01(left + seed * 101.173);
|
|
42
|
+
const b = hash01(left + 1 + seed * 101.173);
|
|
43
|
+
|
|
44
|
+
return lerp(a, b, eased);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const fbm1D = (value, seed) => {
|
|
48
|
+
let total = 0;
|
|
49
|
+
let amplitude = 0.5;
|
|
50
|
+
let frequency = 1;
|
|
51
|
+
|
|
52
|
+
for (let octave = 0; octave < 4; octave += 1) {
|
|
53
|
+
total += noise1D(value * frequency, seed + octave * 17.11) * amplitude;
|
|
54
|
+
frequency *= 2;
|
|
55
|
+
amplitude *= 0.5;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return total;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const createCanvas = (height, width, fill) => {
|
|
62
|
+
const rows = [];
|
|
63
|
+
|
|
64
|
+
for (let row = 0; row < height; row += 1) {
|
|
65
|
+
const columns = [];
|
|
66
|
+
|
|
67
|
+
for (let column = 0; column < width; column += 1) {
|
|
68
|
+
columns.push(fill(row, column));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
rows.push(columns);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return rows;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const setCell = (canvas, row, column, cell) => {
|
|
78
|
+
if (row < 0 || row >= canvas.length || column < 0 || column >= canvas[0].length) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
canvas[row][column] = cell;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const drawSpan = (canvas, row, startColumn, endColumn, cellFactory) => {
|
|
86
|
+
const from = Math.max(0, Math.min(startColumn, endColumn));
|
|
87
|
+
const to = Math.min(canvas[0].length - 1, Math.max(startColumn, endColumn));
|
|
88
|
+
|
|
89
|
+
for (let column = from; column <= to; column += 1) {
|
|
90
|
+
setCell(canvas, row, column, cellFactory(column));
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const drawLine = (canvas, startRow, startColumn, endRow, endColumn, cell) => {
|
|
95
|
+
let x0 = Math.round(startColumn);
|
|
96
|
+
let y0 = Math.round(startRow);
|
|
97
|
+
const x1 = Math.round(endColumn);
|
|
98
|
+
const y1 = Math.round(endRow);
|
|
99
|
+
const deltaX = Math.abs(x1 - x0);
|
|
100
|
+
const deltaY = -Math.abs(y1 - y0);
|
|
101
|
+
const stepX = x0 < x1 ? 1 : -1;
|
|
102
|
+
const stepY = y0 < y1 ? 1 : -1;
|
|
103
|
+
let error = deltaX + deltaY;
|
|
104
|
+
|
|
105
|
+
while (true) {
|
|
106
|
+
setCell(canvas, y0, x0, cell);
|
|
107
|
+
|
|
108
|
+
if (x0 === x1 && y0 === y1) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const next = error * 2;
|
|
113
|
+
|
|
114
|
+
if (next >= deltaY) {
|
|
115
|
+
error += deltaY;
|
|
116
|
+
x0 += stepX;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (next <= deltaX) {
|
|
120
|
+
error += deltaX;
|
|
121
|
+
y0 += stepY;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const fillRect = (canvas, top, left, height, width, cellFactory) => {
|
|
127
|
+
for (let row = top; row < top + height; row += 1) {
|
|
128
|
+
if (row < 0 || row >= canvas.length) {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
for (let column = left; column < left + width; column += 1) {
|
|
133
|
+
if (column < 0 || column >= canvas[0].length) {
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
setCell(canvas, row, column, cellFactory(row, column));
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const fillTriangle = (canvas, apexRow, apexColumn, baseRow, halfWidth, cellFactory) => {
|
|
143
|
+
const start = Math.round(apexRow);
|
|
144
|
+
const end = Math.round(baseRow);
|
|
145
|
+
|
|
146
|
+
for (let row = start; row <= end; row += 1) {
|
|
147
|
+
const amount = end === start ? 1 : (row - start) / (end - start);
|
|
148
|
+
const width = Math.max(1, Math.round(halfWidth * amount));
|
|
149
|
+
drawSpan(canvas, row, Math.round(apexColumn) - width, Math.round(apexColumn) + width, column =>
|
|
150
|
+
cellFactory(row, column)
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
const projectedGroundRow = horizon => distance =>
|
|
156
|
+
horizon + (CAMERA_HEIGHT * FOCAL_LENGTH) / Math.max(0.65, distance);
|
|
157
|
+
|
|
158
|
+
const getLoopFrames = scene => scene.loopFrames ?? DEFAULT_LOOP_FRAMES;
|
|
159
|
+
const getLoopDistance = scene => scene.loopDistance ?? scene.forwardSpeed * getLoopFrames(scene);
|
|
160
|
+
const wrapSceneDistance = (value, scene) => mod(value, getLoopDistance(scene));
|
|
161
|
+
const loopNoise1D = (value, seed, period) => {
|
|
162
|
+
const wrapped = mod(value, period);
|
|
163
|
+
const blend = smoothstep(wrapped / Math.max(1, period));
|
|
164
|
+
const head = fbm1D(wrapped, seed);
|
|
165
|
+
const tail = fbm1D(wrapped - period, seed);
|
|
166
|
+
|
|
167
|
+
return lerp(head, tail, blend);
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const curveAt = (distance, scene) => {
|
|
171
|
+
const wrappedDistance = wrapSceneDistance(distance, scene);
|
|
172
|
+
const loopDistance = getLoopDistance(scene);
|
|
173
|
+
const broad =
|
|
174
|
+
(loopNoise1D(wrappedDistance * 0.03 + scene.seed, scene.seed, loopDistance * 0.03) - 0.45) * scene.curveAmount;
|
|
175
|
+
const medium = Math.sin((wrappedDistance / loopDistance) * Math.PI * 2 + scene.seed * 0.7) * scene.curveAmount * 0.35;
|
|
176
|
+
const fine =
|
|
177
|
+
(loopNoise1D(wrappedDistance * 0.09 + scene.seed * 3, scene.seed + 91, loopDistance * 0.09) - 0.5) *
|
|
178
|
+
scene.curveAmount *
|
|
179
|
+
0.18;
|
|
180
|
+
|
|
181
|
+
return broad + medium + fine;
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
const hillAt = (distance, scene) =>
|
|
185
|
+
(loopNoise1D(
|
|
186
|
+
wrapSceneDistance(distance, scene) * 0.018 + scene.seed * 7,
|
|
187
|
+
scene.seed + 41,
|
|
188
|
+
getLoopDistance(scene) * 0.018
|
|
189
|
+
) -
|
|
190
|
+
0.5) *
|
|
191
|
+
scene.hillAmount;
|
|
192
|
+
|
|
193
|
+
const projectWorld = (scene, progress, horizon, worldX, worldZ) => {
|
|
194
|
+
const curveOffset = curveAt(progress + worldZ, scene) - curveAt(progress, scene);
|
|
195
|
+
const screenX = VIEW_WIDTH / 2 + ((worldX + curveOffset) / Math.max(3, worldZ)) * 18;
|
|
196
|
+
const groundY = projectedGroundRow(horizon)(worldZ);
|
|
197
|
+
|
|
198
|
+
return {
|
|
199
|
+
x: screenX,
|
|
200
|
+
y: groundY
|
|
201
|
+
};
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
const drawTree = (canvas, x, bottom, height, width, palette) => {
|
|
205
|
+
const trunkHeight = Math.max(1, Math.round(height * 0.3));
|
|
206
|
+
const trunkTop = bottom - trunkHeight + 1;
|
|
207
|
+
|
|
208
|
+
fillRect(canvas, trunkTop, x, trunkHeight, 1, () => makeCell(HEAVY_BLOCK, palette.trunk, false, false));
|
|
209
|
+
|
|
210
|
+
fillTriangle(canvas, bottom - height, x, trunkTop, Math.max(1, width), () =>
|
|
211
|
+
makeCell(SOLID_BLOCK, palette.leaf, false, true)
|
|
212
|
+
);
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
const drawBuilding = (canvas, x, bottom, height, width, palette) => {
|
|
216
|
+
const top = bottom - height + 1;
|
|
217
|
+
fillRect(canvas, top, x - Math.floor(width / 2), height, width, (row, column) => {
|
|
218
|
+
const edge = column === x - Math.floor(width / 2) || column === x + Math.floor(width / 2);
|
|
219
|
+
const windowRow = row < bottom - 1 && (row + column) % 3 === 0;
|
|
220
|
+
|
|
221
|
+
if (edge) {
|
|
222
|
+
return makeCell(HEAVY_BLOCK, palette.edge, false, true);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return windowRow ? makeCell(LIGHT_BLOCK, palette.window, true, false) : makeCell(SOLID_BLOCK, palette.face, false, false);
|
|
226
|
+
});
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
const drawLamp = (canvas, x, bottom, height, palette) => {
|
|
230
|
+
fillRect(canvas, bottom - height + 1, x, height, 1, () => makeCell(HEAVY_BLOCK, palette.post, false, false));
|
|
231
|
+
setCell(canvas, bottom - height, x, makeCell(SOLID_BLOCK, palette.light, false, true));
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
const drawPalm = (canvas, x, bottom, height, lean, palette) => {
|
|
235
|
+
const trunkTopRow = bottom - height + 2;
|
|
236
|
+
const topColumn = x + lean;
|
|
237
|
+
drawLine(canvas, bottom, x, trunkTopRow, topColumn, makeCell(HEAVY_BLOCK, palette.trunk, false, false));
|
|
238
|
+
drawLine(canvas, trunkTopRow, topColumn, trunkTopRow - 1, topColumn - 2, makeCell(SOLID_BLOCK, palette.leaf, false, true));
|
|
239
|
+
drawLine(canvas, trunkTopRow, topColumn, trunkTopRow - 1, topColumn + 2, makeCell(SOLID_BLOCK, palette.leaf, false, true));
|
|
240
|
+
drawLine(canvas, trunkTopRow, topColumn, trunkTopRow, topColumn - 2, makeCell(HEAVY_BLOCK, palette.leaf, false, true));
|
|
241
|
+
drawLine(canvas, trunkTopRow, topColumn, trunkTopRow, topColumn + 2, makeCell(HEAVY_BLOCK, palette.leaf, false, true));
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
const drawPost = (canvas, x, bottom, height, palette) => {
|
|
245
|
+
fillRect(canvas, bottom - height + 1, x, height, 1, (row, column) =>
|
|
246
|
+
(row + column) % 2 === 0
|
|
247
|
+
? makeCell(HEAVY_BLOCK, palette.main, false, false)
|
|
248
|
+
: makeCell(SOLID_BLOCK, palette.accent, false, false)
|
|
249
|
+
);
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
const drawHut = (canvas, x, bottom, height, width, palette) => {
|
|
253
|
+
const roofRow = bottom - height;
|
|
254
|
+
const left = x - Math.floor(width / 2);
|
|
255
|
+
const right = x + Math.floor(width / 2);
|
|
256
|
+
|
|
257
|
+
drawLine(canvas, roofRow + 1, x, roofRow, left, makeCell(HEAVY_BLOCK, palette.roof, false, true));
|
|
258
|
+
drawLine(canvas, roofRow + 1, x, roofRow, right, makeCell(HEAVY_BLOCK, palette.roof, false, true));
|
|
259
|
+
fillRect(canvas, roofRow + 1, left + 1, height - 1, width - 1, (row, column) => {
|
|
260
|
+
const doorway = column === x && row >= bottom - 1;
|
|
261
|
+
return doorway ? makeCell(HEAVY_BLOCK, palette.door, false, true) : makeCell(SOLID_BLOCK, palette.wall, false, false);
|
|
262
|
+
});
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
const drawRoundedCluster = (canvas, x, bottom, height, width, palette) => {
|
|
266
|
+
for (let offset = 0; offset < height; offset += 1) {
|
|
267
|
+
const row = bottom - offset;
|
|
268
|
+
const radius = Math.max(1, Math.round(width * (1 - offset / Math.max(1, height))));
|
|
269
|
+
drawSpan(canvas, row, x - radius, x + radius, () => makeCell(HEAVY_BLOCK, palette.main, false, offset === height - 1));
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
// eslint-disable-next-line no-unused-vars -- frame kept for consistent callback signature
|
|
274
|
+
const sceneBaseCell = (scene, row, column, horizon, frame) => {
|
|
275
|
+
if (row < horizon) {
|
|
276
|
+
const skyMix = clamp(row / Math.max(1, horizon), 0, 1);
|
|
277
|
+
const farSky = row < horizon - 4;
|
|
278
|
+
const midSky = row < horizon - 2;
|
|
279
|
+
const char = farSky ? " " : midSky ? LIGHT_BLOCK : MID_BLOCK;
|
|
280
|
+
const color = skyMix < 0.35 ? scene.skyTop : skyMix < 0.75 ? scene.skyMid : scene.skyBottom;
|
|
281
|
+
return makeCell(char, color, farSky, false);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
return EMPTY_CELL;
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
// eslint-disable-next-line no-unused-vars -- frame kept for consistent callback signature
|
|
288
|
+
const drawBackdrop = (canvas, scene, progress, horizon, frame) => {
|
|
289
|
+
for (let column = 0; column < VIEW_WIDTH; column += 1) {
|
|
290
|
+
const ridgeNoise = fbm1D(column * 0.21 + progress * 0.01, scene.seed + 13);
|
|
291
|
+
const ridgeHeight = Math.round(ridgeNoise * scene.backdropHeight);
|
|
292
|
+
const topRow = clamp(horizon - ridgeHeight, 1, horizon);
|
|
293
|
+
|
|
294
|
+
for (let row = topRow; row <= horizon; row += 1) {
|
|
295
|
+
setCell(
|
|
296
|
+
canvas,
|
|
297
|
+
row,
|
|
298
|
+
column,
|
|
299
|
+
makeCell(row === topRow ? scene.backdropEdgeChar : scene.backdropChar, scene.backdropColor, row < horizon, row === topRow)
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
if (scene.backdropStyle === "city") {
|
|
305
|
+
for (let column = 0; column < VIEW_WIDTH; column += 3) {
|
|
306
|
+
const seed = Math.floor(progress * 0.04) + column * 17;
|
|
307
|
+
const buildingHeight = 1 + Math.floor(hash01(seed + scene.seed) * 5);
|
|
308
|
+
const left = column;
|
|
309
|
+
const width = 1 + Math.floor(hash01(seed + scene.seed * 5) * 2);
|
|
310
|
+
fillRect(canvas, horizon - buildingHeight, left, buildingHeight, width, (row, col) => {
|
|
311
|
+
const window = row < horizon - 1 && (row + col) % 4 === 0;
|
|
312
|
+
return window
|
|
313
|
+
? makeCell(LIGHT_BLOCK, "#cde3ff", true, false)
|
|
314
|
+
: makeCell(SOLID_BLOCK, row === horizon - buildingHeight ? "#456785" : "#31495f", false, false);
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
if (scene.backdropStyle === "farm") {
|
|
320
|
+
fillRect(canvas, horizon - 2, 28, 2, 4, () => makeCell(SOLID_BLOCK, "#7c4f2d", false, true));
|
|
321
|
+
drawLine(canvas, horizon - 2, 30, horizon - 4, 27, makeCell(HEAVY_BLOCK, "#d5b273", false, true));
|
|
322
|
+
drawLine(canvas, horizon - 2, 30, horizon - 4, 33, makeCell(HEAVY_BLOCK, "#d5b273", false, true));
|
|
323
|
+
fillRect(canvas, horizon - 3, 34, 3, 1, () => makeCell(HEAVY_BLOCK, "#bfcad4", false, false));
|
|
324
|
+
fillRect(canvas, horizon - 5, 34, 2, 1, () => makeCell(SOLID_BLOCK, "#dce7f2", false, true));
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
if (scene.backdropStyle === "ocean") {
|
|
328
|
+
drawSpan(canvas, horizon + 1, 0, VIEW_WIDTH - 1, () => makeCell(MID_BLOCK, "#5ebff3", true, false));
|
|
329
|
+
for (let column = 6; column < VIEW_WIDTH - 6; column += 8) {
|
|
330
|
+
if (hash01(column + scene.seed) > 0.55) {
|
|
331
|
+
setCell(canvas, horizon - 1, column, makeCell(SOLID_BLOCK, "#f3f8ff", false, true));
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (scene.backdropStyle === "tropical") {
|
|
337
|
+
for (let column = 0; column < VIEW_WIDTH; column += 1) {
|
|
338
|
+
const volcanoHeight = Math.max(0, 5 - Math.abs(column - 26) / 3);
|
|
339
|
+
if (volcanoHeight > 0) {
|
|
340
|
+
setCell(canvas, Math.round(horizon - volcanoHeight), column, makeCell(HEAVY_BLOCK, "#244d48", false, true));
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
const paintGround = (canvas, scene, progress, horizon, frame) => {
|
|
347
|
+
for (let row = horizon + 1; row < VIEW_HEIGHT; row += 1) {
|
|
348
|
+
const distance = (CAMERA_HEIGHT * FOCAL_LENGTH) / Math.max(0.45, row - horizon + 0.25);
|
|
349
|
+
const worldDistance = wrapSceneDistance(progress + distance * scene.depthScale, scene);
|
|
350
|
+
const curveOffset = curveAt(worldDistance, scene) - curveAt(progress, scene);
|
|
351
|
+
const screenCenter = VIEW_WIDTH / 2 + (curveOffset / Math.max(2.8, distance)) * 18;
|
|
352
|
+
const halfWidth = clamp((scene.trackHalfWidth * FOCAL_LENGTH) / Math.max(0.9, distance), 2, 14);
|
|
353
|
+
const nearFactor = clamp((row - horizon) / Math.max(1, VIEW_HEIGHT - horizon - 1), 0, 1);
|
|
354
|
+
|
|
355
|
+
for (let column = 0; column < VIEW_WIDTH; column += 1) {
|
|
356
|
+
const delta = column - screenCenter;
|
|
357
|
+
const onPath = Math.abs(delta) <= halfWidth;
|
|
358
|
+
const onEdge = Math.abs(Math.abs(delta) - halfWidth) <= 0.7;
|
|
359
|
+
|
|
360
|
+
if (onPath) {
|
|
361
|
+
setCell(canvas, row, column, scene.pathCell({row, column, delta, halfWidth, worldDistance, nearFactor, frame}));
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
if (onEdge) {
|
|
366
|
+
setCell(canvas, row, column, scene.edgeCell({row, column, delta, halfWidth, worldDistance, nearFactor, frame}));
|
|
367
|
+
continue;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
setCell(canvas, row, column, scene.sideCell({row, column, delta, halfWidth, worldDistance, nearFactor, frame}));
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
const drawProjectedObjects = (canvas, scene, progress, horizon, frame) => {
|
|
376
|
+
for (let distance = MAX_OBJECT_DISTANCE; distance >= 5; distance -= 3) {
|
|
377
|
+
const segment = Math.floor(wrapSceneDistance(progress + distance * scene.depthScale, scene));
|
|
378
|
+
const objects = scene.generateObjects(segment, distance, progress, frame);
|
|
379
|
+
|
|
380
|
+
for (const object of objects) {
|
|
381
|
+
const projected = projectWorld(scene, progress, horizon, object.worldX, distance);
|
|
382
|
+
const bottom = Math.round(projected.y);
|
|
383
|
+
const height = Math.max(2, Math.round((object.height * FOCAL_LENGTH) / distance));
|
|
384
|
+
const width = Math.max(1, Math.round((object.width * FOCAL_LENGTH) / (distance * 1.4)));
|
|
385
|
+
const screenX = Math.round(projected.x);
|
|
386
|
+
|
|
387
|
+
if (screenX < -4 || screenX > VIEW_WIDTH + 4 || bottom < 0 || bottom >= VIEW_HEIGHT + 4) {
|
|
388
|
+
continue;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
if (object.type === "tree") {
|
|
392
|
+
drawTree(canvas, screenX, bottom, height, width, object.palette);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (object.type === "building") {
|
|
396
|
+
drawBuilding(canvas, screenX, bottom, height, Math.max(2, width), object.palette);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
if (object.type === "lamp") {
|
|
400
|
+
drawLamp(canvas, screenX, bottom, height, object.palette);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
if (object.type === "palm") {
|
|
404
|
+
drawPalm(canvas, screenX, bottom, height, object.lean, object.palette);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
if (object.type === "post") {
|
|
408
|
+
drawPost(canvas, screenX, bottom, height, object.palette);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
if (object.type === "hut") {
|
|
412
|
+
drawHut(canvas, screenX, bottom, height, Math.max(3, width + 1), object.palette);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
if (object.type === "bush") {
|
|
416
|
+
drawRoundedCluster(canvas, screenX, bottom, Math.max(2, Math.round(height * 0.5)), Math.max(1, width), object.palette);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
const drawRunnerShadow = (canvas, scene, frame) => {
|
|
423
|
+
const center = Math.floor(VIEW_WIDTH / 2 + Math.sin(frame * 0.45) * 0.6);
|
|
424
|
+
const row = VIEW_HEIGHT - 2;
|
|
425
|
+
|
|
426
|
+
setCell(canvas, row, center - 1, makeCell(SOLID_BLOCK, scene.shadowColor, true, false));
|
|
427
|
+
setCell(canvas, row, center + 1, makeCell(SOLID_BLOCK, scene.shadowColor, true, false));
|
|
428
|
+
setCell(canvas, row + 1, center, makeCell(HEAVY_BLOCK, scene.shadowColor, true, false));
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
const renderRunnerScene = (scene, frame) => {
|
|
432
|
+
const loopFrame = mod(frame, getLoopFrames(scene));
|
|
433
|
+
const progress = loopFrame * scene.forwardSpeed;
|
|
434
|
+
const horizon = clamp(
|
|
435
|
+
BASE_HORIZON + Math.round(hillAt(progress, scene)) + Math.round(Math.sin(loopFrame * 0.42) * 0.35),
|
|
436
|
+
5,
|
|
437
|
+
9
|
|
438
|
+
);
|
|
439
|
+
const canvas = createCanvas(VIEW_HEIGHT, VIEW_WIDTH, (row, column) =>
|
|
440
|
+
sceneBaseCell(scene, row, column, horizon, loopFrame)
|
|
441
|
+
);
|
|
442
|
+
|
|
443
|
+
drawBackdrop(canvas, scene, progress, horizon, loopFrame);
|
|
444
|
+
paintGround(canvas, scene, progress, horizon, loopFrame);
|
|
445
|
+
drawProjectedObjects(canvas, scene, progress, horizon, loopFrame);
|
|
446
|
+
drawRunnerShadow(canvas, scene, loopFrame);
|
|
447
|
+
|
|
448
|
+
return canvas;
|
|
449
|
+
};
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
const runnerScenes = [];
|
|
453
|
+
|
|
454
|
+
export const runnerSceneVariants = Object.fromEntries(
|
|
455
|
+
runnerScenes.map(({name, title, blurb, speed, scene}) => [
|
|
456
|
+
name,
|
|
457
|
+
{
|
|
458
|
+
title,
|
|
459
|
+
blurb,
|
|
460
|
+
speed,
|
|
461
|
+
painter: frame => renderRunnerScene(scene, frame)
|
|
462
|
+
}
|
|
463
|
+
])
|
|
464
|
+
);
|
package/src/variants.js
ADDED
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import {
|
|
2
|
+
GRID_SIZE,
|
|
3
|
+
ICON_CELL_SET,
|
|
4
|
+
VARIANT_ORDER
|
|
5
|
+
} from "./icon.js";
|
|
6
|
+
import {runnerSceneVariants} from "./runner-scenes.js";
|
|
7
|
+
|
|
8
|
+
const ACTIVE_GLYPHS = ["██", "▓▓", "▒▒", "##", "[]", "<>"];
|
|
9
|
+
const VOID_GLYPHS = [" ", "..", "░░", "::"];
|
|
10
|
+
|
|
11
|
+
const clamp = (value, min, max) => Math.min(max, Math.max(min, value));
|
|
12
|
+
|
|
13
|
+
const mod = (value, divisor) => {
|
|
14
|
+
const result = value % divisor;
|
|
15
|
+
return result < 0 ? result + divisor : result;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const isActive = (row, column) => ICON_CELL_SET.has(`${row},${column}`);
|
|
19
|
+
|
|
20
|
+
const distanceTo = (row, column, targetRow, targetColumn) =>
|
|
21
|
+
Math.abs(row - targetRow) + Math.abs(column - targetColumn);
|
|
22
|
+
|
|
23
|
+
const makeCell = (text, color, dimColor = false, bold = false) => ({
|
|
24
|
+
text,
|
|
25
|
+
color,
|
|
26
|
+
dimColor,
|
|
27
|
+
bold
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const baseVoid = () => makeCell("..", "#1b2238", true, false);
|
|
31
|
+
|
|
32
|
+
const hueBand = (frame, shift = 0) => {
|
|
33
|
+
const palette = ["#6cf2ff", "#7af0b2", "#b6ff6c", "#00ffa7", "#7fdbff"];
|
|
34
|
+
return palette[mod(frame + shift, palette.length)];
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const activeGlyph = frame => ACTIVE_GLYPHS[mod(frame, ACTIVE_GLYPHS.length)];
|
|
38
|
+
|
|
39
|
+
const voidGlyph = frame => VOID_GLYPHS[mod(frame, VOID_GLYPHS.length)];
|
|
40
|
+
|
|
41
|
+
const renderGrid = painter => {
|
|
42
|
+
return renderCanvas(GRID_SIZE, GRID_SIZE, painter);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const renderCanvas = (height, width, painter) => {
|
|
46
|
+
const rows = [];
|
|
47
|
+
|
|
48
|
+
for (let row = 0; row < height; row += 1) {
|
|
49
|
+
const columns = [];
|
|
50
|
+
|
|
51
|
+
for (let column = 0; column < width; column += 1) {
|
|
52
|
+
columns.push(painter(row, column));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
rows.push(columns);
|
|
56
|
+
}
|
|
57
|
+
return rows;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const variants = {
|
|
61
|
+
"neon-pulse": {
|
|
62
|
+
title: "Neon Pulse",
|
|
63
|
+
blurb: "Heartbeat glow with alternating cyan and acid-green charge.",
|
|
64
|
+
speed: 90,
|
|
65
|
+
painter: frame =>
|
|
66
|
+
renderGrid((row, column) => {
|
|
67
|
+
const pulse = Math.sin((frame + row + column) / 2);
|
|
68
|
+
if (!isActive(row, column)) {
|
|
69
|
+
return pulse > 0.75
|
|
70
|
+
? makeCell("░░", "#123047", true, false)
|
|
71
|
+
: baseVoid();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const bright = pulse > 0.3;
|
|
75
|
+
return makeCell(
|
|
76
|
+
bright ? "██" : "▓▓",
|
|
77
|
+
bright ? hueBand(frame, row) : "#2ae6ff",
|
|
78
|
+
false,
|
|
79
|
+
bright
|
|
80
|
+
);
|
|
81
|
+
})
|
|
82
|
+
},
|
|
83
|
+
"scanline-sweep": {
|
|
84
|
+
title: "Scanline Sweep",
|
|
85
|
+
blurb: "A terminal scanner sweeps top to bottom and overcharges intersections.",
|
|
86
|
+
speed: 75,
|
|
87
|
+
painter: frame =>
|
|
88
|
+
renderGrid((row, column) => {
|
|
89
|
+
const sweepRow = mod(frame, GRID_SIZE);
|
|
90
|
+
const delta = Math.abs(row - sweepRow);
|
|
91
|
+
|
|
92
|
+
if (!isActive(row, column)) {
|
|
93
|
+
return delta === 0
|
|
94
|
+
? makeCell("::", "#24495a", true, false)
|
|
95
|
+
: baseVoid();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (delta === 0) {
|
|
99
|
+
return makeCell("██", "#d2fff7", false, true);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (delta === 1) {
|
|
103
|
+
return makeCell("▓▓", "#58ffd1", false, false);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return makeCell("▒▒", "#00c8a2", false, false);
|
|
107
|
+
})
|
|
108
|
+
},
|
|
109
|
+
"glitch-shift": {
|
|
110
|
+
title: "Glitch Shift",
|
|
111
|
+
blurb: "Short digital slips fracture the mark before snapping it back.",
|
|
112
|
+
speed: 85,
|
|
113
|
+
painter: frame =>
|
|
114
|
+
renderGrid((row, column) => {
|
|
115
|
+
const shiftedColumn =
|
|
116
|
+
row === mod(frame, GRID_SIZE) ? clamp(column + (frame % 2 === 0 ? 1 : -1), 0, GRID_SIZE - 1) : column;
|
|
117
|
+
const on = isActive(row, shiftedColumn);
|
|
118
|
+
|
|
119
|
+
if (!on) {
|
|
120
|
+
const glitch = mod(frame + row * 3 + column, 11) === 0;
|
|
121
|
+
return glitch ? makeCell("[]", "#29385a", true, false) : baseVoid();
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const unstable = mod(frame + row + column, 5) === 0;
|
|
125
|
+
return makeCell(
|
|
126
|
+
unstable ? "<>" : activeGlyph(frame),
|
|
127
|
+
unstable ? "#ff5fd2" : "#74f9ff",
|
|
128
|
+
false,
|
|
129
|
+
unstable
|
|
130
|
+
);
|
|
131
|
+
})
|
|
132
|
+
},
|
|
133
|
+
"matrix-drip": {
|
|
134
|
+
title: "Matrix Drip",
|
|
135
|
+
blurb: "Green code rain condenses into the icon and then drains out.",
|
|
136
|
+
speed: 95,
|
|
137
|
+
painter: frame =>
|
|
138
|
+
renderGrid((row, column) => {
|
|
139
|
+
const dripHead = mod(frame + column * 2, GRID_SIZE + 4) - 2;
|
|
140
|
+
const trail = row <= dripHead && row >= dripHead - 2;
|
|
141
|
+
const active = isActive(row, column);
|
|
142
|
+
|
|
143
|
+
if (!active && !trail) {
|
|
144
|
+
return baseVoid();
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (active && row <= dripHead) {
|
|
148
|
+
return makeCell("██", row === dripHead ? "#d8ff8a" : "#58ff6a", false, row === dripHead);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (trail) {
|
|
152
|
+
return makeCell("¦¦", "#2f7a37", true, false);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return makeCell("▓▓", "#35d05d", false, false);
|
|
156
|
+
})
|
|
157
|
+
},
|
|
158
|
+
"packet-trace": {
|
|
159
|
+
title: "Packet Trace",
|
|
160
|
+
blurb: "A bright packet runs the shape path like a traced network route.",
|
|
161
|
+
speed: 80,
|
|
162
|
+
painter: frame => {
|
|
163
|
+
const path = [
|
|
164
|
+
[7, 3],
|
|
165
|
+
[6, 3],
|
|
166
|
+
[5, 3],
|
|
167
|
+
[4, 3],
|
|
168
|
+
[4, 4],
|
|
169
|
+
[3, 5],
|
|
170
|
+
[3, 6],
|
|
171
|
+
[4, 7],
|
|
172
|
+
[3, 3]
|
|
173
|
+
];
|
|
174
|
+
const head = mod(frame, path.length);
|
|
175
|
+
|
|
176
|
+
return renderGrid((row, column) => {
|
|
177
|
+
if (!isActive(row, column)) {
|
|
178
|
+
return baseVoid();
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const pathIndex = path.findIndex(
|
|
182
|
+
([pathRow, pathColumn]) => pathRow === row && pathColumn === column
|
|
183
|
+
);
|
|
184
|
+
|
|
185
|
+
if (pathIndex === -1) {
|
|
186
|
+
return makeCell("▒▒", "#1697a6", false, false);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const distance = mod(head - pathIndex, path.length);
|
|
190
|
+
|
|
191
|
+
if (distance === 0) {
|
|
192
|
+
return makeCell("[]", "#f4fff9", false, true);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (distance <= 2) {
|
|
196
|
+
return makeCell("██", "#6fffe9", false, false);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return makeCell("▓▓", "#198f91", false, false);
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
"firewall-lock": {
|
|
204
|
+
title: "Firewall Lock",
|
|
205
|
+
blurb: "Red alert shutters clamp down, then clear to reveal the glyph.",
|
|
206
|
+
speed: 100,
|
|
207
|
+
painter: frame =>
|
|
208
|
+
renderGrid((row, column) => {
|
|
209
|
+
const barrier = Math.abs(column - 3.5) <= (mod(frame, 8) < 4 ? 0.5 : 2.5);
|
|
210
|
+
const active = isActive(row, column);
|
|
211
|
+
|
|
212
|
+
if (!active) {
|
|
213
|
+
return barrier ? makeCell("##", "#5a1e2b", true, false) : baseVoid();
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return barrier
|
|
217
|
+
? makeCell("██", "#ff5a6b", false, true)
|
|
218
|
+
: makeCell("▓▓", "#ffb347", false, false);
|
|
219
|
+
})
|
|
220
|
+
},
|
|
221
|
+
"quantum-lag": {
|
|
222
|
+
title: "Quantum Lag",
|
|
223
|
+
blurb: "Multiple temporal echoes flicker around the icon before collapsing.",
|
|
224
|
+
speed: 70,
|
|
225
|
+
painter: frame =>
|
|
226
|
+
renderGrid((row, column) => {
|
|
227
|
+
const active = isActive(row, column);
|
|
228
|
+
const echo = isActive(row, clamp(column - 1, 0, GRID_SIZE - 1));
|
|
229
|
+
const ghost = isActive(clamp(row - 1, 0, GRID_SIZE - 1), column);
|
|
230
|
+
|
|
231
|
+
if (!active && !(echo && frame % 3 === 0) && !(ghost && frame % 4 === 0)) {
|
|
232
|
+
return baseVoid();
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (active) {
|
|
236
|
+
const collapsed = mod(frame + row, 6) < 3;
|
|
237
|
+
return makeCell(collapsed ? "██" : "▓▓", collapsed ? "#9cf7ff" : "#6c63ff", false, collapsed);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return makeCell("::", frame % 2 === 0 ? "#3b5bfd" : "#2e9cca", true, false);
|
|
241
|
+
})
|
|
242
|
+
},
|
|
243
|
+
"signal-bloom": {
|
|
244
|
+
title: "Signal Bloom",
|
|
245
|
+
blurb: "Concentric blooms radiate outward from the icon's elbow joint.",
|
|
246
|
+
speed: 90,
|
|
247
|
+
painter: frame => {
|
|
248
|
+
const bloomCenter = [4, 3];
|
|
249
|
+
const radius = mod(frame, 6);
|
|
250
|
+
|
|
251
|
+
return renderGrid((row, column) => {
|
|
252
|
+
const distance = distanceTo(row, column, bloomCenter[0], bloomCenter[1]);
|
|
253
|
+
|
|
254
|
+
if (!isActive(row, column)) {
|
|
255
|
+
return distance === radius
|
|
256
|
+
? makeCell("::", "#234f68", true, false)
|
|
257
|
+
: baseVoid();
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (distance === radius) {
|
|
261
|
+
return makeCell("██", "#ffffff", false, true);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (distance < radius) {
|
|
265
|
+
return makeCell("▓▓", "#7afcff", false, false);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return makeCell("▒▒", "#0ed6b5", false, false);
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
},
|
|
272
|
+
"ghost-shell": {
|
|
273
|
+
title: "Ghost Shell",
|
|
274
|
+
blurb: "A stealth pass leaves dim shell copies hanging in the grid.",
|
|
275
|
+
speed: 85,
|
|
276
|
+
painter: frame =>
|
|
277
|
+
renderGrid((row, column) => {
|
|
278
|
+
const active = isActive(row, column);
|
|
279
|
+
const shell = isActive(row, mod(column - 2, GRID_SIZE));
|
|
280
|
+
|
|
281
|
+
if (active) {
|
|
282
|
+
const hidden = mod(frame + row + column, 7) === 0;
|
|
283
|
+
return makeCell(hidden ? "[]" : "██", hidden ? "#c7b7ff" : "#91f3ff", false, !hidden);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
if (shell && mod(frame, 3) === 0) {
|
|
287
|
+
return makeCell("..", "#385a9c", true, false);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
return voidGlyph(frame).trim()
|
|
291
|
+
? makeCell(voidGlyph(frame), "#162034", true, false)
|
|
292
|
+
: baseVoid();
|
|
293
|
+
})
|
|
294
|
+
},
|
|
295
|
+
"core-breach": {
|
|
296
|
+
title: "Core Breach",
|
|
297
|
+
blurb: "The shape overheats from within, shifting from cyan to amber to white.",
|
|
298
|
+
speed: 65,
|
|
299
|
+
painter: frame =>
|
|
300
|
+
renderGrid((row, column) => {
|
|
301
|
+
if (!isActive(row, column)) {
|
|
302
|
+
const sparks = mod(frame + row * column + column, 13) === 0;
|
|
303
|
+
return sparks ? makeCell("°°", "#7a3620", true, false) : baseVoid();
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const phase = mod(frame + row + column, 9);
|
|
307
|
+
|
|
308
|
+
if (phase <= 2) {
|
|
309
|
+
return makeCell("▒▒", "#26d0ff", false, false);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if (phase <= 5) {
|
|
313
|
+
return makeCell("▓▓", "#ff8c42", false, true);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
return makeCell("██", "#fff4d6", false, true);
|
|
317
|
+
})
|
|
318
|
+
},
|
|
319
|
+
...runnerSceneVariants
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
export const variantNames = VARIANT_ORDER;
|
|
323
|
+
|
|
324
|
+
export const getVariant = name => variants[name];
|
|
325
|
+
|
|
326
|
+
export const getAllVariants = () =>
|
|
327
|
+
variantNames.map(name => ({
|
|
328
|
+
name,
|
|
329
|
+
...variants[name]
|
|
330
|
+
}));
|