2011-12-04 76 views
4

我设法绘制四个不同的曲线与拉斐尔库的例子。现在,我想创建一个带有多个句柄的单曲线。在这个例子中,我如何添加更多的句柄。三把手贝塞尔曲线错位控制点

<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
     <title>Bezier curve</title> 
      <style> 
     #holder { 
      height: 100%; 
      left: 100%; 
      margin: -100% 0 0 -100%; 
      position: absolute; 
      top: 100%; 
      width: 100%; 
     } 
     </style> 
     <script src='jquery.js'></script> 
     <script src="raphael.js"></script> 
     <script> 
      $('document').ready(function() { 
       var r = Raphael("holder", window.innerWidth, window.innerHeight) 

       function curve(x, y, ax, ay, bx, by, zx, zy, color) { 
        var path = [["M", x, y], ["C", ax, ay, bx, by, zx, zy]], 
         path2 = [["M", x, y], ["L", ax, ay], ["M", bx, by], ["L", zx, zy]], 
         curve = r.path(path).attr({stroke: color || Raphael.getColor(), "stroke-width": 4, "stroke-linecap": "round"}), 
         controls = r.set(
          r.path(path2).attr({stroke: "#ccc", "stroke-dasharray": ". ","stroke-width":2}), 
          r.circle(x, y, 5).attr({fill: "#9F2200", stroke: "none"}), 
          r.circle(ax, ay, 5).attr({fill: "#9F2200", stroke: "none"}), 
          r.circle(bx, by, 5).attr({fill: "#9F2200", stroke: "none"}), 
          r.circle(zx, zy, 5).attr({fill: "#9F2200", stroke: "none"}) 
         ); 
        controls[1].update = function (x, y) { 
         var X = this.attr("cx") + x, 
          Y = this.attr("cy") + y; 
         this.attr({cx: X, cy: Y}); 
         path[0][1] = X; 
         path[0][2] = Y; 
         path2[0][2] = X; 
         path2[0][2] = Y; 
         controls[2].update(x, y); 
        }; 
        controls[2].update = function (x, y) { 
         var X = this.attr("cx") + x, 
          Y = this.attr("cy") + y; 
         this.attr({cx: X, cy: Y}); 
         path[1][3] = X; 
         path[1][2] = Y; 
         path2[1][4] = X; 
         path2[1][2] = Y; 
         curve.attr({path: path}); 
         controls[0].attr({path: path2}); 
        }; 
        controls[3].update = function (x, y) { 
         var X = this.attr("cx") + x, 
          Y = this.attr("cy") + y; 
         this.attr({cx: X, cy: Y}); 
         path[1][3] = X; 
         path[1][4] = Y; 
         path2[2][5] = X; 
         path2[2][2] = Y; 
         curve.attr({path: path}); 
         controls[0].attr({path: path2}); 
        }; 
        controls[4].update = function (x, y) { 
         var X = this.attr("cx") + x, 
          Y = this.attr("cy") + y; 
         this.attr({cx: X, cy: Y}); 
         path[1][5] = X; 
         path[1][6] = Y; 
         path2[3][6] = X; 
         path2[3][2] = Y; 
         controls[3].update(x, y); 
        }; 
        controls.drag(move, up); 
       } 
       function move(dx, dy) { 
        this.update(dx - (this.dx || 0), dy - (this.dy || 0)); 
        console.log(this.dx,this.dy); 
        this.dx = dx; 
        this.dy = dy; 
       } 
       function up() { 
        this.dx = this.dy = 0; 
       } 
       curve(70, 100, 110, 100, 130, 200, 170, 200, "hsb(0, 0, 0)"); 
       curve(800, 200, 800, 100, 600, 100, 600, 200, "hsb(0, 0, 0)"); // xp1,yp1, , , , , xp2,yp2 where (xp1,xp2) & (xp2,yp2) are two end points 

        curve(500, 200,500, 300, 300, 300, 300, 200, "hsb(0, 0, 0)"); // xp1,yp1, , , , , xp2,yp2 where (xp1,xp2) & (xp2,yp2) are two end points 

        curve(920, 100,880, 100, 1020, 200, 980, 200, "hsb(0, 0, 0)"); 

      }); 
     </script> 
    </head> 
    <body> 
     <div id="holder"></div> 
    </body> 
</html> 

</body> 

的链接,演示是http://jsfiddle.net/insane36/fddGJ/1/

我编辑的代码,并再次试图把多个手柄,以显示在中间的主手柄,但有一些问题,我不知道如果我了解它背后的概念。我想创建一个如下图所示的手柄图形,并且能够操纵手柄;

enter image description here

三个手柄的代码;

