Skip to content

Commit 77d135b

Browse files
committed
standardize shared coord type
1 parent 0d4c035 commit 77d135b

File tree

10 files changed

+25
-29
lines changed

10 files changed

+25
-29
lines changed

index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// https://www.blobmaker.app/
22

33
import {rand} from "./internal/math/rand";
4-
import {Point} from "./internal/math/geometry";
4+
import {Coord} from "./internal/types";
55
import {rad} from "./internal/math/unit";
66
import {smooth} from "./internal/svg/smooth";
77
import {renderEditable} from "./internal/svg/render";
@@ -72,7 +72,7 @@ blobs.editable = (opt: BlobOptions): XmlElement => {
7272
const angle = 360 / count;
7373
const radius = opt.size / Math.E;
7474

75-
const points: Point[] = [];
75+
const points: Coord[] = [];
7676
for (let i = 0; i < count; i++) {
7777
const rand = 1 - 0.8 * opt.contrast * rgen();
7878

@@ -82,9 +82,11 @@ blobs.editable = (opt: BlobOptions): XmlElement => {
8282
});
8383
}
8484

85+
// https://math.stackexchange.com/a/873589/235756
86+
const smoothingStrength = ((4 / 3) * Math.tan(rad(angle / 4))) / Math.sin(rad(angle / 2));
8587
const smoothed = smooth(points, {
8688
closed: true,
87-
strength: ((4 / 3) * Math.tan(rad(angle / 4))) / Math.sin(rad(angle / 2)),
89+
strength: smoothingStrength,
8890
});
8991

9092
return renderEditable(smoothed, {
@@ -123,7 +125,7 @@ blobs.path = (opt: PathOptions) => {
123125
const angle = 360 / count;
124126
const radius = opt.size / Math.E;
125127

126-
const points: Point[] = [];
128+
const points: Coord[] = [];
127129
for (let i = 0; i < count; i++) {
128130
const rand = 1 - 0.8 * opt.contrast * rgen();
129131

internal/animate/types.ts

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

33
export interface EasingFunc {
44
(progress: number): number;

internal/canvas/draw.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Coord, Shape} from "../shape/types";
1+
import {Coord, Shape} from "../types";
22
import {expandHandle} from "../shape/util";
33

44
const pointSize = 2;

internal/math/geometry.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
import {deg} from "./unit";
2-
import {Coord} from "../shape/types";
3-
4-
export interface Point {
5-
x: number;
6-
y: number;
7-
}
2+
import {Coord} from "../types";
83

94
// Calculates distance between two points.
10-
export const distance = (a: Point, b: Point): number => {
5+
export const distance = (a: Coord, b: Coord): number => {
116
return Math.sqrt((a.x - b.x) ** 2 + (a.y - b.y) ** 2);
127
};
138

149
// Calculates the angle of the line from a to b in degrees.
15-
export const angle = (a: Point, b: Point): number => {
10+
export const angle = (a: Coord, b: Coord): number => {
1611
return deg(Math.atan2(b.y - a.y, b.x - a.x));
1712
};
1813

internal/shape/interpolate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Shape} from "./types";
1+
import {Shape} from "../types";
22
import {split, splitLine} from "../math/geometry";
33

44
const interpolateAngle = (percentage: number, a: number, b: number): number => {

internal/shape/prepare.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {copyPoint, length, reverse, shift, split} from "./util";
2-
import {Point, Shape} from "./types";
2+
import {Point, Shape} from "../types";
33
import {distance} from "../math/geometry";
44

55
const optimizeOrder = (a: Shape, b: Shape): Shape => {

internal/shape/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Coord, Handle, Point, Shape} from "./types";
1+
import {Coord, Handle, Point, Shape} from "../types";
22
import {distance, splitLine} from "../math/geometry";
33

44
export const copyPoint = (p: Point): Point => ({

internal/svg/smooth.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,29 @@ export interface SmoothingOptions {
77
// This option is currently always true.
88
closed: true;
99

10-
// Smoothing strength as ration [0,1].
10+
// Smoothing strength as ratio [0,1].
1111
strength: number;
1212
}
1313

14-
// Smooths out the path made up of the given points. This will override the existing handles.
15-
// https://math.stackexchange.com/questions/873224/calculate-control-points-of-cubic-bezier-curve-approximating-a-part-of-a-circle
14+
// Smooths out the path made up of the given points.
15+
// Existing handles are ignored.
1616
export const smooth = (points: Point[], opt: SmoothingOptions): Point[] => {
17-
if (points.length === 2) return points;
17+
if (points.length < 3) throw new Error("not enough points to smooth shape");
1818

1919
const out: Point[] = [];
2020

2121
for (let i = 0; i < points.length; i++) {
22-
const point = loopAccess(points)(i);
22+
const curr = loopAccess(points)(i);
2323
const before = loopAccess(points)(i - 1);
2424
const after = loopAccess(points)(i + 1);
2525

2626
out.push({
27-
x: point.x,
28-
y: point.y,
27+
x: curr.x,
28+
y: curr.y,
2929
handles: {
3030
angle: angle(before, after),
31-
in: opt.strength * (1 / 2) * distance(point, before),
32-
out: opt.strength * (1 / 2) * distance(point, after),
31+
in: opt.strength * (1 / 2) * distance(curr, before),
32+
out: opt.strength * (1 / 2) * distance(curr, after),
3333
},
3434
});
3535
}

internal/shape/types.ts renamed to internal/types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
// Position in a coordinate system with an origin in the top left corner.
12
export interface Coord {
2-
// Horizontal distance towards the right from the left edge of the canvas.
33
x: number;
4-
// Vertical distance downwards from the top of the canvas.
54
y: number;
65
}
76

testing/animate.ts

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

33
import {interpolateBetweenLoop} from "../internal/shape/interpolate";
44
import {divideShape, prepShapes, splitCurveBy} from "../internal/shape/prepare";
5-
import {Coord, Point, Shape} from "../internal/shape/types";
5+
import {Coord, Point, Shape} from "../internal/types";
66
import {length, split} from "../internal/shape/util";
77
import {clear, drawInfo, drawShape} from "../internal/canvas/draw";
88

0 commit comments

Comments
 (0)