In HTML5, use the Canvas path to draw a custom shape and fill in the example

Source: Internet
Author: User

The following example demonstrates using a path to draw a triangle and fill it.

1, draw attention to the following two places:

(1) After the path has been drawn, call Closepath () to explicitly close the path.
(2) Look at the following code I actually only painted two edges, because Closepath () will automatically draw a line between the last drawing point and the start of the drawing.
(3) It is best to fill the color first, then draw the outline, otherwise the contour line will be partially covered by the fill color.


var canvas = document.getElementById ("MyCanvas");
var context = Canvas.getcontext ("2d");

Draw Path
Context.moveto (200, 50);
Context.lineto (100, 150);
Context.lineto (300,150);
Context.closepath ();

padding Internal
Context.fillstyle = "Orange";
Context.fill ();

Draw outline
Context.linewidth = 10;
Context.strokestyle = "#cd2828";
Context.stroke ();

2, set the LineJoin property of the drawing context to specify the shape of the segment intersection

(1) Mitre: Sharp angle chamfering (default value)
(2) Round: Round Head

Context.linejoin = "Round";

(3) Bevel: Flat head oblique cut

Context.linejoin = "Bevel";

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.