Skip to content

Commit 6b9fff0

Browse files
committed
add angle fix when points overlap
1 parent c79aa24 commit 6b9fff0

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

internal/animate/ease.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {InterpolationFunc} from "./types";
22

33
export const linear: InterpolationFunc = (progress: number) => {
44
return progress;
5-
}
5+
};
66

77
export const ease: InterpolationFunc = (progress: number) => {
88
return 0.5 + 0.5 * Math.sin(Math.PI * (progress + 1.5));

internal/animate/prepare.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
import {copyPoint, length, reverse, shift, insertCount, distance, mod, angleOf} from "../util";
1+
import {
2+
copyPoint,
3+
length,
4+
reverse,
5+
shift,
6+
insertCount,
7+
distance,
8+
mod,
9+
angleOf,
10+
coordEqual,
11+
} from "../util";
212
import {Point, Shape} from "../types";
313

414
const optimizeOrder = (a: Shape, b: Shape): Shape => {
@@ -52,7 +62,24 @@ export const divideShape = (count: number, points: Shape): Shape => {
5262
return out;
5363
};
5464

55-
const fixAngles = (shape: Shape): Shape => {
65+
const fixAnglesWith = (fixee: Shape, fixer: Shape): Shape => {
66+
const out: Shape = [];
67+
for (let i = 0; i < fixee.length; i++) {
68+
const before = fixee[mod(i - 1, fixee.length)];
69+
const after = fixee[mod(i + 1, fixee.length)];
70+
const point = copyPoint(fixee[i]);
71+
if (point.handleIn.length === 0 && coordEqual(before, point)) {
72+
point.handleIn.angle = fixer[i].handleIn.angle;
73+
}
74+
if (point.handleOut.length === 0 && coordEqual(after, point)) {
75+
point.handleOut.angle = fixer[i].handleOut.angle;
76+
}
77+
out.push(point);
78+
}
79+
return out;
80+
};
81+
82+
const fixAnglesSelf = (shape: Shape): Shape => {
5683
const out: Shape = [];
5784
for (let i = 0; i < shape.length; i++) {
5885
const before = shape[mod(i - 1, shape.length)];
@@ -97,5 +124,5 @@ export const prepShapes = (a: Shape, b: Shape): [Shape, Shape] => {
97124
const aNorm = divideShape(points, a);
98125
const bNorm = divideShape(points, b);
99126
const bOpt = optimizeOrder(aNorm, bNorm);
100-
return [fixAngles(aNorm), fixAngles(bOpt)];
127+
return [fixAnglesWith(fixAnglesSelf(aNorm), bNorm), fixAnglesWith(fixAnglesSelf(bOpt), aNorm)];
101128
};

internal/animate/testing/script.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const ctx = temp;
2121

2222
const toggle = document.getElementById("toggle");
2323
if (toggle === null) throw new Error("no toggle");
24-
toggle.onclick = () => debug = !debug;
24+
toggle.onclick = () => (debug = !debug);
2525

2626
const point = (x: number, y: number, ia: number, il: number, oa: number, ol: number): Point => {
2727
return {
@@ -142,6 +142,16 @@ const testPrepShapesC = (percentage: number) => {
142142
drawShape(ctx, debug, interpolateBetweenLoop(percentage, ...prepShapes(a, b)));
143143
};
144144

145+
const testPrepShapesD = (percentage: number) => {
146+
const a = blob("d", 8, 0.15, {x: 0.45, y: 0.65});
147+
const b: Shape = [
148+
point(0.525, 0.725, 0, 0, 0, 0),
149+
point(0.525, 0.725, 0, 0, 0, 0),
150+
point(0.525, 0.725, 0, 0, 0, 0),
151+
];
152+
drawShape(ctx, debug, interpolateBetweenLoop(percentage, ...prepShapes(a, b)));
153+
};
154+
145155
const blob = (seed: string, count: number, scale: number, offset: Coord): Shape => {
146156
const rgen = rand(seed);
147157
const shape = genBlob(count, () => 0.3 + 0.2 * rgen());
@@ -170,6 +180,7 @@ const blob = (seed: string, count: number, scale: number, offset: Coord): Shape
170180
testPrepShapesA(percentage);
171181
testPrepShapesB(percentage);
172182
testPrepShapesC(percentage);
183+
testPrepShapesD(percentage);
173184

174185
percentage += animationSpeed / 1000;
175186
percentage = mod(percentage, 1);

internal/util.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ export const copyPoint = (p: Point): Point => ({
77
handleOut: {...p.handleOut},
88
});
99

10+
// TODO shape iterators
11+
12+
export const coordEqual = (a: Coord, b: Coord): boolean => {
13+
return a.x === b.x && a.y === b.y;
14+
};
15+
1016
export const angleOf = (a: Coord, b: Coord): number => {
1117
const dx = b.x - a.x;
1218
const dy = -b.y + a.y;

0 commit comments

Comments
 (0)