本文介绍了切换setLineDash()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了如何使用HTML5画布绘制虚线,并发现了 setLineDash().它似乎可以在我的浏览器(Chrome,Firefox和Safari)中正常运行,但是我找不到禁用它的标准"方式

I looked up how to draw dashed lines with the HTML5 canvas and discovered setLineDash(). It appears to work just fine in my browsers (Chrome, Firefox, and Safari) but I was not able to find the "standard" way to disable it

我有一个函数,该函数在间隔上被调用,该间隔先绘制一些实线,然后再绘制虚线.我最好的猜测是这样做:

I have a function that gets called on an interval which draws some solid lines and then later dashed lines. My best guess was to do this:

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();

我的主要问题是以下部分是否是关闭虚线的适当方法:

My main question is whether or not the below section is the appropriate way to toggle off the dashed lines:

if (context.setLineDash)
    context.setLineDash([]);

推荐答案

破折号列表以空数组 [] 开始,因此您应将其设置为空数组以将破折号列表重置为其初始状态.

Dash list starts as an empty array [], and thus you should set it to an empty array to reset the dash list to its initial state.

另请参见 setLineDash 定义,该定义还指出,破折号列表默认为空.

See also the setLineDash definition which also notes that the dash list defaults to empty.

  void setLineDash(sequence<unrestricted double> segments); // default empty

这篇关于切换setLineDash()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 23:23
查看更多