@basmilius/sparkle 1.0.0 → 2.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/dist/index.d.mts +1192 -14
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +4552 -370
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -2
- package/src/aurora/consts.ts +3 -0
- package/src/aurora/index.ts +4 -0
- package/src/aurora/layer.ts +152 -0
- package/src/aurora/simulation.ts +19 -0
- package/src/aurora/types.ts +13 -0
- package/src/balloons/consts.ts +3 -0
- package/src/balloons/index.ts +6 -0
- package/src/balloons/layer.ts +138 -0
- package/src/balloons/particle.ts +110 -0
- package/src/balloons/simulation.ts +19 -0
- package/src/balloons/types.ts +14 -0
- package/src/bubbles/consts.ts +3 -0
- package/src/bubbles/index.ts +4 -0
- package/src/bubbles/layer.ts +233 -0
- package/src/bubbles/simulation.ts +20 -0
- package/src/bubbles/types.ts +21 -0
- package/src/canvas.ts +20 -1
- package/src/color.ts +10 -0
- package/src/confetti/consts.ts +13 -13
- package/src/confetti/index.ts +6 -0
- package/src/confetti/layer.ts +152 -0
- package/src/confetti/particle.ts +105 -0
- package/src/confetti/shapes.ts +104 -0
- package/src/confetti/simulation.ts +9 -203
- package/src/confetti/types.ts +4 -1
- package/src/distance.ts +1 -1
- package/src/donuts/consts.ts +19 -0
- package/src/donuts/donut.ts +12 -0
- package/src/donuts/index.ts +3 -0
- package/src/donuts/layer.ts +270 -0
- package/src/donuts/simulation.ts +25 -0
- package/src/fireflies/consts.ts +3 -0
- package/src/fireflies/index.ts +6 -0
- package/src/fireflies/layer.ts +152 -0
- package/src/fireflies/particle.ts +124 -0
- package/src/fireflies/simulation.ts +18 -0
- package/src/fireflies/types.ts +17 -0
- package/src/firepit/consts.ts +3 -0
- package/src/firepit/index.ts +4 -0
- package/src/firepit/layer.ts +174 -0
- package/src/firepit/simulation.ts +17 -0
- package/src/firepit/types.ts +20 -0
- package/src/fireworks/explosion.ts +8 -8
- package/src/fireworks/firework.ts +9 -8
- package/src/fireworks/index.ts +6 -2
- package/src/fireworks/layer.ts +452 -0
- package/src/fireworks/simulation.ts +9 -484
- package/src/fireworks/spark.ts +7 -7
- package/src/glitter/consts.ts +13 -0
- package/src/glitter/index.ts +4 -0
- package/src/glitter/layer.ts +173 -0
- package/src/glitter/simulation.ts +19 -0
- package/src/glitter/types.ts +23 -0
- package/src/index.ts +28 -0
- package/src/lanterns/consts.ts +13 -0
- package/src/lanterns/index.ts +4 -0
- package/src/lanterns/layer.ts +166 -0
- package/src/lanterns/simulation.ts +17 -0
- package/src/lanterns/types.ts +14 -0
- package/src/layer.ts +24 -0
- package/src/layered.ts +185 -0
- package/src/leaves/consts.ts +16 -0
- package/src/leaves/index.ts +4 -0
- package/src/leaves/layer.ts +251 -0
- package/src/leaves/simulation.ts +18 -0
- package/src/leaves/types.ts +16 -0
- package/src/lightning/consts.ts +3 -0
- package/src/lightning/index.ts +6 -0
- package/src/lightning/layer.ts +41 -0
- package/src/lightning/simulation.ts +17 -0
- package/src/lightning/system.ts +196 -0
- package/src/lightning/types.ts +12 -0
- package/src/matrix/consts.ts +5 -0
- package/src/matrix/index.ts +4 -0
- package/src/matrix/layer.ts +146 -0
- package/src/matrix/simulation.ts +18 -0
- package/src/matrix/types.ts +8 -0
- package/src/orbits/consts.ts +13 -0
- package/src/orbits/index.ts +4 -0
- package/src/orbits/layer.ts +183 -0
- package/src/orbits/simulation.ts +19 -0
- package/src/orbits/types.ts +16 -0
- package/src/particles/consts.ts +3 -0
- package/src/particles/index.ts +4 -0
- package/src/particles/layer.ts +317 -0
- package/src/particles/simulation.ts +26 -0
- package/src/particles/types.ts +10 -0
- package/src/petals/consts.ts +13 -0
- package/src/petals/index.ts +4 -0
- package/src/petals/layer.ts +158 -0
- package/src/petals/simulation.ts +18 -0
- package/src/petals/types.ts +15 -0
- package/src/plasma/consts.ts +3 -0
- package/src/plasma/index.ts +4 -0
- package/src/plasma/layer.ts +92 -0
- package/src/plasma/simulation.ts +17 -0
- package/src/plasma/types.ts +5 -0
- package/src/rain/consts.ts +3 -0
- package/src/rain/index.ts +6 -0
- package/src/rain/layer.ts +172 -0
- package/src/rain/particle.ts +132 -0
- package/src/rain/simulation.ts +21 -0
- package/src/rain/types.ts +22 -0
- package/src/sandstorm/consts.ts +3 -0
- package/src/sandstorm/index.ts +4 -0
- package/src/sandstorm/layer.ts +135 -0
- package/src/sandstorm/simulation.ts +18 -0
- package/src/sandstorm/types.ts +10 -0
- package/src/shooting-stars/index.ts +3 -0
- package/src/shooting-stars/system.ts +149 -0
- package/src/shooting-stars/types.ts +10 -0
- package/src/simulation-canvas.ts +47 -0
- package/src/snow/consts.ts +2 -2
- package/src/snow/index.ts +1 -0
- package/src/snow/layer.ts +263 -0
- package/src/snow/simulation.ts +4 -288
- package/src/sparklers/consts.ts +3 -0
- package/src/sparklers/index.ts +6 -0
- package/src/sparklers/layer.ts +174 -0
- package/src/sparklers/particle.ts +89 -0
- package/src/sparklers/simulation.ts +30 -0
- package/src/sparklers/types.ts +13 -0
- package/src/stars/consts.ts +3 -0
- package/src/stars/index.ts +4 -0
- package/src/stars/layer.ts +133 -0
- package/src/stars/simulation.ts +22 -0
- package/src/stars/types.ts +12 -0
- package/src/streamers/consts.ts +14 -0
- package/src/streamers/index.ts +4 -0
- package/src/streamers/layer.ts +211 -0
- package/src/streamers/simulation.ts +16 -0
- package/src/streamers/types.ts +14 -0
- package/src/trail.ts +140 -0
- package/src/waves/consts.ts +3 -0
- package/src/waves/index.ts +4 -0
- package/src/waves/layer.ts +167 -0
- package/src/waves/simulation.ts +18 -0
- package/src/waves/types.ts +9 -0
- package/src/wormhole/consts.ts +3 -0
- package/src/wormhole/index.ts +4 -0
- package/src/wormhole/layer.ts +181 -0
- package/src/wormhole/simulation.ts +17 -0
- package/src/wormhole/types.ts +10 -0
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { hexToRGB } from '@basmilius/utils';
|
|
2
|
+
import { SimulationLayer } from '../layer';
|
|
3
|
+
import { MATRIX_CHARS, MULBERRY } from './consts';
|
|
4
|
+
import type { MatrixSimulationConfig } from './simulation';
|
|
5
|
+
import type { MatrixColumn } from './types';
|
|
6
|
+
|
|
7
|
+
export class MatrixLayer extends SimulationLayer {
|
|
8
|
+
readonly #scale: number;
|
|
9
|
+
readonly #speed: number;
|
|
10
|
+
readonly #fontSize: number;
|
|
11
|
+
readonly #trailLength: number;
|
|
12
|
+
readonly #colorRGB: [number, number, number];
|
|
13
|
+
#maxColumns: number;
|
|
14
|
+
#columns: MatrixColumn[] = [];
|
|
15
|
+
#respawnTimers: number[] = [];
|
|
16
|
+
#width: number = 960;
|
|
17
|
+
#height: number = 540;
|
|
18
|
+
#initialized: boolean = false;
|
|
19
|
+
|
|
20
|
+
constructor(config: MatrixSimulationConfig = {}) {
|
|
21
|
+
super();
|
|
22
|
+
|
|
23
|
+
this.#scale = config.scale ?? 1;
|
|
24
|
+
this.#maxColumns = config.columns ?? 40;
|
|
25
|
+
this.#speed = config.speed ?? 1;
|
|
26
|
+
this.#fontSize = (config.fontSize ?? 14) * this.#scale;
|
|
27
|
+
this.#trailLength = config.trailLength ?? 20;
|
|
28
|
+
this.#colorRGB = hexToRGB(config.color ?? '#00ff41');
|
|
29
|
+
|
|
30
|
+
if (innerWidth < 991) {
|
|
31
|
+
this.#maxColumns = Math.floor(this.#maxColumns / 2);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
onResize(width: number, height: number): void {
|
|
36
|
+
this.#width = width;
|
|
37
|
+
this.#height = height;
|
|
38
|
+
|
|
39
|
+
if (!this.#initialized) {
|
|
40
|
+
this.#initialized = true;
|
|
41
|
+
this.#columns = [];
|
|
42
|
+
this.#respawnTimers = [];
|
|
43
|
+
|
|
44
|
+
const columnWidth = this.#fontSize;
|
|
45
|
+
const totalSlots = Math.floor(width / columnWidth);
|
|
46
|
+
const columnCount = Math.min(this.#maxColumns, totalSlots);
|
|
47
|
+
|
|
48
|
+
for (let i = 0; i < columnCount; ++i) {
|
|
49
|
+
const column = this.#createColumn(totalSlots, height);
|
|
50
|
+
this.#columns.push(column);
|
|
51
|
+
this.#respawnTimers.push(0);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
tick(dt: number, width: number, height: number): void {
|
|
57
|
+
this.#width = width;
|
|
58
|
+
this.#height = height;
|
|
59
|
+
|
|
60
|
+
const columnWidth = this.#fontSize;
|
|
61
|
+
const totalSlots = Math.floor(width / columnWidth);
|
|
62
|
+
|
|
63
|
+
for (let i = 0; i < this.#columns.length; ++i) {
|
|
64
|
+
if (this.#respawnTimers[i] > 0) {
|
|
65
|
+
this.#respawnTimers[i] -= dt;
|
|
66
|
+
|
|
67
|
+
if (this.#respawnTimers[i] <= 0) {
|
|
68
|
+
this.#columns[i] = this.#createColumn(totalSlots, height);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const column = this.#columns[i];
|
|
75
|
+
|
|
76
|
+
column.y += column.speed * this.#speed * dt;
|
|
77
|
+
|
|
78
|
+
for (let ci = 0; ci < column.chars.length; ++ci) {
|
|
79
|
+
if (MULBERRY.next() < 0.03) {
|
|
80
|
+
column.chars[ci] = MATRIX_CHARS[Math.floor(MULBERRY.next() * MATRIX_CHARS.length)];
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const topOfTrail = column.y - (column.chars.length - 1) * this.#fontSize;
|
|
85
|
+
|
|
86
|
+
if (topOfTrail > height) {
|
|
87
|
+
this.#respawnTimers[i] = 10 + MULBERRY.next() * 60;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
draw(ctx: CanvasRenderingContext2D, width: number, height: number): void {
|
|
93
|
+
ctx.fillStyle = 'rgb(0, 0, 0)';
|
|
94
|
+
ctx.fillRect(0, 0, width, height);
|
|
95
|
+
|
|
96
|
+
const [cr, cg, cb] = this.#colorRGB;
|
|
97
|
+
|
|
98
|
+
ctx.font = `${this.#fontSize}px monospace`;
|
|
99
|
+
ctx.textAlign = 'center';
|
|
100
|
+
|
|
101
|
+
for (const column of this.#columns) {
|
|
102
|
+
const charCount = column.chars.length;
|
|
103
|
+
|
|
104
|
+
for (let ci = 0; ci < charCount; ++ci) {
|
|
105
|
+
const charY = column.y - (charCount - 1 - ci) * this.#fontSize;
|
|
106
|
+
|
|
107
|
+
if (charY < -this.#fontSize || charY > height + this.#fontSize) {
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const isHead = ci === charCount - 1;
|
|
112
|
+
|
|
113
|
+
if (isHead) {
|
|
114
|
+
const headAlpha = column.headBrightness;
|
|
115
|
+
ctx.fillStyle = `rgba(255, 255, 255, ${headAlpha})`;
|
|
116
|
+
} else {
|
|
117
|
+
const trailProgress = ci / (charCount - 1);
|
|
118
|
+
const alpha = trailProgress * 0.8 + 0.05;
|
|
119
|
+
ctx.fillStyle = `rgba(${cr}, ${cg}, ${cb}, ${alpha})`;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
ctx.fillText(column.chars[ci], column.x, charY);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
#createColumn(totalSlots: number, height: number): MatrixColumn {
|
|
128
|
+
const columnWidth = this.#fontSize;
|
|
129
|
+
const slot = Math.floor(MULBERRY.next() * totalSlots);
|
|
130
|
+
const length = Math.floor(this.#trailLength * 0.5 + MULBERRY.next() * this.#trailLength);
|
|
131
|
+
const chars: string[] = [];
|
|
132
|
+
|
|
133
|
+
for (let ci = 0; ci < length; ++ci) {
|
|
134
|
+
chars.push(MATRIX_CHARS[Math.floor(MULBERRY.next() * MATRIX_CHARS.length)]);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return {
|
|
138
|
+
x: slot * columnWidth + columnWidth / 2,
|
|
139
|
+
y: -(MULBERRY.next() * height),
|
|
140
|
+
speed: 1.5 + MULBERRY.next() * 3,
|
|
141
|
+
chars,
|
|
142
|
+
length,
|
|
143
|
+
headBrightness: 0.8 + MULBERRY.next() * 0.2
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { SimulationCanvas } from '../simulation-canvas';
|
|
2
|
+
import { MatrixLayer } from './layer';
|
|
3
|
+
|
|
4
|
+
export interface MatrixSimulationConfig {
|
|
5
|
+
readonly columns?: number;
|
|
6
|
+
readonly speed?: number;
|
|
7
|
+
readonly color?: string;
|
|
8
|
+
readonly fontSize?: number;
|
|
9
|
+
readonly trailLength?: number;
|
|
10
|
+
readonly scale?: number;
|
|
11
|
+
readonly canvasOptions?: CanvasRenderingContext2DSettings;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export class MatrixSimulation extends SimulationCanvas {
|
|
15
|
+
constructor(canvas: HTMLCanvasElement, config: MatrixSimulationConfig = {}) {
|
|
16
|
+
super(canvas, new MatrixLayer(config), 60, config.canvasOptions ?? {colorSpace: 'display-p3'});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type Mulberry32, mulberry32 } from '@basmilius/utils';
|
|
2
|
+
|
|
3
|
+
export const MULBERRY: Mulberry32 = mulberry32(13);
|
|
4
|
+
|
|
5
|
+
export const ORBIT_COLORS: string[] = [
|
|
6
|
+
'#60a5fa', // blue
|
|
7
|
+
'#a78bfa', // purple
|
|
8
|
+
'#f472b6', // pink
|
|
9
|
+
'#34d399', // emerald
|
|
10
|
+
'#fbbf24', // amber
|
|
11
|
+
'#fb923c', // orange
|
|
12
|
+
'#38bdf8' // sky
|
|
13
|
+
];
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { hexToRGB } from '@basmilius/utils';
|
|
2
|
+
import { SimulationLayer } from '../layer';
|
|
3
|
+
import { MULBERRY, ORBIT_COLORS } from './consts';
|
|
4
|
+
import type { OrbitSimulationConfig } from './simulation';
|
|
5
|
+
import type { OrbitalCenter, Orbiter } from './types';
|
|
6
|
+
|
|
7
|
+
export class OrbitLayer extends SimulationLayer {
|
|
8
|
+
readonly #centerCount: number;
|
|
9
|
+
readonly #orbitersPerCenter: number;
|
|
10
|
+
readonly #speed: number;
|
|
11
|
+
readonly #colors: string[];
|
|
12
|
+
readonly #trailLength: number;
|
|
13
|
+
readonly #showCenters: boolean;
|
|
14
|
+
readonly #scale: number;
|
|
15
|
+
#centers: OrbitalCenter[] = [];
|
|
16
|
+
#orbiters: Orbiter[] = [];
|
|
17
|
+
#time: number = 0;
|
|
18
|
+
#initialized: boolean = false;
|
|
19
|
+
|
|
20
|
+
constructor(config: OrbitSimulationConfig = {}) {
|
|
21
|
+
super();
|
|
22
|
+
|
|
23
|
+
this.#centerCount = config.centers ?? 3;
|
|
24
|
+
this.#orbitersPerCenter = config.orbitersPerCenter ?? 8;
|
|
25
|
+
this.#speed = config.speed ?? 1;
|
|
26
|
+
this.#colors = config.colors ?? ORBIT_COLORS;
|
|
27
|
+
this.#trailLength = config.trailLength ?? 15;
|
|
28
|
+
this.#showCenters = config.showCenters ?? true;
|
|
29
|
+
this.#scale = config.scale ?? 1;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
onResize(_width: number, _height: number): void {
|
|
33
|
+
if (!this.#initialized) {
|
|
34
|
+
this.#initialized = true;
|
|
35
|
+
this.#centers = [];
|
|
36
|
+
this.#orbiters = [];
|
|
37
|
+
|
|
38
|
+
for (let i = 0; i < this.#centerCount; i++) {
|
|
39
|
+
this.#centers.push({
|
|
40
|
+
x: 0.15 + MULBERRY.next() * 0.7,
|
|
41
|
+
y: 0.15 + MULBERRY.next() * 0.7
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const count = innerWidth < 991
|
|
46
|
+
? Math.floor(this.#orbitersPerCenter / 2)
|
|
47
|
+
: this.#orbitersPerCenter;
|
|
48
|
+
|
|
49
|
+
for (let ci = 0; ci < this.#centers.length; ci++) {
|
|
50
|
+
for (let oi = 0; oi < count; oi++) {
|
|
51
|
+
this.#orbiters.push(this.#createOrbiter(ci));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
tick(dt: number, width: number, height: number): void {
|
|
58
|
+
this.#time += 0.01 * dt * this.#speed;
|
|
59
|
+
|
|
60
|
+
for (const orbiter of this.#orbiters) {
|
|
61
|
+
const center = this.#centers[orbiter.centerIndex];
|
|
62
|
+
const cx = center.x * width;
|
|
63
|
+
const cy = center.y * height;
|
|
64
|
+
|
|
65
|
+
orbiter.angle += orbiter.angularSpeed * dt * this.#speed;
|
|
66
|
+
|
|
67
|
+
const cosAngle = Math.cos(orbiter.angle);
|
|
68
|
+
const sinAngle = Math.sin(orbiter.angle);
|
|
69
|
+
const cosTilt = Math.cos(orbiter.tilt);
|
|
70
|
+
|
|
71
|
+
const localX = cosAngle * orbiter.radiusX * this.#scale;
|
|
72
|
+
const localY = sinAngle * orbiter.radiusY * this.#scale * cosTilt;
|
|
73
|
+
|
|
74
|
+
const rotatedX = localX * Math.cos(orbiter.tilt * 0.3) - localY * Math.sin(orbiter.tilt * 0.3);
|
|
75
|
+
const rotatedY = localX * Math.sin(orbiter.tilt * 0.3) + localY * Math.cos(orbiter.tilt * 0.3);
|
|
76
|
+
|
|
77
|
+
const px = cx + rotatedX;
|
|
78
|
+
const py = cy + rotatedY;
|
|
79
|
+
|
|
80
|
+
orbiter.trail.push({x: px, y: py});
|
|
81
|
+
|
|
82
|
+
if (orbiter.trail.length > this.#trailLength) {
|
|
83
|
+
orbiter.trail.shift();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
draw(ctx: CanvasRenderingContext2D, width: number, height: number): void {
|
|
89
|
+
|
|
90
|
+
if (this.#showCenters) {
|
|
91
|
+
ctx.globalCompositeOperation = 'lighter';
|
|
92
|
+
|
|
93
|
+
for (const center of this.#centers) {
|
|
94
|
+
const cx = center.x * width;
|
|
95
|
+
const cy = center.y * height;
|
|
96
|
+
const glowRadius = 30 * this.#scale;
|
|
97
|
+
|
|
98
|
+
const gradient = ctx.createRadialGradient(cx, cy, 0, cx, cy, glowRadius);
|
|
99
|
+
gradient.addColorStop(0, 'rgba(255, 255, 255, 0.3)');
|
|
100
|
+
gradient.addColorStop(0.4, 'rgba(200, 200, 255, 0.1)');
|
|
101
|
+
gradient.addColorStop(1, 'rgba(200, 200, 255, 0)');
|
|
102
|
+
|
|
103
|
+
ctx.globalAlpha = 1;
|
|
104
|
+
ctx.beginPath();
|
|
105
|
+
ctx.arc(cx, cy, glowRadius, 0, Math.PI * 2);
|
|
106
|
+
ctx.fillStyle = gradient;
|
|
107
|
+
ctx.fill();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
ctx.globalCompositeOperation = 'source-over';
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
ctx.globalCompositeOperation = 'lighter';
|
|
114
|
+
|
|
115
|
+
for (const orbiter of this.#orbiters) {
|
|
116
|
+
const [cr, cg, cb] = hexToRGB(orbiter.color);
|
|
117
|
+
|
|
118
|
+
if (orbiter.trail.length > 1) {
|
|
119
|
+
for (let ti = 0; ti < orbiter.trail.length - 1; ti++) {
|
|
120
|
+
const progress = (ti + 1) / orbiter.trail.length;
|
|
121
|
+
const trailAlpha = progress * 0.5;
|
|
122
|
+
const trailWidth = orbiter.size * progress * this.#scale;
|
|
123
|
+
|
|
124
|
+
if (trailAlpha < 0.01) {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
ctx.globalAlpha = trailAlpha;
|
|
129
|
+
ctx.strokeStyle = `rgb(${cr}, ${cg}, ${cb})`;
|
|
130
|
+
ctx.lineWidth = trailWidth;
|
|
131
|
+
ctx.beginPath();
|
|
132
|
+
ctx.moveTo(orbiter.trail[ti].x, orbiter.trail[ti].y);
|
|
133
|
+
ctx.lineTo(orbiter.trail[ti + 1].x, orbiter.trail[ti + 1].y);
|
|
134
|
+
ctx.stroke();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (orbiter.trail.length > 0) {
|
|
139
|
+
const head = orbiter.trail[orbiter.trail.length - 1];
|
|
140
|
+
const headSize = orbiter.size * this.#scale;
|
|
141
|
+
|
|
142
|
+
const glow = ctx.createRadialGradient(
|
|
143
|
+
head.x, head.y, 0,
|
|
144
|
+
head.x, head.y, headSize * 3
|
|
145
|
+
);
|
|
146
|
+
glow.addColorStop(0, `rgba(${cr}, ${cg}, ${cb}, 0.9)`);
|
|
147
|
+
glow.addColorStop(0.3, `rgba(${cr}, ${cg}, ${cb}, 0.3)`);
|
|
148
|
+
glow.addColorStop(1, `rgba(${cr}, ${cg}, ${cb}, 0)`);
|
|
149
|
+
|
|
150
|
+
ctx.globalAlpha = 1;
|
|
151
|
+
ctx.beginPath();
|
|
152
|
+
ctx.arc(head.x, head.y, headSize * 3, 0, Math.PI * 2);
|
|
153
|
+
ctx.fillStyle = glow;
|
|
154
|
+
ctx.fill();
|
|
155
|
+
|
|
156
|
+
ctx.beginPath();
|
|
157
|
+
ctx.arc(head.x, head.y, headSize, 0, Math.PI * 2);
|
|
158
|
+
ctx.fillStyle = `rgb(${cr}, ${cg}, ${cb})`;
|
|
159
|
+
ctx.fill();
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
ctx.globalCompositeOperation = 'source-over';
|
|
164
|
+
ctx.globalAlpha = 1;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
#createOrbiter(centerIndex: number): Orbiter {
|
|
168
|
+
const minRadius = 40;
|
|
169
|
+
const maxRadius = 160;
|
|
170
|
+
|
|
171
|
+
return {
|
|
172
|
+
centerIndex,
|
|
173
|
+
angle: MULBERRY.next() * Math.PI * 2,
|
|
174
|
+
angularSpeed: 0.01 + MULBERRY.next() * 0.03,
|
|
175
|
+
radiusX: minRadius + MULBERRY.next() * (maxRadius - minRadius),
|
|
176
|
+
radiusY: (minRadius + MULBERRY.next() * (maxRadius - minRadius)) * (0.3 + MULBERRY.next() * 0.7),
|
|
177
|
+
tilt: MULBERRY.next() * Math.PI,
|
|
178
|
+
size: 1.5 + MULBERRY.next() * 2.5,
|
|
179
|
+
color: this.#colors[Math.floor(MULBERRY.next() * this.#colors.length)],
|
|
180
|
+
trail: []
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { SimulationCanvas } from '../simulation-canvas';
|
|
2
|
+
import { OrbitLayer } from './layer';
|
|
3
|
+
|
|
4
|
+
export interface OrbitSimulationConfig {
|
|
5
|
+
readonly centers?: number;
|
|
6
|
+
readonly orbitersPerCenter?: number;
|
|
7
|
+
readonly speed?: number;
|
|
8
|
+
readonly colors?: string[];
|
|
9
|
+
readonly trailLength?: number;
|
|
10
|
+
readonly showCenters?: boolean;
|
|
11
|
+
readonly scale?: number;
|
|
12
|
+
readonly canvasOptions?: CanvasRenderingContext2DSettings;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class OrbitSimulation extends SimulationCanvas {
|
|
16
|
+
constructor(canvas: HTMLCanvasElement, config: OrbitSimulationConfig = {}) {
|
|
17
|
+
super(canvas, new OrbitLayer(config), 60, config.canvasOptions ?? {colorSpace: 'display-p3'});
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type OrbitalCenter = {
|
|
2
|
+
x: number;
|
|
3
|
+
y: number;
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
export type Orbiter = {
|
|
7
|
+
centerIndex: number;
|
|
8
|
+
angle: number;
|
|
9
|
+
angularSpeed: number;
|
|
10
|
+
radiusX: number;
|
|
11
|
+
radiusY: number;
|
|
12
|
+
tilt: number;
|
|
13
|
+
size: number;
|
|
14
|
+
color: string;
|
|
15
|
+
trail: {x: number; y: number}[];
|
|
16
|
+
};
|