【问题标题】:How to fill quadratic curve in canvas?如何在画布中填充二次曲线?
【发布时间】:2018-12-27 19:33:17
【问题描述】:

二次曲线可以填充吗?

我是画布的新手,我想填充我在 context.quadraticCurveTo() 中绘制的上部的颜色,但是当我尝试 context.fill() 时,它没有填充我预期的部分.我想填充画布的上部,它被笔划分开。

见附件sn-p。

const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const ch = canvas.height;
const cw = canvas.width;

// Quadratic Bézier curve
ctx.beginPath();
ctx.moveTo(0, 60);
ctx.quadraticCurveTo(ch, 0, cw, 35);
ctx.stroke();
ctx.fill();
#canvas {
  border: 1px solid;
}
<canvas id="canvas"></canvas>

【问题讨论】:

    标签: javascript html css canvas html5-canvas


    【解决方案1】:

    最简单的方法可能是沿着画布边缘画一些线来形成一个封闭的形状:

    const canvas = document.getElementById('canvas');
    const ctx = canvas.getContext('2d');
    const ch = canvas.height;
    const cw = canvas.width;
    
    // Quadratic Bézier curve
    ctx.beginPath();
    ctx.moveTo(0, 60);
    ctx.quadraticCurveTo(ch, 0, cw, 35);
    ctx.lineTo(cw, 0);   // these two lines
    ctx.lineTo(0, 0);    // are new
                         // you can even add a third one that goes from 0,0
                         // to 0,60 … but .fill() closes the shape even without it
    ctx.stroke();
    ctx.fill();
    #canvas {
      border: 1px solid;
    }
    <canvas id="canvas"></canvas>

    【讨论】:

      猜你喜欢
      • 2013-01-05
      • 2015-01-12
      • 1970-01-01
      • 2015-12-08
      • 2017-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-13
      相关资源
      最近更新 更多