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