canvas2d-wrapper 1.9.1 → 1.11.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.
@@ -49,7 +49,7 @@ function _objectWithoutPropertiesLoose(r, e) {
49
49
  if (null == r) return {};
50
50
  var t = {};
51
51
  for (var n in r) if ({}.hasOwnProperty.call(r, n)) {
52
- if (e.includes(n)) continue;
52
+ if (-1 !== e.indexOf(n)) continue;
53
53
  t[n] = r[n];
54
54
  }
55
55
  return t;
@@ -134,7 +134,11 @@ function collideElement(e, elements, left, top, tileSize, zoom) {
134
134
  validElements.push({
135
135
  id: element.id,
136
136
  element: element,
137
- originalEvent: e
137
+ originalEvent: e,
138
+ posOnMap: {
139
+ x: element.x,
140
+ y: element.y
141
+ }
138
142
  });
139
143
  }
140
144
  break;
@@ -143,7 +147,11 @@ function collideElement(e, elements, left, top, tileSize, zoom) {
143
147
  validElements.push({
144
148
  id: element.id,
145
149
  element: element,
146
- originalEvent: e
150
+ originalEvent: e,
151
+ posOnMap: {
152
+ x: element.x,
153
+ y: element.y
154
+ }
147
155
  });
148
156
  }
149
157
  break;
@@ -152,7 +160,11 @@ function collideElement(e, elements, left, top, tileSize, zoom) {
152
160
  validElements.push({
153
161
  id: element.id,
154
162
  element: element,
155
- originalEvent: e
163
+ originalEvent: e,
164
+ posOnMap: {
165
+ x: element.x,
166
+ y: element.y
167
+ }
156
168
  });
157
169
  }
158
170
  break;
@@ -192,7 +204,11 @@ function elementClick(e, elements, tileSize, state) {
192
204
  return {
193
205
  id: null,
194
206
  element: null,
195
- originalEvent: e
207
+ originalEvent: e,
208
+ posOnMap: {
209
+ x: Math.floor(left / tileSize / state.zoom),
210
+ y: Math.floor(top / tileSize / state.zoom)
211
+ }
196
212
  };
197
213
  }
198
214
 
@@ -206,7 +222,11 @@ function elementRightClick(e, elements, tileSize, state) {
206
222
  return {
207
223
  id: null,
208
224
  element: null,
209
- originalEvent: e
225
+ originalEvent: e,
226
+ posOnMap: {
227
+ x: Math.floor(left / tileSize / state.zoom),
228
+ y: Math.floor(top / tileSize / state.zoom)
229
+ }
210
230
  };
211
231
  }
212
232
 
@@ -283,6 +303,33 @@ function mouseWheel(event, props, setProps, minZoom, maxZoom) {
283
303
  }));
284
304
  }
285
305
 
