What is zrender?
zrender is a lightweight canvas library that provides a powerful and flexible way to create and manipulate 2D graphics. It is often used as a rendering engine for the ECharts library, but it can also be used independently for custom graphics and animations.
What are zrender's main functionalities?
Basic Shapes
This feature allows you to create basic shapes like circles, rectangles, and lines. The code sample demonstrates how to create a red circle and add it to the canvas.
const zrender = require('zrender');
const zr = zrender.init(document.getElementById('main'));
const circle = new zrender.Circle({
shape: {
cx: 150,
cy: 150,
r: 50
},
style: {
fill: 'red'
}
});
zr.add(circle);
Animations
This feature allows you to animate shapes. The code sample demonstrates how to animate a rectangle moving from one position to another over a duration of 1000 milliseconds.
const zrender = require('zrender');
const zr = zrender.init(document.getElementById('main'));
const rect = new zrender.Rect({
shape: {
x: 50,
y: 50,
width: 100,
height: 100
},
style: {
fill: 'blue'
}
});
zr.add(rect);
rect.animate('shape', true)
.when(1000, { x: 200 })
.start();
Event Handling
This feature allows you to handle events like click, mouseover, and mouseout on shapes. The code sample demonstrates how to add a click event listener to a circle that shows an alert when the circle is clicked.
const zrender = require('zrender');
const zr = zrender.init(document.getElementById('main'));
const circle = new zrender.Circle({
shape: {
cx: 150,
cy: 150,
r: 50
},
style: {
fill: 'green'
}
});
zr.add(circle);
circle.on('click', function () {
alert('Circle clicked!');
});
Other packages similar to zrender
fabric
Fabric.js is a powerful and simple JavaScript HTML5 canvas library. It provides an interactive object model on top of the canvas element, allowing for complex shapes, animations, and event handling. Compared to zrender, Fabric.js offers more built-in functionalities for object manipulation and interaction.
paper
Paper.js is an open-source vector graphics scripting framework that runs on top of the HTML5 Canvas. It is particularly well-suited for creating complex vector graphics and animations. Compared to zrender, Paper.js focuses more on vector graphics and provides a more extensive set of tools for path manipulation.
konva
Konva.js is a 2D canvas library that extends the 2D context by enabling high-performance animations, transitions, and event handling. It is designed to be fast and easy to use. Compared to zrender, Konva.js offers a more straightforward API for creating and animating shapes, but it may not be as flexible for custom rendering needs.
ZRender
A lightweight canvas library which providing 2d draw for ECharts
Document
https://ecomfe.github.io/zrender-doc/public/
License
BSD 3-Clause License
Copyright (c) 2017, Baidu Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
-
Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
-
Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
-
Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.