【发布时间】:2018-12-20 18:48:39
【问题描述】:
我想在给定 cx and cy 位置属性和椭圆本身的宽度和高度属性的情况下绘制一个椭圆。
您可以在下面找到此设置的一些工作代码:
但现在我想通过绘制椭圆的百分比(从 0 到 100)而不是整个椭圆来生成一种“进度显示”。
我在这里附上一张图来说明整个事情:
我真的不知道该怎么做。我更喜欢一种无需调整画布大小即可完成的解决方案 - 只是出于性能原因,我希望有人知道如何解决我的问题。
let canvas = document.getElementById("canvas")
let ctx = canvas.getContext("2d");
canvas.width = 400;
canvas.height = 280;
ctx.fillStyle = "black";
ctx.fillRect(0, 0, canvas.width, canvas.height)
let ellipse = function(cx, cy, w, h) {
let lx = cx - w / 2,
rx = cx + w / 2,
ty = cy - h / 2,
by = cy + h / 2;
let magic = 0.551784;
let xmagic = magic * w / 2,
ymagic = h * magic / 2;
let region = new Path2D();
region.moveTo(cx, ty);
region.bezierCurveTo(cx + xmagic, ty, rx, cy - ymagic, rx, cy);
region.bezierCurveTo(rx, cy + ymagic, cx + xmagic, by, cx, by);
region.bezierCurveTo(cx - xmagic, by, lx, cy + ymagic, lx, cy);
region.bezierCurveTo(lx, cy - ymagic, cx - xmagic, ty, cx, ty);
ctx.strokeStyle = "red";
ctx.lineWidth = "10";
region.closePath();
ctx.stroke(region);
}
ellipse(canvas.width / 2, canvas.height / 2, 300, 120)
<canvas id="canvas"></canvas>
【问题讨论】:
标签: javascript html canvas geometry ellipse