konva 4.2.0 → 5.0.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/lib/BaseLayer.js CHANGED
@@ -26,8 +26,8 @@ var BaseLayer = (function (_super) {
26
26
  _this._waitingForDraw = false;
27
27
  _this.on('visibleChange', _this._checkVisibility);
28
28
  _this._checkVisibility();
29
- _this.on('imageSmoothingEnabledChange', _this._checkSmooth);
30
- _this._checkSmooth();
29
+ _this.on('imageSmoothingEnabledChange', _this._setSmoothEnabled);
30
+ _this._setSmoothEnabled();
31
31
  return _this;
32
32
  }
33
33
  BaseLayer.prototype.createPNGStream = function () {
@@ -132,6 +132,7 @@ var BaseLayer = (function (_super) {
132
132
  BaseLayer.prototype.setSize = function (_a) {
133
133
  var width = _a.width, height = _a.height;
134
134
  this.canvas.setSize(width, height);
135
+ this._setSmoothEnabled();
135
136
  return this;
136
137
  };
137
138
  BaseLayer.prototype._toKonvaCanvas = function (config) {
@@ -151,7 +152,7 @@ var BaseLayer = (function (_super) {
151
152
  this.canvas._canvas.style.display = 'none';
152
153
  }
153
154
  };
154
- BaseLayer.prototype._checkSmooth = function () {
155
+ BaseLayer.prototype._setSmoothEnabled = function () {
155
156
  this.getContext()._context.imageSmoothingEnabled = this.imageSmoothingEnabled();
156
157
  };
157
158
  BaseLayer.prototype.getWidth = function () {
package/lib/Container.js CHANGED
@@ -151,17 +151,6 @@ var Container = (function (_super) {
151
151
  }
152
152
  return obj;
153
153
  };
154
- Container.prototype._getDescendants = function (arr) {
155
- var retArr = [];
156
- var len = arr.length;
157
- for (var n = 0; n < len; n++) {
158
- var node = arr[n];
159
- if (this.isAncestorOf(node)) {
160
- retArr.push(node);
161
- }
162
- }
163
- return retArr;
164
- };
165
154
  Container.prototype.isAncestorOf = function (node) {
166
155
  var parent = node.getParent();
167
156
  while (parent) {
@@ -277,10 +266,10 @@ var Container = (function (_super) {
277
266
  var dragSkip = !Global_1.Konva.hitOnDragEnabled && layerUnderDrag;
278
267
  return layer && layer.hitGraphEnabled() && this.isVisible() && !dragSkip;
279
268
  };
280
- Container.prototype.getClientRect = function (attrs) {
281
- attrs = attrs || {};
282
- var skipTransform = attrs.skipTransform;
283
- var relativeTo = attrs.relativeTo;
269
+ Container.prototype.getClientRect = function (config) {
270
+ config = config || {};
271
+ var skipTransform = config.skipTransform;
272
+ var relativeTo = config.relativeTo;
284
273
  var minX, minY, maxX, maxY;
285
274
  var selfRect = {
286
275
  x: Infinity,
@@ -295,8 +284,8 @@ var Container = (function (_super) {
295
284
  }
296
285
  var rect = child.getClientRect({
297
286
  relativeTo: that,
298
- skipShadow: attrs.skipShadow,
299
- skipStroke: attrs.skipStroke
287
+ skipShadow: config.skipShadow,
288
+ skipStroke: config.skipStroke
300
289
  });
301
290
  if (rect.width === 0 && rect.height === 0) {
302
291
  return;
package/lib/FastLayer.js CHANGED
@@ -28,10 +28,6 @@ var FastLayer = (function (_super) {
28
28
  Util_1.Util.throw('You may only add shapes to a fast layer.');
29
29
  }
30
30
  };
31
- FastLayer.prototype._setCanvasSize = function (width, height) {
32
- this.canvas.setSize(width, height);
33
- this._checkSmooth();
34
- };
35
31
  FastLayer.prototype.hitGraphEnabled = function () {
36
32
  return false;
37
33
  };
package/lib/Global.js CHANGED
@@ -47,7 +47,7 @@ exports.glob = typeof global !== 'undefined'
47
47
  : {};
48
48
  exports.Konva = {
49
49
  _global: exports.glob,
50
- version: '4.2.0',
50
+ version: '5.0.1',
51
51
  isBrowser: detectBrowser(),
52
52
  isUnminified: /param/.test(function (param) { }.toString()),
53
53
  dblClickWindow: 400,
package/lib/Layer.js CHANGED
@@ -37,10 +37,11 @@ var Layer = (function (_super) {
37
37
  });
38
38
  return _this;
39
39
  }
40
- Layer.prototype._setCanvasSize = function (width, height) {
41
- this.canvas.setSize(width, height);
40
+ Layer.prototype.setSize = function (_a) {
41
+ var width = _a.width, height = _a.height;
42
+ _super.prototype.setSize.call(this, { width: width, height: height });
42
43
  this.hitCanvas.setSize(width, height);
43
- this._checkSmooth();
44
+ return this;
44
45
  };
45
46
  Layer.prototype._validateAdd = function (child) {
46
47
  var type = child.getType();
@@ -157,12 +158,6 @@ var Layer = (function (_super) {
157
158
  parent.content.appendChild(this.hitCanvas._canvas);
158
159
  }
159
160
  };
160
- Layer.prototype.setSize = function (_a) {
161
- var width = _a.width, height = _a.height;
162
- _super.prototype.setSize.call(this, { width: width, height: height });
163
- this.hitCanvas.setSize(width, height);
164
- return this;
165
- };
166
161
  return Layer;
167
162
  }(BaseLayer_1.BaseLayer));
168
163
  exports.Layer = Layer;
package/lib/Node.js CHANGED
@@ -49,7 +49,7 @@ exports._removeName = function (name, _id) {
49
49
  delete exports.names[name];
50
50
  }
51
51
  };
52
- var ABSOLUTE_OPACITY = 'absoluteOpacity', ABSOLUTE_TRANSFORM = 'absoluteTransform', ABSOLUTE_SCALE = 'absoluteScale', CANVAS = 'canvas', CHANGE = 'Change', CHILDREN = 'children', KONVA = 'konva', LISTENING = 'listening', MOUSEENTER = 'mouseenter', MOUSELEAVE = 'mouseleave', NAME = 'name', SET = 'set', SHAPE = 'Shape', SPACE = ' ', STAGE = 'stage', TRANSFORM = 'transform', UPPER_STAGE = 'Stage', VISIBLE = 'visible', CLONE_BLACK_LIST = ['id'], TRANSFORM_CHANGE_STR = [
52
+ var ABSOLUTE_OPACITY = 'absoluteOpacity', ABSOLUTE_TRANSFORM = 'absoluteTransform', ABSOLUTE_SCALE = 'absoluteScale', CANVAS = 'canvas', CHANGE = 'Change', CHILDREN = 'children', KONVA = 'konva', LISTENING = 'listening', MOUSEENTER = 'mouseenter', MOUSELEAVE = 'mouseleave', NAME = 'name', SET = 'set', SHAPE = 'Shape', SPACE = ' ', STAGE = 'stage', TRANSFORM = 'transform', UPPER_STAGE = 'Stage', VISIBLE = 'visible', TRANSFORM_CHANGE_STR = [
53
53
  'xChange.konva',
54
54
  'yChange.konva',
55
55
  'scaleXChange.konva',
@@ -73,18 +73,21 @@ var Node = (function () {
73
73
  this.parent = null;
74
74
  this._cache = new Map();
75
75
  this._lastPos = null;
76
+ this._batchingTransformChange = false;
77
+ this._needClearTransformCache = false;
76
78
  this._filterUpToDate = false;
77
79
  this._isUnderCache = false;
78
80
  this.children = emptyChildren;
79
81
  this._dragEventId = null;
80
82
  this.setAttrs(config);
81
83
  this.on(TRANSFORM_CHANGE_STR, function () {
84
+ if (_this._batchingTransformChange) {
85
+ _this._needClearTransformCache = true;
86
+ return;
87
+ }
82
88
  _this._clearCache(TRANSFORM);
83
89
  _this._clearSelfAndDescendantCache(ABSOLUTE_TRANSFORM);
84
90
  });
85
- this.on(SCALE_CHANGE_STR, function () {
86
- _this._clearSelfAndDescendantCache(ABSOLUTE_SCALE);
87
- });
88
91
  this.on('visibleChange.konva', function () {
89
92
  _this._clearSelfAndDescendantCache(VISIBLE);
90
93
  });
@@ -120,14 +123,17 @@ var Node = (function () {
120
123
  Node.prototype._getCanvasCache = function () {
121
124
  return this._cache.get(CANVAS);
122
125
  };
123
- Node.prototype._clearSelfAndDescendantCache = function (attr) {
126
+ Node.prototype._clearSelfAndDescendantCache = function (attr, forceEvent) {
124
127
  this._clearCache(attr);
128
+ if (forceEvent && attr === ABSOLUTE_TRANSFORM) {
129
+ this.fire('_clearTransformCache');
130
+ }
125
131
  if (this.isCached()) {
126
132
  return;
127
133
  }
128
134
  if (this.children) {
129
135
  this.children.each(function (node) {
130
- node._clearSelfAndDescendantCache(attr);
136
+ node._clearSelfAndDescendantCache(attr, true);
131
137
  });
132
138
  }
133
139
  };
@@ -163,8 +169,8 @@ var Node = (function () {
163
169
  height: height
164
170
  }), cachedFilterCanvas = new Canvas_1.SceneCanvas({
165
171
  pixelRatio: pixelRatio,
166
- width: width,
167
- height: height
172
+ width: 0,
173
+ height: 0
168
174
  }), cachedHitCanvas = new Canvas_1.HitCanvas({
169
175
  pixelRatio: 1,
170
176
  width: width,
@@ -176,7 +182,6 @@ var Node = (function () {
176
182
  if (conf.imageSmoothingEnabled === false) {
177
183
  cachedSceneCanvas.getContext()._context.imageSmoothingEnabled = false;
178
184
  cachedFilterCanvas.getContext()._context.imageSmoothingEnabled = false;
179
- cachedHitCanvas.getContext()._context.imageSmoothingEnabled = false;
180
185
  }
181
186
  sceneContext.save();
182
187
  hitContext.save();
@@ -265,6 +270,7 @@ var Node = (function () {
265
270
  if (filters) {
266
271
  if (!this._filterUpToDate) {
267
272
  var ratio = sceneCanvas.pixelRatio;
273
+ filterCanvas.setSize(sceneCanvas.width / sceneCanvas.pixelRatio, sceneCanvas.height / sceneCanvas.pixelRatio);
268
274
  try {
269
275
  len = filters.length;
270
276
  filterContext.clear();
@@ -528,9 +534,22 @@ var Node = (function () {
528
534
  }
529
535
  return depth;
530
536
  };
537
+ Node.prototype._batchTransformChanges = function (func) {
538
+ this._batchingTransformChange = true;
539
+ func();
540
+ this._batchingTransformChange = false;
541
+ if (this._needClearTransformCache) {
542
+ this._clearCache(TRANSFORM);
543
+ this._clearSelfAndDescendantCache(ABSOLUTE_TRANSFORM, true);
544
+ }
545
+ this._needClearTransformCache = false;
546
+ };
531
547
  Node.prototype.setPosition = function (pos) {
532
- this.x(pos.x);
533
- this.y(pos.y);
548
+ var _this = this;
549
+ this._batchTransformChanges(function () {
550
+ _this.x(pos.x);
551
+ _this.y(pos.y);
552
+ });
534
553
  return this;
535
554
  };
536
555
  Node.prototype.getPosition = function () {
@@ -563,15 +582,16 @@ var Node = (function () {
563
582
  this.attrs.y = origTrans.y;
564
583
  delete origTrans.x;
565
584
  delete origTrans.y;
566
- it = this.getAbsoluteTransform();
585
+ this._clearCache(TRANSFORM);
586
+ it = this._getAbsoluteTransform();
567
587
  it.invert();
568
588
  it.translate(pos.x, pos.y);
569
589
  pos = {
570
590
  x: this.attrs.x + it.getTranslation().x,
571
591
  y: this.attrs.y + it.getTranslation().y
572
592
  };
573
- this.setPosition({ x: pos.x, y: pos.y });
574
593
  this._setTransform(origTrans);
594
+ this.setPosition({ x: pos.x, y: pos.y });
575
595
  return this;
576
596
  };
577
597
  Node.prototype._setTransform = function (trans) {
@@ -579,8 +599,6 @@ var Node = (function () {
579
599
  for (key in trans) {
580
600
  this.attrs[key] = trans[key];
581
601
  }
582
- this._clearCache(TRANSFORM);
583
- this._clearSelfAndDescendantCache(ABSOLUTE_TRANSFORM);
584
602
  };
585
603
  Node.prototype._clearTransform = function () {
586
604
  var trans = {
@@ -603,8 +621,6 @@ var Node = (function () {
603
621
  this.attrs.offsetY = 0;
604
622
  this.attrs.skewX = 0;
605
623
  this.attrs.skewY = 0;
606
- this._clearCache(TRANSFORM);
607
- this._clearSelfAndDescendantCache(ABSOLUTE_TRANSFORM);
608
624
  return trans;
609
625
  };
610
626
  Node.prototype.move = function (change) {
@@ -827,7 +843,7 @@ var Node = (function () {
827
843
  }
828
844
  };
829
845
  Node.prototype.fire = function (eventType, evt, bubble) {
830
- evt = evt || {};
846
+ if (evt === void 0) { evt = {}; }
831
847
  evt.target = evt.target || this;
832
848
  if (bubble) {
833
849
  this._fireAndBubble(eventType, evt);
@@ -878,14 +894,6 @@ var Node = (function () {
878
894
  }
879
895
  };
880
896
  Node.prototype.getAbsoluteScale = function (top) {
881
- if (top) {
882
- return this._getAbsoluteScale(top);
883
- }
884
- else {
885
- return this._getCache(ABSOLUTE_SCALE, this._getAbsoluteScale);
886
- }
887
- };
888
- Node.prototype._getAbsoluteScale = function (top) {
889
897
  var parent = this;
890
898
  while (parent) {
891
899
  if (parent._isUnderCache) {
@@ -893,24 +901,15 @@ var Node = (function () {
893
901
  }
894
902
  parent = parent.getParent();
895
903
  }
896
- var scaleX = 1, scaleY = 1;
897
- this._eachAncestorReverse(function (node) {
898
- scaleX *= node.scaleX();
899
- scaleY *= node.scaleY();
900
- }, top);
904
+ var transform = this.getAbsoluteTransform(top);
905
+ var attrs = transform.decompose();
901
906
  return {
902
- x: scaleX,
903
- y: scaleY
907
+ x: attrs.scaleX,
908
+ y: attrs.scaleY
904
909
  };
905
910
  };
906
911
  Node.prototype.getAbsoluteRotation = function () {
907
- var parent = this;
908
- var rotation = 0;
909
- while (parent) {
910
- rotation += parent.rotation();
911
- parent = parent.getParent();
912
- }
913
- return rotation;
912
+ return this.getAbsoluteTransform().decompose().rotation;
914
913
  };
915
914
  Node.prototype.getTransform = function () {
916
915
  return this._getCache(TRANSFORM, this._getTransform);
@@ -1153,7 +1152,7 @@ var Node = (function () {
1153
1152
  this.parent.isListening() &&
1154
1153
  !stopBubble) {
1155
1154
  if (compareShape && compareShape.parent) {
1156
- this._fireAndBubble.call(this.parent, eventType, evt, compareShape.parent);
1155
+ this._fireAndBubble.call(this.parent, eventType, evt, compareShape);
1157
1156
  }
1158
1157
  else {
1159
1158
  this._fireAndBubble.call(this.parent, eventType, evt);
package/lib/Shape.js CHANGED
@@ -97,11 +97,11 @@ var Shape = (function (_super) {
97
97
  };
98
98
  Shape.prototype._hasShadow = function () {
99
99
  return (this.shadowEnabled() &&
100
- (this.shadowOpacity() !== 0 &&
101
- !!(this.shadowColor() ||
102
- this.shadowBlur() ||
103
- this.shadowOffsetX() ||
104
- this.shadowOffsetY())));
100
+ this.shadowOpacity() !== 0 &&
101
+ !!(this.shadowColor() ||
102
+ this.shadowBlur() ||
103
+ this.shadowOffsetX() ||
104
+ this.shadowOffsetY()));
105
105
  };
106
106
  Shape.prototype._getFillPattern = function () {
107
107
  return this._getCache(patternImage, this.__getFillPattern);
@@ -163,10 +163,11 @@ var Shape = (function (_super) {
163
163
  }
164
164
  };
165
165
  Shape.prototype.hasFill = function () {
166
- return this.fillEnabled() && !!(this.fill() ||
167
- this.fillPatternImage() ||
168
- this.fillLinearGradientColorStops() ||
169
- this.fillRadialGradientColorStops());
166
+ return (this.fillEnabled() &&
167
+ !!(this.fill() ||
168
+ this.fillPatternImage() ||
169
+ this.fillLinearGradientColorStops() ||
170
+ this.fillRadialGradientColorStops()));
170
171
  };
171
172
  Shape.prototype.hasStroke = function () {
172
173
  return (this.strokeEnabled() &&
@@ -175,8 +176,10 @@ var Shape = (function (_super) {
175
176
  };
176
177
  Shape.prototype.hasHitStroke = function () {
177
178
  var width = this.hitStrokeWidth();
178
- return (this.strokeEnabled() &&
179
- (width || this.strokeWidth() && width === 'auto'));
179
+ if (width === 'auto') {
180
+ return this.hasStroke();
181
+ }
182
+ return this.strokeEnabled() && !!width;
180
183
  };
181
184
  Shape.prototype.intersects = function (point) {
182
185
  var stage = this.getStage(), bufferHitCanvas = stage.bufferHitCanvas, p;
@@ -200,6 +203,7 @@ var Shape = (function (_super) {
200
203
  this.getStage());
201
204
  };
202
205
  Shape.prototype.setStrokeHitEnabled = function (val) {
206
+ Util_1.Util.warn('strokeHitEnabled property is deprecated. Please use hitStrokeWidth instead.');
203
207
  if (val) {
204
208
  this.hitStrokeWidth('auto');
205
209
  }
@@ -218,8 +222,8 @@ var Shape = (function (_super) {
218
222
  Shape.prototype.getSelfRect = function () {
219
223
  var size = this.size();
220
224
  return {
221
- x: this._centroid ? Math.round(-size.width / 2) : 0,
222
- y: this._centroid ? Math.round(-size.height / 2) : 0,
225
+ x: this._centroid ? -size.width / 2 : 0,
226
+ y: this._centroid ? -size.height / 2 : 0,
223
227
  width: size.width,
224
228
  height: size.height
225
229
  };
package/lib/Stage.js CHANGED
@@ -226,7 +226,7 @@ var Stage = (function (_super) {
226
226
  length +
227
227
  ' layers. Recommended maximum number of layers is 3-5. Adding more layers into the stage may drop the performance. Rethink your tree structure, you can use Konva.Group.');
228
228
  }
229
- layer._setCanvasSize(this.width(), this.height());
229
+ layer.setSize({ width: this.width(), height: this.height() });
230
230
  layer.draw();
231
231
  if (Global_1.Konva.isBrowser) {
232
232
  this.content.appendChild(layer.canvas._canvas);
package/lib/Util.js CHANGED
@@ -138,6 +138,45 @@ var Transform = (function () {
138
138
  var m0 = this.m[0], m1 = this.m[1], m2 = this.m[2], m3 = this.m[3], m4 = this.m[4], m5 = this.m[5], yt = (m0 * (y - m5) - m1 * (x - m4)) / (m0 * m3 - m1 * m2), xt = (x - m4 - m2 * yt) / m0;
139
139
  return this.translate(xt, yt);
140
140
  };
141
+ Transform.prototype.decompose = function () {
142
+ var a = this.m[0];
143
+ var b = this.m[1];
144
+ var c = this.m[2];
145
+ var d = this.m[3];
146
+ var e = this.m[4];
147
+ var f = this.m[5];
148
+ var delta = a * d - b * c;
149
+ var result = {
150
+ x: e,
151
+ y: f,
152
+ rotation: 0,
153
+ scaleX: 0,
154
+ scaleY: 0,
155
+ skewX: 0,
156
+ skewY: 0
157
+ };
158
+ if (a != 0 || b != 0) {
159
+ var r = Math.sqrt(a * a + b * b);
160
+ result.rotation = b > 0 ? Math.acos(a / r) : -Math.acos(a / r);
161
+ result.scaleX = r;
162
+ result.scaleY = delta / r;
163
+ result.skewX = Math.atan((a * c + b * d) / (r * r));
164
+ result.skewY = 0;
165
+ }
166
+ else if (c != 0 || d != 0) {
167
+ var s = Math.sqrt(c * c + d * d);
168
+ result.rotation =
169
+ Math.PI / 2 - (d > 0 ? Math.acos(-c / s) : -Math.acos(c / s));
170
+ result.scaleX = delta / s;
171
+ result.scaleY = s;
172
+ result.skewX = 0;
173
+ result.skewY = Math.atan((a * c + b * d) / (s * s));
174
+ }
175
+ else {
176
+ }
177
+ result.rotation = exports.Util._getRotation(result.rotation);
178
+ return result;
179
+ };
141
180
  return Transform;
142
181
  }());
143
182
  exports.Transform = Transform;
@@ -598,6 +637,9 @@ exports.Util = {
598
637
  _radToDeg: function (rad) {
599
638
  return rad * DEG180_OVER_PI;
600
639
  },
640
+ _getRotation: function (radians) {
641
+ return Global_1.Konva.angleDeg ? exports.Util._radToDeg(radians) : radians;
642
+ },
601
643
  _capitalize: function (str) {
602
644
  return str.charAt(0).toUpperCase() + str.slice(1);
603
645
  },
@@ -89,7 +89,7 @@ var Arrow = (function (_super) {
89
89
  x: lineRect.x - offset,
90
90
  y: lineRect.y - offset,
91
91
  width: lineRect.width + offset * 2,
92
- height: lineRect.height + offset * 2,
92
+ height: lineRect.height + offset * 2
93
93
  };
94
94
  };
95
95
  return Arrow;
@@ -144,10 +144,10 @@ var Line = (function (_super) {
144
144
  maxY = Math.max(maxY, y);
145
145
  }
146
146
  return {
147
- x: Math.round(minX),
148
- y: Math.round(minY),
149
- width: Math.round(maxX - minX),
150
- height: Math.round(maxY - minY)
147
+ x: minX,
148
+ y: minY,
149
+ width: maxX - minX,
150
+ height: maxY - minY
151
151
  };
152
152
  };
153
153
  return Line;
@@ -58,9 +58,10 @@ function checkDefaultFill(config) {
58
58
  }
59
59
  return config;
60
60
  }
61
- var trimRight = String.prototype.trimRight || function polyfill() {
62
- return this.replace(/[\s\xa0]+$/, '');
63
- };
61
+ var trimRight = String.prototype.trimRight ||
62
+ function polyfill() {
63
+ return this.replace(/[\s\xa0]+$/, '');
64
+ };
64
65
  var Text = (function (_super) {
65
66
  __extends(Text, _super);
66
67
  function Text(config) {
@@ -167,7 +168,11 @@ var Text = (function (_super) {
167
168
  context.fillStrokeShape(this);
168
169
  };
169
170
  Text.prototype.setText = function (text) {
170
- var str = Util_1.Util._isString(text) ? text : (text === null || text === undefined) ? '' : text + '';
171
+ var str = Util_1.Util._isString(text)
172
+ ? text
173
+ : text === null || text === undefined
174
+ ? ''
175
+ : text + '';
171
176
  this._setAttr(TEXT, str);
172
177
  return this;
173
178
  };
@@ -337,10 +337,10 @@ var TextPath = (function (_super) {
337
337
  }
338
338
  var fontSize = this.fontSize();
339
339
  return {
340
- x: Math.round(minX) - fontSize / 2,
341
- y: Math.round(minY) - fontSize / 2,
342
- width: Math.round(maxX - minX) + fontSize,
343
- height: Math.round(maxY - minY) + fontSize
340
+ x: minX - fontSize / 2,
341
+ y: minY - fontSize / 2,
342
+ width: maxX - minX + fontSize,
343
+ height: maxY - minY + fontSize
344
344
  };
345
345
  };
346
346
  return TextPath;