HTML5Canvas example code for customizing rounded rectangle and dotted line _ html5 tutorial tips-

Source: Internet
Author: User
The native function provided by HTML5Canvas 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 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:







Canvas Rounded Rectangle Demo

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.