306
+ function calcRatioForMinimap(elements, width, height, minimapWidth, minimapHeight, tileSize) {
307
+ var elementsX = elements.map(function (e) {
308
+ if (Array.isArray(e.points)) {
309
+ return e.points.map(function (p) {
310
+ return p.x * tileSize;
311
+ });
312
+ }
313
+ return [e.x * tileSize];
314
+ }).flat();
315
+ var minX = Math.min.apply(Math, elementsX);
316
+ var maxX = Math.max.apply(Math, elementsX);
317
+ width = Math.max(2 * maxX, -2 * minX, width);
318
+ var elementsY = elements.map(function (e) {
319
+ if (Array.isArray(e.points)) {
320
+ return e.points.map(function (p) {
321
+ return p.y * tileSize;
322
+ });
323
+ }
324
+ return [e.y * tileSize];
325
+ }).flat();
326
+ var minY = Math.min.apply(Math, elementsY);
327
+ var maxY = Math.max.apply(Math, elementsY);
328
+ height = Math.max(2 * maxY, -2 * minY, height);
329
+ var ratio = Math.max(width / minimapWidth, height / minimapHeight);
330
+ return ratio;
331
+ }
332
+
286
333
  function sortElements(elements) {
287
334
  elements.sort(function (a, b) {
288
335
  if (a.zIndex !== b.zIndex) {
@@ -332,6 +379,39 @@ var CanvasObject = /*#__PURE__*/function () {
332
379
  }]);
333
380
  }();
334
381
 
382
+ var CanvasImage = /*#__PURE__*/function (_CanvasObject) {
383
+ function CanvasImage(_ref) {
384
+ var _this;
385
+ var id = _ref.id,
386
+ x = _ref.x,
387
+ y = _ref.y,
388
+ width = _ref.width,
389
+ height = _ref.height,
390
+ src = _ref.src,
391
+ zIndex = _ref.zIndex,
392
+ draggable = _ref.draggable;
393
+ _this = _CanvasObject.call(this, id, x, y, zIndex, draggable) || this;
394
+ _this.width = width;
395
+ _this.height = height;
396
+ _this.src = src;
397
+ return _this;
398
+ }
399
+ _inheritsLoose(CanvasImage, _CanvasObject);
400
+ var _proto = CanvasImage.prototype;
401
+ _proto.crop = function crop(sx, swidth, sheight) {
402
+ this.sx = sx;
403
+ this.sy = sx;
404
+ this.swidth = swidth;
405
+ this.sheight = sheight;
406
+ };
407
+ return _createClass(CanvasImage, [{
408
+ key: "constructorName",
409
+ get: function get() {
410
+ return 'CanvasImage';
411
+ }
412
+ }]);
413
+ }(CanvasObject);
414
+
335
415
  var ColoredCanvasObject = /*#__PURE__*/function (_CanvasObject) {
336
416
  function ColoredCanvasObject(id, x, y, fill, stroke, zIndex, draggable) {
337
417
  var _this;
@@ -373,83 +453,76 @@ var Circle = /*#__PURE__*/function (_ColoredCanvasObject) {
373
453
  }]);
374
454
  }(ColoredCanvasObject);
375
455
 
