Skip to content

Commit 8060e09

Browse files
committed
Add experimental Bézier support
1 parent 8db041f commit 8060e09

File tree

11 files changed

+240
-45
lines changed

11 files changed

+240
-45
lines changed

.github/workflows/static.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ jobs:
3131
steps:
3232
- name: Checkout
3333
uses: actions/checkout@v4
34-
- name: Use Node.js v20
34+
- name: Use Node.js v22
3535
uses: actions/setup-node@v4
3636
with:
37-
node-version: '20.x'
37+
node-version: '22.x'
3838
- name: Install dependencies
3939
run: npm ci
4040
- name: Build

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
</div>
5959
<pre id=out></pre>
6060
</div>
61-
<div id=useful_links>
61+
<div id=useful_links style="display:none">
6262
<a target=_blank rel=noreferrer href=https://github.com/mbolis/line.script title="Find me on GitHub">
6363
<svg viewBox="0 0 438.549 438.549">
6464
<path fill="currentColor" d="M409.132 114.573c-19.608-33.596-46.205-60.194-79.798-79.8-33.598-19.607-70.277-29.408-110.063-29.408-39.781 0-76.472 9.804-110.063 29.408-33.596 19.605-60.192 46.204-79.8 79.8C9.803 148.168 0 184.854 0 224.63c0 47.78 13.94 90.745 41.827 128.906 27.884 38.164 63.906 64.572 108.063 79.227 5.14.954 8.945.283 11.419-1.996 2.475-2.282 3.711-5.14 3.711-8.562 0-.571-.049-5.708-.144-15.417a2549.81 2549.81 0 01-.144-25.406l-6.567 1.136c-4.187.767-9.469 1.092-15.846 1-6.374-.089-12.991-.757-19.842-1.999-6.854-1.231-13.229-4.086-19.13-8.559-5.898-4.473-10.085-10.328-12.56-17.556l-2.855-6.57c-1.903-4.374-4.899-9.233-8.992-14.559-4.093-5.331-8.232-8.945-12.419-10.848l-1.999-1.431c-1.332-.951-2.568-2.098-3.711-3.429-1.142-1.331-1.997-2.663-2.568-3.997-.572-1.335-.098-2.43 1.427-3.289 1.525-.859 4.281-1.276 8.28-1.276l5.708.853c3.807.763 8.516 3.042 14.133 6.851 5.614 3.806 10.229 8.754 13.846 14.842 4.38 7.806 9.657 13.754 15.846 17.847 6.184 4.093 12.419 6.136 18.699 6.136 6.28 0 11.704-.476 16.274-1.423 4.565-.952 8.848-2.383 12.847-4.285 1.713-12.758 6.377-22.559 13.988-29.41-10.848-1.14-20.601-2.857-29.264-5.14-8.658-2.286-17.605-5.996-26.835-11.14-9.235-5.137-16.896-11.516-22.985-19.126-6.09-7.614-11.088-17.61-14.987-29.979-3.901-12.374-5.852-26.648-5.852-42.826 0-23.035 7.52-42.637 22.557-58.817-7.044-17.318-6.379-36.732 1.997-58.24 5.52-1.715 13.706-.428 24.554 3.853 10.85 4.283 18.794 7.952 23.84 10.994 5.046 3.041 9.089 5.618 12.135 7.708 17.705-4.947 35.976-7.421 54.818-7.421s37.117 2.474 54.823 7.421l10.849-6.849c7.419-4.57 16.18-8.758 26.262-12.565 10.088-3.805 17.802-4.853 23.134-3.138 8.562 21.509 9.325 40.922 2.279 58.24 15.036 16.18 22.559 35.787 22.559 58.817 0 16.178-1.958 30.497-5.853 42.966-3.9 12.471-8.941 22.457-15.125 29.979-6.191 7.521-13.901 13.85-23.131 18.986-9.232 5.14-18.182 8.85-26.84 11.136-8.662 2.286-18.415 4.004-29.263 5.146 9.894 8.562 14.842 22.077 14.842 40.539v60.237c0 3.422 1.19 6.279 3.572 8.562 2.379 2.279 6.136 2.95 11.276 1.995 44.163-14.653 80.185-41.062 108.068-79.226 27.88-38.161 41.825-81.126 41.825-128.906-.01-39.771-9.818-76.454-29.414-110.049z"></path>

