【问题标题】:Animate a Fill Circle using Canvas使用 Canvas 为填充圆设置动画
【发布时间】:2013-05-27 13:16:17
【问题描述】:

基本上我希望能够使用画布填充一个圆圈,但它会以一定的百分比进行动画处理。 即只有圆圈填满了 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, 0, 2 * Math.PI, false);
 context.fillStyle = '#13a8a4';
 context.fill();
 context.lineWidth = 10;
 context.strokeStyle = '#ffffff';
 context.stroke();

任何帮助将不胜感激

【问题讨论】:

标签: html canvas geometry animated


【解决方案1】:

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

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/

【讨论】:

    【解决方案2】:

    这是一个更动态的面向对象的版本,因此您可以配置选项为圆的半径、边框宽度、颜色、持续时间和动画的步长,您还可以将圆设置为一定百分比的动画。写这个很有趣。

    <canvas id="Circle" width="300" height="300"></canvas>
    <script>
        function Animation( opt ) {
            var context = opt.canvas.getContext("2d");
            var handle = 0;
            var current = 0;
            var percent = 0;
    
            this.start = function( percentage ) {
                percent = percentage;
                // start the interval
                handle = setInterval( draw, opt.interval );
            }
    
            // fill the background color
            context.fillStyle = opt.backcolor;
            context.fillRect( 0, 0, opt.width, opt.height );
    
            // draw a circle
            context.arc( opt.width / 2, opt.height / 2, opt.radius, 0, 2 * Math.PI, false );
            context.lineWidth = opt.linewidth;
            context.strokeStyle = opt.circlecolor;
            context.stroke();
    
            function draw() {
                // make a circular clipping region
                context.beginPath();
                context.arc( opt.width / 2, opt.height / 2, opt.radius-(opt.linewidth/2), 0, 2 * Math.PI, false );
                context.clip();
    
                // draw the current rectangle
                var height = ((100-current)*opt.radius*2)/100 + (opt.height-(opt.radius*2))/2;
                context.fillStyle = opt.fillcolor;
                context.fillRect( 0, height, opt.width, opt.radius*2 );
    
                // clear the interval when the animation is over
                if ( current < percent ) current+=opt.step;
                else clearInterval(handle);
            }
        }
    
        // create the new object, add options, and start the animation with desired percentage
        var canvas = document.getElementById("Circle");
        new Animation({
            'canvas': canvas,
            'width': canvas.width,
            'height': canvas.height,
            'radius': 100,
            'linewidth': 10,        
            'interval': 20,
            'step': 1,
            'backcolor': '#666',
            'circlecolor': '#fff',
            'fillcolor': '#339999'
        }).start( 70 );
    </script>
    

    【讨论】:

      猜你喜欢
      • 2016-05-21
      • 2015-04-12
      • 2016-04-25
      • 1970-01-01
      • 1970-01-01
      • 2014-03-29
      • 1970-01-01
      • 1970-01-01
      • 2013-03-19
      相关资源
      最近更新 更多