2013-05-03 52 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