如何沿着给定路径拖动形状
我有这个简单的虚拟文件,我用它来进行一些测试。预期的结果是沿着路径拖动红色圆圈。问题是我不知道如何关联这两个形状。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<script src="raphael-min.js"></script>
</head>
<body>
<script type="text/javascript">
// Creates canvas 320 × 200 at 10, 50
var r = Raphael(10, 50, 320, 200);
var p = r.path("M100,100c0,50 100-50 100,0c0,50 -100-50 -100,0z").attr({stroke: "#ddd"}),
e = r.ellipse(104, 100, 4, 4).attr({stroke: "none", fill: "#f00"}),
/*var c = r.circle(100, 100, 50).attr({
fill: "hsb(.8, 1, 1)",
stroke: "none",
opacity: .5
});*/
var start = function () {
// storing original coordinates
this.ox = this.attr("cx");
this.oy = this.attr("cy");
this.attr({opacity: 1});
},
move = function (dx, dy) {
// move will be called with dx and dy
this.attr({cx: this.ox + dx, cy: this.oy + dy});
},
up = function () {
// restoring state
this.attr({opacity: 1});
};
e.drag(move, start, up);
</script>
</body>
</html>
I've this simple dummy file that I'm using to do some testing. The intended result is to drag the red circle along the path. The thing is that I can't figure out how to associate both shapes.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<script src="raphael-min.js"></script>
</head>
<body>
<script type="text/javascript">
// Creates canvas 320 × 200 at 10, 50
var r = Raphael(10, 50, 320, 200);
var p = r.path("M100,100c0,50 100-50 100,0c0,50 -100-50 -100,0z").attr({stroke: "#ddd"}),
e = r.ellipse(104, 100, 4, 4).attr({stroke: "none", fill: "#f00"}),
/*var c = r.circle(100, 100, 50).attr({
fill: "hsb(.8, 1, 1)",
stroke: "none",
opacity: .5
});*/
var start = function () {
// storing original coordinates
this.ox = this.attr("cx");
this.oy = this.attr("cy");
this.attr({opacity: 1});
},
move = function (dx, dy) {
// move will be called with dx and dy
this.attr({cx: this.ox + dx, cy: this.oy + dy});
},
up = function () {
// restoring state
this.attr({opacity: 1});
};
e.drag(move, start, up);
</script>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有具体指定您希望交互如何进行,因此我使用了对我来说最自然的方式。
我们可以假设该点必须保留在路径上,因此它的位置必须由
for some
l
给出。为了找到l
,我们可以搜索曲线与光标位置之间距离的局部最小值。我们使用l0
初始化搜索,其中l0
是定义点位置的l
当前 值。请参阅此处的 JSfiddle 以获取工作示例:
http://jsfiddle.net/fuzic/kKLtH/
这里是代码:
You didn't specify exactly how you want the interaction to work, so I used what feels most natural to me.
We can assume the dot must remain on the path, so its position must be given by
for some
l
. To findl
we can search for the local minimum of the distance between the curve and the cursor position. We initialize the search withl0
wherel0
is the value ofl
currently defining the location of the dot.See the JSfiddle here for a working example:
http://jsfiddle.net/fuzic/kKLtH/
Here is the code:
圆形对象的中心有一个 x,y 坐标,还有一个半径。要确保圆保持在直线上,只需找到圆心与直线本身的交点即可。
为此,您需要存储线条的起点和终点坐标。然后使用直线方程:y = mx + b,您可以找到斜率和 y 截距。一旦有了直线的函数,您就可以通过插入不同的 x 值来生成圆的新坐标。
另外,通过将圆的 x,y 坐标插入函数中,您可以检查圆是否在线上。
A circle object has an x,y coordinate for its center, and a radius. To make sure the circle remains on the line, simply find the intersection of the center of the circle and the line itself.
To do this, you will need to store the start and end coordinates of your line. Then using the equation of a line: y = mx + b, you can find the slope and y-intercept. Once you have a function for the line, you can generate new coordinates for the circle by plugging in different values of x.
Also, by plugging in the x,y coordinates of the circle into your function, you can check to see if the circle is on the line.