src/bezier/bezier.ts

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import type { Frame } from "../frames";
2+
import { rad2deg, Vector2d, type Degrees } from "../geometry";
3+
4+
export interface BezierResult {
5+
position: Vector2d;
6+
facing: Degrees;
7+
}
8+
9+
export class Bezier {
10+
private static nextId = 0;
11+
readonly id = Bezier.nextId++;
12+
13+
private mode: "quadratic" | "cubic";
14+
private d10: Vector2d;
15+
private d21: Vector2d;
16+
private d32: Vector2d;
17+
18+
constructor(
19+
protected p0: Vector2d,
20+
protected p1: Vector2d,
21+
protected p2: Vector2d,
22+
protected p3?: Vector2d,
23+
) {
24+
this.mode = p3 ? "cubic" : "quadratic";
25+
26+
this.p0 = Vector2d.of(p0);
27+
this.p1 = Vector2d.of(p1);
28+
this.p2 = Vector2d.of(p2);
29+
this.p3 = p3 && Vector2d.of(p3);
30+
31+
this.d10 = this.p1.minus(p0);
32+
this.d21 = this.p2.minus(p1);
33+
this.d32 = p3 && this.p3.minus(p2);
34+
}
35+
36+
protected reposition({ position, facingRadians }: Frame) {
37+
this.p0 = this.p0.rotate(facingRadians).plus(position);
38+
this.p1 = this.p1.rotate(facingRadians).plus(position);
39+
this.p2 = this.p2.rotate(facingRadians).plus(position);
40+
this.p3 = this.p3 && this.p3.rotate(facingRadians).plus(position);
41+
42+
this.d10 = this.p1.minus(this.p0);
43+
this.d21 = this.p2.minus(this.p1);
44+
this.d32 = this.p3 && this.p3.minus(this.p2);
45+
}
46+
47+
value(t: number) {
48+
return this[this.mode](t);
49+
}
50+
51+
private quadratic(t: number): BezierResult {
52+
const t_ = 1 - t;
53+
54+
const a = t_ ** 2;
55+
const b = 2 * t * t_;
56+
const c = t ** 2;
57+
const position = this.p0.scale(a)
58+
.plus(this.p1.scale(b))
59+
.plus(this.p2.scale(c));
60+
const facingCoords = this.d10.scale(t_)
61+
.plus(this.d21.scale(t));
62+
const facing = rad2deg(Math.atan2(facingCoords.y, facingCoords.x)) - 90;
63+
64+
return { position, facing };
65+
}
66+
67+
private cubic(t: number): BezierResult {
68+
const t_ = 1 - t;
69+
70+
const a = t_ ** 3;
71+
const b = 3 * t * t_ ** 2;
72+
const c = 3 * t ** 2 * t_;
73+
const d = t ** 3;
74+
const position = this.p0.scale(a)
75+
.plus(this.p1.scale(b))
76+
.plus(this.p2.scale(c))
77+
.plus(this.p3.scale(d));
78+
const a_ = t_ ** 2;
79+
const b_ = 2 * t * t_;
80+
const c_ = t ** 2
81+
const facingCoords = this.d10.scale(a_)
82+
.plus(this.d21.scale(b_))
83+
.plus(this.d32.scale(c_));
84+
const facing = rad2deg(Math.atan2(facingCoords.y, facingCoords.x)) - 90;
85+
86+
return { position, facing };
87+
}
88+
89+
private static readonly APPROX_STEP = 1 / 16;
90+
91+
private _approxLength: number;
92+
93+
get approxLength() {
94+
if (this._approxLength !== undefined) {
95+
return this._approxLength;
96+
}
97+
98+
let p = this.p0;
99+
let length = 0;
100+
for (let t = 0; t <= 1; t += Bezier.APPROX_STEP) {
101+
const p_ = this.value(t).position;
102+
length += p_.minus(p).length;
103+
p = p_;
104+
}
105+
return this._approxLength = length | 0;
106+
}
107+
}

