Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit 24503a4

Browse files
committed
feat: add basic tool as an example
1 parent 0baa450 commit 24503a4

File tree

4 files changed

+96
-1
lines changed

4 files changed

+96
-1
lines changed

src/LiterallyCanvas.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import actions from "./actions";
22
import bindEvents from "./bindEvents";
33
import { JSONToShape, shapeToJSON } from "./shapes";
44
import { renderShapeToContext } from "./canvasRenderer";
5+
import defaultOptions from "./defaultOptions";
56
import renderSnapshotToImage from "./renderSnapshotToImage";
67
import renderSnapshotToSVG from "./renderSnapshotToSVG";
78
import {
@@ -637,5 +638,6 @@ class LiterallyCanvas {
637638
}
638639
}
639640

641+
LiterallyCanvas.defaultOptions = defaultOptions;
640642

641643
export default LiterallyCanvas;

src/defaultOptions.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Pencil from "./tools/Pencil";
2+
13
const defaultOptions = {
24
imageURLPrefix: "lib/img",
35
primaryColor: "hsla(0, 0%, 0%, 1)",
@@ -16,6 +18,7 @@ const defaultOptions = {
1618
zoomStep: 0.2,
1719
snapshot: null,
1820
onInit: (() => {}),
21+
tools: [Pencil],
1922
};
2023

21-
export default defaultOptions;
24+
export default defaultOptions;

src/tools/Pencil.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { ToolWithStroke } from "./base";
2+
import { createShape } from "../shapes";
3+
4+
5+
class Pencil extends ToolWithStroke {
6+
7+
begin(x, y, lc) {
8+
this.color = lc.getColor("primary");
9+
this.currentShape = this.makeShape();
10+
this.currentShape.addPoint(this.makePoint(x, y, lc));
11+
this.lastEventTime = Date.now();
12+
}
13+
14+
continue(x, y, lc) {
15+
const timeDiff = Date.now() - this.lastEventTime;
16+
17+
if (timeDiff > this.eventTimeThreshold) {
18+
this.lastEventTime += timeDiff;
19+
this.currentShape.addPoint(this.makePoint(x, y, lc));
20+
lc.drawShapeInProgress(this.currentShape);
21+
}
22+
}
23+
24+
end(x, y, lc) {
25+
lc.saveShape(this.currentShape);
26+
this.currentShape = undefined;
27+
}
28+
29+
makePoint(x, y, lc) {
30+
return createShape("Point", {x, y, size: this.strokeWidth, color: this.color});
31+
}
32+
makeShape() { return createShape("LinePath") }
33+
}
34+
35+
Pencil.prototype.name = "Pencil";
36+
Pencil.prototype.iconName = "pencil";
37+
Pencil.prototype.eventTimeThreshold = 10;
38+
39+
40+
export default Pencil;

src/tools/base.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class Tool {
2+
constructor() {}
3+
4+
// called when the user starts dragging
5+
begin(x, y, lc) {}
6+
7+
// called when the user moves while dragging
8+
continue(x, y, lc) {}
9+
10+
// called when the user finishes dragging
11+
end(x, y, lc) {}
12+
13+
didBecomeActive(lc) {}
14+
willBecomeInactive(lc) {}
15+
}
16+
17+
Tool.prototype.name = null; // for debugging
18+
Tool.prototype.iconName = null; // {imageURLPrefix}/{iconName}.png
19+
Tool.prototype.usesSimpleAPI = true;
20+
Tool.prototype.optionsStyle = null; // kind of options GUI to display
21+
22+
23+
class ToolWithStroke extends Tool {
24+
constructor(lc) {
25+
super();
26+
this.strokeWidth = lc.opts.defaultStrokeWidth;
27+
}
28+
29+
didBecomeActive(lc) {
30+
const unsubscribeFuncs = [];
31+
this.unsubscribe = () => {
32+
unsubscribeFuncs.map((func) => func());
33+
};
34+
35+
unsubscribeFuncs.push(lc.on("setStrokeWidth", strokeWidth => {
36+
this.strokeWidth = strokeWidth;
37+
lc.trigger("toolDidUpdateOptions");
38+
})
39+
);
40+
}
41+
42+
willBecomeInactive(lc) {
43+
this.unsubscribe();
44+
}
45+
}
46+
47+
ToolWithStroke.prototype.optionsStyle = "stroke-width";
48+
49+
50+
export { Tool, ToolWithStroke };

0 commit comments

Comments
 (0)