PathElement Class Hierarchy
PathElement inherits from RenderedElement. It serves as the parent class for several specialized shape elements, including:
RectElementCircleElementEllipseElementLineElementPolylineElementGlyphElement
website·Indexed 30 days ago
https://canvg.js.orgAn SVG renderer on canvas. The library provides tools to convert SVG to PNG and render SVG elements onto a canvas, with support for browser environments, Node.js, Web Workers, and OffscreenCanvas. Includes a comprehensive API for handling SVG elements, transforms, and document options, with migration guidance for v4.
PathElement inherits from RenderedElement. It serves as the parent class for several specialized shape elements, including:
RectElementCircleElementEllipseElementLineElementPolylineElementGlyphElementAnimateElement class is used to represent animation elements within a document. It inherits from the Element class and serves as the base class for more specific animation types such as AnimateColorElement and AnimateTransformElement.AnimateTransformElement class is used to handle transform animations in canvg. It inherits from AnimateElement, which in turn inherits from Element. It provides specialized logic for calculating animation values specifically for transformations.AnimateColorElement class is responsible for handling color animations within a document. It inherits from AnimateElement and is defined in src/Document/AnimateColorElement.ts.next() method to retrieve the next command in the path sequence and isEnd() to determine when the end of the path data has been reached. The reset() method can be used to restart the iteration.const parser = new PathParser(pathString);
parser.reset();
while (!parser.isEnd()) {
const command = parser.next();
// Process command
}Canvg. Update your import statements to use the named export syntax.// v3 (Deprecated)
import Canvg, { presets } from 'canvg';
// v4 (Current)
import { Canvg, presets } from 'canvg';pnpm add canvgThe TextPathElement constructor accepts the following parameters:
document (Document): The document instance associated with the element.node (HTMLElement): The DOM element being processed.captureTextNodes (boolean, optional): Whether to capture text nodes.IOptions object to control how the SVG document is parsed and rendered to the canvas:const options = {
rootEmSize: 16, // Default rem size (number)
emSize: 16, // Default em size (number)
createCanvas: (width, height) => document.createElement('canvas'), // Custom canvas creation function
createImage: (src) => new Image(), // Custom image creation function
anonymousCrossOrigin: true, // Load images anonymously (boolean)
};anonymousCrossOrigin property to true in IDocumentOptions to load images anonymously, which is typically required to avoid CORS issues when manipulating images on a canvas.const options: IDocumentOptions = {
anonymousCrossOrigin: true
};IOptions object:const options = {
enableRedraw: true, // Enable redraw (boolean)
ignoreMouse: false, // Ignore mouse events (boolean)
ignoreAnimation: false, // Ignore animations (boolean)
ignoreDimensions: false, // If true, does not try to resize canvas (boolean)
ignoreClear: false, // If true, does not clear canvas (boolean)
scaleWidth: 800, // Scale horizontally to this width (number)
scaleHeight: 600, // Scale vertically to this height (number)
offsetX: 0, // X offset for drawing (number)
offsetY: 0, // Y offset for drawing (number)
};