Home >Web Front-end >JS Tutorial >Fabric.js: Advanced
This article delves into advanced Fabric.js features: groups, serialization (and deserialization), and custom class creation.
Key Concepts:
loadFromJSON
and loadFromSVG
for seamless canvas state restoration.Groups:
Groups in Fabric.js allow combining objects into a single entity. (See Figure 1). Any number of canvas objects can be grouped, enabling unified manipulation. Scaling, rotation, and modification of group properties (color, transparency, borders) are all possible. Fabric implicitly creates groups when selecting multiple objects.
Figure 1
Programmatic group creation:
<code class="language-javascript">var text = new fabric.Text('hello world', { fontSize: 30 }); var circle = new fabric.Circle({ radius: 100, fill: '#eef', scaleY: 0.5 }); var group = new fabric.Group([text, circle], { left: 150, top: 100, angle: -10 }); canvas.add(group);</code>
(See Figure 2 for the result). Individual objects within a group are accessed and modified using the item
method.
Figure 2
Modifying group objects:
<code class="language-javascript">group.item(0).set({ text: 'trololo', fill: 'white' }); group.item(1).setFill('red');</code>
(See Figure 3). Objects within a group are positioned relative to the group's center unless left
/top
coordinates are specified.
Figure 3
Creating a horizontally aligned group:
<code class="language-javascript">var circle1 = new fabric.Circle({ radius: 50, fill: 'red', left: 0 }); var circle2 = new fabric.Circle({ radius: 50, fill: 'green', left: 100 }); var circle3 = new fabric.Circle({ radius: 50, fill: 'blue', left: 200 }); var group = new fabric.Group([circle1, circle2, circle3], { left: 200, top: 100 }); canvas.add(group);</code>
(See Figure 4). For groups containing images, ensure images are fully loaded before grouping.
Figure 4
Group methods include getObjects
, size
, contains
, item
, forEachObject
, add
, and remove
. addWithUpdate
adds objects while updating group dimensions. Cloning existing canvas objects is necessary before adding them to a new group.
Serialization:
Fabric.js uses toObject
and toJSON
for serialization. toJSON
implicitly calls toObject
.
Example: Serializing an empty canvas:
<code class="language-javascript">var text = new fabric.Text('hello world', { fontSize: 30 }); var circle = new fabric.Circle({ radius: 100, fill: '#eef', scaleY: 0.5 }); var group = new fabric.Group([text, circle], { left: 150, top: 100, angle: -10 }); canvas.add(group);</code>
Adding objects updates the JSON representation. toObject
returns a non-string object representation. Customizing object serialization is achieved by overriding or extending the toObject
method.
Example: Extending toObject
:
<code class="language-javascript">group.item(0).set({ text: 'trololo', fill: 'white' }); group.item(1).setFill('red');</code>
SVG serialization uses the toSVG
method, delegating to individual object's toSVG
methods. This allows for exporting to any SVG-compatible renderer.
Deserialization:
loadFromJSON
and loadFromDatalessJSON
load canvas states from JSON. loadSVGFromURL
and loadSVGFromString
load from SVG data. loadFromDatalessJSON
handles large path data efficiently by referencing external SVG files.
Subclassing:
Fabric.js utilizes fabric.util.createClass
for subclassing. The initialize
property acts as the constructor. callSuper
calls parent class methods.
Example: Creating a custom class:
<code class="language-javascript">var circle1 = new fabric.Circle({ radius: 50, fill: 'red', left: 0 }); var circle2 = new fabric.Circle({ radius: 50, fill: 'green', left: 100 }); var circle3 = new fabric.Circle({ radius: 50, fill: 'blue', left: 200 }); var group = new fabric.Group([circle1, circle2, circle3], { left: 200, top: 100 }); canvas.add(group);</code>
(See Figures 8 and 9 for examples of LabeledRect
). Custom properties should be handled in toObject
and initialize
.
Frequently Asked Questions (FAQs): (The provided FAQs are already well-structured and comprehensive, so no changes are needed here.)
The images are included as requested, maintaining their original format and positions within the text.
The above is the detailed content of Fabric.js: Advanced. For more information, please follow other related articles on the PHP Chinese website!