js-draw 1.0.1 → 1.1.0
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/LICENSE +21 -0
- package/dist/Editor.css +1 -0
- package/dist/bundle.js +1 -1
- package/dist/bundledStyles.js +1 -1
- package/dist/cjs/toolbar/AbstractToolbar.d.ts +9 -13
- package/dist/cjs/toolbar/AbstractToolbar.js +14 -19
- package/dist/cjs/toolbar/widgets/SaveActionWidget.d.ts +10 -0
- package/dist/cjs/toolbar/widgets/SaveActionWidget.js +26 -0
- package/dist/cjs/toolbar/widgets/keybindings.d.ts +1 -0
- package/dist/cjs/toolbar/widgets/keybindings.js +4 -1
- package/dist/cjs/version.js +1 -1
- package/dist/mjs/toolbar/AbstractToolbar.d.ts +9 -13
- package/dist/mjs/toolbar/AbstractToolbar.mjs +14 -19
- package/dist/mjs/toolbar/widgets/SaveActionWidget.d.ts +10 -0
- package/dist/mjs/toolbar/widgets/SaveActionWidget.mjs +21 -0
- package/dist/mjs/toolbar/widgets/keybindings.d.ts +1 -0
- package/dist/mjs/toolbar/widgets/keybindings.mjs +3 -0
- package/dist/mjs/version.mjs +1 -1
- package/docs/img/readme-images/js-draw.jpg +0 -0
- package/docs/img/readme-images/unsupported-elements--in-editor.png +0 -0
- package/package.json +5 -4
- package/src/toolbar/EdgeToolbar.scss +1 -0
- package/dist-test/test_imports/package-lock.json +0 -13
- package/dist-test/test_imports/package.json +0 -12
- package/dist-test/test_imports/test-imports.js +0 -11
- package/dist-test/test_imports/test-require.cjs +0 -14
- package/src/Editor.loadFrom.test.ts +0 -24
- package/src/Editor.test.ts +0 -107
- package/src/Editor.toSVG.test.ts +0 -294
- package/src/Editor.ts +0 -1443
- package/src/EditorImage.test.ts +0 -117
- package/src/EditorImage.ts +0 -609
- package/src/EventDispatcher.test.ts +0 -123
- package/src/EventDispatcher.ts +0 -72
- package/src/Pointer.ts +0 -183
- package/src/SVGLoader.test.ts +0 -114
- package/src/SVGLoader.ts +0 -672
- package/src/UndoRedoHistory.test.ts +0 -34
- package/src/UndoRedoHistory.ts +0 -102
- package/src/Viewport.ts +0 -322
- package/src/bundle/bundled.ts +0 -7
- package/src/commands/Command.ts +0 -45
- package/src/commands/Duplicate.ts +0 -75
- package/src/commands/Erase.ts +0 -95
- package/src/commands/SerializableCommand.ts +0 -49
- package/src/commands/UnresolvedCommand.ts +0 -37
- package/src/commands/invertCommand.ts +0 -58
- package/src/commands/lib.ts +0 -16
- package/src/commands/localization.ts +0 -47
- package/src/commands/uniteCommands.test.ts +0 -23
- package/src/commands/uniteCommands.ts +0 -140
- package/src/components/AbstractComponent.transformBy.test.ts +0 -23
- package/src/components/AbstractComponent.ts +0 -383
- package/src/components/BackgroundComponent.test.ts +0 -44
- package/src/components/BackgroundComponent.ts +0 -348
- package/src/components/ImageComponent.ts +0 -176
- package/src/components/RestylableComponent.ts +0 -161
- package/src/components/SVGGlobalAttributesObject.ts +0 -79
- package/src/components/Stroke.test.ts +0 -137
- package/src/components/Stroke.ts +0 -294
- package/src/components/TextComponent.test.ts +0 -202
- package/src/components/TextComponent.ts +0 -429
- package/src/components/UnknownSVGObject.test.ts +0 -10
- package/src/components/UnknownSVGObject.ts +0 -60
- package/src/components/builders/ArrowBuilder.ts +0 -106
- package/src/components/builders/CircleBuilder.ts +0 -100
- package/src/components/builders/FreehandLineBuilder.test.ts +0 -24
- package/src/components/builders/FreehandLineBuilder.ts +0 -210
- package/src/components/builders/LineBuilder.ts +0 -77
- package/src/components/builders/PressureSensitiveFreehandLineBuilder.ts +0 -453
- package/src/components/builders/RectangleBuilder.ts +0 -73
- package/src/components/builders/types.ts +0 -15
- package/src/components/lib.ts +0 -31
- package/src/components/localization.ts +0 -24
- package/src/components/util/StrokeSmoother.ts +0 -302
- package/src/components/util/describeComponentList.ts +0 -18
- package/src/dialogs/makeAboutDialog.ts +0 -82
- package/src/inputEvents.ts +0 -143
- package/src/lib.ts +0 -91
- package/src/localization.ts +0 -34
- package/src/localizations/de.ts +0 -146
- package/src/localizations/en.ts +0 -8
- package/src/localizations/es.ts +0 -74
- package/src/localizations/getLocalizationTable.test.ts +0 -27
- package/src/localizations/getLocalizationTable.ts +0 -74
- package/src/rendering/Display.ts +0 -247
- package/src/rendering/RenderablePathSpec.ts +0 -88
- package/src/rendering/RenderingStyle.test.ts +0 -68
- package/src/rendering/RenderingStyle.ts +0 -55
- package/src/rendering/TextRenderingStyle.ts +0 -55
- package/src/rendering/caching/CacheRecord.test.ts +0 -48
- package/src/rendering/caching/CacheRecord.ts +0 -76
- package/src/rendering/caching/CacheRecordManager.ts +0 -71
- package/src/rendering/caching/RenderingCache.test.ts +0 -43
- package/src/rendering/caching/RenderingCache.ts +0 -66
- package/src/rendering/caching/RenderingCacheNode.ts +0 -404
- package/src/rendering/caching/testUtils.ts +0 -35
- package/src/rendering/caching/types.ts +0 -34
- package/src/rendering/lib.ts +0 -8
- package/src/rendering/localization.ts +0 -20
- package/src/rendering/renderers/AbstractRenderer.ts +0 -232
- package/src/rendering/renderers/CanvasRenderer.ts +0 -312
- package/src/rendering/renderers/DummyRenderer.test.ts +0 -41
- package/src/rendering/renderers/DummyRenderer.ts +0 -142
- package/src/rendering/renderers/SVGRenderer.ts +0 -434
- package/src/rendering/renderers/TextOnlyRenderer.test.ts +0 -34
- package/src/rendering/renderers/TextOnlyRenderer.ts +0 -68
- package/src/shortcuts/KeyBinding.test.ts +0 -61
- package/src/shortcuts/KeyBinding.ts +0 -257
- package/src/shortcuts/KeyboardShortcutManager.test.ts +0 -95
- package/src/shortcuts/KeyboardShortcutManager.ts +0 -163
- package/src/shortcuts/lib.ts +0 -3
- package/src/testing/createEditor.ts +0 -11
- package/src/testing/getUniquePointerId.ts +0 -18
- package/src/testing/lib.ts +0 -3
- package/src/testing/sendPenEvent.ts +0 -36
- package/src/testing/sendTouchEvent.ts +0 -71
- package/src/toolbar/AbstractToolbar.ts +0 -542
- package/src/toolbar/DropdownToolbar.ts +0 -220
- package/src/toolbar/EdgeToolbar.test.ts +0 -54
- package/src/toolbar/EdgeToolbar.ts +0 -543
- package/src/toolbar/IconProvider.ts +0 -861
- package/src/toolbar/constants.ts +0 -1
- package/src/toolbar/lib.ts +0 -6
- package/src/toolbar/localization.ts +0 -136
- package/src/toolbar/types.ts +0 -13
- package/src/toolbar/widgets/ActionButtonWidget.ts +0 -39
- package/src/toolbar/widgets/BaseToolWidget.ts +0 -81
- package/src/toolbar/widgets/BaseWidget.ts +0 -495
- package/src/toolbar/widgets/DocumentPropertiesWidget.ts +0 -250
- package/src/toolbar/widgets/EraserToolWidget.ts +0 -84
- package/src/toolbar/widgets/HandToolWidget.ts +0 -239
- package/src/toolbar/widgets/InsertImageWidget.ts +0 -248
- package/src/toolbar/widgets/OverflowWidget.ts +0 -92
- package/src/toolbar/widgets/PenToolWidget.ts +0 -369
- package/src/toolbar/widgets/SelectionToolWidget.ts +0 -195
- package/src/toolbar/widgets/TextToolWidget.ts +0 -149
- package/src/toolbar/widgets/components/makeColorInput.ts +0 -184
- package/src/toolbar/widgets/components/makeFileInput.ts +0 -128
- package/src/toolbar/widgets/components/makeGridSelector.ts +0 -179
- package/src/toolbar/widgets/components/makeSeparator.ts +0 -17
- package/src/toolbar/widgets/components/makeThicknessSlider.ts +0 -62
- package/src/toolbar/widgets/keybindings.ts +0 -19
- package/src/toolbar/widgets/layout/DropdownLayoutManager.ts +0 -262
- package/src/toolbar/widgets/layout/EdgeToolbarLayoutManager.ts +0 -71
- package/src/toolbar/widgets/layout/types.ts +0 -74
- package/src/toolbar/widgets/lib.ts +0 -13
- package/src/tools/BaseTool.ts +0 -169
- package/src/tools/Eraser.test.ts +0 -103
- package/src/tools/Eraser.ts +0 -173
- package/src/tools/FindTool.test.ts +0 -67
- package/src/tools/FindTool.ts +0 -153
- package/src/tools/InputFilter/FunctionMapper.ts +0 -17
- package/src/tools/InputFilter/InputMapper.ts +0 -41
- package/src/tools/InputFilter/InputPipeline.test.ts +0 -41
- package/src/tools/InputFilter/InputPipeline.ts +0 -34
- package/src/tools/InputFilter/InputStabilizer.ts +0 -254
- package/src/tools/InputFilter/StrokeKeyboardControl.ts +0 -104
- package/src/tools/PanZoom.test.ts +0 -339
- package/src/tools/PanZoom.ts +0 -525
- package/src/tools/PasteHandler.ts +0 -94
- package/src/tools/Pen.test.ts +0 -260
- package/src/tools/Pen.ts +0 -284
- package/src/tools/PipetteTool.ts +0 -84
- package/src/tools/SelectionTool/SelectAllShortcutHandler.ts +0 -29
- package/src/tools/SelectionTool/Selection.ts +0 -647
- package/src/tools/SelectionTool/SelectionHandle.ts +0 -142
- package/src/tools/SelectionTool/SelectionTool.test.ts +0 -370
- package/src/tools/SelectionTool/SelectionTool.ts +0 -510
- package/src/tools/SelectionTool/TransformMode.ts +0 -112
- package/src/tools/SelectionTool/types.ts +0 -11
- package/src/tools/SoundUITool.ts +0 -221
- package/src/tools/TextTool.ts +0 -339
- package/src/tools/ToolController.ts +0 -224
- package/src/tools/ToolEnabledGroup.ts +0 -14
- package/src/tools/ToolSwitcherShortcut.ts +0 -39
- package/src/tools/ToolbarShortcutHandler.ts +0 -39
- package/src/tools/UndoRedoShortcut.test.ts +0 -62
- package/src/tools/UndoRedoShortcut.ts +0 -24
- package/src/tools/keybindings.ts +0 -85
- package/src/tools/lib.ts +0 -22
- package/src/tools/localization.ts +0 -76
- package/src/types.ts +0 -151
- package/src/util/ReactiveValue.test.ts +0 -168
- package/src/util/ReactiveValue.ts +0 -241
- package/src/util/assertions.ts +0 -55
- package/src/util/fileToBase64.ts +0 -18
- package/src/util/guessKeyCodeFromKey.ts +0 -36
- package/src/util/listPrefixMatch.ts +0 -19
- package/src/util/stopPropagationOfScrollingWheelEvents.ts +0 -20
- package/src/util/untilNextAnimationFrame.ts +0 -9
- package/src/util/waitForAll.ts +0 -18
- package/src/util/waitForTimeout.ts +0 -9
- package/src/version.test.ts +0 -12
- package/src/version.ts +0 -3
- package/tools/allLocales.js +0 -4
- package/tools/copyREADME.ts +0 -62
@@ -1,232 +0,0 @@
|
|
1
|
-
import { Color4, Mat33, Point2, Vec2, Rect2, Path, PathCommandType } from '@js-draw/math';
|
2
|
-
import { LoadSaveDataTable } from '../../components/AbstractComponent';
|
3
|
-
import Viewport from '../../Viewport';
|
4
|
-
import RenderingStyle, { stylesEqual } from '../RenderingStyle';
|
5
|
-
import TextRenderingStyle from '../TextRenderingStyle';
|
6
|
-
import RenderablePathSpec, { pathToRenderable } from '../RenderablePathSpec';
|
7
|
-
|
8
|
-
export interface RenderableImage {
|
9
|
-
transform: Mat33;
|
10
|
-
|
11
|
-
// An Image or HTMLCanvasElement. If an Image, it must be loaded from the same origin as this
|
12
|
-
// (and should have `src=this.base64Url`).
|
13
|
-
image: HTMLImageElement|HTMLCanvasElement;
|
14
|
-
|
15
|
-
// All images that can be drawn **must** have a base64 URL in the form
|
16
|
-
// data:image/[format];base64,[data here]
|
17
|
-
// If `image` is an Image, this should be equivalent to `image.src`.
|
18
|
-
base64Url: string;
|
19
|
-
|
20
|
-
label?: string;
|
21
|
-
}
|
22
|
-
|
23
|
-
/**
|
24
|
-
* Abstract base class for renderers.
|
25
|
-
*
|
26
|
-
* @see {@link EditorImage.render}
|
27
|
-
*/
|
28
|
-
export default abstract class AbstractRenderer {
|
29
|
-
// If null, this' transformation is linked to the Viewport
|
30
|
-
private selfTransform: Mat33|null = null;
|
31
|
-
private transformStack: Array<Mat33|null> = [];
|
32
|
-
|
33
|
-
protected constructor(private viewport: Viewport) { }
|
34
|
-
|
35
|
-
// this.canvasToScreen, etc. should be used instead of the corresponding
|
36
|
-
// methods on Viewport.
|
37
|
-
protected getViewport(): Viewport { return this.viewport; }
|
38
|
-
|
39
|
-
// Returns the size of the rendered region of this on
|
40
|
-
// the display (in pixels).
|
41
|
-
public abstract displaySize(): Vec2;
|
42
|
-
|
43
|
-
public abstract clear(): void;
|
44
|
-
protected abstract beginPath(startPoint: Point2): void;
|
45
|
-
protected abstract endPath(style: RenderingStyle): void;
|
46
|
-
protected abstract lineTo(point: Point2): void;
|
47
|
-
protected abstract moveTo(point: Point2): void;
|
48
|
-
protected abstract traceCubicBezierCurve(
|
49
|
-
p1: Point2, p2: Point2, p3: Point2,
|
50
|
-
): void;
|
51
|
-
protected abstract traceQuadraticBezierCurve(
|
52
|
-
controlPoint: Point2, endPoint: Point2,
|
53
|
-
): void;
|
54
|
-
public abstract drawText(text: string, transform: Mat33, style: TextRenderingStyle): void;
|
55
|
-
public abstract drawImage(image: RenderableImage): void;
|
56
|
-
|
57
|
-
// Returns true iff the given rectangle is so small, rendering anything within
|
58
|
-
// it has no effect on the image.
|
59
|
-
public abstract isTooSmallToRender(rect: Rect2): boolean;
|
60
|
-
|
61
|
-
public setDraftMode(_draftMode: boolean) { }
|
62
|
-
|
63
|
-
protected objectLevel: number = 0;
|
64
|
-
private currentPaths: RenderablePathSpec[]|null = null;
|
65
|
-
private flushPath() {
|
66
|
-
if (!this.currentPaths) {
|
67
|
-
return;
|
68
|
-
}
|
69
|
-
|
70
|
-
let lastStyle: RenderingStyle|null = null;
|
71
|
-
for (const path of this.currentPaths) {
|
72
|
-
const { startPoint, commands, style } = path;
|
73
|
-
|
74
|
-
if (!lastStyle || !stylesEqual(lastStyle, style)) {
|
75
|
-
if (lastStyle) {
|
76
|
-
this.endPath(lastStyle);
|
77
|
-
}
|
78
|
-
|
79
|
-
this.beginPath(startPoint);
|
80
|
-
lastStyle = style;
|
81
|
-
} else {
|
82
|
-
this.moveTo(startPoint);
|
83
|
-
}
|
84
|
-
|
85
|
-
for (const command of commands) {
|
86
|
-
if (command.kind === PathCommandType.LineTo) {
|
87
|
-
this.lineTo(command.point);
|
88
|
-
} else if (command.kind === PathCommandType.MoveTo) {
|
89
|
-
this.moveTo(command.point);
|
90
|
-
} else if (command.kind === PathCommandType.CubicBezierTo) {
|
91
|
-
this.traceCubicBezierCurve(
|
92
|
-
command.controlPoint1, command.controlPoint2, command.endPoint
|
93
|
-
);
|
94
|
-
} else if (command.kind === PathCommandType.QuadraticBezierTo) {
|
95
|
-
this.traceQuadraticBezierCurve(
|
96
|
-
command.controlPoint, command.endPoint
|
97
|
-
);
|
98
|
-
}
|
99
|
-
}
|
100
|
-
}
|
101
|
-
|
102
|
-
if (lastStyle) {
|
103
|
-
this.endPath(lastStyle);
|
104
|
-
}
|
105
|
-
|
106
|
-
this.currentPaths = [];
|
107
|
-
}
|
108
|
-
|
109
|
-
/**
|
110
|
-
* Draws a styled path. If within an object started by {@link startObject},
|
111
|
-
* the resultant path may not be visible until {@link endObject} is called.
|
112
|
-
*/
|
113
|
-
public drawPath(path: RenderablePathSpec) {
|
114
|
-
// If we're being called outside of an object,
|
115
|
-
// we can't delay rendering
|
116
|
-
if (this.objectLevel === 0) {
|
117
|
-
this.currentPaths = [path];
|
118
|
-
this.flushPath();
|
119
|
-
this.currentPaths = null;
|
120
|
-
} else {
|
121
|
-
// Otherwise, don't render paths all at once. This prevents faint lines between
|
122
|
-
// segments of the same stroke from being visible.
|
123
|
-
this.currentPaths!.push(path);
|
124
|
-
}
|
125
|
-
}
|
126
|
-
|
127
|
-
// Strokes a rectangle. Boundary lines have width [lineWidth] and are filled with [lineFill].
|
128
|
-
// This is equivalent to `drawPath(Path.fromRect(...).toRenderable(...))`.
|
129
|
-
public drawRect(rect: Rect2, lineWidth: number, lineFill: RenderingStyle) {
|
130
|
-
const path = Path.fromRect(rect, lineWidth);
|
131
|
-
this.drawPath(pathToRenderable(path, lineFill));
|
132
|
-
}
|
133
|
-
|
134
|
-
/** Draws a filled rectangle. */
|
135
|
-
public fillRect(rect: Rect2, fill: Color4) {
|
136
|
-
const path = Path.fromRect(rect);
|
137
|
-
this.drawPath(pathToRenderable(path, { fill }));
|
138
|
-
}
|
139
|
-
|
140
|
-
/**
|
141
|
-
* This should be called whenever a new object is being drawn.
|
142
|
-
*
|
143
|
-
* @param _boundingBox The bounding box of the object to be drawn.
|
144
|
-
* @param _clip Whether content outside `_boundingBox` should be drawn. Renderers
|
145
|
-
* that override this method are not required to support `_clip`.
|
146
|
-
*/
|
147
|
-
public startObject(_boundingBox: Rect2, _clip?: boolean) {
|
148
|
-
if (this.objectLevel > 0) {
|
149
|
-
this.flushPath();
|
150
|
-
}
|
151
|
-
|
152
|
-
this.currentPaths = [];
|
153
|
-
this.objectLevel ++;
|
154
|
-
}
|
155
|
-
|
156
|
-
/**
|
157
|
-
* Notes the end of an object.
|
158
|
-
* @param _loaderData - a map from strings to JSON-ifyable objects
|
159
|
-
* and contains properties attached to the object by whatever loader loaded the image. This
|
160
|
-
* is used to preserve attributes not supported by js-draw when loading/saving an image.
|
161
|
-
* Renderers may ignore this.
|
162
|
-
*
|
163
|
-
* @param _objectTags - a list of labels (e.g. `className`s) to be attached to the object.
|
164
|
-
* Renderers may ignore this.
|
165
|
-
*/
|
166
|
-
public endObject(_loaderData?: LoadSaveDataTable, _objectTags?: string[]) {
|
167
|
-
// Render the paths all at once
|
168
|
-
this.flushPath();
|
169
|
-
this.currentPaths = null;
|
170
|
-
this.objectLevel --;
|
171
|
-
|
172
|
-
if (this.objectLevel < 0) {
|
173
|
-
throw new Error(
|
174
|
-
'More objects have ended than have been started (negative object nesting level)!'
|
175
|
-
);
|
176
|
-
}
|
177
|
-
}
|
178
|
-
|
179
|
-
protected getNestingLevel() {
|
180
|
-
return this.objectLevel;
|
181
|
-
}
|
182
|
-
|
183
|
-
// Draw a representation of [points]. Intended for debugging.
|
184
|
-
public abstract drawPoints(...points: Point2[]): void;
|
185
|
-
|
186
|
-
|
187
|
-
// Returns true iff other can be rendered onto this without data loss.
|
188
|
-
public canRenderFromWithoutDataLoss(_other: AbstractRenderer): boolean {
|
189
|
-
return false;
|
190
|
-
}
|
191
|
-
|
192
|
-
// MUST throw if other and this are not of the same base class.
|
193
|
-
public renderFromOtherOfSameType(_renderTo: Mat33, other: AbstractRenderer) {
|
194
|
-
throw new Error(`Unable to render from ${other}: Not implemented`);
|
195
|
-
}
|
196
|
-
|
197
|
-
// Set a transformation to apply to things before rendering,
|
198
|
-
// replacing the viewport's transform.
|
199
|
-
public setTransform(transform: Mat33|null) {
|
200
|
-
this.selfTransform = transform;
|
201
|
-
}
|
202
|
-
|
203
|
-
public pushTransform(transform: Mat33) {
|
204
|
-
this.transformStack.push(this.selfTransform);
|
205
|
-
this.setTransform(this.getCanvasToScreenTransform().rightMul(transform));
|
206
|
-
}
|
207
|
-
|
208
|
-
public popTransform() {
|
209
|
-
if (this.transformStack.length === 0) {
|
210
|
-
throw new Error('Unable to pop more transforms than have been pushed!');
|
211
|
-
}
|
212
|
-
|
213
|
-
this.setTransform(this.transformStack.pop() ?? null);
|
214
|
-
}
|
215
|
-
|
216
|
-
// Get the matrix that transforms a vector on the canvas to a vector on this'
|
217
|
-
// rendering target.
|
218
|
-
public getCanvasToScreenTransform(): Mat33 {
|
219
|
-
if (this.selfTransform) {
|
220
|
-
return this.selfTransform;
|
221
|
-
}
|
222
|
-
return this.viewport.canvasToScreenTransform;
|
223
|
-
}
|
224
|
-
|
225
|
-
public canvasToScreen(vec: Vec2): Vec2 {
|
226
|
-
return this.getCanvasToScreenTransform().transformVec2(vec);
|
227
|
-
}
|
228
|
-
|
229
|
-
public getSizeOfCanvasPixelOnScreen(): number {
|
230
|
-
return this.getCanvasToScreenTransform().transformVec3(Vec2.unitX).length();
|
231
|
-
}
|
232
|
-
}
|
@@ -1,312 +0,0 @@
|
|
1
|
-
import TextComponent from '../../components/TextComponent';
|
2
|
-
import { Mat33, Rect2, Point2, Vec2, Vec3, Color4 } from '@js-draw/math';
|
3
|
-
import Viewport from '../../Viewport';
|
4
|
-
import RenderingStyle from '../RenderingStyle';
|
5
|
-
import TextRenderingStyle from '../TextRenderingStyle';
|
6
|
-
import AbstractRenderer, { RenderableImage } from './AbstractRenderer';
|
7
|
-
import RenderablePathSpec, { visualEquivalent } from '../RenderablePathSpec';
|
8
|
-
|
9
|
-
/**
|
10
|
-
* Renders onto a `CanvasRenderingContext2D`.
|
11
|
-
*
|
12
|
-
* @example
|
13
|
-
* ```ts
|
14
|
-
* const editor = new Editor(document.body);
|
15
|
-
*
|
16
|
-
* const canvas = document.createElement('canvas');
|
17
|
-
* const ctx = canvas.getContext('2d');
|
18
|
-
*
|
19
|
-
* // Ensure that the canvas can fit the entire rendering
|
20
|
-
* const viewport = editor.image.getImportExportViewport();
|
21
|
-
* canvas.width = viewport.getScreenRectSize().x;
|
22
|
-
* canvas.height = viewport.getScreenRectSize().y;
|
23
|
-
*
|
24
|
-
* // Render editor.image onto the renderer
|
25
|
-
* const renderer = new CanvasRenderer(ctx, viewport);
|
26
|
-
* editor.image.render(renderer, viewport);
|
27
|
-
* ```
|
28
|
-
*/
|
29
|
-
export default class CanvasRenderer extends AbstractRenderer {
|
30
|
-
private ignoreObjectsAboveLevel: number|null = null;
|
31
|
-
private ignoringObject: boolean = false;
|
32
|
-
private currentObjectBBox: Rect2|null = null;
|
33
|
-
|
34
|
-
// Minimum square distance of a control point from the line between the end points
|
35
|
-
// for the curve not to be drawn as a line.
|
36
|
-
// For example, if [minSquareCurveApproxDist] = 25 = 5², then a control point on a quadratic
|
37
|
-
// bezier curve needs to be at least 5 units away from the line between the curve's end points
|
38
|
-
// for the curve to be drawn as a Bezier curve (and not a line).
|
39
|
-
private minSquareCurveApproxDist: number;
|
40
|
-
|
41
|
-
// Minimum size of an object (in pixels) for it to be rendered.
|
42
|
-
private minRenderSizeAnyDimen: number;
|
43
|
-
private minRenderSizeBothDimens: number;
|
44
|
-
|
45
|
-
/**
|
46
|
-
* Creates a new `CanvasRenderer` that renders to the given rendering context.
|
47
|
-
* The `viewport` is used to determine the translation/rotation/scaling of the content
|
48
|
-
* to draw.
|
49
|
-
*/
|
50
|
-
public constructor(private ctx: CanvasRenderingContext2D, viewport: Viewport) {
|
51
|
-
super(viewport);
|
52
|
-
this.setDraftMode(false);
|
53
|
-
}
|
54
|
-
|
55
|
-
private transformBy(transformBy: Mat33) {
|
56
|
-
// From MDN, transform(a,b,c,d,e,f)
|
57
|
-
// takes input such that
|
58
|
-
// ⎡ a c e ⎤
|
59
|
-
// ⎢ b d f ⎥ transforms content drawn to [ctx].
|
60
|
-
// ⎣ 0 0 1 ⎦
|
61
|
-
this.ctx.transform(
|
62
|
-
transformBy.a1, transformBy.b1, // a, b
|
63
|
-
transformBy.a2, transformBy.b2, // c, d
|
64
|
-
transformBy.a3, transformBy.b3, // e, f
|
65
|
-
);
|
66
|
-
}
|
67
|
-
|
68
|
-
public override canRenderFromWithoutDataLoss(other: AbstractRenderer) {
|
69
|
-
return other instanceof CanvasRenderer;
|
70
|
-
}
|
71
|
-
|
72
|
-
public override renderFromOtherOfSameType(transformBy: Mat33, other: AbstractRenderer): void {
|
73
|
-
if (!(other instanceof CanvasRenderer)) {
|
74
|
-
throw new Error(`${other} cannot be rendered onto ${this}`);
|
75
|
-
}
|
76
|
-
transformBy = this.getCanvasToScreenTransform().rightMul(transformBy);
|
77
|
-
this.ctx.save();
|
78
|
-
this.transformBy(transformBy);
|
79
|
-
this.ctx.drawImage(other.ctx.canvas, 0, 0);
|
80
|
-
this.ctx.restore();
|
81
|
-
}
|
82
|
-
|
83
|
-
// Set parameters for lower/higher quality rendering
|
84
|
-
public override setDraftMode(draftMode: boolean) {
|
85
|
-
if (draftMode) {
|
86
|
-
this.minSquareCurveApproxDist = 9;
|
87
|
-
this.minRenderSizeBothDimens = 2;
|
88
|
-
this.minRenderSizeAnyDimen = 0.5;
|
89
|
-
} else {
|
90
|
-
this.minSquareCurveApproxDist = 0.5;
|
91
|
-
this.minRenderSizeBothDimens = 0.2;
|
92
|
-
this.minRenderSizeAnyDimen = 1e-6;
|
93
|
-
}
|
94
|
-
}
|
95
|
-
|
96
|
-
public displaySize(): Vec2 {
|
97
|
-
return Vec2.of(
|
98
|
-
this.ctx.canvas.clientWidth,
|
99
|
-
this.ctx.canvas.clientHeight,
|
100
|
-
);
|
101
|
-
}
|
102
|
-
|
103
|
-
public clear() {
|
104
|
-
this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
|
105
|
-
}
|
106
|
-
|
107
|
-
protected beginPath(startPoint: Point2) {
|
108
|
-
startPoint = this.canvasToScreen(startPoint);
|
109
|
-
|
110
|
-
this.ctx.beginPath();
|
111
|
-
this.ctx.moveTo(startPoint.x, startPoint.y);
|
112
|
-
}
|
113
|
-
|
114
|
-
protected endPath(style: RenderingStyle) {
|
115
|
-
// Saving and restoring can be slow in some browsers
|
116
|
-
// (e.g. 0.50ms). Avoid.
|
117
|
-
//this.ctx.save();
|
118
|
-
|
119
|
-
// If not a transparent fill
|
120
|
-
if (style.fill.a > 0) {
|
121
|
-
this.ctx.fillStyle = style.fill.toHexString();
|
122
|
-
this.ctx.fill();
|
123
|
-
}
|
124
|
-
|
125
|
-
if (style.stroke) {
|
126
|
-
this.ctx.strokeStyle = style.stroke.color.toHexString();
|
127
|
-
this.ctx.lineWidth = this.getSizeOfCanvasPixelOnScreen() * style.stroke.width;
|
128
|
-
this.ctx.lineCap = 'round';
|
129
|
-
this.ctx.lineJoin = 'round';
|
130
|
-
this.ctx.stroke();
|
131
|
-
|
132
|
-
this.ctx.lineWidth = 1;
|
133
|
-
}
|
134
|
-
|
135
|
-
this.ctx.closePath();
|
136
|
-
//this.ctx.restore();
|
137
|
-
}
|
138
|
-
|
139
|
-
protected lineTo(point: Point2) {
|
140
|
-
point = this.canvasToScreen(point);
|
141
|
-
this.ctx.lineTo(point.x, point.y);
|
142
|
-
}
|
143
|
-
|
144
|
-
protected moveTo(point: Point2) {
|
145
|
-
point = this.canvasToScreen(point);
|
146
|
-
this.ctx.moveTo(point.x, point.y);
|
147
|
-
}
|
148
|
-
|
149
|
-
protected traceCubicBezierCurve(p1: Point2, p2: Point2, p3: Point2) {
|
150
|
-
p1 = this.canvasToScreen(p1);
|
151
|
-
p2 = this.canvasToScreen(p2);
|
152
|
-
p3 = this.canvasToScreen(p3);
|
153
|
-
|
154
|
-
// Approximate the curve if small enough.
|
155
|
-
const delta1 = p2.minus(p1);
|
156
|
-
const delta2 = p3.minus(p2);
|
157
|
-
if (delta1.magnitudeSquared() < this.minSquareCurveApproxDist
|
158
|
-
&& delta2.magnitudeSquared() < this.minSquareCurveApproxDist) {
|
159
|
-
this.ctx.lineTo(p3.x, p3.y);
|
160
|
-
} else {
|
161
|
-
this.ctx.bezierCurveTo(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
|
162
|
-
}
|
163
|
-
}
|
164
|
-
|
165
|
-
protected traceQuadraticBezierCurve(controlPoint: Vec3, endPoint: Vec3) {
|
166
|
-
controlPoint = this.canvasToScreen(controlPoint);
|
167
|
-
endPoint = this.canvasToScreen(endPoint);
|
168
|
-
|
169
|
-
// Approximate the curve with a line if small enough
|
170
|
-
const delta = controlPoint.minus(endPoint);
|
171
|
-
if (delta.magnitudeSquared() < this.minSquareCurveApproxDist) {
|
172
|
-
this.ctx.lineTo(endPoint.x, endPoint.y);
|
173
|
-
} else {
|
174
|
-
this.ctx.quadraticCurveTo(
|
175
|
-
controlPoint.x, controlPoint.y, endPoint.x, endPoint.y
|
176
|
-
);
|
177
|
-
}
|
178
|
-
}
|
179
|
-
|
180
|
-
public override drawPath(path: RenderablePathSpec) {
|
181
|
-
if (this.ignoringObject) {
|
182
|
-
return;
|
183
|
-
}
|
184
|
-
|
185
|
-
// If part of a huge object, it might be worth trimming the path
|
186
|
-
if (this.currentObjectBBox?.containsRect(this.getViewport().visibleRect)) {
|
187
|
-
// Try to trim/remove parts of the path outside of the bounding box.
|
188
|
-
path = visualEquivalent(
|
189
|
-
path,
|
190
|
-
this.getViewport().visibleRect
|
191
|
-
);
|
192
|
-
}
|
193
|
-
|
194
|
-
super.drawPath(path);
|
195
|
-
}
|
196
|
-
|
197
|
-
public drawText(text: string, transform: Mat33, style: TextRenderingStyle): void {
|
198
|
-
this.ctx.save();
|
199
|
-
transform = this.getCanvasToScreenTransform().rightMul(transform);
|
200
|
-
this.transformBy(transform);
|
201
|
-
TextComponent.applyTextStyles(this.ctx, style);
|
202
|
-
|
203
|
-
if (style.renderingStyle.fill.a !== 0) {
|
204
|
-
this.ctx.fillStyle = style.renderingStyle.fill.toHexString();
|
205
|
-
this.ctx.fillText(text, 0, 0);
|
206
|
-
}
|
207
|
-
if (style.renderingStyle.stroke) {
|
208
|
-
this.ctx.strokeStyle = style.renderingStyle.stroke.color.toHexString();
|
209
|
-
this.ctx.lineWidth = style.renderingStyle.stroke.width;
|
210
|
-
this.ctx.strokeText(text, 0, 0);
|
211
|
-
}
|
212
|
-
|
213
|
-
this.ctx.restore();
|
214
|
-
}
|
215
|
-
|
216
|
-
public drawImage(image: RenderableImage) {
|
217
|
-
this.ctx.save();
|
218
|
-
const transform = this.getCanvasToScreenTransform().rightMul(image.transform);
|
219
|
-
this.transformBy(transform);
|
220
|
-
|
221
|
-
this.ctx.drawImage(image.image, 0, 0);
|
222
|
-
this.ctx.restore();
|
223
|
-
}
|
224
|
-
|
225
|
-
private clipLevels: number[] = [];
|
226
|
-
public override startObject(boundingBox: Rect2, clip?: boolean) {
|
227
|
-
if (this.isTooSmallToRender(boundingBox)) {
|
228
|
-
this.ignoreObjectsAboveLevel = this.getNestingLevel();
|
229
|
-
this.ignoringObject = true;
|
230
|
-
}
|
231
|
-
|
232
|
-
super.startObject(boundingBox);
|
233
|
-
this.currentObjectBBox = boundingBox;
|
234
|
-
|
235
|
-
if (!this.ignoringObject && clip) {
|
236
|
-
// Don't clip if it would only remove content already trimmed by
|
237
|
-
// the edge of the screen.
|
238
|
-
const clippedIsOutsideScreen = boundingBox.containsRect(this.getViewport().visibleRect);
|
239
|
-
|
240
|
-
if (!clippedIsOutsideScreen) {
|
241
|
-
this.clipLevels.push(this.objectLevel);
|
242
|
-
this.ctx.save();
|
243
|
-
this.ctx.beginPath();
|
244
|
-
for (const corner of boundingBox.corners) {
|
245
|
-
const screenCorner = this.canvasToScreen(corner);
|
246
|
-
this.ctx.lineTo(screenCorner.x, screenCorner.y);
|
247
|
-
}
|
248
|
-
this.ctx.clip();
|
249
|
-
}
|
250
|
-
}
|
251
|
-
}
|
252
|
-
|
253
|
-
public override endObject() {
|
254
|
-
// Cache this.objectLevel — it may be decremented by super.endObject.
|
255
|
-
const objectLevel = this.objectLevel;
|
256
|
-
|
257
|
-
this.currentObjectBBox = null;
|
258
|
-
super.endObject();
|
259
|
-
|
260
|
-
if (!this.ignoringObject && this.clipLevels.length > 0) {
|
261
|
-
if (this.clipLevels[this.clipLevels.length - 1] === objectLevel) {
|
262
|
-
this.ctx.restore();
|
263
|
-
this.clipLevels.pop();
|
264
|
-
}
|
265
|
-
}
|
266
|
-
|
267
|
-
// If exiting an object with a too-small-to-draw bounding box,
|
268
|
-
if (this.ignoreObjectsAboveLevel !== null && this.getNestingLevel() <= this.ignoreObjectsAboveLevel) {
|
269
|
-
this.ignoreObjectsAboveLevel = null;
|
270
|
-
this.ignoringObject = false;
|
271
|
-
}
|
272
|
-
}
|
273
|
-
|
274
|
-
// @internal
|
275
|
-
public drawPoints(...points: Point2[]) {
|
276
|
-
const pointRadius = 10;
|
277
|
-
|
278
|
-
for (let i = 0; i < points.length; i++) {
|
279
|
-
const point = this.canvasToScreen(points[i]);
|
280
|
-
|
281
|
-
this.ctx.beginPath();
|
282
|
-
this.ctx.arc(point.x, point.y, pointRadius, 0, Math.PI * 2);
|
283
|
-
this.ctx.fillStyle = Color4.ofRGBA(
|
284
|
-
0.5 + Math.sin(i) / 2,
|
285
|
-
1.0,
|
286
|
-
0.5 + Math.cos(i * 0.2) / 4, 0.5
|
287
|
-
).toHexString();
|
288
|
-
this.ctx.lineWidth = 2;
|
289
|
-
this.ctx.fill();
|
290
|
-
this.ctx.stroke();
|
291
|
-
this.ctx.closePath();
|
292
|
-
|
293
|
-
this.ctx.textAlign = 'center';
|
294
|
-
this.ctx.textBaseline = 'middle';
|
295
|
-
this.ctx.fillStyle = 'black';
|
296
|
-
this.ctx.fillText(`${i}`, point.x, point.y, pointRadius * 2);
|
297
|
-
}
|
298
|
-
}
|
299
|
-
|
300
|
-
// @internal
|
301
|
-
public isTooSmallToRender(rect: Rect2): boolean {
|
302
|
-
// Should we ignore all objects within this object's bbox?
|
303
|
-
const diagonal = this.getCanvasToScreenTransform().transformVec3(rect.size);
|
304
|
-
|
305
|
-
const bothDimenMinSize = this.minRenderSizeBothDimens;
|
306
|
-
const bothTooSmall = Math.abs(diagonal.x) < bothDimenMinSize && Math.abs(diagonal.y) < bothDimenMinSize;
|
307
|
-
const anyDimenMinSize = this.minRenderSizeAnyDimen;
|
308
|
-
const anyTooSmall = Math.abs(diagonal.x) < anyDimenMinSize || Math.abs(diagonal.y) < anyDimenMinSize;
|
309
|
-
|
310
|
-
return bothTooSmall || anyTooSmall;
|
311
|
-
}
|
312
|
-
}
|
@@ -1,41 +0,0 @@
|
|
1
|
-
|
2
|
-
import { Mat33, Vec2 } from '@js-draw/math';
|
3
|
-
import Viewport from '../../Viewport';
|
4
|
-
import DummyRenderer from './DummyRenderer';
|
5
|
-
|
6
|
-
const makeRenderer = (): [DummyRenderer, Viewport] => {
|
7
|
-
const viewport = new Viewport(() => {});
|
8
|
-
return [ new DummyRenderer(viewport), viewport ];
|
9
|
-
};
|
10
|
-
|
11
|
-
describe('DummyRenderer', () => {
|
12
|
-
it('should correctly calculate the size of a pixel on the screen', () => {
|
13
|
-
const [ renderer, viewport ] = makeRenderer();
|
14
|
-
viewport.updateScreenSize(Vec2.of(100, 100));
|
15
|
-
viewport.resetTransform(Mat33.identity);
|
16
|
-
|
17
|
-
expect(1/viewport.getScaleFactor()).toBe(1);
|
18
|
-
expect(renderer.getSizeOfCanvasPixelOnScreen()).toBe(1);
|
19
|
-
|
20
|
-
// Updating the translation matrix shouldn't affect the size of a pixel on the
|
21
|
-
// screen.
|
22
|
-
renderer.setTransform(Mat33.translation(Vec2.of(-1, -2)));
|
23
|
-
expect(renderer.getSizeOfCanvasPixelOnScreen()).toBe(1);
|
24
|
-
viewport.resetTransform(Mat33.translation(Vec2.of(3, 4)));
|
25
|
-
expect(renderer.getSizeOfCanvasPixelOnScreen()).toBe(1);
|
26
|
-
|
27
|
-
// Scale objects by a factor of 2 when drawing
|
28
|
-
renderer.setTransform(Mat33.scaling2D(2));
|
29
|
-
expect(renderer.getSizeOfCanvasPixelOnScreen()).toBe(2);
|
30
|
-
viewport.resetTransform(Mat33.scaling2D(0.5));
|
31
|
-
|
32
|
-
// When a renderer transform is set, **only** the renderer transform should be used.
|
33
|
-
expect(renderer.getSizeOfCanvasPixelOnScreen()).toBe(2);
|
34
|
-
renderer.setTransform(null);
|
35
|
-
expect(renderer.getSizeOfCanvasPixelOnScreen()).toBe(0.5);
|
36
|
-
|
37
|
-
// Rotating should not affect the size of a pixel
|
38
|
-
renderer.setTransform(Mat33.zRotation(Math.PI / 4).rightMul(Mat33.scaling2D(4)));
|
39
|
-
expect(renderer.getSizeOfCanvasPixelOnScreen()).toBe(4);
|
40
|
-
});
|
41
|
-
});
|