HTML5 canvas custom rounded rectangle and dotted line (rounded rectangle and dash line)

Source: Internet
Author: User

HTML5 canvas custom rounded rectangle and dotted line (roundedrectangle and dash line)

Demonstrate how to draw dotted lines by adding custom functions to HTML canvas 2D Context

And control the interval between dotted lines to learn how to draw a rounded rectangle.

The native function provided by HTML5 canvas to draw objects does not implement the ability to draw rounded rectangle and dotted line,

The object. Prototype in javascript can be used to add canvasrenderingcontext2d to the object.

Add these two functions. The demo of the Code is as follows:

The code for component fishcomponent. JS is as follows:

CanvasRenderingContext2D.prototype.roundRect =function(x, y, width, height, radius, fill, stroke) {if (typeof stroke == "undefined") {stroke = true;}if (typeof radius === "undefined") {radius = 5;}this.beginPath();this.moveTo(x + radius, y);this.lineTo(x + width - radius, y);this.quadraticCurveTo(x + width, y, x + width, y + radius);this.lineTo(x + width, y + height - radius);this.quadraticCurveTo(x + width, y + height, x + width - radius, y+ height);this.lineTo(x + radius, y + height);this.quadraticCurveTo(x, y + height, x, y + height - radius);this.lineTo(x, y + radius);this.quadraticCurveTo(x, y, x + radius, y);this.closePath();if (stroke) {this.stroke();}if (fill) {this.fill();}};CanvasRenderingContext2D.prototype.dashedLineTo = function (fromX, fromY, toX, toY, pattern) {// default interval distance -> 5pxif (typeof pattern === "undefined") {pattern = 5;}// calculate the delta x and delta yvar dx = (toX - fromX);var dy = (toY - fromY);var distance = Math.floor(Math.sqrt(dx*dx + dy*dy));var dashlineInteveral = (pattern <= 0) ? distance : (distance/pattern);var deltay = (dy/distance) * pattern;var deltax = (dx/distance) * pattern;// draw dash linethis.beginPath();for(var dl=0; dl<dashlineInteveral; dl++) {if(dl%2) {this.lineTo(fromX + dl*deltax, fromY + dl*deltay);} else {    this.moveTo(fromX + dl*deltax, fromY + dl*deltay);    }    }this.stroke();};

Demo of calling in HTML:

<!DOCTYPE html>

Reprinted please note

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.