canvas2d-wrapper 1.10.0 → 1.12.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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2021-2022 Dysnomia
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2021-2025 Dysnomia
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -41,6 +41,10 @@ declare module "canvas2d-wrapper" {
41
41
  // Additional props
42
42
  id: string,
43
43
  className?: string,
44
+ showMinimap?: boolean,
45
+ minimapWidth?: number,
46
+ minimapHeight?: number,
47
+ minimapFilter?: (e: CanvasObject) => boolean,
44
48
  };
45
49
 
46
50
  export function Canvas2D(props: Canvas2DProps);
@@ -52,7 +56,8 @@ declare module "canvas2d-wrapper" {
52
56
  x: number,
53
57
  y: number,
54
58
  zIndex?: number,
55
- draggable?: boolean
59
+ draggable?: boolean,
60
+ hasCollisions?: boolean
56
61
  );
57
62
 
58
63
  get constructorName(): string;
@@ -65,6 +70,7 @@ declare module "canvas2d-wrapper" {
65
70
  x: number;
66
71
  y: number;
67
72
  draggable: boolean | undefined;
73
+ hasCollisions: boolean | undefined;
68
74
  }
69
75
 
70
76
  export class ColoredCanvasObject extends CanvasObject {
@@ -77,7 +83,8 @@ declare module "canvas2d-wrapper" {
77
83
  fill?: string,
78
84
  stroke?: string,
79
85
  zIndex?: number,
80
- draggable?: boolean
86
+ draggable?: boolean,
87
+ hasCollisions?: boolean
81
88
  );
82
89
 
83
90
  fill: string | undefined;
@@ -94,6 +101,7 @@ declare module "canvas2d-wrapper" {
94
101
  src: string,
95
102
  zIndex?: number,
96
103
  draggable?: boolean,
104
+ hasCollisions?: boolean
97
105
  });
98
106
 
99
107
  crop(sx: number, swidth: number, sheight: number);
@@ -113,6 +121,7 @@ declare module "canvas2d-wrapper" {
113
121
  stroke?: string,
114
122
  zIndex?: number,
115
123
  draggable?: boolean,
124
+ hasCollisions?: boolean
116
125
  });
117
126
 
118
127
  radius: number;
@@ -127,6 +136,7 @@ declare module "canvas2d-wrapper" {
127
136
  zIndex?: number,
128
137
  smoothCorners?: boolean,
129
138
  smoothCornersRadius?: number,
139
+ hasCollisions?: boolean
130
140
  });
131
141
 
132
142
  points: Position2D[];
@@ -143,6 +153,7 @@ declare module "canvas2d-wrapper" {
143
153
  stroke?: string,
144
154
  zIndex?: number,
145
155
  draggable?: boolean,
156
+ hasCollisions?: boolean
146
157
  });
147
158
 
148
159
  points: Position2D[];
@@ -159,6 +170,7 @@ declare module "canvas2d-wrapper" {
159
170
  stroke?: string,
160
171
  zIndex?: number,
161
172
  draggable?: boolean,
173
+ hasCollisions?: boolean
162
174
  });
163
175
 
164
176
  width: number;
package/dist/index.js CHANGED
@@ -123,11 +123,52 @@ function inRect(element, x, y, left, top, localTileSize) {
123
123
  return x <= left && left <= x + width && y <= top && top <= y + height;
124
124
  }
125
125
 
126
+ function onLinePath(element, left, top, localTileSize) {
127
+ var radius = element.lineWidth;
128
+ if (inCircle({
129
+ radius: radius
130
+ }, element.points[0].x * localTileSize, element.points[0].y * localTileSize, left, top, localTileSize)) {
131
+ return true;
132
+ }
133
+ if (inCircle({
134
+ radius: radius
135
+ }, element.points[element.points.length - 1].x * localTileSize, element.points[element.points.length - 1].y * localTileSize, left, top, localTileSize)) {
136
+ return true;
137
+ }
138
+ for (var i = 1; i < element.points.length; i++) {
139
+ var from = {
140
+ x: element.points[i - 1].x * localTileSize,
141
+ y: element.points[i - 1].y * localTileSize
142
+ };
143
+ var to = {
144
+ x: element.points[i].x * localTileSize,
145
+ y: element.points[i].y * localTileSize
146
+ };
147
+ var diffX = from.x - to.x;
148
+ var diffY = from.y - to.y;
149
+ var length = Math.sqrt(diffX * diffX + diffY * diffY);
150
+ var dot = ((left - from.x) * (to.x - from.x) + (top - from.y) * (to.y - from.y)) / Math.pow(length, 2);
151
+ var point = {
152
+ x: from.x + dot * (to.x - from.x),
153
+ y: from.y + dot * (to.y - from.y)
154
+ };
155
+ if (inCircle({
156
+ radius: radius
157
+ }, point.x, point.y, left, top, localTileSize)) {
158
+ return true;
159
+ }
160
+ }
161
+ return false;
162
+ }
163
+
126
164
  function collideElement(e, elements, left, top, tileSize, zoom) {
127
165
  var localTileSize = calcTileSize(tileSize, zoom);
128
166
  var validElements = [];
129
167
  for (var _iterator = _createForOfIteratorHelperLoose(elements), _step; !(_step = _iterator()).done;) {
130
168
  var element = _step.value;
169
+ if (!element.hasCollisions) {
170
+ continue;
171
+ }
131
172
  var x = element.x * localTileSize;
132
173
  var y = element.y * localTileSize;
133
174
  switch (element.constructorName) {
@@ -171,6 +212,19 @@ function collideElement(e, elements, left, top, tileSize, zoom) {
171
212
  });
172
213
  }
173
214
  break;
215
+ case 'LinePath':
216
+ if (onLinePath(element, left, top, localTileSize)) {
217
+ validElements.push({
218
+ id: element.id,
219
+ element: element,
220
+ originalEvent: e,
221
+ posOnMap: {
222
+ x: element.x,
223
+ y: element.y
224
+ }
225
+ });
226
+ }
227
+ break;
174
228
  }
