简体   繁体   English

react-konva dragBound 一个可缩放的舞台

[英]react-konva dragBound a zoomable stage

I am trying to make an application with react-konva in which an image covers the entire stage and the stage is draggable and zoomable.我正在尝试使用 react-konva 制作一个应用程序,其中图像覆盖整个舞台,并且舞台是可拖动和可缩放的。 So I am looking for a way to bound the drag to the edges of the image.所以我正在寻找一种将拖动绑定到图像边缘的方法。


I looked at this demo and added a dragBoundFunc in my code but using this code I am able to dragBound my stage on the top and left edges only.我查看了这个演示并在我的代码中添加了一个dragBoundFunc,但是使用此代码我只能在顶部和左侧边缘拖动绑定我的舞台。

Demo: https://codesandbox.io/s/tender-chatterjee-ih31j演示: https://codesandbox.io/s/tender-chatterjee-ih31j

This may work:这可能有效:

const { stageWidth, stageHeight, stageScale } = this.state;

const x = Math.min(0, Math.max(pos.x, stageWidth * (1 - stageScale)));
const y = Math.min(0, Math.max(pos.y, stageHeight* (1 - stageScale)));

return {
      x, y
};

Also, you may need to update zoom function to call bound func.此外,您可能需要更新 zoom function 以调用绑定函数。

Demo: https://codesandbox.io/s/react-konva-dragbound-for-stage-ne71c?file=/src/Demo.jsx演示: https://codesandbox.io/s/react-konva-dragbound-for-stage-ne71c?file=/src/Demo.jsx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM