2012-12-14 47 views
1

我試圖讓我的弧的終止角度,從X到Y.我可以動畫一個圓弧的結束角度嗎?

這是我有:http://jsfiddle.net/97dUJ/

我可以做:

var endAngle = 0 * Math.PI; 

動畫到:

var endAngle = 0.5 * Math.PI; 

不僅僅是跳到那個角度,而且實際上是一個簡單的動畫,比如easeInOut或者其他東西......這可能嗎?謝謝!

+0

嘗試使用新的'requestAnimationFrame()'thingy來完成,在渲染每個幀之後,重新繪製一個稍長的圓弧。這裏有一個相關的鏈接:http://paulirish.com/2011/requestanimationframe-for-smart-animating/ – rmobis

+0

我對HTML5相當陌生,對於如何採取這種方法並將其應用於該小提琴有何想法?謝謝! – ttothec

+0

我假設你是爲了教育目的而這樣做的,但是如果你正在爲這個商業項目工作(並且爲了其他讀者的利益),我強烈建議你看看D3(http:///d3js.org/),因爲它會爲你完成所有繁重的工作,並讓你專注於最終結果。快樂學習! –

回答

0

它很簡單,你只需要添加一個setTimeout或​​並增加你想要乘以結束角度的值。

Live Demo

var canvas = document.getElementById('myCanvas'); 
     var context = canvas.getContext('2d'); 
     var x = canvas.width/2; 
     var y = canvas.height/2; 
     var radius = 75; 
     var startAngle = 1.5 * Math.PI; 
     var curVal = 0; 
     var endAngle = 0 * Math.PI; 
     var counterClockwise = false; 

function animate(){ 
    if(curVal < 0.5){ 
     curVal+= 0.01; 
    } 
     endAngle = curVal * Math.PI; 
     context.beginPath(); 
     context.arc(x, y, radius, startAngle, endAngle, counterClockwise); 
     context.lineWidth = 15; 
     // line color 
     context.strokeStyle = 'black'; 
     context.stroke(); 
    setTimeout(animate,30); 
} 

animate(); 
​ 
+0

這是一個很好的例子,只需要停止功能運行一旦動畫完成。如果(curVal <0.5)setTimeout(animate,30);則添加 ; } 最後。 –

2

你可以動畫則endAngle屬性清除畫布和重繪最新值弧形,這樣的..

var canvas = document.getElementById('myCanvas'); 
var context = canvas.getContext('2d'); 
var props = { 
    x : canvas.width/2, 
    y : canvas.height/2, 
    radius : 75, 
    startAngle : 0, 
    endAngle : 0, 
    counterClockwise : false 
} 

function drawArc() { 
    context.clearRect(0, 0, canvas.width, canvas.height); 

    context.beginPath(); 
    context.arc(props.x, props.y, props.radius, props.startAngle, props.endAngle, props.counterClockwise); 
    context.lineWidth = 15; 

    // line color 
    context.strokeStyle = 'black'; 
    context.stroke();  
} 

TweenMax.to(props, 1, {endAngle: Math.PI * 2, yoyo:true, repeat:-1, ease:Quad.easeInOut, onUpdate:drawArc}); 

http://jsfiddle.net/B8XCw/8/

記:這是使用Tweenmax爲動畫

相關問題