canvas2d-wrapper 1.6.1 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -3,6 +3,65 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'defau
3
3
  var React = require('react');
4
4
  var React__default = _interopDefault(React);
5
5
 
6
+ function _arrayLikeToArray(r, a) {
7
+ (null == a || a > r.length) && (a = r.length);
8
+ for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
9
+ return n;
10
+ }
11
+ function _defineProperties(e, r) {
12
+ for (var t = 0; t < r.length; t++) {
13
+ var o = r[t];
14
+ o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o);
15
+ }
16
+ }
17
+ function _createClass(e, r, t) {
18
+ return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", {
19
+ writable: !1
20
+ }), e;
21
+ }
22
+ function _createForOfIteratorHelperLoose(r, e) {
23
+ var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
24
+ if (t) return (t = t.call(r)).next.bind(t);
25
+ if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) {
26
+ t && (r = t);
27
+ var o = 0;
28
+ return function () {
29
+ return o >= r.length ? {
30
+ done: !0
31
+ } : {
32
+ done: !1,
33
+ value: r[o++]
34
+ };
35
+ };
36
+ }
37
+ throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
38
+ }
39
+ function _extends() {
40
+ return _extends = Object.assign ? Object.assign.bind() : function (n) {
41
+ for (var e = 1; e < arguments.length; e++) {
42
+ var t = arguments[e];
43
+ for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
44
+ }
45
+ return n;
46
+ }, _extends.apply(null, arguments);
47
+ }
48
+ function _inheritsLoose(t, o) {
49
+ t.prototype = Object.create(o.prototype), t.prototype.constructor = t, _setPrototypeOf(t, o);
50
+ }
51
+ function _objectWithoutPropertiesLoose(r, e) {
52
+ if (null == r) return {};
53
+ var t = {};
54
+ for (var n in r) if ({}.hasOwnProperty.call(r, n)) {
55
+ if (e.includes(n)) continue;
56
+ t[n] = r[n];
57
+ }
58
+ return t;
59
+ }
60
+ function _setPrototypeOf(t, e) {
61
+ return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) {
62
+ return t.__proto__ = e, t;
63
+ }, _setPrototypeOf(t, e);
64
+ }
6
65
  function _toPrimitive(t, r) {
7
66
  if ("object" != typeof t || !t) return t;
8
67
  var e = t[Symbol.toPrimitive];
@@ -15,93 +74,14 @@ function _toPrimitive(t, r) {
15
74
  }
16
75
  function _toPropertyKey(t) {
17
76
  var i = _toPrimitive(t, "string");
18
- return "symbol" == typeof i ? i : String(i);
19
- }
20
- function _defineProperties(target, props) {
21
- for (var i = 0; i < props.length; i++) {
22
- var descriptor = props[i];
23
- descriptor.enumerable = descriptor.enumerable || false;
24
- descriptor.configurable = true;
25
- if ("value" in descriptor) descriptor.writable = true;
26
- Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor);
27
- }
28
- }
29
- function _createClass(Constructor, protoProps, staticProps) {
30
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
31
- if (staticProps) _defineProperties(Constructor, staticProps);
32
- Object.defineProperty(Constructor, "prototype", {
33
- writable: false
34
- });
35
- return Constructor;
36
- }
37
- function _extends() {
38
- _extends = Object.assign ? Object.assign.bind() : function (target) {
39
- for (var i = 1; i < arguments.length; i++) {
40
- var source = arguments[i];
41
- for (var key in source) {
42
- if (Object.prototype.hasOwnProperty.call(source, key)) {
43
- target[key] = source[key];
44
- }
45
- }
46
- }
47
- return target;
48
- };
49
- return _extends.apply(this, arguments);
50
- }
51
- function _inheritsLoose(subClass, superClass) {
52
- subClass.prototype = Object.create(superClass.prototype);
53
- subClass.prototype.constructor = subClass;
54
- _setPrototypeOf(subClass, superClass);
77
+ return "symbol" == typeof i ? i : i + "";
55
78
  }