src/bezier/index.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import type { Frame } from "../frames";
2+
import { Vector2d } from "../geometry";
3+
import { Bezier as BezierCurve, type BezierResult } from "./bezier";
4+
5+
const subscribers = new Map<number, (r: BezierResult) => void>();
6+
const worker = new Worker(new URL("./worker", import.meta.url), { type: "module" });
7+
worker.addEventListener("message", e => {
8+
const { id, value, done } = e.data;
9+
if (done) {
10+
subscribers.delete(id);
11+
} else {
12+
value.position = Vector2d.of(value.position);
13+
subscribers.get(id)(value);
14+
}
15+
});
16+
17+
export class Bezier extends BezierCurve {
18+
private lut = [] as BezierResult[];
19+
20+
constructor(
21+
p0: Vector2d,
22+
p1: Vector2d,
23+
p2: Vector2d,
24+
p3?: Vector2d,
25+
) {
26+
super(p0, p1, p2, p3);
27+
28+
const lut = this.lut;
29+
subscribers.set(this.id, v => lut.push(v))
30+
}
31+
32+
repositionAndCalculate(frame: Frame, sync: boolean) {
33+
this.reposition(frame);
34+
if (sync) {
35+
const length = this.approxLength;
36+
for (let i = 0; i < length; i++) {
37+
this.lut.push(this.value(i / (length - 1)));
38+
}
39+
} else {
40+
worker.postMessage({ id: this.id, p0: this.p0, p1: this.p1, p2: this.p2, p3: this.p3 });
41+
}
42+
}
43+
44+
lookupValues(t: number) {
45+
const i = this.approxLength * t | 0;
46+
const values = this.lut.slice(0, i + 1);
47+
if (values.length === 0) return [this.value(t)];
48+
return values;
49+
}
50+
}

src/bezier/worker.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Bezier } from "./bezier";
2+
3+
addEventListener("message", e => {
4+
const { id, p0, p1, p2, p3 } = e.data;
5+
6+
const curve = new Bezier(p0, p1, p2, p3);
7+
const length = curve.approxLength;
8+
for (let i = 0; i < length; i++) {
9+
postMessage({ id, value: curve.value(i / (length - 1)) });
10+
}
11+
postMessage({ done: true });
12+
});

src/frames.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,23 @@ type FrameMutations = {
66
facing?: Degrees;
77
height?: number;
88
opacity?: number;
9-
newStrokes?: Stroke[];
9+
strokes?: Stroke[];
1010
foreground?: string;
1111
background?: string;
1212
speed?: number;
1313
}
14+
1415
export class Frame {
1516
static new() {
1617
return new Frame;
1718
}
1819

1920
private constructor(
20-
readonly position: Vector2d = Vector2d.ORIGIN,
21-
readonly facing: Degrees = 0,
21+
readonly position = Vector2d.ORIGIN,
22+
readonly facing = 0 as Degrees,
2223
readonly height = 0,
2324
readonly opacity = 1,
24-
readonly strokes: Stroke[] = [],
25+
readonly strokes = [] as Stroke[],
2526
readonly foreground = "",
2627
readonly background = "",
2728
readonly speed = 100,
@@ -34,13 +35,13 @@ export class Frame {
3435
with(properties: FrameMutations): Frame {
3536
return new Frame(
3637
properties.position || this.position,
37-
"facing" in properties ? properties.facing : this.facing,
38-
"height" in properties ? properties.height : this.height,
39-
"opacity" in properties ? properties.opacity : this.opacity,
40-
"newStrokes" in properties ? this.strokes.concat(properties.newStrokes) : this.strokes,
41-
"foreground" in properties ? properties.foreground : this.foreground,
42-
"background" in properties ? properties.background : this.background,
43-
"speed" in properties ? properties.speed : this.speed);
38+
properties.facing !== undefined ? properties.facing : this.facing,
39+
properties.height !== undefined ? properties.height : this.height,
40+
properties.opacity !== undefined ? properties.opacity : this.opacity,
41+
properties.strokes || this.strokes,
42+
properties.foreground !== undefined ? properties.foreground : this.foreground,
43+
properties.background !== undefined ? properties.background : this.background,
44+
properties.speed !== undefined ? properties.speed : this.speed);
4445
}
4546
}
4647

src/geometry.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export class Vector2d {
1212
readonly x: number;
1313
readonly y: number;
1414

15+
static of({ x, y }: { x: number, y: number }) {
16+
return new Vector2d(x, y);
17+
}
18+
1519
static x(x: number): Vector2d {
1620
return new Vector2d(x, 0);
1721
}
@@ -27,32 +31,36 @@ export class Vector2d {
2731
this.y = y;
2832
}
2933

30-
plus(that: Vector2d): Vector2d {
34+
get length() {
35+
return Math.sqrt(this.x ** 2 + this.y ** 2);
36+
}
37+
38+
plus(that: Vector2d) {
3139
return new Vector2d(this.x + that.x, this.y + that.y);
3240
}
3341

34-
minus(that: Vector2d): Vector2d {
42+
minus(that: Vector2d) {
3543
return new Vector2d(this.x - that.x, this.y - that.y);
3644
}
3745

38-
scale(k: number): Vector2d {
46+
scale(k: number) {
3947
return new Vector2d(k * this.x, k * this.y);
4048
}
4149

42-
rotate(th: Radians): Vector2d {
50+
rotate(th: Radians) {
4351
let x = this.x * Math.cos(th) - this.y * Math.sin(th);
4452
let y = this.x * Math.sin(th) + this.y * Math.cos(th);
4553
return new Vector2d(x, y);
4654
}
4755

48-
segmentTo(point: Vector2d): Segment {
56+
segmentTo(point: Vector2d) {
4957
return new Segment(this, point);
5058
}
51-
segmentBy(translation: Vector2d): Segment {
59+
segmentBy(translation: Vector2d) {
5260
return new Segment(this, this.plus(translation));
5361
}
5462

55-
toString(): string {
63+
toString() {
5664
return `(${this.x},${this.y})`;
5765
}
5866
};

src/gui.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import { Frame } from "./frames";
44
export const ZOOM_DOWN = 1;
55
export const ZOOM_UP = 1.5;
66

7+
export const DEFAULT_SCALE = 3.2;
8+
79
export class Painter {
810
position: Vector2d = Vector2d.ORIGIN;
911
facing: Degrees = 0;
1012
z = ZOOM_DOWN;
1113
zoom = 0.5;
12-
scale = 3.2;
14+
scale = DEFAULT_SCALE;
1315
opacity = 1;
1416
brushColor = "black";
1517

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import "codemirror/mode/javascript/javascript";
66

77
import { Animation, Frame } from "./frames";
88
import { Interpreter, Instruction } from "./interpreter";
9-
import { Painter } from "./gui";
9+
import { DEFAULT_SCALE, Painter } from "./gui";
1010
import * as disk from "./disk";
1111
import * as output from "./output";
1212
import "./style.css";
@@ -91,11 +91,11 @@ class PainterStudio {
9191

9292
draw() {
9393
this.drawPaper(this.ctx);
94-
this.frame.strokes.forEach(stroke => stroke.defaultColor(this.frame.foreground).draw(ctx, 3.2));
94+
this.frame.strokes.forEach(stroke => stroke.defaultColor(this.frame.foreground).draw(ctx, DEFAULT_SCALE));
9595
this.painter.draw(this.ctx);
9696
}
9797

98-
private drawPaper(ctx: CanvasRenderingContext2D, scale = 3.2) {
98+
private drawPaper(ctx: CanvasRenderingContext2D, scale = DEFAULT_SCALE) {
9999
const WIDTH = ctx.canvas.width;
100100
const HEIGHT = ctx.canvas.height;
101101
const X = WIDTH / 2;

0 commit comments

Comments
 (0)