I would like to draw some dashed lines on HTML5 canvas. But I couldn't find there is such a feature. the canvas path could only draw solid lines. And people have tried to use some border feature (dotted, dashed) in CSS to draw dashed lines, but they could only be horizontal or vertical. So I got stuck on this. I also found a library called RGraph and it could draw dashed lines. But using an external library would make the drawing really slow. So does any body has an idea how to implement this? Any help will be appreciated.
相关问题
- Does using percentages instead of pixels change an
- Method for drawing circles in a pyramid pattern
- HTML5 - Canvas - Optimization for large images
- How to plot line in matlab with theta/rho data
- A function to preload images - need to draw them n
FYI - dotted and dashed lines are part of some new canvas features that are now in the spec - and already implemented in Chrome:
http://www.rgraph.net/blog/2013/january/html5-canvas-dashed-lines.html
You can use the
setLineDash()
method.http://www.rgraph.net/blog/2013/january/html5-canvas-dashed-lines.html
JsFIDDLE
This is an easier way to create dashed lines :
Hope that helps.
Drawing dashed lines on canvas
I offer this up not as a complete solution, but as a simple way to draw a dotted line between any 2 points (a line at any angle). It draws very fast.
You can modify it to fit your needs of a dashed line. Drawing dashes should not noticeably slow down the drawing.
Here is code and a Fiddle: http://jsfiddle.net/m1erickson/pW4De/