Skip to content

Commit 3eaf816

Browse files
committed
separate blob generation from legacy choices
1 parent 2316095 commit 3eaf816

File tree

4 files changed

+61
-88
lines changed

4 files changed

+61
-88
lines changed

internal/animate/testing/script.ts

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import blobs from "../../../legacy/blobs";
2-
31
import {interpolateBetweenLoop} from "../interpolate";
42
import {divideShape, prepShapes} from "../prepare";
53
import {Coord, Point, Shape} from "../../types";
64
import {length, insertAt, insertCount, rad, mod} from "../../util";
75
import {clear, drawInfo, drawShape} from "../../render/canvas";
6+
import {genBlob} from "../../blobs";
7+
import {rand} from "../../rand";
88

99
const animationSpeed = 2;
1010
const animationStart = 0.3;
@@ -103,38 +103,32 @@ const testInterpolateBetween = (percentage: number) => {
103103
};
104104

105105
const testPrepShapesA = (percentage: number) => {
106-
const a = genBlob("a", 0.6, 0.6, 0.3, {x: 0.5, y: 0.2});
107-
const b = genBlob("b", 1, 0.6, 0.3, {x: 0.5, y: 0.2});
106+
const a = blob("a", 6, 0.2, {x: 0.5, y: 0.2});
107+
const b = blob("b", 10, 0.2, {x: 0.5, y: 0.2});
108108
drawShape(ctx, debug, interpolateBetweenLoop(percentage, ...prepShapes(a, b)));
109109
};
110110

111111
const testPrepShapesB = (percentage: number) => {
112-
const a = genBlob("a", 0.6, 0.6, 0.3, {x: 0.5, y: 0.5});
112+
const a = blob("a", 8, 0.2, {x: 0.5, y: 0.5});
113113
const b: Shape = [
114-
point(0.55, 0.5, 0, 0, 0, 0),
115-
point(0.75, 0.5, 0, 0, 0, 0),
116-
point(0.75, 0.7, 0, 0, 0, 0),
117-
point(0.55, 0.7, 0, 0, 0, 0),
114+
point(0.5, 0.5, 0, 0, 0, 0),
115+
point(0.7, 0.5, 0, 0, 0, 0),
116+
point(0.7, 0.7, 0, 0, 0, 0),
117+
point(0.5, 0.7, 0, 0, 0, 0),
118118
];
119119
drawShape(ctx, debug, interpolateBetweenLoop(percentage, ...prepShapes(a, b)));
120120
};
121121

122-
const genBlob = (
123-
seed: string,
124-
complexity: number,
125-
contrast: number,
126-
s: number,
127-
offset: Coord,
128-
): Shape => {
129-
const shape = blobs.path({
130-
complexity,
131-
contrast,
132-
size: s * size,
133-
seed,
134-
});
122+
const blob = (seed: string, count: number, scale: number, offset: Coord): Shape => {
123+
const rgen = rand(seed);
124+
const shape = genBlob(count, () => 0.3 + 0.2 * rgen());
135125
for (let i = 0; i < shape.length; i++) {
126+
shape[i].x *= scale * size;
127+
shape[i].y *= scale * size;
136128
shape[i].x += offset.x * size;
137129
shape[i].y += offset.y * size;
130+
shape[i].handleIn.length *= scale * size;
131+
shape[i].handleOut.length *= scale * size;
138132
}
139133
return shape;
140134
};

internal/blobs.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {Shape} from "./types";
2+
import {smooth} from "../internal/util";
3+
4+
export const genBlob = (points: number, offset: () => number): Shape => {
5+
const angle = (Math.PI * 2) / points;
6+
const boundingSize = 1;
7+
const boundingCenter = boundingSize / 2;
8+
9+
const shape: Shape = [];
10+
for (let i = 0; i < points; i++) {
11+
const randPointOffset = offset();
12+
const pointX = Math.sin(i * angle);
13+
const pointY = Math.cos(i * angle);
14+
shape.push({
15+
x: boundingCenter + pointX * randPointOffset,
16+
y: boundingCenter + pointY * randPointOffset,
17+
handleIn: {angle: 0, length: 0},
18+
handleOut: {angle: 0, length: 0},
19+
});
20+
}
21+
22+
// https://math.stackexchange.com/a/873589/235756
23+
const smoothingStrength = ((4 / 3) * Math.tan(angle / 4)) / Math.sin(angle / 2);
24+
25+
return smooth(shape, smoothingStrength);
26+
};

0 commit comments

Comments
 (0)