微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

html5 – 使用Canvas为填充圆圈设置动画

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

我的画布知识并不令人惊讶,这是我在photoshop中制作的图像,用于显示我想要的内容.

我希望圆圈开始为空,然后填充说圆圈的70%.
Canvas有可能,如果是这样吗?任何人都可以了解如何做到这一点?

这是我管理的一个小提琴

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,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,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,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/

原文地址:https://www.jb51.cc/html5/168089.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。