<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
     <title>Bezier curve</title> 
      <style> 
     #holder { 
      height: 100%; 
      left: 100%; 
      margin: -100% 0 0 -100%; 
      position: absolute; 
      top: 100%; 
      width: 100%; 
     } 
     </style> 
     <script src="raphael.js"></script> 
     <script> 
      window.onload=function() { 
       var r = Raphael("holder", window.innerWidth, window.innerHeight) 

       function curve(x1, y1, cx1, cy1, cx2, cy2, x2, y2,cx3,cy3,cx4,cy4, color) { //zx --x1 
        var path = [["M", x1, y1], ["C", cx1, cy1, cx2, cy2, x2, y2,"S",cx3,cy3,cx4,cy4]], 
         path2 = [["M", x1, y1], ["L", cx1, cy1], ["M", cx2, cy2], ["L", x2, y2],["M", cx3,cy3],['L',cx4,cy4]], 
         curve = r.path(path).attr({stroke: color || Raphael.getColor(), "stroke-width": 4, "stroke-linecap": "round"}), 
         controls = r.set(
          r.path(path2).attr({stroke: "#ccc", "stroke-dasharray": ". ","stroke-width":2}), 
          r.circle(x1, y1, 5).attr({fill: "#9F2200", stroke: "none"}), 
          r.circle(cx1, cy1, 5).attr({fill: "#9F2200", stroke: "none"}), 
          r.circle(cx2, cy2, 5).attr({fill: "#9F2200", stroke: "none"}), 
          r.circle(x2, y2, 5).attr({fill: "#9F2200", stroke: "none"}), 
          r.circle(cx3, cy3, 5).attr({fill: "#9F2200", stroke: "none"}), 
           r.circle(cx4, cy4, 5).attr({fill: "#9F2200", stroke: "none"}) 

         ); 
        controls[1].update = function (x, y) { 
         var X = this.attr("cx") + x, 
          Y = this.attr("cy") + y; 
         this.attr({cx: X, cy: Y}); 
         path[0][9] = X; 
         path[0][2] = Y; 
         path2[0][10] = X; 
         path2[0][2] = Y; 
         controls[2].update(x, y); 
        }; 
        controls[2].update = function (x, y) { 
         var X = this.attr("cx") + x, 
          Y = this.attr("cy") + y; 
         this.attr({cx: X, cy: Y}); 
         path[1][11] = X; 
         path[1][2] = Y; 
         path2[1][12] = X; 
         path2[1][2] = Y; 
         curve.attr({path: path}); 
         controls[0].attr({path: path2}); 
        }; 
        controls[3].update = function (x, y) { 
         var X = this.attr("cx") + x, 
          Y = this.attr("cy") + y; 
         this.attr({cx: X, cy: Y}); 
         path[1][3] = X; 
         path[1][4] = Y; 
         path2[2][13] = X; 
         path2[2][2] = Y; 
         curve.attr({path: path}); 
         controls[0].attr({path: path2}); 
        }; 
        controls[4].update = function (x, y) { 
         var X = this.attr("cx") + x, 
          Y = this.attr("cy") + y; 
         this.attr({cx: X, cy: Y}); 
         path[1][5] = X; 
         path[1][6] = Y; 
         path2[3][14] = X; 
         path2[3][2] = Y; 
         controls[3].update(x, y); 
        }; 
        controls[5].update = function (x, y) { 
         var X = this.attr("cx") + x, 
          Y = this.attr("cy") + y; 
         this.attr({cx: X, cy: Y}); 
         path[1][8] = X; 
         path[1][9] = Y; 
         path2[4][15] = X; 
         path2[4][2] = Y; 
         controls[4].update(x, y); 
        }; 
         controls[6].update = function (x, y) { 
         var X = this.attr("cx") + x, 
          Y = this.attr("cy") + y; 
         this.attr({cx: X, cy: Y}); 
         path[1][10] = X; 
         path[1][11] = Y; 
         path2[5][16] = X; 
         path2[5][2] = Y; 
         controls[5].update(x, y); 
        }; 

        controls.drag(move, up); 
       } 
       function move(dx, dy) { 
        this.update(dx - (this.dx || 0), dy - (this.dy || 0)); 
        console.log(this.dx,this.dy); 
        this.dx = dx; 
        this.dy = dy; 
       } 
       function up() { 
        this.dx = this.dy = 0; 
       } 
       curve(10, 80, 40, 10, 65,10,150,150,95, 80, 180,180, "hsb(0, 0, 0)"); 

      }; 
     </script> 
    </head> 
    <body> 
     <div id="holder"></div> 
    </body> 
</html> 

</body> 
</html> 

我想我已经错过了,并没有安排适当的控制点和值

+1

我做了类似于你想要做的事情:http://type.method.ac,但是我不能通过查看它来告诉你代码有什么问题。我没有在我的路径中使用“S”节点,但可能是我自己的无知。 – Duopixel

+0

您是否使用贝塞尔曲线进行此操作?我没有看到曲线。 – Sandeep

+1

这些字母在一个单独的文件http://shape.method.ac/js/letters.js – Duopixel

回答

1

看看这个fiddle - 我想我有它做你在找什么。 (编辑:固定这个小提琴,这样你就不必指定在curve()构造的反射控制点)

我觉得最关键的是曲线上的中间点的第二个控制点仅仅是一个反射第一个控制点(按照SVG documentation),所以你必须对这种控制进行“假”。 (你的代码确实有一些问题,在您的update()函数试图更新不存在的数组的值,比如path[1][6] = Y;...path[1]只有三个元素)如果你想在两个控制点

(我们认为你必须从你的路径中删除“S”,并且改变一些代码(这里是one like that

如果你想要两个控制点允许从点移动不同的距离,但保持曲线平滑点,我认为你必须手动完成。您可以从第二个示例开始,但是您必须以编程方式将移动控制点的角度反映到相反的控制点,同时允许从相反控制点到曲线上的点的距离保持固定。

+0

谢谢,它看起来不错,但我有一个问题。哪些是中间控制点的变量。 – Sandeep

+0

第一个控制点是cx2,cy2,第二个控制点是cx2b,cy2b。在第一种情况下,在构造函数中计算cx2b,cy2b(即通过反映第一个控制点)。在第二种情况下,cx2b和cy2b被传入构造函数。 –

+0

如果我想要动态创建这样的路径,无论用户点击了什么,我想生成一条小曲线,当点击另一个点时,它将与已经绘制的曲线相关联并创建句柄和新曲线。 – Sandeep