【问题标题】:How to implement drag and drop image only in canvas bounds如何仅在画布边界中实现拖放图像
【发布时间】:2013-03-13 16:57:04
【问题描述】:
我已经实现了拖放功能,但我现在需要的是一个可以禁用从 Canvas 上拖出的功能。
当我的图片在左上角时,坐标 x: 0, y: 0,但如果向左移动坐标变为负数(例如 X: -1, -2 ... -100 )。
我想要做的是不允许图像在画布之外移动。
欢迎任何想法甚至更好的代码
【问题讨论】:
标签:
javascript
jquery
html
canvas
【解决方案1】:
你几乎回答了。如果图片 x 位置是 -2,则只需将其恢复为 0。
// Checks if the picture is beyond the x boundary's...
if (picture.x < 0) {
picture.x = 0;
} else if ((picture.x + picture.width > canvas.width) {
picture.x = canvas.width - picture.width;
}
同样适用于 y 轴。