js-draw 0.7.2 → 0.9.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.
Files changed (82) hide show
  1. package/.github/ISSUE_TEMPLATE/translation.md +1 -0
  2. package/CHANGELOG.md +12 -0
  3. package/dist/bundle.js +1 -1
  4. package/dist/src/Color4.js +3 -0
  5. package/dist/src/SVGLoader.js +5 -7
  6. package/dist/src/components/Stroke.js +10 -3
  7. package/dist/src/components/builders/FreehandLineBuilder.d.ts +10 -23
  8. package/dist/src/components/builders/FreehandLineBuilder.js +70 -396
  9. package/dist/src/components/builders/PressureSensitiveFreehandLineBuilder.d.ts +36 -0
  10. package/dist/src/components/builders/PressureSensitiveFreehandLineBuilder.js +339 -0
  11. package/dist/src/components/lib.d.ts +2 -0
  12. package/dist/src/components/lib.js +2 -0
  13. package/dist/src/components/util/StrokeSmoother.d.ts +35 -0
  14. package/dist/src/components/util/StrokeSmoother.js +206 -0
  15. package/dist/src/math/Mat33.d.ts +2 -0
  16. package/dist/src/math/Mat33.js +4 -0
  17. package/dist/src/math/Path.d.ts +2 -0
  18. package/dist/src/math/Path.js +44 -0
  19. package/dist/src/rendering/renderers/CanvasRenderer.js +2 -0
  20. package/dist/src/rendering/renderers/SVGRenderer.d.ts +2 -0
  21. package/dist/src/rendering/renderers/SVGRenderer.js +20 -0
  22. package/dist/src/toolbar/HTMLToolbar.d.ts +3 -0
  23. package/dist/src/toolbar/HTMLToolbar.js +24 -0
  24. package/dist/src/toolbar/IconProvider.d.ts +1 -0
  25. package/dist/src/toolbar/IconProvider.js +43 -1
  26. package/dist/src/toolbar/localization.d.ts +2 -0
  27. package/dist/src/toolbar/localization.js +2 -0
  28. package/dist/src/toolbar/makeColorInput.d.ts +2 -1
  29. package/dist/src/toolbar/makeColorInput.js +13 -2
  30. package/dist/src/toolbar/widgets/ActionButtonWidget.d.ts +1 -1
  31. package/dist/src/toolbar/widgets/ActionButtonWidget.js +2 -2
  32. package/dist/src/toolbar/widgets/BaseToolWidget.d.ts +1 -2
  33. package/dist/src/toolbar/widgets/BaseToolWidget.js +2 -3
  34. package/dist/src/toolbar/widgets/BaseWidget.d.ts +32 -2
  35. package/dist/src/toolbar/widgets/BaseWidget.js +67 -6
  36. package/dist/src/toolbar/widgets/EraserToolWidget.d.ts +4 -0
  37. package/dist/src/toolbar/widgets/EraserToolWidget.js +3 -0
  38. package/dist/src/toolbar/widgets/HandToolWidget.d.ts +3 -1
  39. package/dist/src/toolbar/widgets/HandToolWidget.js +22 -13
  40. package/dist/src/toolbar/widgets/PenToolWidget.d.ts +7 -1
  41. package/dist/src/toolbar/widgets/PenToolWidget.js +83 -12
  42. package/dist/src/toolbar/widgets/SelectionToolWidget.d.ts +1 -1
  43. package/dist/src/toolbar/widgets/SelectionToolWidget.js +7 -7
  44. package/dist/src/toolbar/widgets/TextToolWidget.d.ts +4 -1
  45. package/dist/src/toolbar/widgets/TextToolWidget.js +17 -3
  46. package/dist/src/tools/PanZoom.d.ts +4 -1
  47. package/dist/src/tools/PanZoom.js +24 -1
  48. package/dist/src/tools/Pen.d.ts +2 -2
  49. package/dist/src/tools/Pen.js +2 -2
  50. package/dist/src/tools/SelectionTool/Selection.d.ts +1 -0
  51. package/dist/src/tools/SelectionTool/Selection.js +8 -1
  52. package/dist/src/tools/ToolController.js +2 -1
  53. package/package.json +1 -1
  54. package/src/Color4.ts +2 -0
  55. package/src/SVGLoader.ts +8 -8
  56. package/src/components/Stroke.ts +16 -3
  57. package/src/components/builders/FreehandLineBuilder.ts +54 -495
  58. package/src/components/builders/PressureSensitiveFreehandLineBuilder.ts +454 -0
  59. package/src/components/lib.ts +3 -1
  60. package/src/components/util/StrokeSmoother.ts +290 -0
  61. package/src/math/Mat33.ts +5 -0
  62. package/src/math/Path.test.ts +49 -0
  63. package/src/math/Path.ts +51 -0
  64. package/src/rendering/renderers/CanvasRenderer.ts +2 -0
  65. package/src/rendering/renderers/SVGRenderer.ts +24 -0
  66. package/src/toolbar/HTMLToolbar.ts +33 -0
  67. package/src/toolbar/IconProvider.ts +49 -1
  68. package/src/toolbar/localization.ts +4 -0
  69. package/src/toolbar/makeColorInput.ts +21 -3
  70. package/src/toolbar/widgets/ActionButtonWidget.ts +6 -3
  71. package/src/toolbar/widgets/BaseToolWidget.ts +4 -3
  72. package/src/toolbar/widgets/BaseWidget.ts +83 -5
  73. package/src/toolbar/widgets/EraserToolWidget.ts +11 -0
  74. package/src/toolbar/widgets/HandToolWidget.ts +48 -17
  75. package/src/toolbar/widgets/PenToolWidget.ts +110 -13
  76. package/src/toolbar/widgets/SelectionToolWidget.ts +8 -5
  77. package/src/toolbar/widgets/TextToolWidget.ts +29 -4
  78. package/src/tools/PanZoom.ts +28 -1
  79. package/src/tools/Pen.test.ts +2 -2
  80. package/src/tools/Pen.ts +1 -1
  81. package/src/tools/SelectionTool/Selection.ts +10 -1
  82. package/src/tools/ToolController.ts +2 -1
