ทำให้วงกลมเติมเคลื่อนไหวโดยใช้ 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, 0, 2 * Math.PI, false);
 context.fillStyle = '#13a8a4';
 context.fill();
 context.lineWidth = 10;
 context.strokeStyle = '#ffffff';
 context.stroke();

ความช่วยเหลือใด ๆ ที่จะได้รับการชื่นชมอย่างมาก


person Blackline    schedule 31.05.2013    source แหล่งที่มา
comment
ฉันแนะนำให้คุณใช้ KineticJS Framework! แต่หากคุณต้องการใช้เฉพาะแคนวาสจริงๆ ลองใช้เอกสารนี้: แคนวาสลิเนียร์โมชั่น   -  person Bruno Croys Felthes    schedule 31.05.2013


คำตอบ (2)


การตัดขอบทำให้ง่ายมาก สิ่งที่คุณต้องทำคือสร้างส่วนที่ตัดเป็นวงกลม จากนั้นเติมสี่เหลี่ยมขนาดใดก็ได้เพื่อให้ได้ค่า "วงกลมบางส่วน" นี่คือตัวอย่าง:

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/

person Simon Sarris    schedule 31.05.2013

นี่เป็นเวอร์ชันเชิงวัตถุแบบไดนามิกมากกว่าเล็กน้อย ดังนั้นคุณจึงสามารถกำหนดค่าตัวเลือกต่างๆ เช่น รัศมีของวงกลม ความกว้างของเส้นขอบ สี ระยะเวลา และขั้นตอนของภาพเคลื่อนไหว คุณยังสามารถทำให้วงกลมเคลื่อนไหวเป็นเปอร์เซ็นต์ที่กำหนดได้ มันค่อนข้างสนุกที่ได้เขียนสิ่งนี้

<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>
person Danijel    schedule 31.05.2013