是否可以填充二次曲线?
我是 canvas 的新手,我想填充我在 context.quadraticCurveTo() 中绘制的上半部分的颜色,但是当我尝试 context.fill() 时,它没有填充我预期的部分.我想填充被笔划分隔的 Canvas 的上部。
见附件。
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>
最简单的方法可能就是沿着 Canvas 边缘画一些线来形成一个封闭的形状:
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>
我是一名优秀的程序员,十分优秀!