2013-05-03 57 views
0

我想以循環(或類似)的增量繪製一個圖像作爲進度條,我原本想用畫布中的線條畫出這個圖像,但最終我對它的外觀感到不滿。畫布以循環增量繪製圖像?

這jsfiddle顯示了我希望它看起來如何100%,沒有任何酒吧將被繪製在0%,酒吧的開始和結束點也被標記。

http://jsfiddle.net/6WZNZ/2/

我想這可能使用額外的drawImage參數來實現,我已經使用這個使用圖像作爲填充繪製一個倒數計時器,但我不知道如何在一個圓形畫忽視中間區域增加複雜性的方式?

context.drawImage(img,sx,sy,swidth,sheight,x,y,width,height); 

這可能是解決問題的錯誤方法,這可能嗎?

回答

1

您可以使用剪切路徑來逐步隱藏/顯示您的邊框圖像。

這裏的僞代碼:

// save the context state and beginPath 
ctx.save(); 
ctx.beginPath(); 

// draw your clipping path here 
// The clipping path will include whatever part of the image you want visible 

// set your path as a clipping path 
ctx.clip(); 

// draw your border image 
// only the clipping path portion of the border image will be visible 
ctx.drawImage(img,0,0); 

// restore the context 
// this turns the clipping off 
ctx.restore(); 


// now draw your level and score (clipping is no longer in effect) 

//game score and level number dont draw over or clear 
ctx.font = 'bold 50px Arial'; 
ctx.fillText("LVL NUMBER", 45, 100); 
ctx.fillText(" TOTAL SCORE", 15, 190); 

//label only ignore 
ctx.font = 'bold 10px Arial'; 
ctx.fillText("end", 57, 25); 
ctx.fillText("start", 125, 25); 
+0

感謝再次,可以在單個線卡止件的圖像或它必須是一個封閉的形狀?你昨天給我畫的一個剪切線(?),而不是一條彩色線的例子,這個例子對於這個任務來說是完美的。 – mao 2013-05-03 21:58:23

+0

不行,行不能剪裁路徑。如果你想用我以前的例子進行裁剪,只需將直線替換爲弧線,將弧線替換爲楔子即可。 – markE 2013-05-03 22:36:19

+0

謝謝,現在就去。我不認爲你可以告訴我我在做什麼錯誤的這些矩形?我把它分成兩個函數一個用於垂直,另一個用於水平,並且保持y座標相同,第一個和第二個函數相同。 http://jsfiddle.net/P2qTq/1/ – mao 2013-05-03 23:23:53