175
229
  }
176
230
  if (validElements.length === 0) {
@@ -306,6 +360,33 @@ function mouseWheel(event, props, setProps, minZoom, maxZoom) {
306
360
  }));
307
361
  }
308
362
 
363
+ function calcRatioForMinimap(elements, width, height, minimapWidth, minimapHeight, tileSize) {
364
+ var elementsX = elements.map(function (e) {
365
+ if (Array.isArray(e.points)) {
366
+ return e.points.map(function (p) {
367
+ return p.x * tileSize;
368
+ });
369
+ }
370
+ return [e.x * tileSize];
371
+ }).flat();
372
+ var minX = Math.min.apply(Math, elementsX);
373
+ var maxX = Math.max.apply(Math, elementsX);
374
+ width = Math.max(2 * maxX, -2 * minX, width);
375
+ var elementsY = elements.map(function (e) {
376
+ if (Array.isArray(e.points)) {
377
+ return e.points.map(function (p) {
378
+ return p.y * tileSize;
379
+ });
380
+ }
381
+ return [e.y * tileSize];
382
+ }).flat();
383
+ var minY = Math.min.apply(Math, elementsY);
384
+ var maxY = Math.max.apply(Math, elementsY);
385
+ height = Math.max(2 * maxY, -2 * minY, height);
386
+ var ratio = Math.max(width / minimapWidth, height / minimapHeight);
387
+ return ratio;
388
+ }
389
+
309
390
  function sortElements(elements) {
310
391
  elements.sort(function (a, b) {
311
392
  if (a.zIndex !== b.zIndex) {
@@ -332,12 +413,13 @@ function sortElements(elements) {
332
413
  }
333
414
 
334
415
  var CanvasObject = /*#__PURE__*/function () {
335
- function CanvasObject(id, x, y, zIndex, draggable) {
416
+ function CanvasObject(id, x, y, zIndex, draggable, hasCollisions) {
336
417
  this.id = id;
337
418
  this.x = x;
338
419
  this.y = y;
339
420
  this['z-index'] = zIndex;
340
421
  this.draggable = draggable;
422
+ this.hasCollisions = hasCollisions;
341
423
  }
342
424
  return _createClass(CanvasObject, [{
343
425
  key: "zIndex",
@@ -365,8 +447,9 @@ var CanvasImage = /*#__PURE__*/function (_CanvasObject) {
365
447
  height = _ref.height,
366
448
  src = _ref.src,
367
449
  zIndex = _ref.zIndex,
368
- draggable = _ref.draggable;
369
- _this = _CanvasObject.call(this, id, x, y, zIndex, draggable) || this;
450
+ draggable = _ref.draggable,
451
+ hasCollisions = _ref.hasCollisions;
452
+ _this = _CanvasObject.call(this, id, x, y, zIndex, draggable, hasCollisions) || this;
370
453
  _this.width = width;
371
454
  _this.height = height;
372
455
  _this.src = src;
@@ -389,9 +472,9 @@ var CanvasImage = /*#__PURE__*/function (_CanvasObject) {
389
472
  }(CanvasObject);
390
473
 
391
474
  var ColoredCanvasObject = /*#__PURE__*/function (_CanvasObject) {
392
- function ColoredCanvasObject(id, x, y, fill, stroke, zIndex, draggable) {
475
+ function ColoredCanvasObject(id, x, y, fill, stroke, zIndex, draggable, hasCollisions) {
393
476
  var _this;
394
- _this = _CanvasObject.call(this, id, x, y, zIndex, draggable) || this;
477
+ _this = _CanvasObject.call(this, id, x, y, zIndex, draggable, hasCollisions) || this;
395
478
  _this.fill = fill;
396
479
  _this.stroke = stroke;
397
480
  return _this;
@@ -415,8 +498,9 @@ var Circle = /*#__PURE__*/function (_ColoredCanvasObject) {
415
498
  fill = _ref.fill,
416
499
  stroke = _ref.stroke,
417
500
  zIndex = _ref.zIndex,
418
- draggable = _ref.draggable;
419
- _this = _ColoredCanvasObject.call(this, id, x, y, fill, stroke, zIndex, draggable) || this;
501
+ draggable = _ref.draggable,
502
+ hasCollisions = _ref.hasCollisions;
503
+ _this = _ColoredCanvasObject.call(this, id, x, y, fill, stroke, zIndex, draggable, hasCollisions) || this;
420
504
  _this.radius = radius;
421
505
  return _this;
422
506
  }
@@ -438,8 +522,9 @@ var LinePath = /*#__PURE__*/function (_ColoredCanvasObject) {
438
522
  stroke = _ref.stroke,
439
523
  zIndex = _ref.zIndex,
440
524
  smoothCorners = _ref.smoothCorners,
441
- smoothCornersRadius = _ref.smoothCornersRadius;
442
- _this = _ColoredCanvasObject.call(this, id, points[0].x, points[0].y, 'none', stroke, zIndex, false) || this;
525
+ smoothCornersRadius = _ref.smoothCornersRadius,
526
+ hasCollisions = _ref.hasCollisions;
527
+ _this = _ColoredCanvasObject.call(this, id, points[0].x, points[0].y, 'none', stroke, zIndex, false, hasCollisions) || this;
443
528
  _this.points = points;
444
529
  _this.lineWidth = lineWidth;
445
530
  _this.smoothCorners = smoothCorners;
@@ -463,8 +548,9 @@ var Polygon = /*#__PURE__*/function (_ColoredCanvasObject) {
463
548
  fill = _ref.fill,
464
549
  stroke = _ref.stroke,
465
550
  zIndex = _ref.zIndex,
466
- draggable = _ref.draggable;
467
- _this = _ColoredCanvasObject.call(this, id, points[0].x, points[0].y, fill, stroke, zIndex, draggable) || this;
551
+ draggable = _ref.draggable,
552
+ hasCollisions = _ref.hasCollisions;
553
+ _this = _ColoredCanvasObject.call(this, id, points[0].x, points[0].y, fill, stroke, zIndex, draggable, hasCollisions) || this;
468
554
  _this.points = points;
469
555
  return _this;
470
556
  }
@@ -488,8 +574,9 @@ var Rect = /*#__PURE__*/function (_ColoredCanvasObject) {
488
574
  fill = _ref.fill,
489
575
  stroke = _ref.stroke,
490
576
  zIndex = _ref.zIndex,
491
- draggable = _ref.draggable;
492
- _this = _ColoredCanvasObject.call(this, id, x, y, fill, stroke, zIndex, draggable) || this;
577
+ draggable = _ref.draggable,
578
+ hasCollisions = _ref.hasCollisions;
579
+ _this = _ColoredCanvasObject.call(this, id, x, y, fill, stroke, zIndex, draggable, hasCollisions) || this;
493
580
  _this.width = width;
494
581
  _this.height = height;
495
582
  return _this;
@@ -571,7 +658,7 @@ function renderLinePath(context, element, left, top, localTileSize) {
571
658
  context.lineTo(points[_i].x, points[_i].y);
572
659
  }
573
660
  }
574
- context.lineWidth = element.lineWidth;
661
+ context.lineWidth = element.lineWidth * localTileSize;
575
662
  context.stroke();
576
663
  context.lineWidth = defaultLineWidth;
577
664
  }
@@ -632,7 +719,7 @@ function renderCanvas(context, width, height, elements, tileSize, state) {
632
719
  }
633
720
  }
634
721
 
635
- var _excluded = ["width", "height", "trackMouseMove", "minZoom", "maxZoom", "tileSize", "onClick", "onRightClick", "onHover", "onElementMoved", "onWheel", "onFrame", "lockXAxis", "lockYAxis", "smoothingQuality", "dragObjects", "deltaLeft", "deltaTop"];
722
+ var _excluded = ["width", "height", "trackMouseMove", "minZoom", "maxZoom", "tileSize", "onClick", "onRightClick", "onHover", "onElementMoved", "onWheel", "onFrame", "lockXAxis", "lockYAxis", "smoothingQuality", "dragObjects", "deltaLeft", "deltaTop", "showMinimap", "minimapWidth", "minimapHeight", "minimapFilter"];
636
723
  var elements = {};
637
724
  function Canvas2D(_ref) {
638
725
  var width = _ref.width,
@@ -663,6 +750,16 @@ function Canvas2D(_ref) {
663
750
  deltaLeft = _ref$deltaLeft === void 0 ? 0 : _ref$deltaLeft,
664
751
  _ref$deltaTop = _ref.deltaTop,
665
752
  deltaTop = _ref$deltaTop === void 0 ? 0 : _ref$deltaTop,
753
+ _ref$showMinimap = _ref.showMinimap,
754
+ showMinimap = _ref$showMinimap === void 0 ? false : _ref$showMinimap,
755
+ _ref$minimapWidth = _ref.minimapWidth,
756
+ minimapWidth = _ref$minimapWidth === void 0 ? 240 : _ref$minimapWidth,
757
+ _ref$minimapHeight = _ref.minimapHeight,
758
+ minimapHeight = _ref$minimapHeight === void 0 ? 120 : _ref$minimapHeight,
759
+ _ref$minimapFilter = _ref.minimapFilter,
760
+ minimapFilter = _ref$minimapFilter === void 0 ? function (e) {
761
+ return true;
762
+ } : _ref$minimapFilter,
666
763
  otherProps = _objectWithoutPropertiesLoose(_ref, _excluded);
667
764
  if (!elements[otherProps.id]) {
668
765
  elements[otherProps.id] = [];
@@ -679,15 +776,29 @@ function Canvas2D(_ref) {
679
776
  context.imageSmoothingEnabled = true;
680
777
  context.imageSmoothingQuality = smoothingQuality;
681
778
  }
682
- setState({
683
- boundingClientRect: canvas.getBoundingClientRect(),
684
- canvas: canvas,
685
- context: context,
686
- left: width / 2,
687
- top: height / 2,
688
- width: width,
689
- height: height,
690
- zoom: 1
779
+ setState(function (s) {
780
+ return _extends({}, s, {
781
+ boundingClientRect: canvas.getBoundingClientRect(),
782
+ canvas: canvas,
783
+ context: context,
784
+ left: width / 2,
785
+ top: height / 2,
786
+ width: width,
787
+ height: height,
788
+ zoom: 1
789
+ });
790
+ });
791
+ }
792
+ }, []);
793
+ var canvasMapRef = React.useCallback(function (minimapCanvas) {
794
+ if (minimapCanvas !== null) {
795
+ var minimapContext = minimapCanvas.getContext('2d');
796
+ setState(function (s) {
797
+ return _extends({}, s, {
798
+ boundingClientRect: minimapCanvas.getBoundingClientRect(),
799
+ minimapCanvas: minimapCanvas,
800
+ minimapContext: minimapContext
801
+ });
691
802
  });
692
803
  }
693
804
  }, []);
@@ -748,6 +859,17 @@ function Canvas2D(_ref) {
748
859
  if (state.context) {
749
860
  renderCanvas(state.context, width, height, sortedElements, tileSize, state);
750
861
  }
862
+ if (showMinimap && state.minimapContext) {
863
+ var filteredElementsList = sortedElements.filter(minimapFilter);
864
+ var ratio = calcRatioForMinimap(filteredElementsList, width, height, minimapWidth, minimapHeight, tileSize * state.zoom);
865
+ renderCanvas(state.minimapContext, minimapWidth, minimapHeight, filteredElementsList, tileSize / ratio, {
866
+ left: minimapWidth / 2,
867
+ top: minimapHeight / 2,
868
+ deltaLeft: 0,
869
+ deltaTop: 0,
870
+ zoom: 1
871
+ });
872
+ }
751
873
  window.requestAnimationFrame(render);
752
874
  }
753
875
  window.requestAnimationFrame(render);
@@ -755,7 +877,7 @@ function Canvas2D(_ref) {
755
877
  shouldRender = false;
756
878
  };
757
879
  }, [state.left, state.top, state.deltaLeft, state.deltaTop, state.zoom, state.context, onFrame]);
758
- return /*#__PURE__*/React__default.createElement("canvas", _extends({
880
+ return /*#__PURE__*/React__default.createElement(React__default.Fragment, null, /*#__PURE__*/React__default.createElement("canvas", _extends({
759
881
  ref: canvasRef,
760
882
  width: width,
761
883
  height: height,
@@ -763,7 +885,12 @@ function Canvas2D(_ref) {
763
885
  onWheel: onWheelFn,
764
886
  onClick: onClickFn,
765
887
  className: "canvas2d-wrapper"
766
- }, otherProps));
888
+ }, otherProps)), showMinimap && /*#__PURE__*/React__default.createElement("canvas", {
889
+ ref: canvasMapRef,
890
+ width: minimapWidth,
891
+ height: minimapHeight,
892
+ className: "canvas2d-wrapper-minimap"
893
+ }));
767
894
  }
768
895
 
769
896
  function preloadImages(images) {