js-draw 0.0.1 → 0.0.4
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 +10 -0
- package/README.md +5 -3
- package/dist/src/Editor.d.ts +1 -1
- package/dist/src/Editor.js +7 -2
- package/dist/src/EditorImage.d.ts +2 -0
- package/dist/src/EditorImage.js +29 -5
- package/dist/src/Pointer.js +1 -1
- package/dist/src/Viewport.d.ts +1 -1
- package/dist/src/components/AbstractComponent.d.ts +1 -1
- package/dist/src/components/Stroke.d.ts +1 -1
- package/dist/src/components/Stroke.js +1 -1
- package/dist/src/components/UnknownSVGObject.d.ts +1 -1
- package/dist/src/components/builders/ArrowBuilder.d.ts +17 -0
- package/dist/src/components/builders/ArrowBuilder.js +83 -0
- package/dist/src/{StrokeBuilder.d.ts → components/builders/FreehandLineBuilder.d.ts} +9 -13
- package/dist/src/{StrokeBuilder.js → components/builders/FreehandLineBuilder.js} +27 -9
- package/dist/src/components/builders/LineBuilder.d.ts +16 -0
- package/dist/src/components/builders/LineBuilder.js +57 -0
- package/dist/src/components/builders/RectangleBuilder.d.ts +18 -0
- package/dist/src/components/builders/RectangleBuilder.js +41 -0
- package/dist/src/components/builders/types.d.ts +12 -0
- package/dist/{scripts/bundle.d.ts → src/components/builders/types.js} +0 -0
- package/dist/src/geometry/Path.d.ts +1 -0
- package/dist/src/geometry/Path.js +32 -0
- package/dist/src/geometry/Vec3.d.ts +2 -0
- package/dist/src/geometry/Vec3.js +13 -0
- package/dist/src/rendering/AbstractRenderer.js +3 -25
- package/dist/src/toolbar/HTMLToolbar.d.ts +4 -1
- package/dist/src/toolbar/HTMLToolbar.js +143 -16
- package/dist/src/toolbar/types.d.ts +6 -0
- package/dist/src/tools/Pen.d.ts +13 -3
- package/dist/src/tools/Pen.js +37 -28
- package/dist/src/tools/ToolController.js +3 -3
- package/dist/src/types.d.ts +14 -2
- package/dist/src/types.js +1 -0
- package/package.json +8 -4
- package/src/Editor.css +11 -0
- package/src/Editor.ts +9 -2
- package/src/EditorImage.ts +31 -3
- package/src/Pointer.ts +1 -1
- package/src/Viewport.ts +1 -1
- package/src/components/AbstractComponent.ts +1 -1
- package/src/components/Stroke.ts +2 -2
- package/src/components/UnknownSVGObject.ts +1 -1
- package/src/components/builders/ArrowBuilder.ts +104 -0
- package/src/{StrokeBuilder.ts → components/builders/FreehandLineBuilder.ts} +36 -18
- package/src/components/builders/LineBuilder.ts +75 -0
- package/src/components/builders/RectangleBuilder.ts +59 -0
- package/src/components/builders/types.ts +15 -0
- package/src/geometry/Path.ts +43 -0
- package/src/geometry/Vec2.test.ts +1 -0
- package/src/geometry/Vec3.test.ts +14 -0
- package/src/geometry/Vec3.ts +16 -0
- package/src/rendering/AbstractRenderer.ts +3 -32
- package/src/{editorStyles.js → styles.js} +0 -0
- package/src/toolbar/HTMLToolbar.ts +172 -22
- package/src/toolbar/toolbar.css +12 -0
- package/src/toolbar/types.ts +6 -0
- package/src/tools/Pen.ts +56 -34
- package/src/tools/ToolController.ts +3 -3
- package/src/types.ts +16 -1
- package/dist/build_tools/BundledFile.d.ts +0 -12
- package/dist/build_tools/BundledFile.js +0 -153
- package/dist/scripts/bundle.js +0 -19
- package/dist/scripts/watchBundle.d.ts +0 -1
- package/dist/scripts/watchBundle.js +0 -9
- package/dist/src/main.d.ts +0 -3
- package/dist/src/main.js +0 -4
@@ -1,6 +1,6 @@
|
|
1
1
|
import Editor from '../Editor';
|
2
2
|
import { ToolType } from '../tools/ToolController';
|
3
|
-
import { EditorEventType } from '../types';
|
3
|
+
import { EditorEventType, StrokeDataPoint } from '../types';
|
4
4
|
|
5
5
|
import { coloris, init as colorisInit } from '@melloware/coloris';
|
6
6
|
import Color4 from '../Color4';
|
@@ -9,6 +9,15 @@ import Eraser from '../tools/Eraser';
|
|
9
9
|
import BaseTool from '../tools/BaseTool';
|
10
10
|
import SelectionTool from '../tools/SelectionTool';
|
11
11
|
import { ToolbarLocalization } from './types';
|
12
|
+
import { makeFreehandLineBuilder } from '../components/builders/FreehandLineBuilder';
|
13
|
+
import { Vec2 } from '../geometry/Vec2';
|
14
|
+
import SVGRenderer from '../rendering/SVGRenderer';
|
15
|
+
import Viewport from '../Viewport';
|
16
|
+
import EventDispatcher from '../EventDispatcher';
|
17
|
+
import { ComponentBuilderFactory } from '../components/builders/types';
|
18
|
+
import { makeArrowBuilder } from '../components/builders/ArrowBuilder';
|
19
|
+
import { makeLineBuilder } from '../components/builders/LineBuilder';
|
20
|
+
import { makeFilledRectangleBuilder, makeOutlinedRectangleBuilder } from '../components/builders/RectangleBuilder';
|
12
21
|
|
13
22
|
const primaryForegroundFill = `
|
14
23
|
style='fill: var(--primary-foreground-color);'
|
@@ -18,6 +27,8 @@ const primaryForegroundStrokeFill = `
|
|
18
27
|
`;
|
19
28
|
|
20
29
|
const toolbarCSSPrefix = 'toolbar-';
|
30
|
+
const svgNamespace = 'http://www.w3.org/2000/svg';
|
31
|
+
|
21
32
|
abstract class ToolbarWidget {
|
22
33
|
protected readonly container: HTMLElement;
|
23
34
|
private button: HTMLElement;
|
@@ -163,7 +174,7 @@ abstract class ToolbarWidget {
|
|
163
174
|
}
|
164
175
|
|
165
176
|
private createDropdownIcon(): Element {
|
166
|
-
const icon = document.createElementNS(
|
177
|
+
const icon = document.createElementNS(svgNamespace, 'svg');
|
167
178
|
icon.innerHTML = `
|
168
179
|
<g>
|
169
180
|
<path
|
@@ -183,9 +194,7 @@ class EraserWidget extends ToolbarWidget {
|
|
183
194
|
return this.localizationTable.eraser;
|
184
195
|
}
|
185
196
|
protected createIcon(): Element {
|
186
|
-
const icon = document.createElementNS(
|
187
|
-
'http://www.w3.org/2000/svg', 'svg'
|
188
|
-
);
|
197
|
+
const icon = document.createElementNS(svgNamespace, 'svg');
|
189
198
|
|
190
199
|
// Draw an eraser-like shape
|
191
200
|
icon.innerHTML = `
|
@@ -220,7 +229,7 @@ class SelectionWidget extends ToolbarWidget {
|
|
220
229
|
}
|
221
230
|
|
222
231
|
protected createIcon(): Element {
|
223
|
-
const icon = document.createElementNS(
|
232
|
+
const icon = document.createElementNS(svgNamespace, 'svg');
|
224
233
|
|
225
234
|
// Draw a cursor-like shape
|
226
235
|
icon.innerHTML = `
|
@@ -270,7 +279,7 @@ class TouchDrawingWidget extends ToolbarWidget {
|
|
270
279
|
}
|
271
280
|
|
272
281
|
protected createIcon(): Element {
|
273
|
-
const icon = document.createElementNS(
|
282
|
+
const icon = document.createElementNS(svgNamespace, 'svg');
|
274
283
|
|
275
284
|
// Draw a cursor-like shape
|
276
285
|
icon.innerHTML = `
|
@@ -307,7 +316,7 @@ class PenWidget extends ToolbarWidget {
|
|
307
316
|
private updateInputs: ()=> void = () => {};
|
308
317
|
|
309
318
|
public constructor(
|
310
|
-
editor: Editor, private tool: Pen, localization: ToolbarLocalization
|
319
|
+
editor: Editor, private tool: Pen, localization: ToolbarLocalization, private penTypes: PenTypeRecord[]
|
311
320
|
) {
|
312
321
|
super(editor, tool, localization);
|
313
322
|
|
@@ -328,13 +337,7 @@ class PenWidget extends ToolbarWidget {
|
|
328
337
|
return this.targetTool.description;
|
329
338
|
}
|
330
339
|
|
331
|
-
|
332
|
-
// We need to use createElementNS to embed an SVG element in HTML.
|
333
|
-
// See http://zhangwenli.com/blog/2017/07/26/createelementns/
|
334
|
-
const icon = document.createElementNS(
|
335
|
-
'http://www.w3.org/2000/svg', 'svg'
|
336
|
-
);
|
337
|
-
|
340
|
+
private makePenIcon(elem: SVGSVGElement) {
|
338
341
|
// Use a square-root scale to prevent the pen's tip from overflowing.
|
339
342
|
const scale = Math.round(Math.sqrt(this.tool.getThickness()) * 2);
|
340
343
|
const color = this.tool.getColor();
|
@@ -342,7 +345,7 @@ class PenWidget extends ToolbarWidget {
|
|
342
345
|
// Draw a pen-like shape
|
343
346
|
const primaryStrokeTipPath = `M14,63 L${50 - scale},95 L${50 + scale},90 L88,60 Z`;
|
344
347
|
const backgroundStrokeTipPath = `M14,63 L${50 - scale},85 L${50 + scale},83 L88,60 Z`;
|
345
|
-
|
348
|
+
elem.innerHTML = `
|
346
349
|
<defs>
|
347
350
|
<pattern
|
348
351
|
id='checkerboard'
|
@@ -375,8 +378,50 @@ class PenWidget extends ToolbarWidget {
|
|
375
378
|
/>
|
376
379
|
</g>
|
377
380
|
`;
|
381
|
+
}
|
382
|
+
|
383
|
+
// Draws an icon with the pen.
|
384
|
+
private makeDrawnIcon(icon: SVGSVGElement) {
|
385
|
+
const strokeFactory = this.tool.getStrokeFactory();
|
386
|
+
|
387
|
+
const toolThickness = this.tool.getThickness();
|
388
|
+
|
389
|
+
const nowTime = (new Date()).getTime();
|
390
|
+
const startPoint: StrokeDataPoint = {
|
391
|
+
pos: Vec2.of(10, 10),
|
392
|
+
width: toolThickness / 5,
|
393
|
+
color: this.tool.getColor(),
|
394
|
+
time: nowTime - 100,
|
395
|
+
};
|
396
|
+
const endPoint: StrokeDataPoint = {
|
397
|
+
pos: Vec2.of(90, 90),
|
398
|
+
width: toolThickness / 5,
|
399
|
+
color: this.tool.getColor(),
|
400
|
+
time: nowTime,
|
401
|
+
};
|
402
|
+
|
403
|
+
const builder = strokeFactory(startPoint, this.editor.viewport);
|
404
|
+
builder.addPoint(endPoint);
|
405
|
+
|
406
|
+
const viewport = new Viewport(new EventDispatcher());
|
407
|
+
viewport.updateScreenSize(Vec2.of(100, 100));
|
408
|
+
const renderer = new SVGRenderer(icon, viewport);
|
409
|
+
builder.preview(renderer);
|
410
|
+
}
|
411
|
+
|
412
|
+
protected createIcon(): Element {
|
413
|
+
// We need to use createElementNS to embed an SVG element in HTML.
|
414
|
+
// See http://zhangwenli.com/blog/2017/07/26/createelementns/
|
415
|
+
const icon = document.createElementNS(svgNamespace, 'svg');
|
378
416
|
icon.setAttribute('viewBox', '0 0 100 100');
|
379
417
|
|
418
|
+
const strokeFactory = this.tool.getStrokeFactory();
|
419
|
+
if (strokeFactory === makeFreehandLineBuilder) {
|
420
|
+
this.makePenIcon(icon);
|
421
|
+
} else {
|
422
|
+
this.makeDrawnIcon(icon);
|
423
|
+
}
|
424
|
+
|
380
425
|
return icon;
|
381
426
|
}
|
382
427
|
|
@@ -384,15 +429,23 @@ class PenWidget extends ToolbarWidget {
|
|
384
429
|
protected fillDropdown(dropdown: HTMLElement): boolean {
|
385
430
|
const container = document.createElement('div');
|
386
431
|
|
387
|
-
// Thickness: Value of the input is squared to allow for finer control/larger values.
|
388
432
|
const thicknessRow = document.createElement('div');
|
433
|
+
const objectTypeRow = document.createElement('div');
|
434
|
+
|
435
|
+
// Thickness: Value of the input is squared to allow for finer control/larger values.
|
389
436
|
const thicknessLabel = document.createElement('label');
|
390
437
|
const thicknessInput = document.createElement('input');
|
438
|
+
const objectSelectLabel = document.createElement('label');
|
439
|
+
const objectTypeSelect = document.createElement('select');
|
391
440
|
|
441
|
+
// Give inputs IDs so we can label them with a <label for=...>Label text</label>
|
392
442
|
thicknessInput.id = `${toolbarCSSPrefix}thicknessInput${PenWidget.idCounter++}`;
|
443
|
+
objectTypeSelect.id = `${toolbarCSSPrefix}builderSelect${PenWidget.idCounter++}`;
|
393
444
|
|
394
445
|
thicknessLabel.innerText = this.localizationTable.thicknessLabel;
|
395
446
|
thicknessLabel.setAttribute('for', thicknessInput.id);
|
447
|
+
objectSelectLabel.innerText = this.localizationTable.selectObjectType;
|
448
|
+
objectSelectLabel.setAttribute('for', objectTypeSelect.id);
|
396
449
|
|
397
450
|
thicknessInput.type = 'range';
|
398
451
|
thicknessInput.min = '1';
|
@@ -404,6 +457,18 @@ class PenWidget extends ToolbarWidget {
|
|
404
457
|
thicknessRow.appendChild(thicknessLabel);
|
405
458
|
thicknessRow.appendChild(thicknessInput);
|
406
459
|
|
460
|
+
objectTypeSelect.oninput = () => {
|
461
|
+
const penTypeIdx = parseInt(objectTypeSelect.value);
|
462
|
+
if (penTypeIdx < 0 || penTypeIdx >= this.penTypes.length) {
|
463
|
+
console.error('Invalid pen type index', penTypeIdx);
|
464
|
+
return;
|
465
|
+
}
|
466
|
+
|
467
|
+
this.tool.setStrokeFactory(this.penTypes[penTypeIdx].factory);
|
468
|
+
};
|
469
|
+
objectTypeRow.appendChild(objectSelectLabel);
|
470
|
+
objectTypeRow.appendChild(objectTypeSelect);
|
471
|
+
|
407
472
|
const colorRow = document.createElement('div');
|
408
473
|
const colorLabel = document.createElement('label');
|
409
474
|
const colorInput = document.createElement('input');
|
@@ -417,6 +482,18 @@ class PenWidget extends ToolbarWidget {
|
|
417
482
|
colorInput.oninput = () => {
|
418
483
|
this.tool.setColor(Color4.fromHex(colorInput.value));
|
419
484
|
};
|
485
|
+
colorInput.addEventListener('open', () => {
|
486
|
+
this.editor.notifier.dispatch(EditorEventType.ColorPickerToggled, {
|
487
|
+
kind: EditorEventType.ColorPickerToggled,
|
488
|
+
open: true,
|
489
|
+
});
|
490
|
+
});
|
491
|
+
colorInput.addEventListener('close', () => {
|
492
|
+
this.editor.notifier.dispatch(EditorEventType.ColorPickerToggled, {
|
493
|
+
kind: EditorEventType.ColorPickerToggled,
|
494
|
+
open: false,
|
495
|
+
});
|
496
|
+
});
|
420
497
|
|
421
498
|
colorRow.appendChild(colorLabel);
|
422
499
|
colorRow.appendChild(colorInput);
|
@@ -424,17 +501,37 @@ class PenWidget extends ToolbarWidget {
|
|
424
501
|
this.updateInputs = () => {
|
425
502
|
colorInput.value = this.tool.getColor().toHexString();
|
426
503
|
thicknessInput.value = Math.sqrt(this.tool.getThickness()).toString();
|
504
|
+
|
505
|
+
objectTypeSelect.replaceChildren();
|
506
|
+
for (let i = 0; i < this.penTypes.length; i ++) {
|
507
|
+
const penType = this.penTypes[i];
|
508
|
+
const option = document.createElement('option');
|
509
|
+
option.value = i.toString();
|
510
|
+
option.innerText = penType.name;
|
511
|
+
|
512
|
+
objectTypeSelect.appendChild(option);
|
513
|
+
|
514
|
+
if (penType.factory === this.tool.getStrokeFactory()) {
|
515
|
+
objectTypeSelect.value = i.toString();
|
516
|
+
}
|
517
|
+
}
|
427
518
|
};
|
428
519
|
this.updateInputs();
|
429
520
|
|
430
|
-
container.replaceChildren(colorRow, thicknessRow);
|
521
|
+
container.replaceChildren(colorRow, thicknessRow, objectTypeRow);
|
431
522
|
dropdown.replaceChildren(container);
|
432
523
|
return true;
|
433
524
|
}
|
434
525
|
}
|
435
526
|
|
527
|
+
interface PenTypeRecord {
|
528
|
+
name: string;
|
529
|
+
factory: ComponentBuilderFactory;
|
530
|
+
}
|
531
|
+
|
436
532
|
export default class HTMLToolbar {
|
437
533
|
private container: HTMLElement;
|
534
|
+
private penTypes: PenTypeRecord[];
|
438
535
|
|
439
536
|
public static defaultLocalization: ToolbarLocalization = {
|
440
537
|
pen: 'Pen',
|
@@ -446,6 +543,13 @@ export default class HTMLToolbar {
|
|
446
543
|
resizeImageToSelection: 'Resize image to selection',
|
447
544
|
undo: 'Undo',
|
448
545
|
redo: 'Redo',
|
546
|
+
selectObjectType: 'Object type: ',
|
547
|
+
|
548
|
+
freehandPen: 'Freehand',
|
549
|
+
arrowPen: 'Arrow',
|
550
|
+
linePen: 'Line',
|
551
|
+
outlinedRectanglePen: 'Outlined rectangle',
|
552
|
+
filledRectanglePen: 'Filled rectangle',
|
449
553
|
|
450
554
|
dropdownShown: (toolName) => `Dropdown for ${toolName} shown`,
|
451
555
|
dropdownHidden: (toolName) => `Dropdown for ${toolName} hidden`,
|
@@ -458,11 +562,41 @@ export default class HTMLToolbar {
|
|
458
562
|
this.container = document.createElement('div');
|
459
563
|
this.container.classList.add(`${toolbarCSSPrefix}root`);
|
460
564
|
this.container.setAttribute('role', 'toolbar');
|
461
|
-
this.addElements();
|
462
565
|
parent.appendChild(this.container);
|
463
566
|
|
464
|
-
// Initialize color choosers
|
465
567
|
colorisInit();
|
568
|
+
this.setupColorPickers();
|
569
|
+
|
570
|
+
// Default pen types
|
571
|
+
this.penTypes = [
|
572
|
+
{
|
573
|
+
name: localizationTable.freehandPen,
|
574
|
+
factory: makeFreehandLineBuilder,
|
575
|
+
},
|
576
|
+
{
|
577
|
+
name: localizationTable.arrowPen,
|
578
|
+
factory: makeArrowBuilder,
|
579
|
+
},
|
580
|
+
{
|
581
|
+
name: localizationTable.linePen,
|
582
|
+
factory: makeLineBuilder,
|
583
|
+
},
|
584
|
+
{
|
585
|
+
name: localizationTable.filledRectanglePen,
|
586
|
+
factory: makeFilledRectangleBuilder,
|
587
|
+
},
|
588
|
+
{
|
589
|
+
name: localizationTable.outlinedRectanglePen,
|
590
|
+
factory: makeOutlinedRectangleBuilder,
|
591
|
+
},
|
592
|
+
];
|
593
|
+
}
|
594
|
+
|
595
|
+
public setupColorPickers() {
|
596
|
+
const closePickerOverlay = document.createElement('div');
|
597
|
+
closePickerOverlay.className = `${toolbarCSSPrefix}closeColorPickerOverlay`;
|
598
|
+
this.editor.createHTMLOverlay(closePickerOverlay);
|
599
|
+
|
466
600
|
coloris({
|
467
601
|
el: '.coloris_input',
|
468
602
|
format: 'hex',
|
@@ -479,6 +613,16 @@ export default class HTMLToolbar {
|
|
479
613
|
Color4.white.toHexString(),
|
480
614
|
],
|
481
615
|
});
|
616
|
+
|
617
|
+
this.editor.notifier.on(EditorEventType.ColorPickerToggled, event => {
|
618
|
+
if (event.kind !== EditorEventType.ColorPickerToggled) {
|
619
|
+
return;
|
620
|
+
}
|
621
|
+
|
622
|
+
// Show/hide the overlay. Making the overlay visible gives users a surface to click
|
623
|
+
// on that shows/hides the color picker.
|
624
|
+
closePickerOverlay.style.display = event.open ? 'block' : 'none';
|
625
|
+
});
|
482
626
|
}
|
483
627
|
|
484
628
|
public addActionButton(text: string, command: ()=> void, parent?: Element) {
|
@@ -515,14 +659,16 @@ export default class HTMLToolbar {
|
|
515
659
|
});
|
516
660
|
}
|
517
661
|
|
518
|
-
|
662
|
+
public addDefaultToolWidgets() {
|
519
663
|
const toolController = this.editor.toolController;
|
520
664
|
for (const tool of toolController.getMatchingTools(ToolType.Pen)) {
|
521
665
|
if (!(tool instanceof Pen)) {
|
522
666
|
throw new Error('All `Pen` tools must have kind === ToolType.Pen');
|
523
667
|
}
|
524
668
|
|
525
|
-
const widget = new PenWidget(
|
669
|
+
const widget = new PenWidget(
|
670
|
+
this.editor, tool, this.localizationTable, this.penTypes,
|
671
|
+
);
|
526
672
|
widget.addTo(this.container);
|
527
673
|
}
|
528
674
|
|
@@ -546,6 +692,10 @@ export default class HTMLToolbar {
|
|
546
692
|
(new TouchDrawingWidget(this.editor, tool, this.localizationTable)).addTo(this.container);
|
547
693
|
}
|
548
694
|
|
695
|
+
this.setupColorPickers();
|
696
|
+
}
|
697
|
+
|
698
|
+
public addDefaultActionButtons() {
|
549
699
|
this.addUndoRedoButtons();
|
550
700
|
}
|
551
701
|
}
|
package/src/toolbar/toolbar.css
CHANGED
@@ -100,6 +100,18 @@
|
|
100
100
|
flex-direction: row;
|
101
101
|
}
|
102
102
|
|
103
|
+
.toolbar-closeColorPickerOverlay {
|
104
|
+
display: none;
|
105
|
+
position: fixed;
|
106
|
+
top: 0;
|
107
|
+
left: 0;
|
108
|
+
bottom: 0;
|
109
|
+
right: 0;
|
110
|
+
|
111
|
+
background-color: var(--primary-background-color);
|
112
|
+
opacity: 0.3;
|
113
|
+
}
|
114
|
+
|
103
115
|
/* Make color selection buttons fill their containing label */
|
104
116
|
.toolbar-dropdown .clr-field button {
|
105
117
|
width: 100%;
|
package/src/toolbar/types.ts
CHANGED
@@ -5,6 +5,12 @@ export enum ToolbarButtonType {
|
|
5
5
|
|
6
6
|
|
7
7
|
export interface ToolbarLocalization {
|
8
|
+
outlinedRectanglePen: string;
|
9
|
+
filledRectanglePen: string;
|
10
|
+
linePen: string;
|
11
|
+
arrowPen: string;
|
12
|
+
freehandPen: string;
|
13
|
+
selectObjectType: string;
|
8
14
|
colorLabel: string;
|
9
15
|
pen: string;
|
10
16
|
eraser: string;
|
package/src/tools/Pen.ts
CHANGED
@@ -1,49 +1,60 @@
|
|
1
1
|
import Color4 from '../Color4';
|
2
2
|
import Editor from '../Editor';
|
3
3
|
import EditorImage from '../EditorImage';
|
4
|
-
import { Vec2 } from '../geometry/Vec2';
|
5
4
|
import Pointer, { PointerDevice } from '../Pointer';
|
6
|
-
import
|
7
|
-
import { EditorEventType, PointerEvt } from '../types';
|
5
|
+
import { makeFreehandLineBuilder } from '../components/builders/FreehandLineBuilder';
|
6
|
+
import { EditorEventType, PointerEvt, StrokeDataPoint } from '../types';
|
8
7
|
import BaseTool from './BaseTool';
|
9
8
|
import { ToolType } from './ToolController';
|
9
|
+
import { ComponentBuilder, ComponentBuilderFactory } from '../components/builders/types';
|
10
|
+
|
11
|
+
interface PenStyle {
|
12
|
+
color: Color4;
|
13
|
+
thickness: number;
|
14
|
+
}
|
10
15
|
|
11
16
|
export default class Pen extends BaseTool {
|
12
|
-
private builder:
|
17
|
+
private builder: ComponentBuilder|null = null;
|
18
|
+
private builderFactory: ComponentBuilderFactory = makeFreehandLineBuilder;
|
19
|
+
private lastPoint: StrokeDataPoint|null = null;
|
20
|
+
|
13
21
|
public readonly kind: ToolType = ToolType.Pen;
|
14
22
|
|
15
23
|
public constructor(
|
16
24
|
private editor: Editor,
|
17
25
|
description: string,
|
18
|
-
private
|
19
|
-
private thickness: number = 16.0,
|
26
|
+
private style: PenStyle,
|
20
27
|
) {
|
21
28
|
super(editor.notifier, description);
|
22
29
|
}
|
23
30
|
|
24
31
|
private getPressureMultiplier() {
|
25
|
-
return 1 / this.editor.viewport.getScaleFactor() * this.thickness;
|
32
|
+
return 1 / this.editor.viewport.getScaleFactor() * this.style.thickness;
|
26
33
|
}
|
27
34
|
|
28
|
-
private getStrokePoint(pointer: Pointer) {
|
35
|
+
private getStrokePoint(pointer: Pointer): StrokeDataPoint {
|
29
36
|
const minPressure = 0.3;
|
30
37
|
const pressure = Math.max(pointer.pressure ?? 1.0, minPressure);
|
31
38
|
return {
|
32
39
|
pos: pointer.canvasPos,
|
33
40
|
width: pressure * this.getPressureMultiplier(),
|
34
|
-
color: this.color,
|
41
|
+
color: this.style.color,
|
35
42
|
time: pointer.timeStamp,
|
36
43
|
};
|
37
44
|
}
|
38
45
|
|
39
|
-
private
|
46
|
+
private previewStroke() {
|
47
|
+
this.editor.clearWetInk();
|
48
|
+
this.builder?.preview(this.editor.display.getWetInkRenderer());
|
49
|
+
}
|
50
|
+
|
51
|
+
private addPointToStroke(point: StrokeDataPoint) {
|
40
52
|
if (!this.builder) {
|
41
53
|
throw new Error('No stroke is currently being generated.');
|
42
54
|
}
|
43
|
-
this.builder.addPoint(
|
44
|
-
|
45
|
-
this.
|
46
|
-
this.editor.drawWetInk(...this.builder.preview());
|
55
|
+
this.builder.addPoint(point);
|
56
|
+
this.lastPoint = point;
|
57
|
+
this.previewStroke();
|
47
58
|
}
|
48
59
|
|
49
60
|
public onPointerDown({ current, allPointers }: PointerEvt): boolean {
|
@@ -52,15 +63,7 @@ export default class Pen extends BaseTool {
|
|
52
63
|
}
|
53
64
|
|
54
65
|
if (allPointers.length === 1 || current.device === PointerDevice.Pen) {
|
55
|
-
|
56
|
-
// less than ± 2 px from the curve.
|
57
|
-
const canvasTransform = this.editor.viewport.screenToCanvasTransform;
|
58
|
-
const maxSmoothingDist = canvasTransform.transformVec3(Vec2.unitX).magnitude() * 7;
|
59
|
-
const minSmoothingDist = canvasTransform.transformVec3(Vec2.unitX).magnitude() * 2;
|
60
|
-
|
61
|
-
this.builder = new StrokeBuilder(
|
62
|
-
this.getStrokePoint(current), minSmoothingDist, maxSmoothingDist
|
63
|
-
);
|
66
|
+
this.builder = this.builderFactory(this.getStrokePoint(current), this.editor.viewport);
|
64
67
|
return true;
|
65
68
|
}
|
66
69
|
|
@@ -68,7 +71,7 @@ export default class Pen extends BaseTool {
|
|
68
71
|
}
|
69
72
|
|
70
73
|
public onPointerMove({ current }: PointerEvt): void {
|
71
|
-
this.addPointToStroke(current);
|
74
|
+
this.addPointToStroke(this.getStrokePoint(current));
|
72
75
|
}
|
73
76
|
|
74
77
|
public onPointerUp({ current }: PointerEvt): void {
|
@@ -76,12 +79,17 @@ export default class Pen extends BaseTool {
|
|
76
79
|
return;
|
77
80
|
}
|
78
81
|
|
79
|
-
|
82
|
+
// onPointerUp events can have zero pressure. Use the last pressure instead.
|
83
|
+
const currentPoint = this.getStrokePoint(current);
|
84
|
+
const strokePoint = {
|
85
|
+
...currentPoint,
|
86
|
+
width: this.lastPoint?.width ?? currentPoint.width,
|
87
|
+
};
|
88
|
+
|
89
|
+
this.addPointToStroke(strokePoint);
|
80
90
|
if (this.builder && current.isPrimary) {
|
81
91
|
const stroke = this.builder.build();
|
82
|
-
|
83
|
-
this.editor.clearWetInk();
|
84
|
-
this.editor.drawWetInk(...this.builder.preview());
|
92
|
+
this.previewStroke();
|
85
93
|
|
86
94
|
const canFlatten = true;
|
87
95
|
const action = new EditorImage.AddElementCommand(stroke, canFlatten);
|
@@ -103,19 +111,33 @@ export default class Pen extends BaseTool {
|
|
103
111
|
}
|
104
112
|
|
105
113
|
public setColor(color: Color4): void {
|
106
|
-
if (color.toHexString() !== this.color.toHexString()) {
|
107
|
-
this.
|
114
|
+
if (color.toHexString() !== this.style.color.toHexString()) {
|
115
|
+
this.style = {
|
116
|
+
...this.style,
|
117
|
+
color,
|
118
|
+
};
|
108
119
|
this.noteUpdated();
|
109
120
|
}
|
110
121
|
}
|
111
122
|
|
112
123
|
public setThickness(thickness: number) {
|
113
|
-
if (thickness !== this.thickness) {
|
114
|
-
this.
|
124
|
+
if (thickness !== this.style.thickness) {
|
125
|
+
this.style = {
|
126
|
+
...this.style,
|
127
|
+
thickness,
|
128
|
+
};
|
129
|
+
this.noteUpdated();
|
130
|
+
}
|
131
|
+
}
|
132
|
+
|
133
|
+
public setStrokeFactory(factory: ComponentBuilderFactory) {
|
134
|
+
if (factory !== this.builderFactory) {
|
135
|
+
this.builderFactory = factory;
|
115
136
|
this.noteUpdated();
|
116
137
|
}
|
117
138
|
}
|
118
139
|
|
119
|
-
public getThickness() { return this.thickness; }
|
120
|
-
public getColor() { return this.color; }
|
140
|
+
public getThickness() { return this.style.thickness; }
|
141
|
+
public getColor() { return this.style.color; }
|
142
|
+
public getStrokeFactory() { return this.builderFactory; }
|
121
143
|
}
|
@@ -24,17 +24,17 @@ export default class ToolController {
|
|
24
24
|
public constructor(editor: Editor, localization: ToolLocalization) {
|
25
25
|
const primaryToolEnabledGroup = new ToolEnabledGroup();
|
26
26
|
const touchPanZoom = new PanZoom(editor, PanZoomMode.OneFingerGestures, localization.touchPanTool);
|
27
|
-
const primaryPenTool = new Pen(editor, localization.penTool(1));
|
27
|
+
const primaryPenTool = new Pen(editor, localization.penTool(1), { color: Color4.purple, thickness: 16 });
|
28
28
|
const primaryTools = [
|
29
29
|
new SelectionTool(editor, localization.selectionTool),
|
30
30
|
new Eraser(editor, localization.eraserTool),
|
31
31
|
|
32
32
|
// Three pens
|
33
33
|
primaryPenTool,
|
34
|
-
new Pen(editor, localization.penTool(2), Color4.clay, 8),
|
34
|
+
new Pen(editor, localization.penTool(2), { color: Color4.clay, thickness: 8 }),
|
35
35
|
|
36
36
|
// Highlighter-like pen with width=64
|
37
|
-
new Pen(editor, localization.penTool(3), Color4.ofRGBA(1, 1, 0, 0.5), 64),
|
37
|
+
new Pen(editor, localization.penTool(3), { color: Color4.ofRGBA(1, 1, 0, 0.5), thickness: 64 }),
|
38
38
|
];
|
39
39
|
this.tools = [
|
40
40
|
touchPanZoom,
|
package/src/types.ts
CHANGED
@@ -8,6 +8,7 @@ import BaseTool from './tools/BaseTool';
|
|
8
8
|
import AbstractComponent from './components/AbstractComponent';
|
9
9
|
import Rect2 from './geometry/Rect2';
|
10
10
|
import Pointer from './Pointer';
|
11
|
+
import Color4 from './Color4';
|
11
12
|
|
12
13
|
|
13
14
|
export interface PointerEvtListener {
|
@@ -74,6 +75,7 @@ export type InputEvt = KeyPressEvent | WheelEvt | GestureCancelEvt | PointerEvt;
|
|
74
75
|
|
75
76
|
export type EditorNotifier = EventDispatcher<EditorEventType, EditorEventDataType>;
|
76
77
|
|
78
|
+
|
77
79
|
export enum EditorEventType {
|
78
80
|
ToolEnabled,
|
79
81
|
ToolDisabled,
|
@@ -82,6 +84,7 @@ export enum EditorEventType {
|
|
82
84
|
ObjectAdded,
|
83
85
|
ViewportChanged,
|
84
86
|
DisplayResized,
|
87
|
+
ColorPickerToggled,
|
85
88
|
}
|
86
89
|
|
87
90
|
type EditorToolEventType = EditorEventType.ToolEnabled
|
@@ -115,7 +118,12 @@ export interface EditorUndoStackUpdated {
|
|
115
118
|
readonly redoStackSize: number;
|
116
119
|
}
|
117
120
|
|
118
|
-
export
|
121
|
+
export interface ColorPickerToggled {
|
122
|
+
readonly kind: EditorEventType.ColorPickerToggled;
|
123
|
+
readonly open: boolean;
|
124
|
+
}
|
125
|
+
|
126
|
+
export type EditorEventDataType = EditorToolEvent | EditorObjectEvent | EditorViewportChangedEvent | DisplayResizedEvent | EditorUndoStackUpdated | ColorPickerToggled;
|
119
127
|
|
120
128
|
|
121
129
|
// Returns a Promise to indicate that the event source should pause until the Promise resolves.
|
@@ -131,3 +139,10 @@ export interface ImageLoader {
|
|
131
139
|
onAddComponent: ComponentAddedListener, onProgressListener: OnProgressListener
|
132
140
|
): Promise<Rect2>;
|
133
141
|
}
|
142
|
+
|
143
|
+
export interface StrokeDataPoint {
|
144
|
+
pos: Point2;
|
145
|
+
width: number;
|
146
|
+
time: number;
|
147
|
+
color: Color4;
|
148
|
+
}
|
@@ -1,12 +0,0 @@
|
|
1
|
-
export default class BundledFile {
|
2
|
-
readonly bundleName: string;
|
3
|
-
private readonly sourceFilePath;
|
4
|
-
private readonly bundleBaseName;
|
5
|
-
private readonly rootFileDirectory;
|
6
|
-
constructor(bundleName: string, sourceFilePath: string);
|
7
|
-
private getWebpackOptions;
|
8
|
-
private handleErrors;
|
9
|
-
build(): Promise<void>;
|
10
|
-
startWatching(): void;
|
11
|
-
}
|
12
|
-
export declare const bundledFiles: BundledFile[];
|