js-draw 0.0.9 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/dist/bundle.js +1 -1
- package/dist/src/Editor.d.ts +2 -2
- package/dist/src/Editor.js +15 -7
- package/dist/src/EditorImage.d.ts +15 -7
- package/dist/src/EditorImage.js +43 -37
- package/dist/src/SVGLoader.d.ts +3 -2
- package/dist/src/SVGLoader.js +9 -7
- package/dist/src/Viewport.d.ts +4 -0
- package/dist/src/Viewport.js +41 -0
- package/dist/src/components/AbstractComponent.d.ts +3 -2
- package/dist/src/components/AbstractComponent.js +3 -0
- package/dist/src/components/SVGGlobalAttributesObject.d.ts +1 -1
- package/dist/src/components/SVGGlobalAttributesObject.js +1 -1
- package/dist/src/components/Stroke.d.ts +1 -1
- package/dist/src/components/UnknownSVGObject.d.ts +1 -1
- package/dist/src/components/UnknownSVGObject.js +1 -1
- package/dist/src/components/builders/ArrowBuilder.d.ts +1 -1
- package/dist/src/components/builders/FreehandLineBuilder.d.ts +1 -1
- package/dist/src/components/builders/FreehandLineBuilder.js +1 -1
- package/dist/src/components/builders/LineBuilder.d.ts +1 -1
- package/dist/src/components/builders/RectangleBuilder.d.ts +1 -1
- package/dist/src/components/builders/types.d.ts +1 -1
- package/dist/src/geometry/Mat33.js +3 -0
- package/dist/src/geometry/Path.d.ts +1 -1
- package/dist/src/geometry/Path.js +5 -3
- package/dist/src/geometry/Rect2.d.ts +1 -0
- package/dist/src/geometry/Rect2.js +47 -9
- package/dist/src/{Display.d.ts → rendering/Display.d.ts} +6 -2
- package/dist/src/{Display.js → rendering/Display.js} +37 -4
- package/dist/src/rendering/caching/CacheRecord.d.ts +19 -0
- package/dist/src/rendering/caching/CacheRecord.js +52 -0
- package/dist/src/rendering/caching/CacheRecordManager.d.ts +11 -0
- package/dist/src/rendering/caching/CacheRecordManager.js +31 -0
- package/dist/src/rendering/caching/RenderingCache.d.ts +12 -0
- package/dist/src/rendering/caching/RenderingCache.js +42 -0
- package/dist/src/rendering/caching/RenderingCacheNode.d.ts +28 -0
- package/dist/src/rendering/caching/RenderingCacheNode.js +301 -0
- package/dist/src/rendering/caching/testUtils.d.ts +9 -0
- package/dist/src/rendering/caching/testUtils.js +20 -0
- package/dist/src/rendering/caching/types.d.ts +21 -0
- package/dist/src/rendering/caching/types.js +1 -0
- package/dist/src/rendering/{AbstractRenderer.d.ts → renderers/AbstractRenderer.d.ts} +19 -8
- package/dist/src/rendering/{AbstractRenderer.js → renderers/AbstractRenderer.js} +37 -2
- package/dist/src/rendering/{CanvasRenderer.d.ts → renderers/CanvasRenderer.d.ts} +14 -5
- package/dist/src/rendering/renderers/CanvasRenderer.js +164 -0
- package/dist/src/rendering/{DummyRenderer.d.ts → renderers/DummyRenderer.d.ts} +9 -5
- package/dist/src/rendering/{DummyRenderer.js → renderers/DummyRenderer.js} +35 -4
- package/dist/src/rendering/{SVGRenderer.d.ts → renderers/SVGRenderer.d.ts} +4 -3
- package/dist/src/rendering/{SVGRenderer.js → renderers/SVGRenderer.js} +14 -11
- package/dist/src/testing/createEditor.js +1 -1
- package/dist/src/toolbar/HTMLToolbar.js +11 -2
- package/dist/src/toolbar/localization.d.ts +1 -0
- package/dist/src/toolbar/localization.js +1 -0
- package/dist/src/tools/PanZoom.js +3 -0
- package/dist/src/tools/SelectionTool.d.ts +3 -0
- package/dist/src/tools/SelectionTool.js +22 -24
- package/dist/src/types.d.ts +2 -1
- package/package.json +1 -1
- package/src/Editor.ts +17 -8
- package/src/EditorImage.test.ts +2 -2
- package/src/EditorImage.ts +54 -42
- package/src/SVGLoader.ts +11 -8
- package/src/Viewport.ts +56 -0
- package/src/components/AbstractComponent.ts +6 -2
- package/src/components/SVGGlobalAttributesObject.ts +2 -2
- package/src/components/Stroke.ts +1 -1
- package/src/components/UnknownSVGObject.ts +2 -2
- package/src/components/builders/ArrowBuilder.ts +1 -1
- package/src/components/builders/FreehandLineBuilder.ts +2 -2
- package/src/components/builders/LineBuilder.ts +1 -1
- package/src/components/builders/RectangleBuilder.ts +1 -1
- package/src/components/builders/types.ts +1 -1
- package/src/geometry/Mat33.ts +3 -0
- package/src/geometry/Path.toString.test.ts +12 -2
- package/src/geometry/Path.ts +8 -4
- package/src/geometry/Rect2.test.ts +47 -8
- package/src/geometry/Rect2.ts +57 -9
- package/src/{Display.ts → rendering/Display.ts} +43 -6
- package/src/rendering/caching/CacheRecord.test.ts +49 -0
- package/src/rendering/caching/CacheRecord.ts +73 -0
- package/src/rendering/caching/CacheRecordManager.ts +45 -0
- package/src/rendering/caching/RenderingCache.test.ts +44 -0
- package/src/rendering/caching/RenderingCache.ts +63 -0
- package/src/rendering/caching/RenderingCacheNode.ts +378 -0
- package/src/rendering/caching/testUtils.ts +35 -0
- package/src/rendering/caching/types.ts +39 -0
- package/src/rendering/{AbstractRenderer.ts → renderers/AbstractRenderer.ts} +57 -8
- package/src/rendering/renderers/CanvasRenderer.ts +219 -0
- package/src/rendering/renderers/DummyRenderer.test.ts +43 -0
- package/src/rendering/{DummyRenderer.ts → renderers/DummyRenderer.ts} +50 -7
- package/src/rendering/{SVGRenderer.ts → renderers/SVGRenderer.ts} +17 -13
- package/src/testing/createEditor.ts +1 -1
- package/src/toolbar/HTMLToolbar.ts +13 -2
- package/src/toolbar/localization.ts +2 -0
- package/src/tools/PanZoom.ts +3 -0
- package/src/tools/SelectionTool.test.ts +1 -1
- package/src/tools/SelectionTool.ts +28 -33
- package/src/types.ts +10 -3
- package/tsconfig.json +1 -0
- package/dist/__mocks__/coloris.d.ts +0 -2
- package/dist/__mocks__/coloris.js +0 -5
- package/dist/src/rendering/CanvasRenderer.js +0 -108
- package/src/rendering/CanvasRenderer.ts +0 -141
@@ -1,108 +0,0 @@
|
|
1
|
-
import Color4 from '../Color4';
|
2
|
-
import { Vec2 } from '../geometry/Vec2';
|
3
|
-
import AbstractRenderer from './AbstractRenderer';
|
4
|
-
const minSquareCurveApproxDist = 25;
|
5
|
-
export default class CanvasRenderer extends AbstractRenderer {
|
6
|
-
constructor(ctx, viewport) {
|
7
|
-
super(viewport);
|
8
|
-
this.ctx = ctx;
|
9
|
-
this.ignoreObjectsAboveLevel = null;
|
10
|
-
this.ignoringObject = false;
|
11
|
-
}
|
12
|
-
displaySize() {
|
13
|
-
return Vec2.of(this.ctx.canvas.clientWidth, this.ctx.canvas.clientHeight);
|
14
|
-
}
|
15
|
-
clear() {
|
16
|
-
this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
|
17
|
-
}
|
18
|
-
beginPath(startPoint) {
|
19
|
-
startPoint = this.viewport.canvasToScreen(startPoint);
|
20
|
-
this.ctx.beginPath();
|
21
|
-
this.ctx.moveTo(startPoint.x, startPoint.y);
|
22
|
-
}
|
23
|
-
endPath(style) {
|
24
|
-
this.ctx.fillStyle = style.fill.toHexString();
|
25
|
-
this.ctx.fill();
|
26
|
-
if (style.stroke) {
|
27
|
-
this.ctx.strokeStyle = style.stroke.color.toHexString();
|
28
|
-
this.ctx.lineWidth = this.viewport.getScaleFactor() * style.stroke.width;
|
29
|
-
this.ctx.stroke();
|
30
|
-
}
|
31
|
-
this.ctx.closePath();
|
32
|
-
}
|
33
|
-
lineTo(point) {
|
34
|
-
point = this.viewport.canvasToScreen(point);
|
35
|
-
this.ctx.lineTo(point.x, point.y);
|
36
|
-
}
|
37
|
-
moveTo(point) {
|
38
|
-
point = this.viewport.canvasToScreen(point);
|
39
|
-
this.ctx.moveTo(point.x, point.y);
|
40
|
-
}
|
41
|
-
traceCubicBezierCurve(p1, p2, p3) {
|
42
|
-
p1 = this.viewport.canvasToScreen(p1);
|
43
|
-
p2 = this.viewport.canvasToScreen(p2);
|
44
|
-
p3 = this.viewport.canvasToScreen(p3);
|
45
|
-
// Approximate the curve if small enough.
|
46
|
-
const delta1 = p2.minus(p1);
|
47
|
-
const delta2 = p3.minus(p2);
|
48
|
-
if (delta1.magnitudeSquared() < minSquareCurveApproxDist
|
49
|
-
&& delta2.magnitudeSquared() < minSquareCurveApproxDist) {
|
50
|
-
this.ctx.lineTo(p3.x, p3.y);
|
51
|
-
}
|
52
|
-
else {
|
53
|
-
this.ctx.bezierCurveTo(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
|
54
|
-
}
|
55
|
-
}
|
56
|
-
traceQuadraticBezierCurve(controlPoint, endPoint) {
|
57
|
-
controlPoint = this.viewport.canvasToScreen(controlPoint);
|
58
|
-
endPoint = this.viewport.canvasToScreen(endPoint);
|
59
|
-
// Approximate the curve with a line if small enough
|
60
|
-
const delta = controlPoint.minus(endPoint);
|
61
|
-
if (delta.magnitudeSquared() < minSquareCurveApproxDist) {
|
62
|
-
this.ctx.lineTo(endPoint.x, endPoint.y);
|
63
|
-
}
|
64
|
-
else {
|
65
|
-
this.ctx.quadraticCurveTo(controlPoint.x, controlPoint.y, endPoint.x, endPoint.y);
|
66
|
-
}
|
67
|
-
}
|
68
|
-
drawPath(path) {
|
69
|
-
if (this.ignoringObject) {
|
70
|
-
return;
|
71
|
-
}
|
72
|
-
super.drawPath(path);
|
73
|
-
}
|
74
|
-
startObject(boundingBox) {
|
75
|
-
// Should we ignore all objects within this object's bbox?
|
76
|
-
const diagonal = this.viewport.canvasToScreenTransform.transformVec3(boundingBox.size);
|
77
|
-
const minRenderSize = 4;
|
78
|
-
if (Math.abs(diagonal.x) < minRenderSize && Math.abs(diagonal.y) < minRenderSize) {
|
79
|
-
this.ignoreObjectsAboveLevel = this.getNestingLevel();
|
80
|
-
this.ignoringObject = true;
|
81
|
-
}
|
82
|
-
super.startObject(boundingBox);
|
83
|
-
}
|
84
|
-
endObject() {
|
85
|
-
super.endObject();
|
86
|
-
// If exiting an object with a too-small-to-draw bounding box,
|
87
|
-
if (this.ignoreObjectsAboveLevel !== null && this.getNestingLevel() <= this.ignoreObjectsAboveLevel) {
|
88
|
-
this.ignoreObjectsAboveLevel = null;
|
89
|
-
this.ignoringObject = false;
|
90
|
-
}
|
91
|
-
}
|
92
|
-
drawPoints(...points) {
|
93
|
-
const pointRadius = 10;
|
94
|
-
for (let i = 0; i < points.length; i++) {
|
95
|
-
const point = this.viewport.canvasToScreen(points[i]);
|
96
|
-
this.ctx.beginPath();
|
97
|
-
this.ctx.arc(point.x, point.y, pointRadius, 0, Math.PI * 2);
|
98
|
-
this.ctx.fillStyle = Color4.ofRGBA(0.5 + Math.sin(i) / 2, 1.0, 0.5 + Math.cos(i * 0.2) / 4, 0.5).toHexString();
|
99
|
-
this.ctx.fill();
|
100
|
-
this.ctx.stroke();
|
101
|
-
this.ctx.closePath();
|
102
|
-
this.ctx.textAlign = 'center';
|
103
|
-
this.ctx.textBaseline = 'middle';
|
104
|
-
this.ctx.fillStyle = 'black';
|
105
|
-
this.ctx.fillText(`${i}`, point.x, point.y, pointRadius * 2);
|
106
|
-
}
|
107
|
-
}
|
108
|
-
}
|
@@ -1,141 +0,0 @@
|
|
1
|
-
import Color4 from '../Color4';
|
2
|
-
import Rect2 from '../geometry/Rect2';
|
3
|
-
import { Point2, Vec2 } from '../geometry/Vec2';
|
4
|
-
import Vec3 from '../geometry/Vec3';
|
5
|
-
import Viewport from '../Viewport';
|
6
|
-
import AbstractRenderer, { RenderablePathSpec, RenderingStyle } from './AbstractRenderer';
|
7
|
-
|
8
|
-
const minSquareCurveApproxDist = 25;
|
9
|
-
export default class CanvasRenderer extends AbstractRenderer {
|
10
|
-
private ignoreObjectsAboveLevel: number|null = null;
|
11
|
-
private ignoringObject: boolean = false;
|
12
|
-
|
13
|
-
public constructor(private ctx: CanvasRenderingContext2D, viewport: Viewport) {
|
14
|
-
super(viewport);
|
15
|
-
}
|
16
|
-
|
17
|
-
public displaySize(): Vec2 {
|
18
|
-
return Vec2.of(
|
19
|
-
this.ctx.canvas.clientWidth,
|
20
|
-
this.ctx.canvas.clientHeight
|
21
|
-
);
|
22
|
-
}
|
23
|
-
|
24
|
-
public clear() {
|
25
|
-
this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
|
26
|
-
}
|
27
|
-
|
28
|
-
protected beginPath(startPoint: Point2) {
|
29
|
-
startPoint = this.viewport.canvasToScreen(startPoint);
|
30
|
-
|
31
|
-
this.ctx.beginPath();
|
32
|
-
this.ctx.moveTo(startPoint.x, startPoint.y);
|
33
|
-
}
|
34
|
-
|
35
|
-
protected endPath(style: RenderingStyle) {
|
36
|
-
this.ctx.fillStyle = style.fill.toHexString();
|
37
|
-
this.ctx.fill();
|
38
|
-
|
39
|
-
if (style.stroke) {
|
40
|
-
this.ctx.strokeStyle = style.stroke.color.toHexString();
|
41
|
-
this.ctx.lineWidth = this.viewport.getScaleFactor() * style.stroke.width;
|
42
|
-
this.ctx.stroke();
|
43
|
-
}
|
44
|
-
|
45
|
-
this.ctx.closePath();
|
46
|
-
}
|
47
|
-
|
48
|
-
protected lineTo(point: Point2) {
|
49
|
-
point = this.viewport.canvasToScreen(point);
|
50
|
-
this.ctx.lineTo(point.x, point.y);
|
51
|
-
}
|
52
|
-
|
53
|
-
protected moveTo(point: Point2) {
|
54
|
-
point = this.viewport.canvasToScreen(point);
|
55
|
-
this.ctx.moveTo(point.x, point.y);
|
56
|
-
}
|
57
|
-
|
58
|
-
protected traceCubicBezierCurve(p1: Point2, p2: Point2, p3: Point2) {
|
59
|
-
p1 = this.viewport.canvasToScreen(p1);
|
60
|
-
p2 = this.viewport.canvasToScreen(p2);
|
61
|
-
p3 = this.viewport.canvasToScreen(p3);
|
62
|
-
|
63
|
-
// Approximate the curve if small enough.
|
64
|
-
const delta1 = p2.minus(p1);
|
65
|
-
const delta2 = p3.minus(p2);
|
66
|
-
if (delta1.magnitudeSquared() < minSquareCurveApproxDist
|
67
|
-
&& delta2.magnitudeSquared() < minSquareCurveApproxDist) {
|
68
|
-
this.ctx.lineTo(p3.x, p3.y);
|
69
|
-
} else {
|
70
|
-
this.ctx.bezierCurveTo(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
|
71
|
-
}
|
72
|
-
}
|
73
|
-
|
74
|
-
protected traceQuadraticBezierCurve(controlPoint: Vec3, endPoint: Vec3) {
|
75
|
-
controlPoint = this.viewport.canvasToScreen(controlPoint);
|
76
|
-
endPoint = this.viewport.canvasToScreen(endPoint);
|
77
|
-
|
78
|
-
// Approximate the curve with a line if small enough
|
79
|
-
const delta = controlPoint.minus(endPoint);
|
80
|
-
if (delta.magnitudeSquared() < minSquareCurveApproxDist) {
|
81
|
-
this.ctx.lineTo(endPoint.x, endPoint.y);
|
82
|
-
} else {
|
83
|
-
this.ctx.quadraticCurveTo(
|
84
|
-
controlPoint.x, controlPoint.y, endPoint.x, endPoint.y
|
85
|
-
);
|
86
|
-
}
|
87
|
-
}
|
88
|
-
|
89
|
-
public drawPath(path: RenderablePathSpec) {
|
90
|
-
if (this.ignoringObject) {
|
91
|
-
return;
|
92
|
-
}
|
93
|
-
|
94
|
-
super.drawPath(path);
|
95
|
-
}
|
96
|
-
|
97
|
-
public startObject(boundingBox: Rect2) {
|
98
|
-
// Should we ignore all objects within this object's bbox?
|
99
|
-
const diagonal = this.viewport.canvasToScreenTransform.transformVec3(boundingBox.size);
|
100
|
-
const minRenderSize = 4;
|
101
|
-
if (Math.abs(diagonal.x) < minRenderSize && Math.abs(diagonal.y) < minRenderSize) {
|
102
|
-
this.ignoreObjectsAboveLevel = this.getNestingLevel();
|
103
|
-
this.ignoringObject = true;
|
104
|
-
}
|
105
|
-
|
106
|
-
super.startObject(boundingBox);
|
107
|
-
}
|
108
|
-
public endObject() {
|
109
|
-
super.endObject();
|
110
|
-
|
111
|
-
// If exiting an object with a too-small-to-draw bounding box,
|
112
|
-
if (this.ignoreObjectsAboveLevel !== null && this.getNestingLevel() <= this.ignoreObjectsAboveLevel) {
|
113
|
-
this.ignoreObjectsAboveLevel = null;
|
114
|
-
this.ignoringObject = false;
|
115
|
-
}
|
116
|
-
}
|
117
|
-
|
118
|
-
public drawPoints(...points: Point2[]) {
|
119
|
-
const pointRadius = 10;
|
120
|
-
|
121
|
-
for (let i = 0; i < points.length; i++) {
|
122
|
-
const point = this.viewport.canvasToScreen(points[i]);
|
123
|
-
|
124
|
-
this.ctx.beginPath();
|
125
|
-
this.ctx.arc(point.x, point.y, pointRadius, 0, Math.PI * 2);
|
126
|
-
this.ctx.fillStyle = Color4.ofRGBA(
|
127
|
-
0.5 + Math.sin(i) / 2,
|
128
|
-
1.0,
|
129
|
-
0.5 + Math.cos(i * 0.2) / 4, 0.5
|
130
|
-
).toHexString();
|
131
|
-
this.ctx.fill();
|
132
|
-
this.ctx.stroke();
|
133
|
-
this.ctx.closePath();
|
134
|
-
|
135
|
-
this.ctx.textAlign = 'center';
|
136
|
-
this.ctx.textBaseline = 'middle';
|
137
|
-
this.ctx.fillStyle = 'black';
|
138
|
-
this.ctx.fillText(`${i}`, point.x, point.y, pointRadius * 2);
|
139
|
-
}
|
140
|
-
}
|
141
|
-
}
|