Home Web Front-end JS Tutorial What is fabricjs? Fabricjs front-end drawing library usage arrangement (with code)

What is fabricjs? Fabricjs front-end drawing library usage arrangement (with code)

Aug 17, 2018 am 11:44 AM
canvas javascript front end

This article brings you what is fabricjs? The usage of the fabricjs front-end drawing library is organized (with code), which has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

I have been using it for a while. Generally speaking, it is a very powerful vector drawing tool. The official documentation is also rich, but the document organization is not very good, and a small number of API designs are not consistent. It is still needed. Sort it out.

Canvas

Canvas is the most basic object. It is encapsulated for the tag and can manage all objects drawn internally.
This canvas object is not an element in the DOM. If you need to control the DOM or the corresponding context, you still need to obtain it yourself.

canvasElement = document.getElementById(canvasEle);
ctx = canvasElement.getContext("2d");
Copy after login

When creating a new canvas object, you can specify the width and height:

canvas = new fabric.Canvas(canvasElement, { 
            selection: false,
            width: 800,
            height:600
});
Copy after login

The width and height specified here will override the ones set in css. Note that this form of creating objects is basically similar in Fabric.js. The class name indicates the type of object to be created. The first parameter is the necessary data, and the second parameter is various options.

All modifications to the canvas, including adding and deleting objects, and modifying object parameters, require calling the rendering method to be displayed:

canvas.renderAll();
Copy after login

Basic shape

Geometric shapes such as Line, Circle, Rectangel, etc. are all basic shapes.

All basic shapes have corresponding classes, so that their position, color, size and other styles can be controlled through the properties and methods of class instances. All classes inherit from the Object class and have some public properties and methods.

Create

The following is an example of drawing a line (given two vertex coordinates):

        var line =  new fabric.Line([x1, y1, x2, y2], {
            strokeWidth: 2, //线宽
            stroke: rgba(255,0,0,0.8), //线的颜色
            selectable: false
        });
        canvas.add(line);
Copy after login

