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

后端 未结 11 737
时光说笑
时光说笑 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:19

    This code is perfect for me:

    this.context.beginPath();
    this.context.moveTo(data[0].x, data[0].y);
    for (let i = 1; i < data.length; i++) {
      this.context.bezierCurveTo(
        data[i - 1].x + (data[i].x - data[i - 1].x) / 2,
        data[i - 1].y,
        data[i - 1].x + (data[i].x - data[i - 1].x) / 2,
        data[i].y,
        data[i].x,
        data[i].y);
    }
    

    you have correct smooth line and correct endPoints NOTICE! (y = "canvas height" - y);

提交回复
热议问题