@nasser-sw/fabric 7.0.1-beta3 → 7.0.1-beta5

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 (105) hide show
  1. package/0 +0 -0
  2. package/dist/index.js +345 -162
  3. package/dist/index.js.map +1 -1
  4. package/dist/index.min.js +1 -1
  5. package/dist/index.min.js.map +1 -1
  6. package/dist/index.min.mjs +1 -1
  7. package/dist/index.min.mjs.map +1 -1
  8. package/dist/index.mjs +345 -162
  9. package/dist/index.mjs.map +1 -1
  10. package/dist/index.node.cjs +345 -162
  11. package/dist/index.node.cjs.map +1 -1
  12. package/dist/index.node.mjs +345 -162
  13. package/dist/index.node.mjs.map +1 -1
  14. package/dist/package.json.min.mjs +1 -1
  15. package/dist/package.json.mjs +1 -1
  16. package/dist/src/shapes/Line.d.ts +32 -86
  17. package/dist/src/shapes/Line.d.ts.map +1 -1
  18. package/dist/src/shapes/Line.min.mjs +1 -1
  19. package/dist/src/shapes/Line.min.mjs.map +1 -1
  20. package/dist/src/shapes/Line.mjs +345 -161
  21. package/dist/src/shapes/Line.mjs.map +1 -1
  22. package/dist-extensions/src/shapes/CustomLine.d.ts +10 -0
  23. package/dist-extensions/src/shapes/CustomLine.d.ts.map +1 -0
  24. package/dist-extensions/src/shapes/Line.d.ts +32 -86
  25. package/dist-extensions/src/shapes/Line.d.ts.map +1 -1
  26. package/fabric-test-editor.html +157 -8
  27. package/fabric-test2.html +513 -0
  28. package/fabric.ts +182 -182
  29. package/package.json +1 -1
  30. package/src/shapes/Line.ts +397 -164
  31. package/debug/konva/CHANGELOG.md +0 -1474
  32. package/debug/konva/LICENSE +0 -22
  33. package/debug/konva/README.md +0 -205
  34. package/debug/konva/gulpfile.mjs +0 -110
  35. package/debug/konva/package.json +0 -139
  36. package/debug/konva/release.sh +0 -65
  37. package/debug/konva/resources/doc-includes/ContainerParams.txt +0 -6
  38. package/debug/konva/resources/doc-includes/NodeParams.txt +0 -20
  39. package/debug/konva/resources/doc-includes/ShapeParams.txt +0 -53
  40. package/debug/konva/resources/jsdoc.conf.json +0 -28
  41. package/debug/konva/rollup.config.mjs +0 -32
  42. package/debug/konva/src/Animation.ts +0 -237
  43. package/debug/konva/src/BezierFunctions.ts +0 -826
  44. package/debug/konva/src/Canvas.ts +0 -193
  45. package/debug/konva/src/Container.ts +0 -649
  46. package/debug/konva/src/Context.ts +0 -1017
  47. package/debug/konva/src/Core.ts +0 -5
  48. package/debug/konva/src/DragAndDrop.ts +0 -173
  49. package/debug/konva/src/Factory.ts +0 -246
  50. package/debug/konva/src/FastLayer.ts +0 -29
  51. package/debug/konva/src/Global.ts +0 -210
  52. package/debug/konva/src/Group.ts +0 -31
  53. package/debug/konva/src/Layer.ts +0 -546
  54. package/debug/konva/src/Node.ts +0 -3477
  55. package/debug/konva/src/PointerEvents.ts +0 -67
  56. package/debug/konva/src/Shape.ts +0 -2081
  57. package/debug/konva/src/Stage.ts +0 -1000
  58. package/debug/konva/src/Tween.ts +0 -811
  59. package/debug/konva/src/Util.ts +0 -1123
  60. package/debug/konva/src/Validators.ts +0 -210
  61. package/debug/konva/src/_CoreInternals.ts +0 -85
  62. package/debug/konva/src/_FullInternals.ts +0 -171
  63. package/debug/konva/src/canvas-backend.ts +0 -36
  64. package/debug/konva/src/filters/Blur.ts +0 -388
  65. package/debug/konva/src/filters/Brighten.ts +0 -48
  66. package/debug/konva/src/filters/Brightness.ts +0 -30
  67. package/debug/konva/src/filters/Contrast.ts +0 -75
  68. package/debug/konva/src/filters/Emboss.ts +0 -207
  69. package/debug/konva/src/filters/Enhance.ts +0 -154
  70. package/debug/konva/src/filters/Grayscale.ts +0 -25
  71. package/debug/konva/src/filters/HSL.ts +0 -108
  72. package/debug/konva/src/filters/HSV.ts +0 -106
  73. package/debug/konva/src/filters/Invert.ts +0 -23
  74. package/debug/konva/src/filters/Kaleidoscope.ts +0 -274
  75. package/debug/konva/src/filters/Mask.ts +0 -220
  76. package/debug/konva/src/filters/Noise.ts +0 -44
  77. package/debug/konva/src/filters/Pixelate.ts +0 -107
  78. package/debug/konva/src/filters/Posterize.ts +0 -46
  79. package/debug/konva/src/filters/RGB.ts +0 -82
  80. package/debug/konva/src/filters/RGBA.ts +0 -103
  81. package/debug/konva/src/filters/Sepia.ts +0 -27
  82. package/debug/konva/src/filters/Solarize.ts +0 -29
  83. package/debug/konva/src/filters/Threshold.ts +0 -44
  84. package/debug/konva/src/index.ts +0 -3
  85. package/debug/konva/src/shapes/Arc.ts +0 -176
  86. package/debug/konva/src/shapes/Arrow.ts +0 -231
  87. package/debug/konva/src/shapes/Circle.ts +0 -76
  88. package/debug/konva/src/shapes/Ellipse.ts +0 -121
  89. package/debug/konva/src/shapes/Image.ts +0 -319
  90. package/debug/konva/src/shapes/Label.ts +0 -386
  91. package/debug/konva/src/shapes/Line.ts +0 -364
  92. package/debug/konva/src/shapes/Path.ts +0 -1013
  93. package/debug/konva/src/shapes/Rect.ts +0 -79
  94. package/debug/konva/src/shapes/RegularPolygon.ts +0 -167
  95. package/debug/konva/src/shapes/Ring.ts +0 -94
  96. package/debug/konva/src/shapes/Sprite.ts +0 -370
  97. package/debug/konva/src/shapes/Star.ts +0 -125
  98. package/debug/konva/src/shapes/Text.ts +0 -1065
  99. package/debug/konva/src/shapes/TextPath.ts +0 -583
  100. package/debug/konva/src/shapes/Transformer.ts +0 -1889
  101. package/debug/konva/src/shapes/Wedge.ts +0 -129
  102. package/debug/konva/src/skia-backend.ts +0 -35
  103. package/debug/konva/src/types.ts +0 -84
  104. package/debug/konva/tsconfig.json +0 -31
  105. package/debug/konva/tsconfig.test.json +0 -7
