konva 10.8.0 → 10.9.1
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/konva.js +387 -248
- package/konva.min.js +4 -4
- package/lib/Animation.js +13 -10
- package/lib/Container.d.ts +1 -0
- package/lib/Container.js +22 -32
- package/lib/Context.d.ts +1 -0
- package/lib/Context.js +15 -1
- package/lib/Global.js +2 -2
- package/lib/Node.d.ts +8 -3
- package/lib/Node.js +97 -70
- package/lib/Shape.d.ts +5 -1
- package/lib/Shape.js +41 -28
- package/lib/Util.js +2 -0
- package/lib/_CoreInternals.d.ts +26 -0
- package/lib/_CoreInternals.js +26 -0
- package/lib/_FullInternals.d.ts +1 -0
- package/lib/filters/Blur.js +25 -1
- package/lib/filters/Brightness.js +8 -2
- package/lib/filters/Contrast.js +7 -3
- package/lib/filters/Emboss.js +11 -4
- package/lib/shapes/Text.js +4 -4
- package/lib/shapes/TextPath.d.ts +3 -2
- package/lib/shapes/TextPath.js +65 -40
- package/lib/shapes/Transformer.d.ts +5 -3
- package/lib/shapes/Transformer.js +49 -47
- package/package.json +1 -1
package/lib/filters/Blur.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Factory } from "../Factory.js";
|
|
2
|
-
import { Node } from "../Node.js";
|
|
2
|
+
import { Node, _registerCSSFilterFallback } from "../Node.js";
|
|
3
3
|
import { getNumberValidator } from "../Validators.js";
|
|
4
4
|
/*
|
|
5
5
|
the Gauss filter
|
|
@@ -302,6 +302,30 @@ export const Blur = function Blur(imageData, pixelRatio = 1) {
|
|
|
302
302
|
filterGaussBlurRGBA(imageData, radius);
|
|
303
303
|
}
|
|
304
304
|
};
|
|
305
|
+
// CSS blur(σ) where the 2D context has no `filter`. A stack blur of radius 2σ
|
|
306
|
+
// stays within about 10/255 alpha of the native Gaussian in Chromium, Firefox
|
|
307
|
+
// and WebKit. Like the native filter, it has no radius limit and reads the
|
|
308
|
+
// area beyond the image as transparent: a 1px transparent border is what the
|
|
309
|
+
// blur repeats past the edges.
|
|
310
|
+
_registerCSSFilterFallback('blur', (imageData, sigma, pixelRatio) => {
|
|
311
|
+
const radius = Math.round(2 * sigma * pixelRatio);
|
|
312
|
+
if (radius <= 0) {
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
const { width, height, data } = imageData, row = width * 4, framedRow = row + 8, framed = {
|
|
316
|
+
width: width + 2,
|
|
317
|
+
height: height + 2,
|
|
318
|
+
data: new Uint8ClampedArray(framedRow * (height + 2)),
|
|
319
|
+
};
|
|
320
|
+
for (let y = 0; y < height; y++) {
|
|
321
|
+
framed.data.set(data.subarray(y * row, y * row + row), (y + 1) * framedRow + 4);
|
|
322
|
+
}
|
|
323
|
+
filterGaussBlurRGBA(framed, radius);
|
|
324
|
+
for (let y = 0; y < height; y++) {
|
|
325
|
+
const start = (y + 1) * framedRow + 4;
|
|
326
|
+
data.set(framed.data.subarray(start, start + row), y * row);
|
|
327
|
+
}
|
|
328
|
+
});
|
|
305
329
|
Factory.addGetterSetter(Node, 'blurRadius', 0, getNumberValidator(), Factory.afterSetFilter);
|
|
306
330
|
/**
|
|
307
331
|
* get/set blur radius. Use with {@link Konva.Filters.Blur} filter
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { _registerCSSFilterFallback } from "../Node.js";
|
|
1
2
|
// The shared brightness accessor is registered by Brighten.
|
|
2
3
|
import "./Brighten.js";
|
|
3
4
|
/**
|
|
@@ -12,7 +13,12 @@ import "./Brighten.js";
|
|
|
12
13
|
* node.brightness(1.5); // 50% brighter (CSS-compatible)
|
|
13
14
|
*/
|
|
14
15
|
export const Brightness = function (imageData) {
|
|
15
|
-
|
|
16
|
+
brighten(imageData, this.attrs.brightness === undefined ? 1 : this.brightness());
|
|
17
|
+
};
|
|
18
|
+
// CSS brightness(v) is the same multiplier.
|
|
19
|
+
_registerCSSFilterFallback('brightness', brighten);
|
|
20
|
+
function brighten(imageData, brightness) {
|
|
21
|
+
const data = imageData.data, len = data.length;
|
|
16
22
|
for (let i = 0; i < len; i += 4) {
|
|
17
23
|
// red
|
|
18
24
|
data[i] = Math.min(255, data[i] * brightness);
|
|
@@ -21,4 +27,4 @@ export const Brightness = function (imageData) {
|
|
|
21
27
|
// blue
|
|
22
28
|
data[i + 2] = Math.min(255, data[i + 2] * brightness);
|
|
23
29
|
}
|
|
24
|
-
}
|
|
30
|
+
}
|
package/lib/filters/Contrast.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Factory } from "../Factory.js";
|
|
2
|
-
import { Node } from "../Node.js";
|
|
2
|
+
import { Node, _registerCSSFilterFallback } from "../Node.js";
|
|
3
3
|
import { getNumberValidator } from "../Validators.js";
|
|
4
4
|
/**
|
|
5
5
|
* Contrast Filter.
|
|
@@ -12,7 +12,11 @@ import { getNumberValidator } from "../Validators.js";
|
|
|
12
12
|
* node.contrast(10);
|
|
13
13
|
*/
|
|
14
14
|
export const Contrast = function (imageData) {
|
|
15
|
-
|
|
15
|
+
applyContrast(imageData, Math.pow((this.contrast() + 100) / 100, 2));
|
|
16
|
+
};
|
|
17
|
+
// CSS contrast(v) scales the distance from mid-grey by v.
|
|
18
|
+
_registerCSSFilterFallback('contrast', applyContrast);
|
|
19
|
+
function applyContrast(imageData, adjust) {
|
|
16
20
|
const data = imageData.data, nPixels = data.length;
|
|
17
21
|
let red = 150, green = 150, blue = 150;
|
|
18
22
|
for (let i = 0; i < nPixels; i += 4) {
|
|
@@ -44,7 +48,7 @@ export const Contrast = function (imageData) {
|
|
|
44
48
|
data[i + 1] = green;
|
|
45
49
|
data[i + 2] = blue;
|
|
46
50
|
}
|
|
47
|
-
}
|
|
51
|
+
}
|
|
48
52
|
/**
|
|
49
53
|
* get/set filter contrast. The contrast is a number between -100 and 100.
|
|
50
54
|
* Use with {@link Konva.Filters.Contrast} filter.
|
package/lib/filters/Emboss.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Factory } from "../Factory.js";
|
|
2
|
+
import { Util } from "../Util.js";
|
|
2
3
|
import { Node } from "../Node.js";
|
|
3
4
|
import { getNumberValidator } from "../Validators.js";
|
|
4
5
|
/**
|
|
@@ -15,7 +16,7 @@ import { getNumberValidator } from "../Validators.js";
|
|
|
15
16
|
* node.embossBlend(true);
|
|
16
17
|
*/
|
|
17
18
|
export const Emboss = function (imageData) {
|
|
18
|
-
var _a, _b, _c, _d, _e, _f, _g, _h
|
|
19
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
19
20
|
const data = imageData.data;
|
|
20
21
|
const w = imageData.width;
|
|
21
22
|
const h = imageData.height;
|
|
@@ -33,8 +34,13 @@ export const Emboss = function (imageData) {
|
|
|
33
34
|
'bottom-left': 45,
|
|
34
35
|
left: 0,
|
|
35
36
|
};
|
|
36
|
-
const
|
|
37
|
-
|
|
37
|
+
const direction = (_f = (_e = this.embossDirection) === null || _e === void 0 ? void 0 : _e.call(this)) !== null && _f !== void 0 ? _f : 'top-left';
|
|
38
|
+
let directionDeg = directionMap[direction]; // degrees
|
|
39
|
+
if (directionDeg === undefined) {
|
|
40
|
+
Util.warn('Unknown emboss direction: "' + direction + '". Using "top-left".');
|
|
41
|
+
directionDeg = directionMap['top-left'];
|
|
42
|
+
}
|
|
43
|
+
const blend = !!((_h = (_g = this.embossBlend) === null || _g === void 0 ? void 0 : _g.call(this)) !== null && _h !== void 0 ? _h : false);
|
|
38
44
|
// Internal mapping:
|
|
39
45
|
// - "strength" was 0..10; we honor your 0..1 API and scale accordingly.
|
|
40
46
|
// - Sobel directional response is roughly in [-1020..1020] for 8-bit luminance; scale to ~±128.
|
|
@@ -42,7 +48,8 @@ export const Emboss = function (imageData) {
|
|
|
42
48
|
const bias = whiteLevel01 * 255;
|
|
43
49
|
const dirRad = (directionDeg * Math.PI) / 180;
|
|
44
50
|
const cx = Math.cos(dirRad);
|
|
45
|
-
|
|
51
|
+
// the angles above are measured with y up, but image rows go down
|
|
52
|
+
const cy = -Math.sin(dirRad);
|
|
46
53
|
const SCALE = (128 / 1020) * strength; // ≈0.1255 * strength
|
|
47
54
|
// Precompute luminance (Rec.709)
|
|
48
55
|
const lum = new Float32Array(w * h);
|
package/lib/shapes/Text.js
CHANGED
|
@@ -239,8 +239,8 @@ export class Text extends Shape {
|
|
|
239
239
|
const lineWidth = align === JUSTIFY && !lastLine ? totalWidth - padding * 2 : width;
|
|
240
240
|
context.lineTo(x + Math.round(lineWidth), y);
|
|
241
241
|
context.lineWidth = getDecorationLineWidth(fontSize);
|
|
242
|
-
|
|
243
|
-
|
|
242
|
+
if (context._applyDecorationStyle(this))
|
|
243
|
+
context.stroke();
|
|
244
244
|
context.restore();
|
|
245
245
|
}
|
|
246
246
|
// store the starting x position for line-through which is drawn after text
|
|
@@ -318,8 +318,8 @@ export class Text extends Shape {
|
|
|
318
318
|
const lineWidth = align === JUSTIFY && !lastLine ? totalWidth - padding * 2 : width;
|
|
319
319
|
context.lineTo(x + Math.round(lineWidth), translateY + lineTranslateY + yOffset);
|
|
320
320
|
context.lineWidth = getDecorationLineWidth(fontSize);
|
|
321
|
-
|
|
322
|
-
|
|
321
|
+
if (context._applyDecorationStyle(this))
|
|
322
|
+
context.stroke();
|
|
323
323
|
context.restore();
|
|
324
324
|
}
|
|
325
325
|
context.restore();
|
package/lib/shapes/TextPath.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Context } from '../Context.ts';
|
|
1
|
+
import type { Context, SceneContext } from '../Context.ts';
|
|
2
2
|
import type { ShapeConfig } from '../Shape.ts';
|
|
3
3
|
import { Shape } from '../Shape.ts';
|
|
4
4
|
import { Text } from './Text.ts';
|
|
@@ -84,7 +84,7 @@ export declare class TextPath extends Shape<TextPathConfig> {
|
|
|
84
84
|
y: number;
|
|
85
85
|
} | null;
|
|
86
86
|
_readDataAttribute(): void;
|
|
87
|
-
_sceneFunc(context:
|
|
87
|
+
_sceneFunc(context: SceneContext): void;
|
|
88
88
|
_hitFunc(context: Context): void;
|
|
89
89
|
/**
|
|
90
90
|
* get text width in pixels
|
|
@@ -106,6 +106,7 @@ export declare class TextPath extends Shape<TextPathConfig> {
|
|
|
106
106
|
width: number;
|
|
107
107
|
height: number;
|
|
108
108
|
};
|
|
109
|
+
_getUnderlineOffset(): number;
|
|
109
110
|
getStrokeScaleEnabled(): boolean;
|
|
110
111
|
_getSelfRectForDrawing(): {
|
|
111
112
|
x: number;
|
package/lib/shapes/TextPath.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Util } from "../Util.js";
|
|
1
|
+
import { Transform, Util } from "../Util.js";
|
|
2
2
|
import { Factory } from "../Factory.js";
|
|
3
3
|
import { Shape } from "../Shape.js";
|
|
4
4
|
import { Path } from "./Path.js";
|
|
@@ -88,40 +88,49 @@ export class TextPath extends Shape {
|
|
|
88
88
|
context.setAttr('textAlign', 'left');
|
|
89
89
|
context.save();
|
|
90
90
|
const textDecoration = this.textDecoration();
|
|
91
|
-
const fill = this.fill();
|
|
92
91
|
const fontSize = this.fontSize();
|
|
92
|
+
const underlineOffset = this._getUnderlineOffset();
|
|
93
93
|
const glyphInfo = this.glyphInfo;
|
|
94
94
|
const hasUnderline = textDecoration.indexOf('underline') !== -1;
|
|
95
95
|
const hasLineThrough = textDecoration.indexOf('line-through') !== -1;
|
|
96
96
|
if (hasUnderline) {
|
|
97
97
|
context.beginPath();
|
|
98
98
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
99
|
+
try {
|
|
100
|
+
for (let i = 0; i < glyphInfo.length; i++) {
|
|
101
|
+
context.save();
|
|
102
|
+
const p0 = glyphInfo[i].p0;
|
|
103
|
+
const rotation = glyphInfo[i].rotation;
|
|
104
|
+
context.translate(p0.x, p0.y);
|
|
105
|
+
context.rotate(rotation);
|
|
106
|
+
this.partialText = glyphInfo[i].text;
|
|
107
|
+
// keep gradients and patterns in the shape's space, not the glyph's
|
|
108
|
+
this._styleTransform = new Transform()
|
|
109
|
+
.rotate(-rotation)
|
|
110
|
+
.translate(-p0.x, -p0.y);
|
|
111
|
+
context.fillStrokeShape(this);
|
|
112
|
+
if (hasUnderline) {
|
|
113
|
+
if (i === 0) {
|
|
114
|
+
context.moveTo(0, underlineOffset);
|
|
115
|
+
}
|
|
116
|
+
context.lineTo(glyphInfo[i].width, underlineOffset);
|
|
109
117
|
}
|
|
110
|
-
context.
|
|
118
|
+
context.restore();
|
|
119
|
+
//// To assist with debugging visually, uncomment following
|
|
120
|
+
//
|
|
121
|
+
// if (i % 2) context.strokeStyle = 'cyan';
|
|
122
|
+
// else context.strokeStyle = 'green';
|
|
123
|
+
// var p1 = glyphInfo[i].p1;
|
|
124
|
+
// context.moveTo(p0.x, p0.y);
|
|
125
|
+
// context.lineTo(p1.x, p1.y);
|
|
126
|
+
// context.stroke();
|
|
111
127
|
}
|
|
112
|
-
context.restore();
|
|
113
|
-
//// To assist with debugging visually, uncomment following
|
|
114
|
-
//
|
|
115
|
-
// if (i % 2) context.strokeStyle = 'cyan';
|
|
116
|
-
// else context.strokeStyle = 'green';
|
|
117
|
-
// var p1 = glyphInfo[i].p1;
|
|
118
|
-
// context.moveTo(p0.x, p0.y);
|
|
119
|
-
// context.lineTo(p1.x, p1.y);
|
|
120
|
-
// context.stroke();
|
|
121
128
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
129
|
+
finally {
|
|
130
|
+
this._styleTransform = undefined;
|
|
131
|
+
}
|
|
132
|
+
context.lineWidth = getDecorationLineWidth(fontSize);
|
|
133
|
+
if (hasUnderline && context._applyDecorationStyle(this)) {
|
|
125
134
|
context.stroke();
|
|
126
135
|
}
|
|
127
136
|
if (hasLineThrough) {
|
|
@@ -137,9 +146,8 @@ export class TextPath extends Shape {
|
|
|
137
146
|
context.lineTo(glyphInfo[i].width, 0);
|
|
138
147
|
context.restore();
|
|
139
148
|
}
|
|
140
|
-
context.
|
|
141
|
-
|
|
142
|
-
context.stroke();
|
|
149
|
+
if (context._applyDecorationStyle(this))
|
|
150
|
+
context.stroke();
|
|
143
151
|
}
|
|
144
152
|
context.restore();
|
|
145
153
|
}
|
|
@@ -306,12 +314,30 @@ export class TextPath extends Shape {
|
|
|
306
314
|
});
|
|
307
315
|
const rect = Util._getPointsRect(points);
|
|
308
316
|
const fontSize = this.fontSize();
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
317
|
+
let x = rect.x - fontSize / 2, y = rect.y - fontSize / 2, right = rect.x + rect.width + fontSize / 2, bottom = rect.y + rect.height + fontSize / 2;
|
|
318
|
+
if (this.textDecoration().indexOf('underline') !== -1) {
|
|
319
|
+
// the underline is drawn below the glyphs, past the padding
|
|
320
|
+
const lineWidth = getDecorationLineWidth(fontSize), lineY = this._getUnderlineOffset() - lineWidth / 2;
|
|
321
|
+
this.glyphInfo.forEach(function (info) {
|
|
322
|
+
const line = new Transform()
|
|
323
|
+
.translate(info.p0.x, info.p0.y)
|
|
324
|
+
.rotate(info.rotation)
|
|
325
|
+
._getTransformedRect({
|
|
326
|
+
x: 0,
|
|
327
|
+
y: lineY,
|
|
328
|
+
width: info.width,
|
|
329
|
+
height: lineWidth,
|
|
330
|
+
});
|
|
331
|
+
x = Math.min(x, line.x);
|
|
332
|
+
y = Math.min(y, line.y);
|
|
333
|
+
right = Math.max(right, line.x + line.width);
|
|
334
|
+
bottom = Math.max(bottom, line.y + line.height);
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
return { x, y, width: right - x, height: bottom - y };
|
|
338
|
+
}
|
|
339
|
+
_getUnderlineOffset() {
|
|
340
|
+
return this.fontSize() / 2 + 1;
|
|
315
341
|
}
|
|
316
342
|
// Like Text: a glyph stroke is drawn with the text, so it always scales.
|
|
317
343
|
getStrokeScaleEnabled() {
|
|
@@ -334,10 +360,9 @@ TextPath.prototype._strokeFunc = _strokeFunc;
|
|
|
334
360
|
TextPath.prototype._fillFuncHit = _fillFunc;
|
|
335
361
|
TextPath.prototype._strokeFuncHit = _strokeFunc;
|
|
336
362
|
TextPath.prototype.className = 'TextPath';
|
|
337
|
-
|
|
363
|
+
const ATTR_CHANGE_LIST = [
|
|
338
364
|
'text',
|
|
339
365
|
'fontSize',
|
|
340
|
-
'data',
|
|
341
366
|
'align',
|
|
342
367
|
'letterSpacing',
|
|
343
368
|
'kerningFunc',
|
|
@@ -346,16 +371,16 @@ TextPath.prototype._attrsAffectingSize = [
|
|
|
346
371
|
'fontVariant',
|
|
347
372
|
'direction',
|
|
348
373
|
];
|
|
374
|
+
// data relayouts in its own listener; textDecoration changes the bounds
|
|
375
|
+
// through getSelfRect, but needs no relayout
|
|
376
|
+
TextPath.prototype._attrsAffectingSize = ATTR_CHANGE_LIST.concat('data', 'textDecoration');
|
|
349
377
|
_registerNode(TextPath, true);
|
|
350
378
|
TextPath.prototype.on('dataChange.konva', function () {
|
|
351
379
|
this._readDataAttribute();
|
|
352
380
|
this._setTextData();
|
|
353
381
|
});
|
|
354
382
|
// update text data for certain attr changes
|
|
355
|
-
TextPath.prototype.on(
|
|
356
|
-
.filter((attr) => attr !== 'data')
|
|
357
|
-
.map((attr) => attr + 'Change.konva')
|
|
358
|
-
.join(' '), function () {
|
|
383
|
+
TextPath.prototype.on(ATTR_CHANGE_LIST.map((attr) => attr + 'Change.konva').join(' '), function () {
|
|
359
384
|
this._setTextData();
|
|
360
385
|
});
|
|
361
386
|
/**
|
|
@@ -115,9 +115,8 @@ export declare class Transformer extends Group {
|
|
|
115
115
|
cos: number;
|
|
116
116
|
_cursorChange: boolean;
|
|
117
117
|
_elementsCreated: boolean;
|
|
118
|
-
_updateScheduled: boolean;
|
|
119
118
|
_lastNodeRect?: Readonly<Box>;
|
|
120
|
-
|
|
119
|
+
_updatePending: boolean;
|
|
121
120
|
static isTransforming: () => boolean;
|
|
122
121
|
static _isLayerTransforming(layer: Node): boolean;
|
|
123
122
|
constructor(config?: TransformerConfig);
|
|
@@ -135,6 +134,9 @@ export declare class Transformer extends Group {
|
|
|
135
134
|
_getEventNamespace(): string;
|
|
136
135
|
setNodes(nodes?: Array<Node>): this;
|
|
137
136
|
_proxyDrag(node: Node): void;
|
|
137
|
+
_detachNode(node: Node): void;
|
|
138
|
+
_resetRotation(): void;
|
|
139
|
+
_removeNode(node: Node): void;
|
|
138
140
|
getNodes(): Node<import("../Node.ts").NodeConfig>[];
|
|
139
141
|
/**
|
|
140
142
|
* return the name of current active anchor
|
|
@@ -169,7 +171,7 @@ export declare class Transformer extends Group {
|
|
|
169
171
|
*/
|
|
170
172
|
_resetTransformCache(changedNode?: Node): void;
|
|
171
173
|
_onSubtreeChange(node: Node): void;
|
|
172
|
-
|
|
174
|
+
_syncUpdate(): void;
|
|
173
175
|
drawScene(can?: SceneCanvas, top?: Node): this;
|
|
174
176
|
drawHit(can?: HitCanvas, top?: Node): this;
|
|
175
177
|
_getNodeRect(): any;
|
|
@@ -289,14 +289,14 @@ export class Transformer extends Group {
|
|
|
289
289
|
// rendered in another window than the one Konva was imported into
|
|
290
290
|
this._transformWindow = null;
|
|
291
291
|
this._elementsCreated = false;
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
this._subtreeChanged = false;
|
|
292
|
+
// the layout is stale; the next sync point runs _update() (see _syncUpdate)
|
|
293
|
+
this._updatePending = false;
|
|
295
294
|
this._createElements();
|
|
296
295
|
// bindings
|
|
297
296
|
this._handleMouseMove = this._handleMouseMove.bind(this);
|
|
298
297
|
this._handleMouseUp = this._handleMouseUp.bind(this);
|
|
299
298
|
this.update = this.update.bind(this);
|
|
299
|
+
this._syncUpdate = this._syncUpdate.bind(this);
|
|
300
300
|
// update transformer data for certain attr changes
|
|
301
301
|
this.on(ATTR_CHANGE_LIST, (event) => {
|
|
302
302
|
if (event.type === 'ignoreStrokeChange')
|
|
@@ -352,12 +352,7 @@ export class Transformer extends Group {
|
|
|
352
352
|
return true;
|
|
353
353
|
});
|
|
354
354
|
this._nodes = nodes = filteredNodes;
|
|
355
|
-
|
|
356
|
-
this.rotation(nodes[0].getAbsoluteRotation());
|
|
357
|
-
}
|
|
358
|
-
else {
|
|
359
|
-
this.rotation(0);
|
|
360
|
-
}
|
|
355
|
+
this._resetRotation();
|
|
361
356
|
this._nodes.forEach((node) => {
|
|
362
357
|
const onChange = () => {
|
|
363
358
|
var _a;
|
|
@@ -369,6 +364,7 @@ export class Transformer extends Group {
|
|
|
369
364
|
(_a = this._nodeRectCache) === null || _a === void 0 ? void 0 : _a.delete(node);
|
|
370
365
|
return;
|
|
371
366
|
}
|
|
367
|
+
// unlike _resetRotation, a multi-node box keeps its rotation here
|
|
372
368
|
if (this.nodes().length === 1 && this.useSingleNodeRotation()) {
|
|
373
369
|
this.rotation(this.nodes()[0].getAbsoluteRotation());
|
|
374
370
|
}
|
|
@@ -393,8 +389,7 @@ export class Transformer extends Group {
|
|
|
393
389
|
if (node instanceof Container)
|
|
394
390
|
node._addSubtreeObserver(this);
|
|
395
391
|
node.on(`destroy.${this._getEventNamespace()}`, () => {
|
|
396
|
-
|
|
397
|
-
this.setNodes(this._nodes.filter((n) => n !== node));
|
|
392
|
+
this._removeNode(node);
|
|
398
393
|
});
|
|
399
394
|
this._proxyDrag(node);
|
|
400
395
|
});
|
|
@@ -440,6 +435,23 @@ export class Transformer extends Group {
|
|
|
440
435
|
lastPos = null;
|
|
441
436
|
});
|
|
442
437
|
}
|
|
438
|
+
_detachNode(node) {
|
|
439
|
+
node.off('.' + this._getEventNamespace());
|
|
440
|
+
node._removeSubtreeObserver(this);
|
|
441
|
+
}
|
|
442
|
+
_resetRotation() {
|
|
443
|
+
const single = this._nodes.length === 1 && this.useSingleNodeRotation();
|
|
444
|
+
this.rotation(single ? this._nodes[0].getAbsoluteRotation() : 0);
|
|
445
|
+
}
|
|
446
|
+
// Drops one destroyed node. Re-attaching the survivors instead costs
|
|
447
|
+
// O(N) listener rebinds per destroyed node, O(N^2) for a deleted selection.
|
|
448
|
+
_removeNode(node) {
|
|
449
|
+
this._detachNode(node);
|
|
450
|
+
this._nodes = this._nodes.filter((n) => n !== node);
|
|
451
|
+
this._resetRotation();
|
|
452
|
+
this._resetTransformCache(node);
|
|
453
|
+
this._scheduleUpdate();
|
|
454
|
+
}
|
|
443
455
|
getNodes() {
|
|
444
456
|
return this._nodes || [];
|
|
445
457
|
}
|
|
@@ -463,17 +475,12 @@ export class Transformer extends Group {
|
|
|
463
475
|
* transformer.detach();
|
|
464
476
|
*/
|
|
465
477
|
detach() {
|
|
466
|
-
var _a;
|
|
467
|
-
|
|
468
|
-
if (this._nodes) {
|
|
469
|
-
this._nodes.forEach((node) => {
|
|
470
|
-
node.off('.' + this._getEventNamespace());
|
|
471
|
-
node._removeSubtreeObserver(this);
|
|
472
|
-
});
|
|
473
|
-
}
|
|
478
|
+
var _a, _b;
|
|
479
|
+
(_a = this._nodes) === null || _a === void 0 ? void 0 : _a.forEach((node) => this._detachNode(node));
|
|
474
480
|
this._nodes = [];
|
|
481
|
+
this._updatePending = false;
|
|
475
482
|
this._resetTransformCache();
|
|
476
|
-
(
|
|
483
|
+
(_b = this.getLayer()) === null || _b === void 0 ? void 0 : _b.batchDraw();
|
|
477
484
|
}
|
|
478
485
|
/**
|
|
479
486
|
* bind events to the Transformer. You can use events: `transform`, `transformstart`, `transformend`, `dragstart`, `dragmove`, `dragend`
|
|
@@ -501,37 +508,42 @@ export class Transformer extends Group {
|
|
|
501
508
|
// Called for every change below an attached container. A burst of child
|
|
502
509
|
// changes only drops cached values here. The new bounds are measured once,
|
|
503
510
|
// before the next frame, or earlier if a draw or transform step comes first
|
|
504
|
-
// (
|
|
511
|
+
// (_syncUpdate).
|
|
505
512
|
_onSubtreeChange(node) {
|
|
506
513
|
var _a, _b;
|
|
507
514
|
if (this._fitting)
|
|
508
515
|
return;
|
|
509
516
|
// nothing was measured since the last change, so nothing cached is stale
|
|
510
|
-
|
|
517
|
+
const measured = !this._updatePending || !!this._cache[NODES_RECT];
|
|
518
|
+
if (measured)
|
|
519
|
+
this._resetTransformCache(node);
|
|
520
|
+
if (Node._updateBatchDepth > 0) {
|
|
521
|
+
// the batch flush lays the new bounds out, no frame needed
|
|
522
|
+
this._scheduleUpdate();
|
|
511
523
|
return;
|
|
512
|
-
|
|
513
|
-
if (!this.
|
|
514
|
-
this.
|
|
524
|
+
}
|
|
525
|
+
if (!this._updatePending) {
|
|
526
|
+
this._updatePending = true;
|
|
515
527
|
// queued ahead of the draw the change requests, so that draw shows the
|
|
516
528
|
// new layout. Updating inside the draw would request one more frame.
|
|
517
|
-
Util.requestAnimFrame(
|
|
529
|
+
Util.requestAnimFrame(this._syncUpdate, (_a = this.getStage()) === null || _a === void 0 ? void 0 : _a._getOwnerWindow());
|
|
518
530
|
// the transformer can be on another layer than the changed node
|
|
519
531
|
(_b = this.getLayer()) === null || _b === void 0 ? void 0 : _b.batchDraw();
|
|
520
532
|
}
|
|
521
533
|
}
|
|
522
|
-
|
|
523
|
-
|
|
534
|
+
// Runs a deferred update before anything draws or measures the anchors.
|
|
535
|
+
_syncUpdate() {
|
|
524
536
|
// while dragging, the dragmove handler updates the layout
|
|
525
|
-
if (this.
|
|
537
|
+
if (this._updatePending && !this._fitting && !this.isDragging()) {
|
|
526
538
|
this._update();
|
|
527
539
|
}
|
|
528
540
|
}
|
|
529
541
|
drawScene(can, top) {
|
|
530
|
-
this.
|
|
542
|
+
this._syncUpdate();
|
|
531
543
|
return super.drawScene(can, top);
|
|
532
544
|
}
|
|
533
545
|
drawHit(can, top) {
|
|
534
|
-
this.
|
|
546
|
+
this._syncUpdate();
|
|
535
547
|
return super.drawHit(can, top);
|
|
536
548
|
}
|
|
537
549
|
_getNodeRect() {
|
|
@@ -818,7 +830,7 @@ export class Transformer extends Group {
|
|
|
818
830
|
}
|
|
819
831
|
_moveTransform(e) {
|
|
820
832
|
// anchors must show the current bounds before they are measured against
|
|
821
|
-
this.
|
|
833
|
+
this._syncUpdate();
|
|
822
834
|
let x, y, newHypotenuse;
|
|
823
835
|
const anchorNode = this._anchors[this._movingAnchorName];
|
|
824
836
|
const stage = anchorNode.getStage();
|
|
@@ -1252,21 +1264,10 @@ export class Transformer extends Group {
|
|
|
1252
1264
|
layersToDraw.add(this.getLayer());
|
|
1253
1265
|
layersToDraw.forEach((layer) => layer && layer.batchDraw());
|
|
1254
1266
|
}
|
|
1255
|
-
//
|
|
1256
|
-
// the cascade ends; outside a cascade, run synchronously so reads of
|
|
1257
|
-
// transformer state stay consistent.
|
|
1267
|
+
// deferred to the batch flush when one is open, else synchronous
|
|
1258
1268
|
_scheduleUpdate() {
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
this._updateScheduled = true;
|
|
1262
|
-
Node._runAfterAbsTransformCascade(() => {
|
|
1263
|
-
var _a;
|
|
1264
|
-
this._updateScheduled = false;
|
|
1265
|
-
if (!((_a = this._nodes) === null || _a === void 0 ? void 0 : _a.length) || this._fitting || this.isDragging()) {
|
|
1266
|
-
return;
|
|
1267
|
-
}
|
|
1268
|
-
this._update();
|
|
1269
|
-
});
|
|
1269
|
+
this._updatePending = true;
|
|
1270
|
+
Node._runAfterUpdateBatch(this._syncUpdate);
|
|
1270
1271
|
}
|
|
1271
1272
|
/**
|
|
1272
1273
|
* force update of Konva.Transformer.
|
|
@@ -1288,7 +1289,7 @@ export class Transformer extends Group {
|
|
|
1288
1289
|
}
|
|
1289
1290
|
_update() {
|
|
1290
1291
|
var _a;
|
|
1291
|
-
this.
|
|
1292
|
+
this._updatePending = false;
|
|
1292
1293
|
const attrs = this._getNodeRect();
|
|
1293
1294
|
this._updateElements(attrs);
|
|
1294
1295
|
const draggable = this.nodes().some((node) => node.draggable());
|
|
@@ -1468,6 +1469,7 @@ export class Transformer extends Group {
|
|
|
1468
1469
|
return node;
|
|
1469
1470
|
}
|
|
1470
1471
|
getClientRect(config) {
|
|
1472
|
+
this._syncUpdate();
|
|
1471
1473
|
if (this.nodes().length > 0) {
|
|
1472
1474
|
return super.getClientRect(config);
|
|
1473
1475
|
}
|