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

Commit 62dcfed

Browse files
Merge pull request #14 from hamal-ho/hamal-ho
feat(tools): Line, Eraser, Ellipse, Eyedropper, Pan, Rectangle, SelectShape, Text, Polygon
2 parents ad9626b + 645084e commit 62dcfed

File tree

13 files changed

+1134
-3
lines changed

13 files changed

+1134
-3
lines changed

demo/simple.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,44 @@
3333
<button onclick="demoLC.trigger('setStrokeWidth', 10)">10px</button>
3434
<button onclick="demoLC.trigger('setStrokeWidth', 20)">20px</button>
3535
<button onclick="demoLC.trigger('setStrokeWidth', 30)">30px</button>
36+
<br>
37+
<button onclick="demoLC.setTool(Pencil)">Pencil</button>
38+
<button onclick="demoLC.setTool(Line)">Line</button>
39+
<button onclick="demoLC.setTool(Eraser)">Eraser</button>
40+
<button onclick="demoLC.setTool(Ellipse)">Ellipse</button>
41+
<button onclick="demoLC.setTool(Pan)">Pan</button>
42+
<button onclick="demoLC.setTool(Rectangle)">Rectangle</button>
43+
<button onclick="demoLC.setTool(SelectShape)">SelectShape</button>
44+
<button onclick="demoLC.setTool(Text)">Text</button>
45+
<button onclick="demoLC.setTool(Eyedropper)">Eyedropper</button>
46+
<button onclick="demoLC.setTool(Polygon)">Polygon</button>
3647

3748
<script src="../dist/bundle.js"></script>
3849

