使用画布为填充圆圈设置动画 [英] Animate a Fill Circle using Canvas

查看:184
本文介绍了使用画布为填充圆圈设置动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我希望能够使用画布填充圆圈,但它会以一定的百分比动画。
即只有圆圈填满了80%的路。

Basically I want to be able to Fill a Circle using canvas, but it animate to a certain percentage. I.e only have the circle fill up 80% of the way.

我的画布知识并不出色,下面是我在Photoshop中制作的图片我想要什么。

My canvas knowledge isn't amazing, Here is an image i made in photoshop to display what i want.

我希望圆圈开始为空,然后填充说圆圈的70%。
Canvas可以使用Canvas吗?任何人都可以阐明如何做到这一点?

I want the circle to start empty and then Fill up to say 70% of the circle. Is this possible with Canvas, if so? can anyone shed some light on how to do it?

这是我管理的一把小刀

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();

任何帮助都会被大量赞赏

Any help would be massively appreciated

推荐答案

剪切区域使这非常简单。你所要做的就是制作一个圆形剪裁区域,然后填充一个大小的矩形,以获得一个值得填充的部分圆。这是一个例子:

Clipping regions make this very easy. All you have to do is make a circular clipping region and then fill a rectangle of some size to get a "partial circle" worth of fill. Here's an example:

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/

这篇关于使用画布为填充圆圈设置动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