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