Skip to content

Commit e66e528

Browse files
committed
Extract dragged sprites' drawables in screen space
1 parent d20b68c commit e66e528

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/containers/stage.jsx

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -304,25 +304,25 @@ class Stage extends React.Component {
304304
}
305305
this.setState({mouseDownTimeoutId: null});
306306
}
307-
drawDragCanvas (drawableData) {
307+
drawDragCanvas (drawableData, x, y) {
308308
const {
309-
data,
310-
width,
311-
height,
312-
x,
313-
y
309+
imageData,
310+
x: boundsX,
311+
y: boundsY,
312+
width: boundsWidth,
313+
height: boundsHeight
314314
} = drawableData;
315-
this.dragCanvas.width = width;
316-
this.dragCanvas.height = height;
317-
// Need to convert uint8array from WebGL readPixels into Uint8ClampedArray
318-
// for ImageData constructor. Shares underlying buffer, so it is fast.
319-
const imageData = new ImageData(
320-
new Uint8ClampedArray(data.buffer), width, height);
315+
this.dragCanvas.width = imageData.width;
316+
this.dragCanvas.height = imageData.height;
317+
// On high-DPI devices, the canvas size in layout-pixels is not equal to the size of the extracted data.
318+
this.dragCanvas.style.width = `${boundsWidth}px`;
319+
this.dragCanvas.style.height = `${boundsHeight}px`;
320+
321321
this.dragCanvas.getContext('2d').putImageData(imageData, 0, 0);
322322
// Position so that pick location is at (0, 0) so that positionDragCanvas()
323323
// can use translation to move to mouse position smoothly.
324-
this.dragCanvas.style.left = `${-x}px`;
325-
this.dragCanvas.style.top = `${-y}px`;
324+
this.dragCanvas.style.left = `${boundsX - x}px`;
325+
this.dragCanvas.style.top = `${boundsY - y}px`;
326326
this.dragCanvas.style.display = 'block';
327327
}
328328
clearDragCanvas () {
@@ -350,16 +350,20 @@ class Stage extends React.Component {
350350
target.goToFront();
351351

352352
// Extract the drawable art
353-
const drawableData = this.renderer.extractDrawable(drawableId, x, y);
353+
const drawableData = this.renderer.extractDrawableScreenSpace(drawableId);
354+
355+
const [scratchMouseX, scratchMouseY] = this.getScratchCoords(x, y);
356+
const offsetX = target.x - scratchMouseX;
357+
const offsetY = -(target.y + scratchMouseY);
354358

355359
this.props.vm.startDrag(targetId);
356360
this.setState({
357361
isDragging: true,
358362
dragId: targetId,
359-
dragOffset: drawableData.scratchOffset
363+
dragOffset: [offsetX, offsetY]
360364
});
361365
if (this.props.useEditorDragStyle) {
362-
this.drawDragCanvas(drawableData);
366+
this.drawDragCanvas(drawableData, x, y);
363367
this.positionDragCanvas(x, y);
364368
this.props.vm.postSpriteInfo({visible: false});
365369
}

0 commit comments

Comments
 (0)