@@ -63,6 +63,9 @@ export default class Color4 {
63
63
  if (text.startsWith('#')) {
64
64
  return Color4.fromHex(text);
65
65
  }
66
+ else if (text === 'none' || text === 'transparent') {
67
+ return Color4.transparent;
68
+ }
66
69
  else {
67
70
  // Otherwise, try to use an HTML5Canvas to determine the color
68
71
  const canvas = document.createElement('canvas');
@@ -22,6 +22,7 @@ export const defaultSVGViewRect = new Rect2(0, 0, 500, 500);
22
22
  // Key to retrieve unrecognised attributes from an AbstractComponent
23
23
  export const svgAttributesDataKey = 'svgAttrs';
24
24
  export const svgStyleAttributesDataKey = 'svgStyleAttrs';
25
+ const supportedStrokeFillStyleAttrs = ['stroke', 'fill', 'stroke-width'];
25
26
  export default class SVGLoader {
26
27
  constructor(source, onFinish, storeUnknown = true) {
27
28
  this.source = source;
@@ -123,8 +124,7 @@ export default class SVGLoader {
123
124
  try {
124
125
  const strokeData = this.strokeDataFromElem(node);
125
126
  elem = new Stroke(strokeData);
126
- const supportedStyleAttrs = ['stroke', 'fill', 'stroke-width'];
127
- this.attachUnrecognisedAttrs(elem, node, new Set([...supportedStyleAttrs, 'd']), new Set(supportedStyleAttrs));
127
+ this.attachUnrecognisedAttrs(elem, node, new Set([...supportedStrokeFillStyleAttrs, 'd']), new Set(supportedStrokeFillStyleAttrs));
128
128
  }
129
129
  catch (e) {
130
130
  console.error('Invalid path in node', node, '\nError:', e, '\nAdding as an unknown object.');
@@ -193,8 +193,8 @@ export default class SVGLoader {
193
193
  const fontSizeMatch = /^([-0-9.e]+)px/i.exec(computedStyles.fontSize);
194
194
  const supportedStyleAttrs = [
195
195
  'fontFamily',
196
- 'fill',
197
- 'transform'
196
+ 'transform',
197
+ ...supportedStrokeFillStyleAttrs,
198
198
  ];
199
199
  let fontSize = 12;
200
200
  if (fontSizeMatch) {
@@ -204,9 +204,7 @@ export default class SVGLoader {
204
204
  const style = {
205
205
  size: fontSize,
206
206
  fontFamily: computedStyles.fontFamily || elem.style.fontFamily || 'sans-serif',
207
- renderingStyle: {
208
- fill: Color4.fromString(computedStyles.fill || elem.style.fill || '#000')
209
- },
207
+ renderingStyle: this.getStyle(elem),
210
208
  };
211
209
  const supportedAttrs = [];
212
210
  const transform = this.getTransform(elem, supportedAttrs, computedStyles);
@@ -36,6 +36,7 @@ export default class Stroke extends AbstractComponent {
36
36
  return false;
37
37
  }
38
38
  render(canvas, visibleRect) {
39
+ var _a, _b;
39
40
  canvas.startObject(this.getBBox());
40
41
  for (const part of this.parts) {
41
42
  const bbox = this.bboxForPart(part.path.bbox, part.style);
@@ -44,7 +45,7 @@ export default class Stroke extends AbstractComponent {
44
45
  continue;
45
46
  }
46
47
  const muchBiggerThanVisible = bbox.size.x > visibleRect.size.x * 2 || bbox.size.y > visibleRect.size.y * 2;
47
- if (muchBiggerThanVisible && !part.path.closedRoughlyIntersects(visibleRect)) {
48
+ if (muchBiggerThanVisible && !part.path.roughlyIntersects(visibleRect, (_b = (_a = part.style.stroke) === null || _a === void 0 ? void 0 : _a.width) !== null && _b !== void 0 ? _b : 0)) {
48
49
  continue;
49
50
  }
50
51
  }
@@ -65,7 +66,13 @@ export default class Stroke extends AbstractComponent {
65
66
  // Update each part
66
67
  this.parts = this.parts.map((part) => {
67
68
  const newPath = part.path.transformedBy(affineTransfm);
68
- const newBBox = this.bboxForPart(newPath.bbox, part.style);
69
+ const newStyle = Object.assign(Object.assign({}, part.style), { stroke: part.style.stroke ? Object.assign({}, part.style.stroke) : undefined });
70
+ // Approximate the scale factor.
71
+ if (newStyle.stroke) {
72
+ const scaleFactor = affineTransfm.getScaleFactor();
73
+ newStyle.stroke.width *= scaleFactor;
74
+ }
75
+ const newBBox = this.bboxForPart(newPath.bbox, newStyle);
69
76
  if (isFirstPart) {
70
77
  this.contentBBox = newBBox;
71
78
  isFirstPart = false;
@@ -77,7 +84,7 @@ export default class Stroke extends AbstractComponent {
77
84
  path: newPath,
78
85
  startPoint: newPath.startPoint,
79
86
  commands: newPath.parts,
80
- style: part.style,
87
+ style: newStyle,
81
88
  };
82
89
  });
83
90
  }
@@ -1,44 +1,31 @@
1
- import AbstractRenderer from '../../rendering/renderers/AbstractRenderer';
1
+ import AbstractRenderer, { RenderablePathSpec } from '../../rendering/renderers/AbstractRenderer';
2
2
  import Rect2 from '../../math/Rect2';
3
3
  import Stroke from '../Stroke';
4
4
  import Viewport from '../../Viewport';
5
5
  import { StrokeDataPoint } from '../../types';
6
6
  import { ComponentBuilder, ComponentBuilderFactory } from './types';
7
+ import RenderingStyle from '../../rendering/RenderingStyle';
7
8
  export declare const makeFreehandLineBuilder: ComponentBuilderFactory;
8
9
  export default class FreehandLineBuilder implements ComponentBuilder {
9
10
  private startPoint;
10
11
  private minFitAllowed;
11
- private maxFitAllowed;
12
12
  private viewport;
13
13
  private isFirstSegment;
14
- private pathStartConnector;
15
- private mostRecentConnector;
16
- private upperSegments;
17
- private lowerSegments;
18
- private lastUpperBezier;
19
- private lastLowerBezier;
20
14
  private parts;
21
- private buffer;
22
- private lastPoint;
23
- private lastExitingVec;
24
- private currentCurve;
25
- private curveStartWidth;
26
- private curveEndWidth;
27
- private momentum;
15
+ private curveFitter;
28
16
  private bbox;
17
+ private averageWidth;
18
+ private widthAverageNumSamples;
29
19
  constructor(startPoint: StrokeDataPoint, minFitAllowed: number, maxFitAllowed: number, viewport: Viewport);
30
20
  getBBox(): Rect2;
31
- private getRenderingStyle;
32
- private previewCurrentPath;
33
- private previewFullPath;
21
+ protected getRenderingStyle(): RenderingStyle;
22
+ protected previewCurrentPath(): RenderablePathSpec | null;
23
+ protected previewFullPath(): RenderablePathSpec[] | null;
34
24
  private previewStroke;
35
25
  preview(renderer: AbstractRenderer): void;
36
26
  build(): Stroke;
37
27
  private roundPoint;
38
- private shouldStartNewSegment;
39
- private approxCurrentCurveLength;
40
- private finalizeCurrentCurve;
41
- private currentSegmentToPath;
42
- private computeExitingVec;
28
+ private curveToPathCommands;
29
+ private addCurve;
43
30
  addPoint(newPoint: StrokeDataPoint): void;
44
31
  }