Comments: The native function provided by HTML5 Canvas to draw objects does not implement the ability to draw rounded rectangle and dotted line. prototype can add these two functions to the object CanvasRenderingContext2D. The specific implementation is as follows. If you are interested, refer to the custom rounded rectangle and dotted Line of HTML5 Canvas (RoundedRectangle and Dash Line)
Demonstrate how to add custom functions to HTML Canvas 2d context to draw objects. Learn how to draw dotted lines and control the interval between dotted lines to draw rounded rectangle.
The native function provided in HTML5 Canvas painting objects does not provide the ability to draw rounded rectangle and dotted line. However, the Object. prototype in JavaScript can be used to add these two functions to the Object CanvasRenderingContext2D. The demo of the Code is as follows:
The code for component fishcomponent. js is as follows:
The Code 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-> 5px
If (typeof pattern = "undefined "){
Pattern = 5;
}
// Calculate the delta x and delta y
Var 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 line
This. 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:
The Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Meta http-equiv = "X-UA-Compatible" content = "chrome = IE8">
<Meta http-equiv = "Content-type" content = "text/html; charset = UTF-8">
<Title> Canvas Rounded Rectangle Demo </title>
<Script src = "fishcomponent. js"> </script>
<Link href = "default.css" rel = "stylesheet"/>
<Script>
Var ctx = null; // global variable 2d context
Var imageTexture = null;
Window. onload = function (){
Var canvas = document. getElementById ("text_canvas ");
Console. log (canvas. parentNode. clientWidth );
Canvas. width = canvas. parentNode. clientWidth;
Canvas. height = canvas. parentNode. clientHeight;
If (! Canvas. getContext ){
Console. log ("Canvas not supported. Please install a HTML5 compatible browser .");
Return;
}
Var context = canvas. getContext ('2d ');
Context. strokeStyle = "red ";
Context. fillStyle = "RGBA (100,255,100, 0.5 )";
Context. roundRect (50, 50,150,150, 5, true );
Context. strokeStyle = "blue ";
For (var I = 0; I <10; I ++ ){
Var delta = I * 20;
Var pattern = I * 2 + 1;
Context. dashedLineTo (250, 50 + delta, 550, 50 + delta, pattern );
}
}
</Script>
</Head>
<Body>
<H1> HTML5 Canvas Dash-line Demo-By Gloomy Fish <Pre> Dash line and Rounded Rectangle </pre>
<Div id = "box_plot">
<Canvas id = "text_canvas"> </canvas>
</Div>
</Body>
</Html>