检查对象是否超出画布(Raphael.js)
目前我正在使用 Raphael.js 在画布周围拖放一个圆圈。但是,我想禁止用户将圆圈拖出画布。我的代码通常如下:
<script type="text/javascript" src="Javascript/raphael.js"></script>
<script type="text/javascript">
window.onload = function() {
//create canvas matching the image's dimensions
var paper = new Raphael(document.getElementById('canvas_container'), <%=width%>, <%=height%>);
//put the schematic image onto the canvas
paper.image("<%=path%>",0,0,<%=width%>,<%=height%>);
//create the marker on the canvas
var c = paper.circle(<%=markerx%>, <%=markery%>, 5).attr({
fill: "#800517",
stroke: "none"
});
var start = function () {
// storing original coordinates
this.ox = this.attr("cx");
this.oy = this.attr("cy");
},
move = function (dx, dy) {
// move will be called with dx and dy
if (this.ox + dx >= <%=width%> || this.oy + dy >= <%=height%>){
this.attr({cx: <%=width%>, cy: <%=height%>});
}else{
this.attr({cx: this.ox + dx, cy: this.oy + dy});
}
},
up = function () {
// restoring state
};
c.drag(move, start, up);
}
</script>
我想知道是否有人尝试过做这样的事情并设法做到正确。
currently I am using Raphael.js to drag and drop a circle around a canvas. However, I would like to disallow the user to drag the circle out of the canvas. My code is generally as follows:
<script type="text/javascript" src="Javascript/raphael.js"></script>
<script type="text/javascript">
window.onload = function() {
//create canvas matching the image's dimensions
var paper = new Raphael(document.getElementById('canvas_container'), <%=width%>, <%=height%>);
//put the schematic image onto the canvas
paper.image("<%=path%>",0,0,<%=width%>,<%=height%>);
//create the marker on the canvas
var c = paper.circle(<%=markerx%>, <%=markery%>, 5).attr({
fill: "#800517",
stroke: "none"
});
var start = function () {
// storing original coordinates
this.ox = this.attr("cx");
this.oy = this.attr("cy");
},
move = function (dx, dy) {
// move will be called with dx and dy
if (this.ox + dx >= <%=width%> || this.oy + dy >= <%=height%>){
this.attr({cx: <%=width%>, cy: <%=height%>});
}else{
this.attr({cx: this.ox + dx, cy: this.oy + dy});
}
},
up = function () {
// restoring state
};
c.drag(move, start, up);
}
</script>
I was wondering if there was anyone out there who has tried to do something like this and managed to get it right.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
if
语句,如果您不想移动,则无需执行任何操作。另外,不要忘记圆的半径,也不要忘记检查值是否太小,而不仅仅是太大:jsFiddle 示例
但我更喜欢使用
min
和max
:所以,这是一个完整的工作示例 (jsFiddle)
With an
if
statement, you don't have to do anything if you don't want to move. Also, don't forget about the radius of the circle, and don't forget about checking for the value being to small, not just too big:jsFiddle example
But I like to use
min
andmax
more:So, here's a full working example (jsFiddle)
嘿伙计们,我设法解决了它。我意识到我不应该使用 this.attr 来设置 cx 和 cy 相反,我应该使用 c.attr 来识别我的圈子的 attr。
Hey guys, i managed to solve it. I realised it was I should not have used this.attr to set the cx and cy instead I should've used c.attr to identify the attr to my circle instead.