Section No. 04: Fabric.js Using the path to draw irregular graphics

Source: Internet
Author: User

It is easy to draw squares, circles, and triangles on canvas, as long as you call the fabric corresponding method, but these are the rules of the graph, if you want to draw an irregular shape, you can use Fabric.js to provide the path of the drawing method. The so-called path drawing is to use the point and line of the way to draw. By applying lines, curves, and arcs you can have very complex graphics.

Let's take a look at the code first:

var new fabric. Canvas (' canvas '); var new fabric. Path (' M 0 0 L(top:120,fill: ' Red ' }); Canvas.add (path);

The top code needs to be aware of the second line, fabric. The Path () method is followed by a string of strings, which looks a bit complicated, but it's not hard to understand. "M" stands for the "move" command, and this "M 00" represents the movement of the brush to the (0,0) point coordinates. "L" stands for "line", "L 200 100" means to draw a line with a pen, drawing from (0,0) coordinates to (200,100) coordinates. "Z" means to have the graph close the path. So we easily draw a triangle. After the triangle is drawn, we can use the set () method to set the position, color, angle, transparency and other properties of the triangle.

Although it is easy to draw a graph with a path, but the graphics are more complex, you will find it very difficult to control, the code will become bloated and unreadable, in the actual work is not used in this way. We can use SVG instead of the form of this path.

(go) Section No. 04: Fabric.js using paths to draw irregular shapes

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.