@nasser-sw/fabric 7.0.1-beta8 → 7.0.1-beta9
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/debug/konva-master/CHANGELOG.md +1475 -0
- package/debug/konva-master/LICENSE +22 -0
- package/debug/konva-master/README.md +209 -0
- package/debug/konva-master/gulpfile.mjs +110 -0
- package/debug/konva-master/package.json +139 -0
- package/debug/konva-master/release.sh +62 -0
- package/debug/konva-master/resources/doc-includes/ContainerParams.txt +6 -0
- package/debug/konva-master/resources/doc-includes/NodeParams.txt +20 -0
- package/debug/konva-master/resources/doc-includes/ShapeParams.txt +53 -0
- package/debug/konva-master/resources/jsdoc.conf.json +28 -0
- package/debug/konva-master/rollup.config.mjs +32 -0
- package/debug/konva-master/src/Animation.ts +237 -0
- package/debug/konva-master/src/BezierFunctions.ts +826 -0
- package/debug/konva-master/src/Canvas.ts +230 -0
- package/debug/konva-master/src/Container.ts +649 -0
- package/debug/konva-master/src/Context.ts +1017 -0
- package/debug/konva-master/src/Core.ts +5 -0
- package/debug/konva-master/src/DragAndDrop.ts +173 -0
- package/debug/konva-master/src/Factory.ts +246 -0
- package/debug/konva-master/src/FastLayer.ts +29 -0
- package/debug/konva-master/src/Global.ts +210 -0
- package/debug/konva-master/src/Group.ts +31 -0
- package/debug/konva-master/src/Layer.ts +546 -0
- package/debug/konva-master/src/Node.ts +3477 -0
- package/debug/konva-master/src/PointerEvents.ts +67 -0
- package/debug/konva-master/src/Shape.ts +2081 -0
- package/debug/konva-master/src/Stage.ts +1000 -0
- package/debug/konva-master/src/Tween.ts +811 -0
- package/debug/konva-master/src/Util.ts +1123 -0
- package/debug/konva-master/src/Validators.ts +210 -0
- package/debug/konva-master/src/_CoreInternals.ts +85 -0
- package/debug/konva-master/src/_FullInternals.ts +171 -0
- package/debug/konva-master/src/canvas-backend.ts +36 -0
- package/debug/konva-master/src/filters/Blur.ts +388 -0
- package/debug/konva-master/src/filters/Brighten.ts +48 -0
- package/debug/konva-master/src/filters/Brightness.ts +30 -0
- package/debug/konva-master/src/filters/Contrast.ts +75 -0
- package/debug/konva-master/src/filters/Emboss.ts +207 -0
- package/debug/konva-master/src/filters/Enhance.ts +154 -0
- package/debug/konva-master/src/filters/Grayscale.ts +25 -0
- package/debug/konva-master/src/filters/HSL.ts +108 -0
- package/debug/konva-master/src/filters/HSV.ts +106 -0
- package/debug/konva-master/src/filters/Invert.ts +23 -0
- package/debug/konva-master/src/filters/Kaleidoscope.ts +274 -0
- package/debug/konva-master/src/filters/Mask.ts +220 -0
- package/debug/konva-master/src/filters/Noise.ts +44 -0
- package/debug/konva-master/src/filters/Pixelate.ts +107 -0
- package/debug/konva-master/src/filters/Posterize.ts +46 -0
- package/debug/konva-master/src/filters/RGB.ts +82 -0
- package/debug/konva-master/src/filters/RGBA.ts +103 -0
- package/debug/konva-master/src/filters/Sepia.ts +27 -0
- package/debug/konva-master/src/filters/Solarize.ts +29 -0
- package/debug/konva-master/src/filters/Threshold.ts +44 -0
- package/debug/konva-master/src/index.ts +3 -0
- package/debug/konva-master/src/shapes/Arc.ts +176 -0
- package/debug/konva-master/src/shapes/Arrow.ts +231 -0
- package/debug/konva-master/src/shapes/Circle.ts +76 -0
- package/debug/konva-master/src/shapes/Ellipse.ts +121 -0
- package/debug/konva-master/src/shapes/Image.ts +319 -0
- package/debug/konva-master/src/shapes/Label.ts +386 -0
- package/debug/konva-master/src/shapes/Line.ts +364 -0
- package/debug/konva-master/src/shapes/Path.ts +1013 -0
- package/debug/konva-master/src/shapes/Rect.ts +79 -0
- package/debug/konva-master/src/shapes/RegularPolygon.ts +167 -0
- package/debug/konva-master/src/shapes/Ring.ts +94 -0
- package/debug/konva-master/src/shapes/Sprite.ts +370 -0
- package/debug/konva-master/src/shapes/Star.ts +125 -0
- package/debug/konva-master/src/shapes/Text.ts +1065 -0
- package/debug/konva-master/src/shapes/TextPath.ts +583 -0
- package/debug/konva-master/src/shapes/Transformer.ts +1889 -0
- package/debug/konva-master/src/shapes/Wedge.ts +129 -0
- package/debug/konva-master/src/skia-backend.ts +35 -0
- package/debug/konva-master/src/types.ts +84 -0
- package/debug/konva-master/tsconfig.json +31 -0
- package/debug/konva-master/tsconfig.test.json +7 -0
- package/dist/index.js +915 -23
- package/dist/index.js.map +1 -1
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +1 -1
- package/dist/index.min.mjs +1 -1
- package/dist/index.min.mjs.map +1 -1
- package/dist/index.mjs +915 -23
- package/dist/index.mjs.map +1 -1
- package/dist/index.node.cjs +915 -23
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.mjs +915 -23
- package/dist/index.node.mjs.map +1 -1
- package/dist/package.json.min.mjs +1 -1
- package/dist/package.json.mjs +1 -1
- package/dist/src/shapes/Text/Text.d.ts +19 -0
- package/dist/src/shapes/Text/Text.d.ts.map +1 -1
- package/dist/src/shapes/Text/Text.min.mjs +1 -1
- package/dist/src/shapes/Text/Text.min.mjs.map +1 -1
- package/dist/src/shapes/Text/Text.mjs +238 -4
- package/dist/src/shapes/Text/Text.mjs.map +1 -1
- package/dist/src/shapes/Textbox.d.ts +38 -1
- package/dist/src/shapes/Textbox.d.ts.map +1 -1
- package/dist/src/shapes/Textbox.min.mjs +1 -1
- package/dist/src/shapes/Textbox.min.mjs.map +1 -1
- package/dist/src/shapes/Textbox.mjs +497 -15
- package/dist/src/shapes/Textbox.mjs.map +1 -1
- package/dist/src/text/examples/arabicTextExample.d.ts +60 -0
- package/dist/src/text/examples/arabicTextExample.d.ts.map +1 -0
- package/dist/src/text/measure.d.ts +9 -0
- package/dist/src/text/measure.d.ts.map +1 -1
- package/dist/src/text/measure.min.mjs +1 -1
- package/dist/src/text/measure.min.mjs.map +1 -1
- package/dist/src/text/measure.mjs +175 -4
- package/dist/src/text/measure.mjs.map +1 -1
- package/dist/src/text/overlayEditor.d.ts.map +1 -1
- package/dist/src/text/overlayEditor.min.mjs +1 -1
- package/dist/src/text/overlayEditor.min.mjs.map +1 -1
- package/dist/src/text/overlayEditor.mjs +7 -0
- package/dist/src/text/overlayEditor.mjs.map +1 -1
- package/dist/src/text/scriptUtils.d.ts +142 -0
- package/dist/src/text/scriptUtils.d.ts.map +1 -0
- package/dist/src/text/scriptUtils.min.mjs +2 -0
- package/dist/src/text/scriptUtils.min.mjs.map +1 -0
- package/dist/src/text/scriptUtils.mjs +212 -0
- package/dist/src/text/scriptUtils.mjs.map +1 -0
- package/dist-extensions/src/shapes/Text/Text.d.ts +19 -0
- package/dist-extensions/src/shapes/Text/Text.d.ts.map +1 -1
- package/dist-extensions/src/shapes/Textbox.d.ts +38 -1
- package/dist-extensions/src/shapes/Textbox.d.ts.map +1 -1
- package/dist-extensions/src/text/measure.d.ts +9 -0
- package/dist-extensions/src/text/measure.d.ts.map +1 -1
- package/dist-extensions/src/text/overlayEditor.d.ts.map +1 -1
- package/dist-extensions/src/text/scriptUtils.d.ts +142 -0
- package/dist-extensions/src/text/scriptUtils.d.ts.map +1 -0
- package/fabric-test-editor.html +2401 -46
- package/fonts/STV Bold.ttf +0 -0
- package/fonts/STV Light.ttf +0 -0
- package/fonts/STV Regular.ttf +0 -0
- package/package.json +1 -1
- package/src/shapes/Text/Text.ts +238 -5
- package/src/shapes/Textbox.ts +521 -11
- package/src/text/measure.ts +200 -50
- package/src/text/overlayEditor.ts +7 -0
|
@@ -0,0 +1,1017 @@
|
|
|
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
|
+
}
|