56
- function _setPrototypeOf(o, p) {
57
- _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
58
- o.__proto__ = p;
59
- return o;
60
- };
61
- return _setPrototypeOf(o, p);
62
- }
63
- function _objectWithoutPropertiesLoose(source, excluded) {
64
- if (source == null) return {};
65
- var target = {};
66
- var sourceKeys = Object.keys(source);
67
- var key, i;
68
- for (i = 0; i < sourceKeys.length; i++) {
69
- key = sourceKeys[i];
70
- if (excluded.indexOf(key) >= 0) continue;
71
- target[key] = source[key];
72
- }
73
- return target;
74
- }
75
- function _unsupportedIterableToArray(o, minLen) {
76
- if (!o) return;
77
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
78
- var n = Object.prototype.toString.call(o).slice(8, -1);
79
- if (n === "Object" && o.constructor) n = o.constructor.name;
80
- if (n === "Map" || n === "Set") return Array.from(o);
81
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
82
- }
83
- function _arrayLikeToArray(arr, len) {
84
- if (len == null || len > arr.length) len = arr.length;
85
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
86
- return arr2;
87
- }
88
- function _createForOfIteratorHelperLoose(o, allowArrayLike) {
89
- var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
90
- if (it) return (it = it.call(o)).next.bind(it);
91
- if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
92
- if (it) o = it;
93
- var i = 0;
94
- return function () {
95
- if (i >= o.length) return {
96
- done: true
97
- };
98
- return {
99
- done: false,
100
- value: o[i++]
101
- };
102
- };
79
+ function _unsupportedIterableToArray(r, a) {
80
+ if (r) {
81
+ if ("string" == typeof r) return _arrayLikeToArray(r, a);
82
+ var t = {}.toString.call(r).slice(8, -1);
83
+ return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
103
84
  }
104
- throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
105
85
  }
106
86
 
107
87
  function calcTileSize(tileSize, zoom) {
@@ -145,6 +125,7 @@ function inRect(element, x, y, left, top, localTileSize) {
145
125
 
146
126
  function collideElement(e, elements, left, top, tileSize, zoom) {
147
127
  var localTileSize = calcTileSize(tileSize, zoom);
128
+ var validElements = [];
148
129
  for (var _iterator = _createForOfIteratorHelperLoose(elements), _step; !(_step = _iterator()).done;) {
149
130
  var element = _step.value;
150
131
  var x = element.x * localTileSize;
@@ -153,34 +134,55 @@ function collideElement(e, elements, left, top, tileSize, zoom) {
153
134
  case 'Rect':
154
135
  case 'CanvasImage':
155
136
  if (inRect(element, x, y, left, top, localTileSize)) {
156
- return {
137
+ validElements.push({
157
138
  id: element.id,
158
139
  element: element,
159
140
  originalEvent: e
160
- };
141
+ });
161
142
  }
162
143
  break;
163
144
  case 'Circle':
164
145
  if (inCircle(element, x, y, left, top, localTileSize)) {
165
- return {
146
+ validElements.push({
166
147
  id: element.id,
167
148
  element: element,
168
149
  originalEvent: e
169
- };
150
+ });
170
151
  }
171
152
  break;
172
153
  case 'Polygon':
173
154
  if (inPoly(element, x, y, left, top, localTileSize)) {
174
- return {
155
+ validElements.push({
175
156
  id: element.id,
176
157
  element: element,
177
158
  originalEvent: e
178
- };
159
+ });
179
160
  }
180
161
  break;
181
162
  }
182
163
  }
183
- return null;
164
+ if (validElements.length === 0) {
165
+ return null;
166
+ }
167
+ if (validElements.length === 1) {
168
+ return validElements[0];
169
+ }
170
+ validElements.sort(function (a, b) {
171
+ if (a.zIndex > b.zIndex) {
172
+ return 1;
173
+ }
174
+ if (a.zIndex < b.zIndex) {
175
+ return -1;
176
+ }
177
+ if (a.id > b.id) {
178
+ return 1;
179
+ }
180
+ if (a.id < b.id) {
181
+ return -1;
182
+ }
183
+ return 0;
184
+ });
185
+ return validElements[0];
184
186
  }
185
187
 
186
188
  function elementClick(e, elements, tileSize, state) {
@@ -314,7 +316,7 @@ var CanvasObject = /*#__PURE__*/function () {
314
316
  this['z-index'] = zIndex;
315
317
  this.draggable = draggable;
316
318
  }
317
- _createClass(CanvasObject, [{
319
+ return _createClass(CanvasObject, [{
318
320
  key: "zIndex",
319
321
  get: function get() {
320
322
  return this['z-index'] || 0;
@@ -328,11 +330,9 @@ var CanvasObject = /*#__PURE__*/function () {
328
330
  return 'CanvasObject';
329
331
  }
330
332
  }]);
331
- return CanvasObject;
332
333
  }();
333
334
 
334
335
  var ColoredCanvasObject = /*#__PURE__*/function (_CanvasObject) {
335
- _inheritsLoose(ColoredCanvasObject, _CanvasObject);
336
336
  function ColoredCanvasObject(id, x, y, fill, stroke, zIndex, draggable) {
337
337
  var _this;
338
338
  _this = _CanvasObject.call(this, id, x, y, zIndex, draggable) || this;
@@ -340,17 +340,16 @@ var ColoredCanvasObject = /*#__PURE__*/function (_CanvasObject) {
340
340
  _this.stroke = stroke;
341
341
  return _this;
342
342
  }
343
- _createClass(ColoredCanvasObject, [{
343
+ _inheritsLoose(ColoredCanvasObject, _CanvasObject);
344
+ return _createClass(ColoredCanvasObject, [{
344
345
  key: "constructorName",
345
346
  get: function get() {
346
347
  return 'ColoredCanvasObject';
347
348
  }
348
349
  }]);
349
- return ColoredCanvasObject;
350
350
  }(CanvasObject);
351
351
 
352
352
  var Circle = /*#__PURE__*/function (_ColoredCanvasObject) {
353
- _inheritsLoose(Circle, _ColoredCanvasObject);
354
353
  function Circle(_ref) {
355
354
  var _this;
356
355
  var id = _ref.id,
@@ -365,17 +364,16 @@ var Circle = /*#__PURE__*/function (_ColoredCanvasObject) {
365
364
  _this.radius = radius;
366
365
  return _this;
367
366
  }
368
- _createClass(Circle, [{
367
+ _inheritsLoose(Circle, _ColoredCanvasObject);
368
+ return _createClass(Circle, [{
369
369
  key: "constructorName",
370
370
  get: function get() {
371
371
  return 'Circle';
372
372
  }
373
373
  }]);
374
- return Circle;
375
374
  }(ColoredCanvasObject);
376
375
 
377
376
  var CanvasImage = /*#__PURE__*/function (_CanvasObject) {
378
- _inheritsLoose(CanvasImage, _CanvasObject);
379
377
  function CanvasImage(_ref) {
380
378
  var _this;
381
379
  var id = _ref.id,
@@ -392,6 +390,7 @@ var CanvasImage = /*#__PURE__*/function (_CanvasObject) {
392
390
  _this.src = src;
393
391
  return _this;
394
392
  }
393
+ _inheritsLoose(CanvasImage, _CanvasObject);
395
394
  var _proto = CanvasImage.prototype;
396
395
  _proto.crop = function crop(x, y, width, height) {
397
396
  this.sx = sx;
@@ -399,17 +398,15 @@ var CanvasImage = /*#__PURE__*/function (_CanvasObject) {
399
398
  this.swidth = swidth;
400
399
  this.sheight = sheight;
401
400
  };
402
- _createClass(CanvasImage, [{
401
+ return _createClass(CanvasImage, [{
403
402
  key: "constructorName",
404
403
  get: function get() {
405
404
  return 'CanvasImage';
406
405
  }
407
406
  }]);
408
- return CanvasImage;
409
407
  }(CanvasObject);
410
408
 
411
409
  var Rect = /*#__PURE__*/function (_ColoredCanvasObject) {
412
- _inheritsLoose(Rect, _ColoredCanvasObject);
413
410
  function Rect(_ref) {
414
411
  var _this;
415
412
  var id = _ref.id,
@@ -426,17 +423,16 @@ var Rect = /*#__PURE__*/function (_ColoredCanvasObject) {
426
423
  _this.height = height;
427
424
  return _this;
428
425
  }
429
- _createClass(Rect, [{
426
+ _inheritsLoose(Rect, _ColoredCanvasObject);
427
+ return _createClass(Rect, [{
430
428
  key: "constructorName",
431
429
  get: function get() {
432
430
  return 'Rect';
433
431
  }
434
432
  }]);
435
- return Rect;
436
433
  }(ColoredCanvasObject);
437
434
 
438
435
  var Polygon = /*#__PURE__*/function (_ColoredCanvasObject) {
439
- _inheritsLoose(Polygon, _ColoredCanvasObject);
440
436
  function Polygon(_ref) {
441
437
  var _this;
442
438
  var id = _ref.id,
@@ -449,13 +445,13 @@ var Polygon = /*#__PURE__*/function (_ColoredCanvasObject) {
449
445
  _this.points = points;
450
446
  return _this;
451
447
  }
452
- _createClass(Polygon, [{
448
+ _inheritsLoose(Polygon, _ColoredCanvasObject);
449
+ return _createClass(Polygon, [{
453
450
  key: "constructorName",
454
451
  get: function get() {
455
452
  return 'Polygon';
456
453
  }
457
454
  }]);
458
- return Polygon;
459
455
  }(ColoredCanvasObject);
460
456
 
461
457
  function renderCircle(context, element, left, top, localTileSize) {
@@ -538,7 +534,7 @@ function renderCanvas(context, width, height, elements, tileSize, state) {
538
534
  }
539
535
 
540
536
  var _excluded = ["width", "height", "trackMouseMove", "minZoom", "maxZoom", "tileSize", "onClick", "onRightClick", "onHover", "onElementMoved", "onWheel", "onFrame", "lockXAxis", "lockYAxis", "smoothingQuality", "dragObjects", "deltaLeft", "deltaTop"];
541
- var elements = [];
537
+ var elements = {};
542
538
  function Canvas2D(_ref) {
543
539
  var width = _ref.width,
544
540
  height = _ref.height,
@@ -569,6 +565,9 @@ function Canvas2D(_ref) {
569
565
  _ref$deltaTop = _ref.deltaTop,
570
566
  deltaTop = _ref$deltaTop === void 0 ? 0 : _ref$deltaTop,
571
567
  otherProps = _objectWithoutPropertiesLoose(_ref, _excluded);
568
+ if (!elements[otherProps.id]) {
569
+ elements[otherProps.id] = [];
570
+ }
572
571
  var _useState = React.useState({}),
573
572
  state = _useState[0],
574
573
  setState = _useState[1];
@@ -610,7 +609,7 @@ function Canvas2D(_ref) {
610
609
  var onMouseMove = null;
611
610
  if (trackMouseMove) {
612
611
  onMouseMove = function onMouseMove(e) {
613
- return mouseMove(e, elements, tileSize, state, setState, lockXAxis, lockYAxis, dragObjects, onElementMoved, onHover);
612
+ return mouseMove(e, elements[otherProps.id], tileSize, state, setState, lockXAxis, lockYAxis, dragObjects, onElementMoved, onHover);
614
613
  };
615
614
  }
616
615
  var onWheelFn = function onWheelFn(e) {
@@ -624,13 +623,13 @@ function Canvas2D(_ref) {
624
623
  var onClickFn = null;
625
624
  if (onClick) {
626
625
  onClickFn = function onClickFn(e) {
627
- return onClick(elementClick(e, elements, tileSize, state));
626
+ return onClick(elementClick(e, elements[otherProps.id], tileSize, state));
628
627
  };
629
628
  }
630
629
  React.useEffect(function () {
631
630
  function onRightClickEvent(e) {
632
631
  e.preventDefault();
633
- onRightClick(elementRightClick(e, elements, tileSize, state));
632
+ onRightClick(elementRightClick(e, elements[otherProps.id], tileSize, state));
634
633
  }
635
634
  if (onRightClick && state.canvas) {
636
635
  state.canvas.addEventListener('contextmenu', onRightClickEvent);
@@ -645,8 +644,8 @@ function Canvas2D(_ref) {
645
644
  if (!shouldRender) {
646
645
  return;
647
646
  }
648
- elements = onFrame();
649
- var sortedElements = sortElements(elements);
647
+ elements[otherProps.id] = onFrame();
648
+ var sortedElements = sortElements(elements[otherProps.id]);
650
649
  if (state.context) {
651
650
  renderCanvas(state.context, width, height, sortedElements, tileSize, state);
652
651
  }