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