Skip to content

Commit 7e96316

Browse files
committed
Add color property
1 parent c584f58 commit 7e96316

File tree

5 files changed

+30
-96
lines changed

5 files changed

+30
-96
lines changed

app.js

Lines changed: 0 additions & 79 deletions
This file was deleted.

app.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,10 @@ <h2>Properties:</h2>
130130
</blockquote>
131131
</div>
132132
<dl>
133-
<dt>foreground: <em>color string</em></dt>
133+
<dt>color: <em>color string</em></dt>
134134
<dd>Set the color of the pencil.</dd>
135+
<dt>foreground: <em>color string</em></dt>
136+
<dd>Set the color of all the strokes on paper.</dd>
135137
<dt>background: <em>color string</em></dt>
136138
<dd>Set the color of the paper. Set to "transparent" to revert to the default background.</dd>
137139
<dt>speed: <em>number</em></dt>

src/index.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import MainLoop from "mainloop.js"
1+
import MainLoop from "mainloop.js";
22

33
import CodeMirror from "codemirror";
44
import "codemirror/lib/codemirror.css";
55
import "codemirror/mode/javascript/javascript";
66

7+
import * as disk from "./disk";
78
import { Animation, Frame } from "./frames";
8-
import { Interpreter, Instruction } from "./interpreter";
99
import { DEFAULT_SCALE, Painter } from "./gui";
10-
import * as disk from "./disk";
10+
import { Instruction, Interpreter } from "./interpreter";
1111
import * as output from "./output";
1212
import "./style.css";
1313

@@ -403,15 +403,19 @@ function ensureRunning() {
403403
try {
404404
interpreter = new Interpreter(txtCode.value);
405405
} catch (err) {
406-
//console.error(err)
407406
setState("done");
408407

409-
const [, message, line, ch] = /(.*) \((\d+):(\d+)\)\s*$/.exec(err.message);
410-
const startPos = CodeMirror.Pos(+line, +ch);
411-
const token = editor.getTokenAt(startPos, true);
412-
startPos.line--;
413-
const endPos = { ...startPos, ch: startPos.ch + token.string.length };
414-
markErrorPos(startPos, endPos, `${message} '${token.string}' (${line}:${ch})`);
408+
const errMessage = /(.*) \((\d+):(\d+)\)\s*$/.exec(err.message);
409+
if (errMessage) {
410+
const [, message, line, ch] = errMessage;
411+
const startPos = CodeMirror.Pos(+line, +ch);
412+
const token = editor.getTokenAt(startPos, true);
413+
startPos.line--;
414+
const endPos = { ...startPos, ch: startPos.ch + token.string.length };
415+
markErrorPos(startPos, endPos, `${message} '${token.string}' (${line}:${ch})`);
416+
} else {
417+
output.error(err);
418+
}
415419

416420
throw err;
417421
}

src/interpreter.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import JSInterpreter from "js-interpreter";
21
import { Node } from "acorn";
2+
import JSInterpreter from "js-interpreter";
33
import { deg2rad, Degrees, Vector2d } from "./geometry";
44

55
import { Animation, Frame } from "./frames";
6-
import { Stroke } from "./scene";
76
import * as output from "./output";
7+
import { Stroke } from "./scene";
88

9-
import MainLoop from "mainloop.js" // FIXME: we don't want you here!
9+
import MainLoop from "mainloop.js"; // FIXME: we don't want you here!
1010
import { Bezier } from "./bezier";
1111

1212
export type State = {
@@ -147,7 +147,15 @@ export class Interpreter {
147147
}
148148

149149
private readonly init: InitFunc = (interpreter, scope) => {
150-
setFn("forward", (distance: number, color?: string) => {
150+
let currentColor = "black";
151+
prop("color", {
152+
get: fn(() => currentColor),
153+
set: fn((c: any) => {
154+
currentColor = String(c);
155+
}),
156+
});
157+
158+
setFn("forward", (distance: number, color = currentColor) => {
151159
this.animation = new Animation(MOVE_DURATION * distance / 10, (delta: number, keyFrame: Frame) => {
152160
const from = keyFrame.position, angle = keyFrame.facingRadians;
153161
const to = from.plus(Vector2d.polar(angle, distance * delta));
@@ -158,7 +166,7 @@ export class Interpreter {
158166
return keyFrame.with({ position: to });
159167
});
160168
});
161-
setFn("back", (distance: number, color?: string) => {
169+
setFn("back", (distance: number, color = currentColor) => {
162170
this.animation = new Animation(MOVE_DURATION * distance / 10, (delta: number, keyFrame: Frame) => {
163171
const from = keyFrame.position, angle = keyFrame.facingRadians;
164172
const to = from.plus(Vector2d.polar(angle, -distance * delta));

0 commit comments

Comments
 (0)