html - 使用 Canvas 为填充圆制作动画

标签 html canvas geometry animated

基本上我希望能够使用 Canvas 填充一个圆,但它会按一定百分比进行动画处理。 即只让圆圈填满 80%。

我的 Canvas 知识并不惊人,这是我在 photoshop 中制作的图像以显示我想要的内容。

AnimateSequence

我希望圆圈从空开始,然后填充到圆圈的 70%。 如果可以的话,这对 Canvas 来说可能吗?任何人都可以阐明如何去做吗?

这是我所管理的 fiddle

http://jsfiddle.net/6Vm67/

 var canvas = document.getElementById('Circle');
 var context = canvas.getContext('2d');
 var centerX = canvas.width / 2;
 var centerY = canvas.height / 2;
 var radius = 80;

 context.beginPath();
 context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
 context.fillStyle = '#13a8a4';
 context.fill();
 context.lineWidth = 10;
 context.strokeStyle = '#ffffff';
 context.stroke();

任何帮助将不胜感激

最佳答案

裁剪区域使这变得非常容易。您所要做的就是创建一个圆形裁剪区域,然后填充一个具有一定大小的矩形以获得一个“部分圆”的填充值。这是一个例子:

var canvas = document.getElementById('Circle');
var context = canvas.getContext('2d');
var centerX = canvas.width / 2;
var centerY = canvas.height / 2;
var radius = 80;

var full = radius*2;
var amount = 0;
var amountToIncrease = 10;

function draw() {
    context.save();
    context.beginPath();
    context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
    context.clip(); // Make a clipping region out of this path
    // instead of filling the arc, we fill a variable-sized rectangle
    // that is clipped to the arc
    context.fillStyle = '#13a8a4';
    // We want the rectangle to get progressively taller starting from the bottom
    // There are two ways to do this:
    // 1. Change the Y value and height every time
    // 2. Using a negative height
    // I'm lazy, so we're going with 2
    context.fillRect(centerX - radius, centerY + radius, radius * 2, -amount);
    context.restore(); // reset clipping region

    context.beginPath();
    context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
    context.lineWidth = 10;
    context.strokeStyle = '#000000';
    context.stroke();

    // Every time, raise amount by some value:
    amount += amountToIncrease;
    if (amount > full) amount = 0; // restart
}

draw();
// Every second we'll fill more;
setInterval(draw, 1000);

http://jsfiddle.net/simonsarris/pby9r/

关于html - 使用 Canvas 为填充圆制作动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16861994/

相关文章:

c# - 正则表达式从网页中提取 Favicon url

javascript - 无法在 Canvas 上添加背景图像

javascript canvas 回合限制和 onclick 变量

javascript - Firefox 在第一次访问时不会绘制到 Canvas 上

algorithm - 找到给定几条对角线的所有多边形面

python - 如何使用 python 从 beautifulsoup 输出中删除所有对齐和缩进?

java - 如何使用 Jsoup 删除文本级别的所有元素?

matlab - 生成每个点之间距离最小的 3-d 随机点?

javascript - 在p5js中沿着弧线移动

HTML/CSS - 从容器的右侧定位标签,到另一个标签的右侧