3950
<script type="text/javascript">
51+
4052
const LiterallyCanvas = LC.LiterallyCanvas;
53+
console.log(LC, '=====LiterallyCanvas')
4154
const mainDiv = document.querySelector('.literally');
4255
const lc = new LiterallyCanvas(mainDiv, {
4356
...LC.defaultOptions,
4457
defaultStrokeWidth: 10,
4558
backgroundColor: '#FFF',
59+
tools:[LC.Pencil,LC.Line,LC.Eraser]
4660
});
61+
console.log(lc)
4762
window.demoLC = lc;
63+
// Tools
64+
window.Pencil = new LC.Pencil(lc);
65+
window.Line = new LC.Line(lc);
66+
window.Eraser = new LC.Eraser(lc);
67+
window.Ellipse = new LC.Ellipse(lc);
68+
window.Pan = new LC.Pan(lc);
69+
window.Rectangle = new LC.Rectangle(lc);
70+
window.SelectShape = new LC.SelectShape(lc);
71+
window.Eyedropper = new LC.Eyedropper(lc);
72+
window.Text = new LC.Text(lc);
73+
window.Polygon = new LC.Polygon(lc);
4874
</script>
4975
</body>
5076
</html>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "literallycanvas-core",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "HTML5 drawing widget - core lib",
55
"main": "dist/bundle.js",
66
"files": [

src/index.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1-
import * as toolsBase from "./tools/base";
2-
import Pencil from "./tools/Pencil";
1+
import {
2+
toolsBase,
3+
Pencil,
4+
Line,
5+
Eraser,
6+
Ellipse,
7+
Eyedropper,
8+
Pan,
9+
Rectangle,
10+
SelectShape,
11+
Text,
12+
Polygon,
13+
} from "./tools";
314
import * as actions from "./actions";
415
import bindEvents from "./bindEvents";
516
import * as canvasRenderer from "./canvasRenderer";
@@ -32,6 +43,15 @@ export {
3243
util,
3344
toolsBase,
3445
Pencil,
46+
Line,
47+
Eraser,
48+
Ellipse,
49+
Eyedropper,
50+
Pan,
51+
Rectangle,
52+
SelectShape,
53+
Text,
54+
Polygon,
3555
};
3656

3757
if (window) {
@@ -52,6 +72,15 @@ if (window) {
5272
util,
5373
toolsBase,
5474
Pencil,
75+
Line,
76+
Eraser,
77+
Ellipse,
78+
Eyedropper,
79+
Pan,
80+
Rectangle,
81+
SelectShape,
82+
Text,
83+
Polygon,
5584
};
5685
}
5786

src/tools/Ellipse.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {ToolWithStroke} from "./base";
2+
import {createShape} from "../shapes";
3+
4+
class Ellipse extends ToolWithStroke {
5+
begin(x, y, lc) {
6+
return (this.currentShape = createShape("Ellipse", {
7+
x: x,
8+
y: y,
9+
strokeWidth: this.strokeWidth,
10+
strokeColor: lc.getColor("primary"),
11+
fillColor: lc.getColor("secondary"),
12+
}));
13+
}
14+
15+
continue(x, y, lc) {
16+
this.currentShape.width = x - this.currentShape.x;
17+
this.currentShape.height = y - this.currentShape.y;
18+
return lc.drawShapeInProgress(this.currentShape);
19+
}
20+
21+
end(x, y, lc) {
22+
return lc.saveShape(this.currentShape);
23+
}
24+
}
25+
26+
Ellipse.prototype.name = "Ellipse";
27+
Ellipse.prototype.iconName = "ellipse";
28+
29+
export default Ellipse;

src/tools/Eraser.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Pencil from "./Pencil";
2+
import {createShape} from "../shapes";
3+
4+
class Eraser extends Pencil {
5+
makePoint(x, y, lc) {
6+
return createShape("Point", {
7+
x: x,
8+
y: y,
9+
size: this.strokeWidth,
10+
color: "#000",
11+
});
12+
}
13+
14+
makeShape() {
15+
return createShape("ErasedLinePath");
16+
}
17+
}
18+
19+
Eraser.prototype.name = "Eraser";
20+
Eraser.prototype.iconName = "eraser";
21+
22+
export default Eraser;

src/tools/Eyedropper.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import {Tool} from "./base";
2+
3+
const getPixel = function(ctx, arg) {
4+
var pixel, x, y;
5+
(x = arg.x), (y = arg.y);
6+
pixel = ctx.getImageData(x, y, 1, 1).data;
7+
if (pixel[3]) {
8+
return "rgb(" + pixel[0] + ", " + pixel[1] + ", " + pixel[2] + ")";
9+
} else {
10+
return null;
11+
}
12+
};
13+
14+
class Eyedropper extends Tool {
15+
readColor(x, y, lc) {
16+
var canvas, color, newColor, offset;
17+
offset = lc.getDefaultImageRect();
18+
canvas = lc.getImage();
19+
newColor = getPixel(canvas.getContext("2d"), {
20+
x: x - offset.x,
21+
y: y - offset.y,
22+
});
23+
color = newColor || lc.getColor("background");
24+
if (this.strokeOrFill === "stroke") {
25+
return lc.setColor("primary", newColor);
26+
} else {
27+
return lc.setColor("secondary", newColor);
28+
}
29+
}
30+
31+
begin(x, y, lc) {
32+
return this.readColor(x, y, lc);
33+
}
34+
35+
continue(x, y, lc) {
36+
return this.readColor(x, y, lc);
37+
}
38+
}
39+
40+
Eyedropper.prototype.name = "Eyedropper";
41+
Eyedropper.prototype.iconName = "eyedropper";
42+
Eyedropper.prototype.optionsStyle = "stroke-or-fill";
43+
44+
export default Eyedropper;

src/tools/Line.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+
class Line extends ToolWithStroke {
5+
begin(x, y, lc) {
6+
return (this.currentShape = createShape("Line", {
7+
x1: x,
8+
y1: y,
9+
x2: x,
10+
y2: y,
11+
strokeWidth: this.strokeWidth,
12+
dash: function() {
13+
switch (false) {
14+
case !this.isDashed:
15+
return [this.strokeWidth * 2, this.strokeWidth * 4];
16+
default:
17+
return null;
18+
}
19+
}.call(this),
20+
endCapShapes: this.hasEndArrow ? [null, "arrow"] : null,
21+
color: lc.getColor("primary"),
22+
}));
23+
}
24+
25+
continue(x, y, lc) {
26+
this.currentShape.x2 = x;
27+
this.currentShape.y2 = y;
28+
return lc.drawShapeInProgress(this.currentShape);
29+
}
30+
31+
end(x, y, lc) {
32+
return lc.saveShape(this.currentShape);
33+
}
34+
}
35+
36+
Line.prototype.name = "Line";
37+
Line.prototype.iconName = "line";
38+
Line.prototype.optionsStyle = "line-options-and-stroke-width";
39+
40+
export default Line;

src/tools/Pan.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import {Tool} from "./base";
2+
3+
class Pan extends Tool {
4+
didBecomeActive(lc) {
5+
const unsubscribeFuncs = [];
6+
this.unsubscribe = (function(_this) {
7+
return function() {
8+
let func, i, len, results;
9+
results = [];
10+
for (i = 0, len = unsubscribeFuncs.length; i < len; i++) {
11+
func = unsubscribeFuncs[i];
12+
results.push(func());
13+
}
14+
return results;
15+
};
16+
})(this);
17+
unsubscribeFuncs.push(
18+
lc.on(
19+
"lc-pointerdown",
20+
(function(_this) {
21+
return function(arg) {
22+
let rawX, rawY;
23+
(rawX = arg.rawX), (rawY = arg.rawY);
24+
_this.oldPosition = lc.position;
25+
return (_this.pointerStart = {
26+
x: rawX,
27+
y: rawY,
28+
});
29+
};
30+
})(this),
31+
),
32+
);
33+
return unsubscribeFuncs.push(
34+
lc.on(
35+
"lc-pointerdrag",
36+
(function(_this) {
37+
return function(arg) {
38+
let dp, rawX, rawY;
39+
(rawX = arg.rawX), (rawY = arg.rawY);
40+
dp = {
41+
x: (rawX - _this.pointerStart.x) * lc.backingScale,
42+
y: (rawY - _this.pointerStart.y) * lc.backingScale,
43+
};
44+
return lc.setPan(
45+
_this.oldPosition.x + dp.x,
46+
_this.oldPosition.y + dp.y,
47+
);
48+
};
49+
})(this),
50+
),
51+
);
52+
}
53+
54+
willBecomeInactive(lc) {
55+
return this.unsubscribe();
56+
}
57+
}
58+
59+
Pan.prototype.name = "Pan";
60+
Pan.prototype.iconName = "pan";
61+
Pan.prototype.usesSimpleAPI = false;
62+
63+
export default Pan;

0 commit comments

Comments
 (0)