@@ -1,1017 +0,0 @@
1
- import { Util } from './Util.ts';
2
- import { Konva } from './Global.ts';
3
- import type { Canvas } from './Canvas.ts';
4
- import type { Shape } from './Shape.ts';
5
- import type { IRect } from './types.ts';
6
- import type { Node } from './Node.ts';
7
-
8
- function simplifyArray(arr: Array<any>) {
9
- const retArr: Array<any> = [],
10
- len = arr.length,
11
- util = Util;
12
-
13
- for (let n = 0; n < len; n++) {
14
- let val = arr[n];
15
- if (util._isNumber(val)) {
16
- val = Math.round(val * 1000) / 1000;
17
- } else if (!util._isString(val)) {
18
- val = val + '';
19
- }
20
-
21
- retArr.push(val);
22
- }
23
-
24
- return retArr;
25
- }
26
-
27
- const COMMA = ',',
28
- OPEN_PAREN = '(',
29
- CLOSE_PAREN = ')',
30
- OPEN_PAREN_BRACKET = '([',
31
- CLOSE_BRACKET_PAREN = '])',
32
- SEMICOLON = ';',
33
- DOUBLE_PAREN = '()',
34
- // EMPTY_STRING = '',
35
- EQUALS = '=',
36
- // SET = 'set',
37
- CONTEXT_METHODS = [
38
- 'arc',
39
- 'arcTo',
40
- 'beginPath',
41
- 'bezierCurveTo',
42
- 'clearRect',
43
- 'clip',
44
- 'closePath',
45
- 'createLinearGradient',
46
- 'createPattern',
47
- 'createRadialGradient',
48
- 'drawImage',
49
- 'ellipse',
50
- 'fill',
51
- 'fillText',
52
- 'getImageData',
53
- 'createImageData',
54
- 'lineTo',
55
- 'moveTo',
56
- 'putImageData',
57
- 'quadraticCurveTo',
58
- 'rect',
59
- 'roundRect',
60
- 'restore',
61
- 'rotate',
62
- 'save',
63
- 'scale',
64
- 'setLineDash',
65
- 'setTransform',
66
- 'stroke',
67
- 'strokeText',
68
- 'transform',
69
- 'translate',
70
- ];
71
-
72
- const CONTEXT_PROPERTIES = [
73
- 'fillStyle',
74
- 'strokeStyle',
75
- 'shadowColor',
76
- 'shadowBlur',
77
- 'shadowOffsetX',
78
- 'shadowOffsetY',
79
- 'letterSpacing',
80
- 'lineCap',
81
- 'lineDashOffset',
82
- 'lineJoin',
83
- 'lineWidth',
84
- 'miterLimit',
85
- 'direction',
86
- 'font',
87
- 'textAlign',
88
- 'textBaseline',
89
- 'globalAlpha',
90
- 'globalCompositeOperation',
91
- 'imageSmoothingEnabled',
92
- 'filter',
93
- ] as const;
94
-
95
- const traceArrMax = 100;
96
-
97
- // Check if CSS filters are supported in the current browser
98
- let _cssFiltersSupported: boolean | null = null;
99
- export function isCSSFiltersSupported(): boolean {
100
- if (_cssFiltersSupported !== null) {
101
- return _cssFiltersSupported;
102
- }
103
-
104
- try {
105
- const canvas = Util.createCanvasElement();
106
- const ctx = canvas.getContext('2d');
107
- if (!ctx) {
108
- _cssFiltersSupported = false;
109
- return false;
110
- }
111
- return !!ctx && 'filter' in ctx;
112
- } catch (e) {
113
- _cssFiltersSupported = false;
114
- return false;
115
- }
116
- }
117
-
118
- interface ExtendedCanvasRenderingContext2D extends CanvasRenderingContext2D {
119
- letterSpacing: string;
120
- }
121
-
122
- /**
123
- * Konva wrapper around native 2d canvas context. It has almost the same API of 2d context with some additional functions.
124
- * With core Konva shapes you don't need to use this object. But you will use it if you want to create
125
- * a [custom shape](/docs/react/Custom_Shape.html) or a [custom hit regions](/docs/events/Custom_Hit_Region.html).
126
- * For full information about each 2d context API use [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D)
127
- * @constructor
128
- * @memberof Konva
129
- * @example
130
- * const rect = new Konva.Shape({
131
- * fill: 'red',
132
- * width: 100,
133
- * height: 100,
134
- * sceneFunc: (ctx, shape) => {
135
- * // ctx - is context wrapper
136
- * // shape - is instance of Konva.Shape, so it equals to "rect" variable
137
- * ctx.rect(0, 0, shape.getAttr('width'), shape.getAttr('height'));
138
- *
139
- * // automatically fill shape from props and draw hit region
140
- * ctx.fillStrokeShape(shape);
141
- * }
142
- * })
143
- */
144
- export class Context {
145
- canvas: Canvas;
146
- _context: CanvasRenderingContext2D;
147
- traceArr: Array<string>;
148
-
149
- constructor(canvas: Canvas) {
150
- this.canvas = canvas;
151
-
152
- if (Konva.enableTrace) {
153
- this.traceArr = [];
154
- this._enableTrace();
155
- }
156
- }
157
-
158
- /**
159
- * fill shape
160
- * @method
161
- * @name Konva.Context#fillShape
162
- * @param {Konva.Shape} shape
163
- */
164
- fillShape(shape: Shape) {
165
- if (shape.fillEnabled()) {
166
- this._fill(shape);
167
- }
168
- }
169
-
170
- _fill(shape: Shape) {
171
- // abstract
172
- }
173
- /**
174
- * stroke shape
175
- * @method
176
- * @name Konva.Context#strokeShape
177
- * @param {Konva.Shape} shape
178
- */
179
- strokeShape(shape: Shape) {
180
- if (shape.hasStroke()) {
181
- this._stroke(shape);
182
- }
183
- }
184
-
185
- _stroke(shape: Shape) {
186
- // abstract
187
- }
188
-
189
- /**
190
- * fill then stroke
191
- * @method
192
- * @name Konva.Context#fillStrokeShape
193
- * @param {Konva.Shape} shape
194
- */
195
- fillStrokeShape(shape: Shape) {
196
- if (shape.attrs.fillAfterStrokeEnabled) {
197
- this.strokeShape(shape);
198
- this.fillShape(shape);
199
- } else {
200
- this.fillShape(shape);
201
- this.strokeShape(shape);
202
- }
203
- }
204
-
205
- getTrace(relaxed?: boolean, rounded?: boolean) {
206
- let traceArr = this.traceArr,
207
- len = traceArr.length,
208
- str = '',
209
- n,
210
- trace,
211
- method,
212
- args;
213
-
214
- for (n = 0; n < len; n++) {
215
- trace = traceArr[n];
216
- method = trace.method;
217
-
218
- // methods
219
- if (method) {
220
- args = trace.args;
221
- str += method;
222
- if (relaxed) {
223
- str += DOUBLE_PAREN;
224
- } else {
225
- if (Util._isArray(args[0])) {
226
- str += OPEN_PAREN_BRACKET + args.join(COMMA) + CLOSE_BRACKET_PAREN;
227
- } else {
228
- if (rounded) {
229
- args = args.map((a) =>
230
- typeof a === 'number' ? Math.floor(a) : a
231
- );
232
- }
233
- str += OPEN_PAREN + args.join(COMMA) + CLOSE_PAREN;
234
- }
235
- }
236
- } else {
237
- // properties
238
- str += trace.property;
239
- if (!relaxed) {
240
- str += EQUALS + trace.val;
241
- }
242
- }
243
-
244
- str += SEMICOLON;
245
- }
246
-
247
- return str;
248
- }
249
-
250
- clearTrace() {
251
- this.traceArr = [];
252
- }
253
- _trace(str) {
254
- let traceArr = this.traceArr,
255
- len;
256
-
257
- traceArr.push(str);
258
- len = traceArr.length;
259
-
260
- if (len >= traceArrMax) {
261
- traceArr.shift();
262
- }
263
- }
264
- /**
265
- * reset canvas context transform
266
- * @method
267
- * @name Konva.Context#reset
268
- */
269
- reset() {
270
- const pixelRatio = this.getCanvas().getPixelRatio();
271
- this.setTransform(1 * pixelRatio, 0, 0, 1 * pixelRatio, 0, 0);
272
- }
273
- /**
274
- * get canvas wrapper
275
- * @method
276
- * @name Konva.Context#getCanvas
277
- * @returns {Konva.Canvas}
278
- */
279
- getCanvas() {
280
- return this.canvas;
281
- }
282
- /**
283
- * clear canvas
284
- * @method
285
- * @name Konva.Context#clear
286
- * @param {Object} [bounds]
287
- * @param {Number} [bounds.x]
288
- * @param {Number} [bounds.y]
289
- * @param {Number} [bounds.width]
290
- * @param {Number} [bounds.height]
291
- */
292
- clear(bounds?: IRect) {
293
- const canvas = this.getCanvas();
294
-
295
- if (bounds) {
296
- this.clearRect(
297
- bounds.x || 0,
298
- bounds.y || 0,
299
- bounds.width || 0,
300
- bounds.height || 0
301
- );
302
- } else {
303
- this.clearRect(
304
- 0,
305
- 0,
306
- canvas.getWidth() / canvas.pixelRatio,
307
- canvas.getHeight() / canvas.pixelRatio
308
- );
309
- }
310
- }
311
- _applyLineCap(shape: Shape) {
312
- const lineCap = shape.attrs.lineCap;
313
- if (lineCap) {
314
- this.setAttr('lineCap', lineCap);
315
- }
316
- }
317
- _applyOpacity(shape: Node) {
318
- const absOpacity = shape.getAbsoluteOpacity();
319
- if (absOpacity !== 1) {
320
- this.setAttr('globalAlpha', absOpacity);
321
- }
322
- }
323
- _applyLineJoin(shape: Shape) {
324
- const lineJoin = shape.attrs.lineJoin;
325
- if (lineJoin) {
326
- this.setAttr('lineJoin', lineJoin);
327
- }
328
- }
329
- _applyMiterLimit(shape: Shape) {
330
- const miterLimit = shape.attrs.miterLimit;
331
- if (miterLimit != null) {
332
- this.setAttr('miterLimit', miterLimit);
333
- }
334
- }
335
-
336
- setAttr(attr: string, val) {
337
- this._context[attr] = val;
338
- }
339
-
340
- /**
341
- * arc function.
342
- * @method
343
- * @name Konva.Context#arc
344
- */
345
- arc(
346
- x: number,
347
- y: number,
348
- radius: number,
349
- startAngle: number,
350
- endAngle: number,
351
- counterClockwise?: boolean
352
- ) {
353
- this._context.arc(x, y, radius, startAngle, endAngle, counterClockwise);
354
- }
355
- /**
356
- * arcTo function.
357
- * @method
358
- * @name Konva.Context#arcTo
359
- *
360
- */
361
- arcTo(x1: number, y1: number, x2: number, y2: number, radius: number) {
362
- this._context.arcTo(x1, y1, x2, y2, radius);
363
- }
364
- /**
365
- * beginPath function.
366
- * @method
367
- * @name Konva.Context#beginPath
368
- */
369
- beginPath() {
370
- this._context.beginPath();
371
- }
372
- /**
373
- * bezierCurveTo function.
374
- * @method
375
- * @name Konva.Context#bezierCurveTo
376
- */
377
-
378
- bezierCurveTo(
379
- cp1x: number,
380
- cp1y: number,
381
- cp2x: number,
382
- cp2y: number,
383
- x: number,
384
- y: number
385
- ) {
386
- this._context.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);
387
- }
388
- /**
389
- * clearRect function.
390
- * @method
391
- * @name Konva.Context#clearRect
392
- */
393
- clearRect(x: number, y: number, width: number, height: number) {
394
- this._context.clearRect(x, y, width, height);
395
- }
396
- /**
397
- * clip function.
398
- * @method
399
- * @name Konva.Context#clip
400
- */
401
- clip(fillRule?: CanvasFillRule): void;
402
- clip(path: Path2D, fillRule?: CanvasFillRule): void;
403
- clip(...args: any[]) {
404
- this._context.clip.apply(this._context, args as any);
405
- }
406
- /**
407
- * closePath function.
408
- * @method
409
- * @name Konva.Context#closePath
410
- */
411
- closePath() {
412
- this._context.closePath();
413
- }
414
- /**
415
- * createImageData function.
416
- * @method
417
- * @name Konva.Context#createImageData
418
- */
419
- createImageData(width, height) {
420
- const a = arguments;
421
- if (a.length === 2) {
422
- return this._context.createImageData(width, height);
423
- } else if (a.length === 1) {
424
- return this._context.createImageData(width);
425
- }
426
- }
427
- /**
428
- * createLinearGradient function.
429
- * @method
430
- * @name Konva.Context#createLinearGradient
431
- */
432
- createLinearGradient(x0: number, y0: number, x1: number, y1: number) {
433
- return this._context.createLinearGradient(x0, y0, x1, y1);
434
- }
435
- /**
436
- * createPattern function.
437
- * @method
438
- * @name Konva.Context#createPattern
439
- */
440
- createPattern(image: CanvasImageSource, repetition: string | null) {
441
- return this._context.createPattern(image, repetition);
442
- }
443
- /**
444
- * createRadialGradient function.
445
- * @method
446
- * @name Konva.Context#createRadialGradient
447
- */
448
- createRadialGradient(
449
- x0: number,
450
- y0: number,
451
- r0: number,
452
- x1: number,
453
- y1: number,
454
- r1: number
455
- ) {
456
- return this._context.createRadialGradient(x0, y0, r0, x1, y1, r1);
457
- }
458
- /**
459
- * drawImage function.
460
- * @method
461
- * @name Konva.Context#drawImage
462
- */
463
- drawImage(
464
- image: CanvasImageSource,
465
- sx: number,
466
- sy: number,
467
- sWidth?: number,
468
- sHeight?: number,
469
- dx?: number,
470
- dy?: number,
471
- dWidth?: number,
472
- dHeight?: number
473
- ) {
474
- // this._context.drawImage(...arguments);
475
- const a = arguments,
476
- _context = this._context;
477
- if (a.length === 3) {
478
- _context.drawImage(image, sx, sy);
479
- } else if (a.length === 5) {
480
- _context.drawImage(image, sx, sy, sWidth as number, sHeight as number);
481
- } else if (a.length === 9) {
482
- _context.drawImage(
483
- image,
484
- sx,
485
- sy,
486
- sWidth as number,
487
- sHeight as number,
488
- dx as number,
489
- dy as number,
490
- dWidth as number,
491
- dHeight as number
492
- );
493
- }
494
- }
495
- /**
496
- * ellipse function.
497
- * @method
498
- * @name Konva.Context#ellipse
499
- */
500
- ellipse(
501
- x: number,
502
- y: number,
503
- radiusX: number,
504
- radiusY: number,
505
- rotation: number,
506
- startAngle: number,
507
- endAngle: number,
508
- counterclockwise?: boolean
509
- ) {
510
- this._context.ellipse(
511
- x,
512
- y,
513
- radiusX,
514
- radiusY,
515
- rotation,
516
- startAngle,
517
- endAngle,
518
- counterclockwise
519
- );
520
- }
521
- /**
522
- * isPointInPath function.
523
- * @method
524
- * @name Konva.Context#isPointInPath
525
- */
526
- isPointInPath(
527
- x: number,
528
- y: number,
529
- path?: Path2D,
530
- fillRule?: CanvasFillRule
531
- ) {
532
- if (path) {
533
- return this._context.isPointInPath(path, x, y, fillRule);
534
- }
535
- return this._context.isPointInPath(x, y, fillRule);
536
- }
537
- /**
538
- * fill function.
539
- * @method
540
- * @name Konva.Context#fill
541
- */
542
- fill(fillRule?: CanvasFillRule): void;
543
- fill(path: Path2D, fillRule?: CanvasFillRule): void;
544
- fill(...args: any[]) {
545
- // this._context.fill();
546
- this._context.fill.apply(this._context, args as any);
547
- }
548
- /**
549
- * fillRect function.
550
- * @method
551
- * @name Konva.Context#fillRect
552
- */
553
- fillRect(x: number, y: number, width: number, height: number) {
554
- this._context.fillRect(x, y, width, height);
555
- }
556
- /**
557
- * strokeRect function.
558
- * @method
559
- * @name Konva.Context#strokeRect
560
- */
561
- strokeRect(x: number, y: number, width: number, height: number) {
562
- this._context.strokeRect(x, y, width, height);
563
- }
564
- /**
565
- * fillText function.
566
- * @method
567
- * @name Konva.Context#fillText
568
- */
569
- fillText(text: string, x: number, y: number, maxWidth?: number) {
570
- if (maxWidth) {
571
- this._context.fillText(text, x, y, maxWidth);
572
- } else {
573
- this._context.fillText(text, x, y);
574
- }
575
- }
576
- /**
577
- * measureText function.
578
- * @method
579
- * @name Konva.Context#measureText
580
- */
581
- measureText(text: string) {
582
- return this._context.measureText(text);
583
- }
584
- /**
585
- * getImageData function.
586
- * @method
587
- * @name Konva.Context#getImageData
588
- */
589
- getImageData(sx: number, sy: number, sw: number, sh: number) {
590
- return this._context.getImageData(sx, sy, sw, sh);
591
- }
592
- /**
593
- * lineTo function.
594
- * @method
595
- * @name Konva.Context#lineTo
596
- */
597
- lineTo(x: number, y: number) {
598
- this._context.lineTo(x, y);
599
- }
600
- /**
601
- * moveTo function.
602
- * @method
603
- * @name Konva.Context#moveTo
604
- */
605
- moveTo(x: number, y: number) {
606
- this._context.moveTo(x, y);
607
- }
608
- /**
609
- * rect function.
610
- * @method
611
- * @name Konva.Context#rect
612
- */
613
- rect(x: number, y: number, width: number, height: number) {
614
- this._context.rect(x, y, width, height);
615
- }
616
- /**
617
- * roundRect function.
618
- * @method
619
- * @name Konva.Context#roundRect
620
- */
621
- roundRect(
622
- x: number,
623
- y: number,
624
- width: number,
625
- height: number,
626
- radii: number | DOMPointInit | (number | DOMPointInit)[]
627
- ) {
628
- this._context.roundRect(x, y, width, height, radii);
629
- }
630
- /**
631
- * putImageData function.
632
- * @method
633
- * @name Konva.Context#putImageData
634
- */
635
- putImageData(imageData: ImageData, dx: number, dy: number) {
636
- this._context.putImageData(imageData, dx, dy);
637
- }
638
- /**
639
- * quadraticCurveTo function.
640
- * @method
641
- * @name Konva.Context#quadraticCurveTo
642
- */
643
- quadraticCurveTo(cpx: number, cpy: number, x: number, y: number) {
644
- this._context.quadraticCurveTo(cpx, cpy, x, y);
645
- }
646
- /**
647
- * restore function.
648
- * @method
649
- * @name Konva.Context#restore
650
- */
651
- restore() {
652
- this._context.restore();
653
- }
654
- /**
655
- * rotate function.
656
- * @method
657
- * @name Konva.Context#rotate
658
- */
659
- rotate(angle: number) {
660
- this._context.rotate(angle);
661
- }
662
- /**
663
- * save function.
664
- * @method
665
- * @name Konva.Context#save
666
- */
667
- save() {
668
- this._context.save();
669
- }
670
- /**
671
- * scale function.
672
- * @method
673
- * @name Konva.Context#scale
674
- */
675
- scale(x: number, y: number) {
676
- this._context.scale(x, y);
677
- }
678
- /**
679
- * setLineDash function.
680
- * @method
681
- * @name Konva.Context#setLineDash
682
- */
683
- setLineDash(segments: number[]) {
684
- // works for Chrome and IE11
685
- if (this._context.setLineDash) {
686
- this._context.setLineDash(segments);
687
- } else if ('mozDash' in this._context) {
688
- // verified that this works in firefox
689
- (this._context as any)['mozDash'] = segments;
690
- } else if ('webkitLineDash' in this._context) {
691
- // does not currently work for Safari
692
- (this._context as any)['webkitLineDash'] = segments;
693
- }
694
-
695
- // no support for IE9 and IE10
696
- }
697
- /**
698
- * getLineDash function.
699
- * @method
700
- * @name Konva.Context#getLineDash
701
- */
702
- getLineDash() {
703
- return this._context.getLineDash();
704
- }
705
- /**
706
- * setTransform function.
707
- * @method
708
- * @name Konva.Context#setTransform
709
- */
710
- setTransform(
711
- a: number,
712
- b: number,
713
- c: number,
714
- d: number,
715
- e: number,
716
- f: number
717
- ) {
718
- this._context.setTransform(a, b, c, d, e, f);
719
- }
720
- /**
721
- * stroke function.
722
- * @method
723
- * @name Konva.Context#stroke
724
- */
725
- stroke(path2d?: Path2D) {
726
- if (path2d) {
727
- this._context.stroke(path2d);
728
- } else {
729
- this._context.stroke();
730
- }
731
- }
732
- /**
733
- * strokeText function.
734
- * @method
735
- * @name Konva.Context#strokeText
736
- */
737
- strokeText(text: string, x: number, y: number, maxWidth?: number) {
738
- this._context.strokeText(text, x, y, maxWidth);
739
- }
740
- /**
741
- * transform function.
742
- * @method
743
- * @name Konva.Context#transform
744
- */
745
- transform(a: number, b: number, c: number, d: number, e: number, f: number) {
746
- this._context.transform(a, b, c, d, e, f);
747
- }
748
- /**
749
- * translate function.
750
- * @method
751
- * @name Konva.Context#translate
752
- */
753
- translate(x: number, y: number) {
754
- this._context.translate(x, y);
755
- }
756
- _enableTrace() {
757
- let that = this,
758
- len = CONTEXT_METHODS.length,
759
- origSetter = this.setAttr,
760
- n,
761
- args;
762
-
763
- // to prevent creating scope function at each loop
764
- const func = function (methodName) {
765
- let origMethod = that[methodName],
766
- ret;
767
-
768
- that[methodName] = function () {
769
- args = simplifyArray(Array.prototype.slice.call(arguments, 0));
770
- ret = origMethod.apply(that, arguments);
771
-
772
- that._trace({
773
- method: methodName,
774
- args: args,
775
- });
776
-
777
- return ret;
778
- };
779
- };
780
- // methods
781
- for (n = 0; n < len; n++) {
782
- func(CONTEXT_METHODS[n]);
783
- }
784
-
785
- // attrs
786
- that.setAttr = function () {
787
- origSetter.apply(that, arguments as any);
788
- const prop = arguments[0];
789
- let val = arguments[1];
790
- if (
791
- prop === 'shadowOffsetX' ||
792
- prop === 'shadowOffsetY' ||
793
- prop === 'shadowBlur'
794
- ) {
795
- val = val / this.canvas.getPixelRatio();
796
- }
797
- that._trace({
798
- property: prop,
799
- val: val,
800
- });
801
- };
802
- }
803
- _applyGlobalCompositeOperation(node) {
804
- const op = node.attrs.globalCompositeOperation;
805
- const def = !op || op === 'source-over';
806
- if (!def) {
807
- this.setAttr('globalCompositeOperation', op);
808
- }
809
- }
810
- }
811
-
812
- // supported context properties
813
- type CanvasContextProps = Pick<
814
- ExtendedCanvasRenderingContext2D,
815
- (typeof CONTEXT_PROPERTIES)[number]
816
- >;
817
-
818
- export interface Context extends CanvasContextProps {}
819
-
820
- CONTEXT_PROPERTIES.forEach(function (prop) {
821
- Object.defineProperty(Context.prototype, prop, {
822
- get() {
823
- return this._context[prop];
824
- },
825
- set(val) {
826
- this._context[prop] = val;
827
- },
828
- });
829
- });
830
-
831
- export class SceneContext extends Context {
832
- constructor(canvas: Canvas, { willReadFrequently = false } = {}) {
833
- super(canvas);
834
- this._context = canvas._canvas.getContext('2d', {
835
- willReadFrequently,
836
- }) as CanvasRenderingContext2D;
837
- }
838
- _fillColor(shape: Shape) {
839
- const fill = shape.fill();
840
-
841
- this.setAttr('fillStyle', fill);
842
- shape._fillFunc(this);
843
- }
844
- _fillPattern(shape: Shape) {
845
- this.setAttr('fillStyle', shape._getFillPattern());
846
- shape._fillFunc(this);
847
- }
848
- _fillLinearGradient(shape: Shape) {
849
- const grd = shape._getLinearGradient();
850
-
851
- if (grd) {
852
- this.setAttr('fillStyle', grd);
853
- shape._fillFunc(this);
854
- }
855
- }
856
- _fillRadialGradient(shape: Shape) {
857
- const grd = shape._getRadialGradient();
858
- if (grd) {
859
- this.setAttr('fillStyle', grd);
860
- shape._fillFunc(this);
861
- }
862
- }
863
- _fill(shape) {
864
- const hasColor = shape.fill(),
865
- fillPriority = shape.getFillPriority();
866
-
867
- // priority fills
868
- if (hasColor && fillPriority === 'color') {
869
- this._fillColor(shape);
870
- return;
871
- }
872
-
873
- const hasPattern = shape.getFillPatternImage();
874
- if (hasPattern && fillPriority === 'pattern') {
875
- this._fillPattern(shape);
876
- return;
877
- }
878
-
879
- const hasLinearGradient = shape.getFillLinearGradientColorStops();
880
- if (hasLinearGradient && fillPriority === 'linear-gradient') {
881
- this._fillLinearGradient(shape);
882
- return;
883
- }
884
-
885
- const hasRadialGradient = shape.getFillRadialGradientColorStops();
886
- if (hasRadialGradient && fillPriority === 'radial-gradient') {
887
- this._fillRadialGradient(shape);
888
- return;
889
- }
890
-
891
- // now just try and fill with whatever is available
892
- if (hasColor) {
893
- this._fillColor(shape);
894
- } else if (hasPattern) {
895
- this._fillPattern(shape);
896
- } else if (hasLinearGradient) {
897
- this._fillLinearGradient(shape);
898
- } else if (hasRadialGradient) {
899
- this._fillRadialGradient(shape);
900
- }
901
- }
902
- _strokeLinearGradient(shape) {
903
- const start = shape.getStrokeLinearGradientStartPoint(),
904
- end = shape.getStrokeLinearGradientEndPoint(),
905
- colorStops = shape.getStrokeLinearGradientColorStops(),
906
- grd = this.createLinearGradient(start.x, start.y, end.x, end.y);
907
-
908
- if (colorStops) {
909
- // build color stops
910
- for (let n = 0; n < colorStops.length; n += 2) {
911
- grd.addColorStop(colorStops[n] as number, colorStops[n + 1] as string);
912
- }
913
- this.setAttr('strokeStyle', grd);
914
- }
915
- }
916
- _stroke(shape) {
917
- const dash = shape.dash(),
918
- // ignore strokeScaleEnabled for Text
919
- strokeScaleEnabled = shape.getStrokeScaleEnabled();
920
-
921
- if (shape.hasStroke()) {
922
- if (!strokeScaleEnabled) {
923
- this.save();
924
- const pixelRatio = this.getCanvas().getPixelRatio();
925
- this.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0);
926
- }
927
-
928
- this._applyLineCap(shape);
929
- if (dash && shape.dashEnabled()) {
930
- this.setLineDash(dash);
931
- this.setAttr('lineDashOffset', shape.dashOffset());
932
- }
933
-
934
- this.setAttr('lineWidth', shape.strokeWidth());
935
-
936
- if (!shape.getShadowForStrokeEnabled()) {
937
- this.setAttr('shadowColor', 'rgba(0,0,0,0)');
938
- }
939
-
940
- const hasLinearGradient = shape.getStrokeLinearGradientColorStops();
941
- if (hasLinearGradient) {
942
- this._strokeLinearGradient(shape);
943
- } else {
944
- this.setAttr('strokeStyle', shape.stroke());
945
- }
946
-
947
- shape._strokeFunc(this);
948
-
949
- if (!strokeScaleEnabled) {
950
- this.restore();
951
- }
952
- }
953
- }
954
- _applyShadow(shape) {
955
- const color = shape.getShadowRGBA() ?? 'black',
956
- blur = shape.getShadowBlur() ?? 5,
957
- offset = shape.getShadowOffset() ?? {
958
- x: 0,
959
- y: 0,
960
- },
961
- scale = shape.getAbsoluteScale(),
962
- ratio = this.canvas.getPixelRatio(),
963
- scaleX = scale.x * ratio,
964
- scaleY = scale.y * ratio;
965
-
966
- this.setAttr('shadowColor', color);
967
- this.setAttr(
968
- 'shadowBlur',
969
- blur * Math.min(Math.abs(scaleX), Math.abs(scaleY))
970
- );
971
- this.setAttr('shadowOffsetX', offset.x * scaleX);
972
- this.setAttr('shadowOffsetY', offset.y * scaleY);
973
- }
974
- }
975
-
976
- export class HitContext extends Context {
977
- constructor(canvas: Canvas) {
978
- super(canvas);
979
- this._context = canvas._canvas.getContext('2d', {
980
- willReadFrequently: true,
981
- }) as CanvasRenderingContext2D;
982
- }
983
- _fill(shape: Shape) {
984
- this.save();
985
- this.setAttr('fillStyle', shape.colorKey);
986
- shape._fillFuncHit(this);
987
- this.restore();
988
- }
989
- strokeShape(shape: Shape) {
990
- if (shape.hasHitStroke()) {
991
- this._stroke(shape);
992
- }
993
- }
994
- _stroke(shape) {
995
- if (shape.hasHitStroke()) {
996
- // ignore strokeScaleEnabled for Text
997
- const strokeScaleEnabled = shape.getStrokeScaleEnabled();
998
- if (!strokeScaleEnabled) {
999
- this.save();
1000
- const pixelRatio = this.getCanvas().getPixelRatio();
1001
- this.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0);
1002
- }
1003
- this._applyLineCap(shape);
1004
-
1005
- const hitStrokeWidth = shape.hitStrokeWidth();
1006
- const strokeWidth =
1007
- hitStrokeWidth === 'auto' ? shape.strokeWidth() : hitStrokeWidth;
1008
-
1009
- this.setAttr('lineWidth', strokeWidth);
1010
- this.setAttr('strokeStyle', shape.colorKey);
1011
- shape._strokeFuncHit(this);
1012
- if (!strokeScaleEnabled) {
1013
- this.restore();
1014
- }
1015
- }
1016
- }
1017
- }