Draw a dotted line on the canvas using trigonometric functions.

Source: Internet
Author: User

Draw a dotted line on the canvas using trigonometric functions.

Because canvas APIs do not have dotted lines

So you need to implement it yourself

By the way, review trigonometric functions.

 

Var context = document. getElementById ("canvas "). getContext ("2d"); function drawDashedLine (context, x1, y1, x2, y2, dashlength) {dashlength = dashlength === undefined? 5: dashlength; var deltaX = x2-x1; // long var deltay = y2-y1 for a straight corner edge; // long var numDashes = Math for another side. floor (Math. sqrt (deltaX * deltaX + deltay * deltay)/dashlength // Math. sqrt returns the square root of a number. The length of each vertex in the dashlength dotted line) var everydashLength_x = deltaX/numDashes // determine the starting point of each dotted line on the X axis var everydashLength_y = deltay/numDashes // determine the starting point of each dotted line on the Y axis for (var I = 0; I <numDashes; I ++) {context [I % 2 = 0? 'Moveto ': "lineTo"] (x1 + everydashLength_x * I, y1 + everydashLength_y * I)} context. stroke ()} context. lineWidth = 3context. strokeStyle = "blue" drawDashedLine (context, 20, 20, context. canvas. width-20, 20, 20)

Effect

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.