gpt4 book ai didi

javascript - 将 KineticJS 形状保留在 Canvas 内

转载 作者:行者123 更新时间:2023-11-28 20:45:02 25 4
gpt4 key购买 nike

在使用 KineticJS 的拖放功能时,是否有一种简单的方法可以确保您的形状保留在 Canvas 内?

标准,形状离开 Canvas 一点,直到鼠标碰到 Canvas 边框。我希望它是形状边框,而不是鼠标。

有办法实现这一点吗?

最佳答案

使用KineticJS tutorial中解释的dragBoundFunc

这是一个JS fiddle

代码:

dragBoundFunc: function(pos) {
console.log(bbox.getWidth());
var xBound = stage.getWidth() - bbox.getWidth();
var yBound = stage.getHeight() - bbox.getHeight();

// Check X boundries
if (pos.x > xBound) {
var newX = xBound;
} else if (pos.x <= 0) {
var newX = 0;
} else {
var newX = pos.x;
}

// Check Y boundries
if (pos.y > yBound) {
var newY = yBound;
} else if (pos.y <= 0) {
var newY = 0;
} else {
var newY = pos.y;
}

return {
x: newX,
y: newY
};
}

关于javascript - 将 KineticJS 形状保留在 Canvas 内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13645845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com