填充由path2d对象定义的自定义对象

时间:2020-03-24 16:13:08

标签: html canvas fill path-2d

我正在尝试填充HTML5 canvas.context的path2d对象。

根据该站点,我已经绘制了一条自定义路径,该路径为贝塞尔曲线: https://javascript.info/bezier-curve

但是无法使其填充为纯色。

这是jsfiddle中的一些代码来说明我的问题。如果我取消注释//this.ctx.stroke(this.p2d);线,则将绘制贝塞尔曲线的轮廓,但似乎无法填充完整的路径。


constructor () {
    this.canv = document.getElementById('canv');
    this.ctx = this.canv.getContext('2d');
  this.ctx.beginPath();
  this.ctx.moveTo(160,350);
  this.ctx.restore();
  this.p2d = new Path2D();
    this.t = 0;
    this.currentPoint = [160,350];
  this.to = setInterval(() => this.plot(), 10, this.to);
}

plot(intid) {
      const p1x = 160;
      const p2x = 20;
      const p3x = 320;
      const p4x = 160;
      const p1y = 350;
      const p2y = 50;
      const p3y = 50;
      const p4y = 350;
      let t = this.t;
      let x = (((1 - t)*(1 - t)*(1 - t)) * p1x) + ((3 * ((1 - t) * (1 -t))) * (t * p2x)) + ((3 * ((1 - t) * (1 -t))) * (t * p3x)) + ((t * t * t) * p4x);
      let y = (((1 - t)*(1 - t)*(1 - t)) * p1y) + ((3 * ((1 - t) * (1 -t))) * (t * p2y)) + ((3 * ((1 - t) * (1 -t))) * (t * p3y)) + ((t * t * t) * p4y);
      this.t = t + 0.01;
      if (t <= 1.01) {

        //this.p2d.fillStyle = "#1000ff";
        this.p2d.moveTo(this.currentPoint[0], this.currentPoint[1]);
        this.p2d.lineTo(x, y);
        this.currentPoint[0] = x;
        this.currentPoint[1] = y;
        console.log(x + " " + y + " " + t)
      }
      else
      {
        //this.p2d.closePath();
        this.ctx.lineWidth = 2;
        this.ctx.strokeStyle = "blue";
        this.ctx.fillStyle = "blue";
        //this.ctx.stroke(this.p2d);
        this.ctx.fill(this.p2d, "evenodd");
        clearInterval(this.to);  
      }
  }
}

Window.cl = new clazz();

https://jsfiddle.net/9oL4xw1b/2/

ps。这对我来说是高级数学,因此尽管我计算x和y的公式是正确的,但可能无法正确优化。

1 个答案:

答案 0 :(得分:0)

每次移动时,都没有东西可以填满...
最后,您所要绘制的只是使用该技术的代码,请参见下面的代码。

canv = document.getElementById('canv');
ctx = canv.getContext('2d');
ctx.fillStyle = "blue";

ctx.moveTo(10, 10);
ctx.lineTo(20, 40);
ctx.lineTo(99, 40);
ctx.lineTo(90, 30);

ctx.fill();
ctx.stroke();


ctx.moveTo(100, 10);
ctx.lineTo(120, 40);
ctx.moveTo(120, 40);
ctx.lineTo(200, 40);
ctx.moveTo(200, 40);
ctx.lineTo(190, 30);

ctx.fill();
ctx.stroke();
<canvas id="canv" width="500px" height="100px"></canvas>


因此,在您的代码中只需注释this.p2d.moveTo即可

class clazz {
  constructor() {
    this.canv = document.getElementById('canv');
    this.ctx = this.canv.getContext('2d');
    this.ctx.moveTo(160, 350);
    this.p2d = new Path2D();
    this.t = 0;
    this.currentPoint = [160, 350];
    this.to = setInterval(() => this.plot(), 5, this.to);
  }

  plot(intid) {
    const p1x = 160; const p2x = 20;
    const p3x = 320; const p4x = 160;
    const p1y = 350; const p2y = 50;
    const p3y = 50;  const p4y = 350;
    let t = this.t;
    let x = (((1 - t) * (1 - t) * (1 - t)) * p1x) + ((3 * ((1 - t) * (1 - t))) * (t * p2x)) + ((3 * ((1 - t) * (1 - t))) * (t * p3x)) + ((t * t * t) * p4x);
    let y = (((1 - t) * (1 - t) * (1 - t)) * p1y) + ((3 * ((1 - t) * (1 - t))) * (t * p2y)) + ((3 * ((1 - t) * (1 - t))) * (t * p3y)) + ((t * t * t) * p4y);
    this.t = t + 0.01;
    if (t <= 1.01) {
      //this.p2d.moveTo(this.currentPoint[0], this.currentPoint[1]);
      this.p2d.lineTo(x, y);
      this.ctx.stroke(this.p2d);
    } else {
      this.ctx.lineWidth = 2;
      this.ctx.strokeStyle = "blue";
      this.ctx.fillStyle = "blue";
      this.ctx.fill(this.p2d, "evenodd");
      clearInterval(this.to);
    }
  }
}

Window.cl = new clazz();
<canvas id="canv" width="500px" height="500px"></canvas>

相关问题