2
我抬头看看如何用HTML5画布绘制虚线并发现setLineDash()
。它似乎在我的浏览器(Chrome,Firefox和Safari)中工作得很好,但我无法找到禁用它的“标准”方法切换setLineDash()
我有一个函数在绘制一些固体线条和后来的虚线。我最好的猜测是要做到这一点:
context.beginPath();
// Toggle off the previous loop's dashed lines
if (context.setLineDash)
context.setLineDash([]);
// Code to draw the solid lines
context.stroke();
context.closePath();
if (context.setLineDash)
context.setLineDash([8, 8]);
context.beginPath();
// Code to draw the dashed lines
context.stroke();
context.closePath();
我的主要问题是下面的部分是否是虚线切换掉适当的方式:
if (context.setLineDash)
context.setLineDash([]);
太好了,谢谢 – asimes 2015-03-03 05:30:04
要真正回答你的问题,你的代码片段是正确的。保持'if(context.setLineDash)'确保代码在不受支持的浏览器上仍能正常工作。 – skeggse 2015-03-03 05:34:15