How to draw dashed lines in HTML5 canvas

Source: Internet
Author: User
Tags dashed line pow

The dashed line can also be seen as a segment of the solid lines, while the solid line is the use of Context.moveto (x, y); Context.lineto (x2,y2); Context.stroke ();

Then we can use the basic method of the context to complete the implementation of the dashed line principle, as follows:

var context = document.getElementById (' canvas '). GetContext (' 2d ');//Seek hypotenuse length function getbeveling (x, y) {return math.sqrt ( Math.pow (x,2) +math.pow (y,2));}  function Drawdashline (context,x1,y1,x2,y2,dashlen) {Dashlen = Dashlen = = = undefined? 5:dashlen;//Gets the total length of the hypotenuse var beveling =  Getbeveling (x2-x1,y2-y1);//Calculate how many segments var num = Math.floor (Beveling/dashlen); for (var i = 0; i < num; i++) {context[i%2 = = 2} ' MoveTo ': ' LineTo ' (x1+ (x2-x1)/num*i,y1+ (y2-y1)/num*i);} Context.stroke ();} Drawdashline (context,50,50,300,180,5);
The results are shown below:



How to draw dashed lines in HTML5 canvas

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.