376
- var CanvasImage = /*#__PURE__*/function (_CanvasObject) {
377
- function CanvasImage(_ref) {
456
+ var LinePath = /*#__PURE__*/function (_ColoredCanvasObject) {
457
+ function LinePath(_ref) {
378
458
  var _this;
379
459
  var id = _ref.id,
380
- x = _ref.x,
381
- y = _ref.y,
382
- width = _ref.width,
383
- height = _ref.height,
384
- src = _ref.src,
460
+ lineWidth = _ref.lineWidth,
461
+ points = _ref.points,
462
+ stroke = _ref.stroke,
385
463
  zIndex = _ref.zIndex,
386
- draggable = _ref.draggable;
387
- _this = _CanvasObject.call(this, id, x, y, zIndex, draggable) || this;
388
- _this.width = width;
389
- _this.height = height;
390
- _this.src = src;
464
+ smoothCorners = _ref.smoothCorners,
465
+ smoothCornersRadius = _ref.smoothCornersRadius;
466
+ _this = _ColoredCanvasObject.call(this, id, points[0].x, points[0].y, 'none', stroke, zIndex, false) || this;
467
+ _this.points = points;
468
+ _this.lineWidth = lineWidth;
469
+ _this.smoothCorners = smoothCorners;
470
+ _this.smoothCornersRadius = smoothCornersRadius;
391
471
  return _this;
392
472
  }
393
- _inheritsLoose(CanvasImage, _CanvasObject);
394
- var _proto = CanvasImage.prototype;
395
- _proto.crop = function crop(sx, swidth, sheight) {
396
- this.sx = sx;
397
- this.sy = sx;
398
- this.swidth = swidth;
399
- this.sheight = sheight;
400
- };
401
- return _createClass(CanvasImage, [{
473
+ _inheritsLoose(LinePath, _ColoredCanvasObject);
474
+ return _createClass(LinePath, [{
402
475
  key: "constructorName",
403
476
  get: function get() {
404
- return 'CanvasImage';
477
+ return 'LinePath';
405
478
  }
406
479
  }]);
407
- }(CanvasObject);
480
+ }(ColoredCanvasObject);
408
481
 
409
- var Rect = /*#__PURE__*/function (_ColoredCanvasObject) {
410
- function Rect(_ref) {
482
+ var Polygon = /*#__PURE__*/function (_ColoredCanvasObject) {
483
+ function Polygon(_ref) {
411
484
  var _this;
412
485
  var id = _ref.id,
413
- x = _ref.x,
414
- y = _ref.y,
415
- width = _ref.width,
416
- height = _ref.height,
486
+ points = _ref.points,
417
487
  fill = _ref.fill,
418
488
  stroke = _ref.stroke,
419
489
  zIndex = _ref.zIndex,
420
490
  draggable = _ref.draggable;
421
- _this = _ColoredCanvasObject.call(this, id, x, y, fill, stroke, zIndex, draggable) || this;
422
- _this.width = width;
423
- _this.height = height;
491
+ _this = _ColoredCanvasObject.call(this, id, points[0].x, points[0].y, fill, stroke, zIndex, draggable) || this;
492
+ _this.points = points;
424
493
  return _this;
425
494
  }
426
- _inheritsLoose(Rect, _ColoredCanvasObject);
427
- return _createClass(Rect, [{
495
+ _inheritsLoose(Polygon, _ColoredCanvasObject);
496
+ return _createClass(Polygon, [{
428
497
  key: "constructorName",
429
498
  get: function get() {
430
- return 'Rect';
499
+ return 'Polygon';
431
500
  }
432
501
  }]);
433
502
  }(ColoredCanvasObject);
434
503
 
435
- var Polygon = /*#__PURE__*/function (_ColoredCanvasObject) {
436
- function Polygon(_ref) {
504
+ var Rect = /*#__PURE__*/function (_ColoredCanvasObject) {
505
+ function Rect(_ref) {
437
506
  var _this;
438
507
  var id = _ref.id,
439
- points = _ref.points,
508
+ x = _ref.x,
509
+ y = _ref.y,
510
+ width = _ref.width,
511
+ height = _ref.height,
440
512
  fill = _ref.fill,
441
513
  stroke = _ref.stroke,
442
514
  zIndex = _ref.zIndex,
443
515
  draggable = _ref.draggable;
444
- _this = _ColoredCanvasObject.call(this, id, points[0].x, points[0].y, fill, stroke, zIndex, draggable) || this;
445
- _this.points = points;
516
+ _this = _ColoredCanvasObject.call(this, id, x, y, fill, stroke, zIndex, draggable) || this;
517
+ _this.width = width;
518
+ _this.height = height;
446
519
  return _this;
447
520
  }
448
- _inheritsLoose(Polygon, _ColoredCanvasObject);
449
- return _createClass(Polygon, [{
521
+ _inheritsLoose(Rect, _ColoredCanvasObject);
522
+ return _createClass(Rect, [{
450
523
  key: "constructorName",
451
524
  get: function get() {
452
- return 'Polygon';
525
+ return 'Rect';
453
526
  }
454
527
  }]);
455
528
  }(ColoredCanvasObject);
@@ -479,16 +552,55 @@ function renderImage(context, element, left, top, localTileSize) {
479
552
  }
480
553
  }
481
554
 
482
- function renderRect(context, element, left, top, localTileSize) {
483
- if (element.fill) {
484
- context.fillRect(left + element.x * localTileSize, top + element.y * localTileSize, element.width * localTileSize, element.height * localTileSize);
485
- }
486
- if (element.stroke) {
487
- context.strokeRect(left + element.x * localTileSize, top + element.y * localTileSize, element.width * localTileSize, element.height * localTileSize);
555
+ function renderLinePath(context, element, left, top, localTileSize) {
556
+ var defaultLineWidth = context.lineWidth;
557
+ var points = element.points.map(function (p) {
558
+ return {
559
+ x: left + p.x * localTileSize,
560
+ y: top + p.y * localTileSize
561
+ };
562
+ });
563
+ if (points.length < 2) return;
564
+ context.beginPath();
565
+ context.moveTo(points[0].x, points[0].y);
566
+ if (element.smoothCorners) {
567
+ for (var i = 1; i < points.length - 1; i++) {
568
+ var prev = points[i - 1];
569
+ var curr = points[i];
570
+ var next = points[i + 1];
571
+ var dx1 = curr.x - prev.x;
572
+ var dy1 = curr.y - prev.y;
573
+ var dx2 = next.x - curr.x;
574
+ var dy2 = next.y - curr.y;
575
+ var len1 = Math.hypot(dx1, dy1);
576
+ var len2 = Math.hypot(dx2, dy2);
577
+ var offset1X = dx1 / len1 * element.smoothCornersRadius;
578
+ var offset1Y = dy1 / len1 * element.smoothCornersRadius;
579
+ var offset2X = dx2 / len2 * element.smoothCornersRadius;
580
+ var offset2Y = dy2 / len2 * element.smoothCornersRadius;
581
+ var cornerStart = {
582
+ x: curr.x - offset1X,
583
+ y: curr.y - offset1Y
584
+ };
585
+ var cornerEnd = {
586
+ x: curr.x + offset2X,
587
+ y: curr.y + offset2Y
588
+ };
589
+ context.lineTo(cornerStart.x, cornerStart.y);
590
+ context.quadraticCurveTo(curr.x, curr.y, cornerEnd.x, cornerEnd.y);
591
+ }
592
+ context.lineTo(points[points.length - 1].x, points[points.length - 1].y);
593
+ } else {
594
+ for (var _i = 1; _i < points.length; _i++) {
595
+ context.lineTo(points[_i].x, points[_i].y);
596
+ }
488
597
  }
598
+ context.lineWidth = element.lineWidth * localTileSize;
599
+ context.stroke();
600
+ context.lineWidth = defaultLineWidth;
489
601
  }
490
602
 
491
- function renderRect$1(context, element, left, top, localTileSize) {
603
+ function renderPolygon(context, element, left, top, localTileSize) {
492
604
  context.beginPath();
493
605
  context.moveTo(left + element.points[0].x * localTileSize, top + element.points[0].y * localTileSize);
494
606
  for (var i = 0; i < element.points.length; i++) {
@@ -503,10 +615,21 @@ function renderRect$1(context, element, left, top, localTileSize) {
503
615
  }
504
616
  }
505
617
 
618
+ function renderRect(context, element, left, top, localTileSize) {
619
+ if (element.fill) {
620
+ context.fillRect(left + element.x * localTileSize, top + element.y * localTileSize, element.width * localTileSize, element.height * localTileSize);
621
+ }
622
+ if (element.stroke) {
623
+ context.strokeRect(left + element.x * localTileSize, top + element.y * localTileSize, element.width * localTileSize, element.height * localTileSize);
624
+ }
625
+ }
626
+
506
627
  var _renderFn;
507
- var renderFn = (_renderFn = {}, _renderFn[new Circle({}).constructorName] = renderCircle, _renderFn[new CanvasImage({}).constructorName] = renderImage, _renderFn[new Rect({}).constructorName] = renderRect, _renderFn[new Polygon({
628
+ var renderFn = (_renderFn = {}, _renderFn[new Circle({}).constructorName] = renderCircle, _renderFn[new CanvasImage({}).constructorName] = renderImage, _renderFn[new LinePath({
629
+ points: [{}]
630
+ }).constructorName] = renderLinePath, _renderFn[new Polygon({
508
631
  points: [{}]
509
- }).constructorName] = renderRect$1, _renderFn);
632
+ }).constructorName] = renderPolygon, _renderFn[new Rect({}).constructorName] = renderRect, _renderFn);
510
633
  function renderCanvas(context, width, height, elements, tileSize, state) {
511
634
  var left = state.left + state.deltaLeft;
512
635
  var top = state.top + state.deltaTop;
@@ -533,7 +656,7 @@ function renderCanvas(context, width, height, elements, tileSize, state) {
533
656
  }
534
657
  }
535
658
 
536
- var _excluded = ["width", "height", "trackMouseMove", "minZoom", "maxZoom", "tileSize", "onClick", "onRightClick", "onHover", "onElementMoved", "onWheel", "onFrame", "lockXAxis", "lockYAxis", "smoothingQuality", "dragObjects", "deltaLeft", "deltaTop"];
659
+ var _excluded = ["width", "height", "trackMouseMove", "minZoom", "maxZoom", "tileSize", "onClick", "onRightClick", "onHover", "onElementMoved", "onWheel", "onFrame", "lockXAxis", "lockYAxis", "smoothingQuality", "dragObjects", "deltaLeft", "deltaTop", "showMinimap", "minimapWidth", "minimapHeight", "minimapFilter"];
537
660
  var elements = {};
538
661
  function Canvas2D(_ref) {
539
662
  var width = _ref.width,
@@ -564,6 +687,16 @@ function Canvas2D(_ref) {
564
687
  deltaLeft = _ref$deltaLeft === void 0 ? 0 : _ref$deltaLeft,
565
688
  _ref$deltaTop = _ref.deltaTop,
566
689
  deltaTop = _ref$deltaTop === void 0 ? 0 : _ref$deltaTop,
690
+ _ref$showMinimap = _ref.showMinimap,
691
+ showMinimap = _ref$showMinimap === void 0 ? false : _ref$showMinimap,
692
+ _ref$minimapWidth = _ref.minimapWidth,
693
+ minimapWidth = _ref$minimapWidth === void 0 ? 240 : _ref$minimapWidth,
694
+ _ref$minimapHeight = _ref.minimapHeight,
695
+ minimapHeight = _ref$minimapHeight === void 0 ? 120 : _ref$minimapHeight,
696
+ _ref$minimapFilter = _ref.minimapFilter,
697
+ minimapFilter = _ref$minimapFilter === void 0 ? function (e) {
698
+ return true;
699
+ } : _ref$minimapFilter,
567
700
  otherProps = _objectWithoutPropertiesLoose(_ref, _excluded);
568
701
  if (!elements[otherProps.id]) {
569
702
  elements[otherProps.id] = [];
@@ -580,15 +713,29 @@ function Canvas2D(_ref) {
580
713
  context.imageSmoothingEnabled = true;
581
714
  context.imageSmoothingQuality = smoothingQuality;
582
715
  }
583
- setState({
584
- boundingClientRect: canvas.getBoundingClientRect(),
585
- canvas: canvas,
586
- context: context,
587
- left: width / 2,
588
- top: height / 2,
589
- width: width,
590
- height: height,
591
- zoom: 1
716
+ setState(function (s) {
717
+ return _extends({}, s, {
718
+ boundingClientRect: canvas.getBoundingClientRect(),
719
+ canvas: canvas,
720
+ context: context,
721
+ left: width / 2,
722
+ top: height / 2,
723
+ width: width,
724
+ height: height,
725
+ zoom: 1
726
+ });
727
+ });
728
+ }
729
+ }, []);
730
+ var canvasMapRef = useCallback(function (minimapCanvas) {
731
+ if (minimapCanvas !== null) {
732
+ var minimapContext = minimapCanvas.getContext('2d');
733
+ setState(function (s) {
734
+ return _extends({}, s, {
735
+ boundingClientRect: minimapCanvas.getBoundingClientRect(),
736
+ minimapCanvas: minimapCanvas,
737
+ minimapContext: minimapContext
738
+ });
592
739
  });
593
740
  }
594
741
  }, []);
@@ -649,14 +796,25 @@ function Canvas2D(_ref) {
649
796
  if (state.context) {
650
797
  renderCanvas(state.context, width, height, sortedElements, tileSize, state);
651
798
  }
799
+ if (showMinimap && state.minimapContext) {
800
+ var filteredElementsList = sortedElements.filter(minimapFilter);
801
+ var ratio = calcRatioForMinimap(filteredElementsList, width, height, minimapWidth, minimapHeight, tileSize * state.zoom);
802
+ renderCanvas(state.minimapContext, minimapWidth, minimapHeight, filteredElementsList, tileSize / ratio, {
803
+ left: minimapWidth / 2,
804
+ top: minimapHeight / 2,
805
+ deltaLeft: 0,
806
+ deltaTop: 0,
807
+ zoom: 1
808
+ });
809
+ }
652
810
  window.requestAnimationFrame(render);
653
811
  }
654
812
  window.requestAnimationFrame(render);
655
813
  return function () {
656
814
  shouldRender = false;
657
815
  };
658
- }, [state.context, onFrame]);
659
- return /*#__PURE__*/React.createElement("canvas", _extends({
816
+ }, [state.left, state.top, state.deltaLeft, state.deltaTop, state.zoom, state.context, onFrame]);
817
+ return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("canvas", _extends({
660
818
  ref: canvasRef,
661
819
  width: width,
662
820
  height: height,
@@ -664,7 +822,12 @@ function Canvas2D(_ref) {
664
822
  onWheel: onWheelFn,
665
823
  onClick: onClickFn,
666
824
  className: "canvas2d-wrapper"
667
- }, otherProps));
825
+ }, otherProps)), showMinimap && /*#__PURE__*/React.createElement("canvas", {
826
+ ref: canvasMapRef,
827
+ width: minimapWidth,
828
+ height: minimapHeight,
829
+ className: "canvas2d-wrapper-minimap"
830
+ }));
668
831
  }
669
832
 
670
833
  function preloadImages(images) {
@@ -695,14 +858,20 @@ function useKeyboard() {
695
858
  keyboard = _useState[0],
696
859
  setKeyboard = _useState[1];
697
860
  useEffect(function () {
698
- document.addEventListener('keydown', function (e) {
861
+ var keydown = function keydown(e) {
699
862
  keyboard[e.key] = true;
700
863
  setKeyboard(JSON.parse(JSON.stringify(keyboard)));
701
- });
702
- document.addEventListener('keyup', function (e) {
864
+ };
865
+ document.addEventListener('keydown', keydown);
866
+ var keyup = function keyup(e) {
703
867
  keyboard[e.key] = false;
704
868
  setKeyboard(JSON.parse(JSON.stringify(keyboard)));
705
- });
869
+ };
870
+ document.addEventListener('keyup', keyup);
871
+ return function () {
872
+ document.removeEventListener('keydown', keydown);
873
+ document.removeEventListener('keyup', keyup);
874
+ };
706
875
  }, []);
707
876
  return keyboard;
708
877
  }
@@ -774,5 +943,5 @@ function useWindowDimensions() {
774
943
  return windowDimensions;
775
944
  }
776
945
 
777
- export { Canvas2D, CanvasImage, CanvasObject, Circle, Polygon, Rect, preloadImages, useGamepad, useKeyboard, useMousePosition, useWindowDimensions };
946
+ export { Canvas2D, CanvasImage, CanvasObject, Circle, LinePath, Polygon, Rect, preloadImages, useGamepad, useKeyboard, useMousePosition, useWindowDimensions };
778
947
  //# sourceMappingURL=index.modern.js.map