how to draw smooth curve through N points using javascript HTML5 canvas?

后端 未结 11 560
时光说笑
时光说笑 2020-11-22 16:40

For a drawing application, I\'m saving the mouse movement coordinates to an array then drawing them with lineTo. The resulting line is not smooth. How can I produce a sing

11条回答
  •  有刺的猬
    2020-11-22 17:14

    The first answer will not pass through all the points. This graph will exactly pass through all the points and will be a perfect curve with the points as [{x:,y:}] n such points.

    var points = [{x:1,y:1},{x:2,y:3},{x:3,y:4},{x:4,y:2},{x:5,y:6}] //took 5 example points
    ctx.moveTo((points[0].x), points[0].y);
    
    for(var i = 0; i < points.length-1; i ++)
    {
    
      var x_mid = (points[i].x + points[i+1].x) / 2;
      var y_mid = (points[i].y + points[i+1].y) / 2;
      var cp_x1 = (x_mid + points[i].x) / 2;
      var cp_x2 = (x_mid + points[i+1].x) / 2;
      ctx.quadraticCurveTo(cp_x1,points[i].y ,x_mid, y_mid);
      ctx.quadraticCurveTo(cp_x2,points[i+1].y ,points[i+1].x,points[i+1].y);
    }
    

提交回复
热议问题