Skip to content

Commit 70ff59b

Browse files
committed
added better timing to it
1 parent fc0fc0c commit 70ff59b

File tree

3 files changed

+60
-42
lines changed

3 files changed

+60
-42
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"@sveltejs/adapter-auto": "^2.1.1",
1616
"@sveltejs/adapter-static": "^2.0.3",
1717
"@sveltejs/kit": "^1.30.4",
18+
"@types/node": "^22.15.19",
1819
"@typescript-eslint/eslint-plugin": "^5.62.0",
1920
"@typescript-eslint/parser": "^5.62.0",
2021
"@vite-pwa/sveltekit": "^1.0.0",

pnpm-lock.yaml

Lines changed: 41 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/routes/porcupine/+page.svelte

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
type Animation,
2121
ZERO_VEC2,
2222
copy,
23-
copyObject
23+
copyObject,
24+
type unsubscribe
2425
} from 'aninest';
2526
import { getUpdateLayer } from '@aninest/extensions';
2627
import { onMount } from 'svelte';
@@ -90,8 +91,8 @@
9091
addLocalListener(anim.children.shape.children.p2, 'start', () =>
9192
onPointChange(anim.children.shape.children.p2)
9293
);
93-
94-
return {
94+
let cancelRandomize: unsubscribe | undefined = undefined;
95+
const out = {
9596
setP1(p1: Vec2) {
9697
modifyTo(anim.children.shape, { p1 });
9798
},
@@ -104,9 +105,15 @@
104105
setColor(color: Color) {
105106
return modifyTo(anim, { color });
106107
},
108+
randomize(withTimeout: boolean) {
109+
if (cancelRandomize) cancelRandomize();
110+
cancelRandomize = randomizeLine(out, withTimeout);
111+
},
107112
update: animLoop,
108-
draw: draw
113+
draw: draw,
114+
symbol: Symbol()
109115
};
116+
return out;
110117
};
111118
const canvas: HTMLCanvasElement = document.querySelector('#porky-canvas')!;
112119
const increasinglySlower = (x: number) => {
@@ -127,7 +134,7 @@
127134
return Math.floor(brightness + randomness);
128135
};
129136
130-
const randomizeLine = async (line: DrawableLine, withTimeout = true) => {
137+
const randomizeLine = (line: DrawableLine, withTimeout = true) => {
131138
// wait for a random amount of time
132139
const canvasMag = mag(newVec2(canvas.width, canvas.height));
133140
const p1 = newVec2(canvas.width * Math.random(), canvas.height * Math.random());
@@ -152,24 +159,25 @@
152159
});
153160
if (!withTimeout) return;
154161
const sinceLastClick = (performance.now() - lastClicked) / 1000;
155-
setTimeout(
156-
() => randomizeLine(line),
162+
const cancel = setTimeout(
163+
() => line.randomize(),
157164
increasinglySlower(1000 * Math.random() + 1000) * sinceLastClick + 1000 * Math.random()
158165
);
166+
return () => clearTimeout(cancel);
159167
};
160168
const randomizeLines = (withTimeout = true) => {
161169
const sinceLastClick = (performance.now() - lastClicked) / 1000;
162170
for (let line of lines) {
163171
setTimeout(
164-
() => randomizeLine(line, withTimeout),
172+
() => line.randomize(withTimeout),
165173
increasinglySlower(10000 * Math.random() * sinceLastClick + 1000 * sinceLastClick + 50)
166174
);
167175
}
168176
};
169177
let lastClicked = performance.now();
170178
171179
const onUp = async (e: PointerEvent) => {
172-
if (downCt != 0) randomizeLines(false);
180+
if (downCt != 0) randomizeLines();
173181
downCt = Math.max(0, downCt - 1);
174182
};
175183
@@ -210,7 +218,7 @@
210218
setTimeout(() => {
211219
onResize();
212220
const canvasMag = mag(newVec2(canvas.width, canvas.height));
213-
for (let i = 0; i < canvasMag; i++) {
221+
for (let i = 0; i < canvasMag * 2; i++) {
214222
const canvasCenter = newVec2(canvas.width / 2, canvas.height / 2);
215223
lines.push(createLine(canvasCenter, canvasCenter));
216224
}

0 commit comments

Comments
 (0)