【发布时间】:2014-08-11 18:52:29
【问题描述】:
我有一个简单的虚拟文件,我用它来做一些测试。预期的结果是沿着路径拖动红色圆圈。问题是我不知道如何关联这两种形状。
<!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>
【问题讨论】:
-
主要思想是获得类似于 animateAlong 但拖动而不是动画的东西。
-
这是一个供人们使用的 jsFiddle:jsfiddle.net/8T9NQ
-
一般情况下,需要将光标位置投影到路径上,找到路径上最近的点。很可能在最接近光标的路径上有两个(或更多,或无限)点,因此您需要消除歧义以选择最佳的。
标签: javascript graphics svg raphael