Example of drawing a circle (the vertex and radius are in the options), here left and top are actually (x, y), which should be borrowed from the naming in css.

        var circle =  new fabric.Circle({
            radius: 2,
            left: left,
            top: top,
            originX: 'center',
            originY: 'center',
            fill: rgba(0,200,0,0.8), 
            strokeWidth: 1,
            stroke: rgba(255,0,0,0.8),
            selectable: false
        };
        canvas.add(circle);
Copy after login

It can be seen from here that it is similar to creating canvas. The first parameter is specific to this class (such as the starting and ending point coordinates passed when drawing a straight line), and the second parameter is a general option. If there is no Special parameters, then the first parameter is directly the general option. All created shapes can only be displayed if they are added to the scene through the add method of canvas.

Control

left and top are properties of every Object. As for which point in the graph it refers to, the coordinates are determined by the originX and originY parameters. Determine, they are equivalent to the alignment in text editing software. originX has three optional values: left, center, right; originY also has three optional values: top, center, bottom.

Their schematic diagrams are as follows:
What is fabricjs? Fabricjs front-end drawing library usage arrangement (with code)
http://fabricjs.com/test/misc...

If you want the default origin of each object to be The center can be set like this:

fabric.Object.prototype.originX = fabric.Object.prototype.originY = 'center'
Copy after login

width and height are also attributes that can be directly accessed. As the name suggests, they represent length and width (all shapes have an enclosing rectangle, so you can use length and width to control the size) .

Except for the above properties that can be directly accessed, most properties need to be read and written using the get/set method. For example:

line.left = pointer.x;
line.top = pointer.y;
line.set('stroke', startColor);
line.set('height', 20);
Copy after login

Some articles on the Internet will write line.stroke=color, Or forms such as line.setProperty('stroke',color) are not valid and may be expressions of earlier versions.

Image

Image is similar to other shapes and is a subclass of Object. The biggest difference is that the loading of What is fabricjs? Fabricjs front-end drawing library usage arrangement (with code) files is asynchronous, so the subsequent processing of Image Operations must be completed in callbacks.

var bkImage = fabric.Image.fromURL(imgUrl,function(img) {
    canvas.add(img);
}
Copy after login

Related recommendations:

How to use plotly.js drawing library

##VML drawing board ④Simplified server side- -server.php, server.asp

The above is the detailed content of What is fabricjs? Fabricjs front-end drawing library usage arrangement (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Vue: a perfect pairing of front-end development tools PHP and Vue: a perfect pairing of front-end development tools Mar 16, 2024 pm 12:09 PM

PHP and Vue: a perfect pairing of front-end development tools. In today's era of rapid development of the Internet, front-end development has become increasingly important. As users have higher and higher requirements for the experience of websites and applications, front-end developers need to use more efficient and flexible tools to create responsive and interactive interfaces. As two important technologies in the field of front-end development, PHP and Vue.js can be regarded as perfect tools when paired together. This article will explore the combination of PHP and Vue, as well as detailed code examples to help readers better understand and apply these two

Questions frequently asked by front-end interviewers Questions frequently asked by front-end interviewers Mar 19, 2024 pm 02:24 PM

In front-end development interviews, common questions cover a wide range of topics, including HTML/CSS basics, JavaScript basics, frameworks and libraries, project experience, algorithms and data structures, performance optimization, cross-domain requests, front-end engineering, design patterns, and new technologies and trends. . Interviewer questions are designed to assess the candidate's technical skills, project experience, and understanding of industry trends. Therefore, candidates should be fully prepared in these areas to demonstrate their abilities and expertise.

Learn the canvas framework and explain the commonly used canvas framework in detail Learn the canvas framework and explain the commonly used canvas framework in detail Jan 17, 2024 am 11:03 AM

Explore the Canvas framework: To understand what are the commonly used Canvas frameworks, specific code examples are required. Introduction: Canvas is a drawing API provided in HTML5, through which we can achieve rich graphics and animation effects. In order to improve the efficiency and convenience of drawing, many developers have developed different Canvas frameworks. This article will introduce some commonly used Canvas frameworks and provide specific code examples to help readers gain a deeper understanding of how to use these frameworks. 1. EaselJS framework Ea

Is Django front-end or back-end? check it out! Is Django front-end or back-end? check it out! Jan 19, 2024 am 08:37 AM

Django is a web application framework written in Python that emphasizes rapid development and clean methods. Although Django is a web framework, to answer the question whether Django is a front-end or a back-end, you need to have a deep understanding of the concepts of front-end and back-end. The front end refers to the interface that users directly interact with, and the back end refers to server-side programs. They interact with data through the HTTP protocol. When the front-end and back-end are separated, the front-end and back-end programs can be developed independently to implement business logic and interactive effects respectively, and data exchange.

Exploring Go language front-end technology: a new vision for front-end development Exploring Go language front-end technology: a new vision for front-end development Mar 28, 2024 pm 01:06 PM

As a fast and efficient programming language, Go language is widely popular in the field of back-end development. However, few people associate Go language with front-end development. In fact, using Go language for front-end development can not only improve efficiency, but also bring new horizons to developers. This article will explore the possibility of using the Go language for front-end development and provide specific code examples to help readers better understand this area. In traditional front-end development, JavaScript, HTML, and CSS are often used to build user interfaces

Django: A magical framework that can handle both front-end and back-end development! Django: A magical framework that can handle both front-end and back-end development! Jan 19, 2024 am 08:52 AM

Django: A magical framework that can handle both front-end and back-end development! Django is an efficient and scalable web application framework. It is able to support multiple web development models, including MVC and MTV, and can easily develop high-quality web applications. Django not only supports back-end development, but can also quickly build front-end interfaces and achieve flexible view display through template language. Django combines front-end development and back-end development into a seamless integration, so developers don’t have to specialize in learning

Combination of Golang and front-end technology: explore how Golang plays a role in the front-end field Combination of Golang and front-end technology: explore how Golang plays a role in the front-end field Mar 19, 2024 pm 06:15 PM

Combination of Golang and front-end technology: To explore how Golang plays a role in the front-end field, specific code examples are needed. With the rapid development of the Internet and mobile applications, front-end technology has become increasingly important. In this field, Golang, as a powerful back-end programming language, can also play an important role. This article will explore how Golang is combined with front-end technology and demonstrate its potential in the front-end field through specific code examples. The role of Golang in the front-end field is as an efficient, concise and easy-to-learn

Explore the powerful role and application of canvas in game development Explore the powerful role and application of canvas in game development Jan 17, 2024 am 11:00 AM

Understand the power and application of canvas in game development Overview: With the rapid development of Internet technology, web games are becoming more and more popular among players. As an important part of web game development, canvas technology has gradually emerged in game development, showing its powerful power and application. This article will introduce the potential of canvas in game development and demonstrate its application through specific code examples. 1. Introduction to canvas technology Canvas is a new element in HTML5, which allows us to use

See all articles