canvas2d-wrapper 1.12.1 → 1.14.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/README.md +1 -1
- package/canvas2d-wrapper.d.ts +6 -2
- package/dist/index.js +95 -28
- package/dist/index.js.map +1 -1
- package/dist/index.modern.js +95 -28
- package/dist/index.modern.js.map +1 -1
- package/package.json +5 -5
package/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# canvas2d-wrapper
|
2
2
|
|
3
|
-
> A React Wrapper to
|
3
|
+
> A React Wrapper to draw on an HTML5 canvas with mouse move and zoom abilities.
|
4
4
|
|
5
5
|
[](https://www.npmjs.com/package/canvas2d-wrapper)
|
6
6
|
[](https://sonar.dysnomia.studio/dashboard?id=canvas2d-wrapper) [](https://sonar.dysnomia.studio/dashboard?id=canvas2d-wrapper) [](https://sonar.dysnomia.studio/dashboard?id=canvas2d-wrapper)
|
package/canvas2d-wrapper.d.ts
CHANGED
@@ -101,7 +101,8 @@ declare module "canvas2d-wrapper" {
|
|
101
101
|
src: string,
|
102
102
|
zIndex?: number,
|
103
103
|
draggable?: boolean,
|
104
|
-
hasCollisions?: boolean
|
104
|
+
hasCollisions?: boolean,
|
105
|
+
rotation?: number,
|
105
106
|
});
|
106
107
|
|
107
108
|
crop(sx: number, swidth: number, sheight: number);
|
@@ -109,6 +110,7 @@ declare module "canvas2d-wrapper" {
|
|
109
110
|
src: string;
|
110
111
|
width: number;
|
111
112
|
height: number;
|
113
|
+
rotation?: number;
|
112
114
|
}
|
113
115
|
|
114
116
|
export class Circle extends ColoredCanvasObject {
|
@@ -170,11 +172,13 @@ declare module "canvas2d-wrapper" {
|
|
170
172
|
stroke?: string,
|
171
173
|
zIndex?: number,
|
172
174
|
draggable?: boolean,
|
173
|
-
hasCollisions?: boolean
|
175
|
+
hasCollisions?: boolean,
|
176
|
+
rotation?: number,
|
174
177
|
});
|
175
178
|
|
176
179
|
width: number;
|
177
180
|
height: number;
|
181
|
+
rotation?: number;
|
178
182
|
}
|
179
183
|
|
180
184
|
// Functions
|
package/dist/index.js
CHANGED
@@ -117,10 +117,18 @@ function inPoly(element, x, y, mouseX, mouseY, localTileSize) {
|
|
117
117
|
return counter;
|
118
118
|
}
|
119
119
|
|
120
|
-
function inRect(element, x, y, left, top, localTileSize) {
|
120
|
+
function inRect(element, x, y, left, top, localTileSize, rotation) {
|
121
121
|
var width = element.width * localTileSize;
|
122
122
|
var height = element.height * localTileSize;
|
123
|
-
|
123
|
+
var rotatedLeft = left;
|
124
|
+
var rotatedTop = top;
|
125
|
+
if (rotation) {
|
126
|
+
var centerX = x + width / 2;
|
127
|
+
var centerY = y + height / 2;
|
128
|
+
rotatedLeft = centerX + (left - centerX) * Math.cos(-rotation) - (top - centerY) * Math.sin(-rotation);
|
129
|
+
rotatedTop = centerY + (top - centerY) * Math.cos(-rotation) + (left - centerX) * Math.sin(-rotation);
|
130
|
+
}
|
131
|
+
return x <= rotatedLeft && rotatedLeft <= x + width && y <= rotatedTop && rotatedTop <= y + height;
|
124
132
|
}
|
125
133
|
|
126
134
|
function onLinePath(element, left, top, localTileSize) {
|
@@ -174,7 +182,7 @@ function collideElement(e, elements, left, top, tileSize, zoom) {
|
|
174
182
|
switch (element.constructorName) {
|
175
183
|
case 'Rect':
|
176
184
|
case 'CanvasImage':
|
177
|
-
if (inRect(element, x, y, left, top, localTileSize)) {
|
185
|
+
if (inRect(element, x, y, left, top, localTileSize, element.rotation)) {
|
178
186
|
validElements.push({
|
179
187
|
id: element.id,
|
180
188
|
element: element,
|
@@ -248,12 +256,28 @@ function collideElement(e, elements, left, top, tileSize, zoom) {
|
|
248
256
|
}
|
249
257
|
return 0;
|
250
258
|
});
|
251
|
-
return validElements[
|
259
|
+
return validElements[0];
|
260
|
+
}
|
261
|
+
|
262
|
+
function computeEventPositions(props, event, tileSize) {
|
263
|
+
var left = -props.left - props.deltaLeft + event.pageX - event.target.offsetLeft;
|
264
|
+
var top = -props.top - props.deltaTop + event.pageY - event.target.offsetTop;
|
265
|
+
var posOnMap = {
|
266
|
+
x: Math.floor(left / tileSize / props.zoom),
|
267
|
+
y: Math.floor(top / tileSize / props.zoom)
|
268
|
+
};
|
269
|
+
return {
|
270
|
+
left: left,
|
271
|
+
top: top,
|
272
|
+
posOnMap: posOnMap
|
273
|
+
};
|
252
274
|
}
|
253
275
|
|
254
276
|
function elementClick(e, elements, tileSize, state) {
|
255
|
-
var
|
256
|
-
|
277
|
+
var _computeEventPosition = computeEventPositions(state, e, tileSize),
|
278
|
+
left = _computeEventPosition.left,
|
279
|
+
top = _computeEventPosition.top,
|
280
|
+
posOnMap = _computeEventPosition.posOnMap;
|
257
281
|
var clickedElement = collideElement(e, elements, left, top, tileSize, state.zoom);
|
258
282
|
if (clickedElement !== null) {
|
259
283
|
return clickedElement;
|
@@ -262,10 +286,7 @@ function elementClick(e, elements, tileSize, state) {
|
|
262
286
|
id: null,
|
263
287
|
element: null,
|
264
288
|
originalEvent: e,
|
265
|
-
posOnMap:
|
266
|
-
x: Math.floor(left / tileSize / state.zoom),
|
267
|
-
y: Math.floor(top / tileSize / state.zoom)
|
268
|
-
}
|
289
|
+
posOnMap: posOnMap
|
269
290
|
};
|
270
291
|
}
|
271
292
|
|
@@ -293,8 +314,10 @@ var unselectTimeoutId = 0;
|
|
293
314
|
var UNSELECT_TIMEOUT_MS = 300;
|
294
315
|
function mouseMove(event, elements, tileSize, props, setProps, lockXAxis, lockYAxis, dragObjects, onElementMoved, onHover) {
|
295
316
|
var newProps = _extends({}, props);
|
296
|
-
var
|
297
|
-
|
317
|
+
var _computeEventPosition = computeEventPositions(props, event, tileSize),
|
318
|
+
left = _computeEventPosition.left,
|
319
|
+
top = _computeEventPosition.top,
|
320
|
+
posOnMap = _computeEventPosition.posOnMap;
|
298
321
|
if (event.nativeEvent.buttons === LEFT_BUTTON && !!props.prevX) {
|
299
322
|
var moved = false;
|
300
323
|
if (dragObjects) {
|
@@ -309,7 +332,7 @@ function mouseMove(event, elements, tileSize, props, setProps, lockXAxis, lockYA
|
|
309
332
|
onElementMoved(selectedObject, event.screenX - props.prevX, event.screenY - props.prevY);
|
310
333
|
}
|
311
334
|
if (onHover) {
|
312
|
-
onHover(null);
|
335
|
+
onHover(null, posOnMap);
|
313
336
|
}
|
314
337
|
}
|
315
338
|
}
|
@@ -332,10 +355,7 @@ function mouseMove(event, elements, tileSize, props, setProps, lockXAxis, lockYA
|
|
332
355
|
}
|
333
356
|
} else {
|
334
357
|
if (onHover) {
|
335
|
-
onHover(collideElement(event, elements, left, top, tileSize, props.zoom),
|
336
|
-
x: left,
|
337
|
-
y: top
|
338
|
-
});
|
358
|
+
onHover(collideElement(event, elements, left, top, tileSize, props.zoom), posOnMap);
|
339
359
|
}
|
340
360
|
selectedObject = null;
|
341
361
|
}
|
@@ -448,11 +468,13 @@ var CanvasImage = /*#__PURE__*/function (_CanvasObject) {
|
|
448
468
|
src = _ref.src,
|
449
469
|
zIndex = _ref.zIndex,
|
450
470
|
draggable = _ref.draggable,
|
451
|
-
hasCollisions = _ref.hasCollisions
|
471
|
+
hasCollisions = _ref.hasCollisions,
|
472
|
+
rotation = _ref.rotation;
|
452
473
|
_this = _CanvasObject.call(this, id, x, y, zIndex, draggable, hasCollisions) || this;
|
453
474
|
_this.width = width;
|
454
475
|
_this.height = height;
|
455
476
|
_this.src = src;
|
477
|
+
_this.rotation = rotation;
|
456
478
|
return _this;
|
457
479
|
}
|
458
480
|
_inheritsLoose(CanvasImage, _CanvasObject);
|
@@ -575,10 +597,12 @@ var Rect = /*#__PURE__*/function (_ColoredCanvasObject) {
|
|
575
597
|
stroke = _ref.stroke,
|
576
598
|
zIndex = _ref.zIndex,
|
577
599
|
draggable = _ref.draggable,
|
578
|
-
hasCollisions = _ref.hasCollisions
|
600
|
+
hasCollisions = _ref.hasCollisions,
|
601
|
+
rotation = _ref.rotation;
|
579
602
|
_this = _ColoredCanvasObject.call(this, id, x, y, fill, stroke, zIndex, draggable, hasCollisions) || this;
|
580
603
|
_this.width = width;
|
581
604
|
_this.height = height;
|
605
|
+
_this.rotation = rotation;
|
582
606
|
return _this;
|
583
607
|
}
|
584
608
|
_inheritsLoose(Rect, _ColoredCanvasObject);
|
@@ -608,10 +632,26 @@ function renderImage(context, element, left, top, localTileSize) {
|
|
608
632
|
__canvas2dWrapper__.imgCache[element.src] = new Image();
|
609
633
|
__canvas2dWrapper__.imgCache[element.src].src = element.src;
|
610
634
|
}
|
611
|
-
|
612
|
-
|
635
|
+
var positionX = left + element.x * localTileSize;
|
636
|
+
var positionY = top + element.y * localTileSize;
|
637
|
+
var width = element.width * localTileSize;
|
638
|
+
var height = element.height * localTileSize;
|
639
|
+
if (element.rotation) {
|
640
|
+
context.save();
|
641
|
+
context.translate(positionX + width / 2, positionY + height / 2);
|
642
|
+
context.rotate(element.rotation);
|
643
|
+
if (!element.sx) {
|
644
|
+
context.drawImage(__canvas2dWrapper__.imgCache[element.src], -width / 2, -height / 2, width, height);
|
645
|
+
} else {
|
646
|
+
context.drawImage(__canvas2dWrapper__.imgCache[element.src], element.sx, element.sy, element.swidth, element.sheight, -element.swidth / 2, -element.sheight / 2, width, height);
|
647
|
+
}
|
648
|
+
context.restore();
|
613
649
|
} else {
|
614
|
-
|
650
|
+
if (!element.sx) {
|
651
|
+
context.drawImage(__canvas2dWrapper__.imgCache[element.src], positionX, positionY, width, height);
|
652
|
+
} else {
|
653
|
+
context.drawImage(__canvas2dWrapper__.imgCache[element.src], element.sx, element.sy, element.swidth, element.sheight, positionX, positionY, width, height);
|
654
|
+
}
|
615
655
|
}
|
616
656
|
}
|
617
657
|
|
@@ -679,11 +719,28 @@ function renderPolygon(context, element, left, top, localTileSize) {
|
|
679
719
|
}
|
680
720
|
|
681
721
|
function renderRect(context, element, left, top, localTileSize) {
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
722
|
+
var positionX = left + element.x * localTileSize;
|
723
|
+
var positionY = top + element.y * localTileSize;
|
724
|
+
var width = element.width * localTileSize;
|
725
|
+
var height = element.height * localTileSize;
|
726
|
+
if (element.rotation) {
|
727
|
+
context.save();
|
728
|
+
context.translate(positionX + width / 2, positionY + height / 2);
|
729
|
+
context.rotate(element.rotation);
|
730
|
+
if (element.fill) {
|
731
|
+
context.fillRect(-width / 2, -width / 2, width, height);
|
732
|
+
}
|
733
|
+
if (element.stroke) {
|
734
|
+
context.strokeRect(-width / 2, -width / 2, width, height);
|
735
|
+
}
|
736
|
+
context.restore();
|
737
|
+
} else {
|
738
|
+
if (element.fill) {
|
739
|
+
context.fillRect(positionX, positionY, width, height);
|
740
|
+
}
|
741
|
+
if (element.stroke) {
|
742
|
+
context.strokeRect(positionX, positionY, width, height);
|
743
|
+
}
|
687
744
|
}
|
688
745
|
}
|
689
746
|
|
@@ -861,7 +918,7 @@ function Canvas2D(_ref) {
|
|
861
918
|
}
|
862
919
|
if (showMinimap && state.minimapContext) {
|
863
920
|
var filteredElementsList = sortedElements.filter(minimapFilter);
|
864
|
-
var ratio = calcRatioForMinimap(filteredElementsList, width, height, minimapWidth, minimapHeight, tileSize
|
921
|
+
var ratio = calcRatioForMinimap(filteredElementsList, width, height, minimapWidth, minimapHeight, tileSize);
|
865
922
|
renderCanvas(state.minimapContext, minimapWidth, minimapHeight, filteredElementsList, tileSize / ratio, {
|
866
923
|
left: minimapWidth / 2,
|
867
924
|
top: minimapHeight / 2,
|
@@ -877,6 +934,16 @@ function Canvas2D(_ref) {
|
|
877
934
|
shouldRender = false;
|
878
935
|
};
|
879
936
|
}, [state.left, state.top, state.deltaLeft, state.deltaTop, state.zoom, state.context, onFrame]);
|
937
|
+
React.useEffect(function () {
|
938
|
+
setState(function (s) {
|
939
|
+
return _extends({}, s, {
|
940
|
+
left: width / 2,
|
941
|
+
top: height / 2,
|
942
|
+
width: width,
|
943
|
+
height: height
|
944
|
+
});
|
945
|
+
});
|
946
|
+
}, [width, height]);
|
880
947
|
return /*#__PURE__*/React__default.createElement(React__default.Fragment, null, /*#__PURE__*/React__default.createElement("canvas", _extends({
|
881
948
|
ref: canvasRef,
|
882
949
|
width: width,
|