ThorVG WebCanvas API - v1.1.4
    Preparing search index...

    Class Canvas

    Canvas rendering context for ThorVG vector graphics.

    Manages the rendering pipeline and provides methods for adding/removing Paint objects and controlling the render loop.

    // Initialize with renderer
    const TVG = await ThorVG.init({ renderer: 'gl' });

    // Basic canvas setup with shapes
    const canvas = new TVG.Canvas('#canvas', { width: 800, height: 600 });

    const shape = new TVG.Shape();
    shape.appendRect(100, 100, 200, 150, 10)
    .fill(255, 100, 50, 255);

    canvas.add(shape).render();
    // Animation loop
    const canvas = new TVG.Canvas('#canvas');
    const shape = new TVG.Shape();

    let rotation = 0;
    function animate() {
    shape.reset()
    .appendRect(0, 0, 100, 100)
    .fill(100, 150, 255, 255)
    .rotate(rotation++)
    .translate(400, 300);

    canvas.update().render();
    requestAnimationFrame(animate);
    }
    animate();
    Index
    • Creates a new Canvas rendering context.

      The renderer is determined by the global setting from ThorVG.init().

      Parameters

      • target: string | constants.CanvasTarget

        CSS selector for the target HTML canvas element (e.g., '#canvas', '.my-canvas'), the HTMLCanvasElement itself, or an OffscreenCanvas

      • options: CanvasOptions = {}

        Configuration options for the canvas

      Returns Canvas

      If the canvas element is not found or renderer initialization fails

      // Initialize with renderer
      const TVG = await ThorVG.init({ renderer: 'gl' });

      // Basic canvas with default options (DPR enabled by default)
      const canvas = new TVG.Canvas('#canvas');
      // Canvas with custom size
      const TVG = await ThorVG.init({ renderer: 'wg' });
      const canvas = new TVG.Canvas('#myCanvas', {
      width: 1920,
      height: 1080
      });
      // Canvas with DPR disabled for consistent rendering across devices
      const canvas = new TVG.Canvas('#canvas', {
      width: 800,
      height: 600,
      enableDevicePixelRatio: false
      });
      // Enable smart (partial) rendering (SW renderer)
      const TVG = await ThorVG.init({ renderer: 'sw' });
      const canvas = new TVG.Canvas('#canvas', {
      engineOption: TVG.EngineOption.SmartRender
      });
    • get dpr(): number

      Gets the current device pixel ratio applied to this canvas.

      ThorVG uses an optimized DPR formula for best performance: 1 + ((window.devicePixelRatio - 1) * 0.75)

      This provides a balance between visual quality and rendering performance, especially on high-DPI displays.

      Returns number

      The current effective DPR value, or 1.0 if DPR scaling is disabled

      // Getting the current DPR
      const canvas = new TVG.Canvas('#canvas', {
      enableDevicePixelRatio: true
      });

      console.log(canvas.dpr); // e.g., 1.75 on a 2x display
      console.log(window.devicePixelRatio); // e.g., 2.0
      // Using DPR for responsive calculations
      const canvas = new TVG.Canvas('#canvas');
      const shape = new TVG.Shape();

      // Adjust stroke width based on DPR for consistent appearance
      const strokeWidth = 2 / canvas.dpr;
      shape.appendCircle(100, 100, 50)
      .stroke(255, 0, 0, 255)
      .strokeWidth(strokeWidth);

      CanvasOptions.enableDevicePixelRatio for controlling DPR scaling

    • get renderer(): string

      Gets the rendering backend type currently in use.

      Returns string

      The renderer type: 'sw' (Software), 'gl' (WebGL), or 'wg' (WebGPU)

      const canvas = new TVG.Canvas('#canvas', { renderer: 'wg' });
      console.log(canvas.renderer); // 'wg'
    • Adds a Paint object to the canvas for rendering.

      Paint objects include Shape, Scene, Picture, Text, and Animation.picture. Objects are rendered in the order they are added (painter's algorithm).

      Parameters

      • paint: Paint | Video

        A Paint object to add to the canvas

      Returns this

      The canvas instance for method chaining

      const shape = new TVG.Shape();
      const text = new TVG.Text();
      canvas.add(shape);
      canvas.add(text);
      // Method chaining
      canvas.add(shape1)
      .add(shape2)
      .render();
    • Removes one or all Paint objects from the canvas.

      Parameters

      • Optionalpaint: Paint | Video

        Optional Paint object to remove. If omitted, removes all Paint objects.

      Returns this

      The canvas instance for method chaining

      // Remove a specific paint
      canvas.remove(shape);
      // Remove all paints
      canvas.remove();
    • Clears all Paint objects from the canvas and renders an empty frame.

      This is equivalent to remove() without arguments, but also immediately renders the cleared canvas.

      Returns this

      The canvas instance for method chaining

      canvas.clear(); // Clears and renders empty canvas
      
    • Updates the canvas state before rendering.

      This method should be called before render when working with animations or when Paint objects have been modified. It ensures all transformations and changes are processed.

      Returns this

      The canvas instance for method chaining

      // Animation loop pattern
      function animate() {
      animation.frame(currentFrame++);
      canvas.update().render();
      requestAnimationFrame(animate);
      }

      For static scenes, calling render alone is sufficient. For animated content, always call update() before render().

    • Renders all Paint objects to the canvas.

      This method draws all added Paint objects to the canvas using the configured rendering backend (Software, WebGL, or WebGPU).

      Returns this

      The canvas instance for method chaining

      // Static rendering
      canvas.add(shape).add(text).render();
      // Animation loop
      function animate() {
      canvas.update().render();
      requestAnimationFrame(animate);
      }

      For animated content, call update before render(). For static scenes, render() can be called directly.

    • Resizes the canvas to new dimensions.

      Parameters

      • width: number

        New width in pixels

      • height: number

        New height in pixels

      Returns this

      The canvas instance for method chaining

      canvas.resize(1920, 1080).render();
      
      // Responsive canvas
      window.addEventListener('resize', () => {
      canvas.resize(window.innerWidth, window.innerHeight).render();
      });
    • Sets the viewport for rendering a specific region of the canvas.

      The viewport defines the rectangular region where rendering occurs. Useful for rendering to a portion of the canvas or implementing split-screen views.

      Parameters

      • x: number

        X coordinate of the viewport origin

      • y: number

        Y coordinate of the viewport origin

      • w: number

        Viewport width

      • h: number

        Viewport height

      Returns this

      The canvas instance for method chaining

      // Render to top-left quarter of canvas
      canvas.viewport(0, 0, canvas.width / 2, canvas.height / 2);
    • Destroys the canvas and frees its WASM memory.

      After calling destroy(), this canvas instance cannot be used. The ThorVG module remains loaded and new canvases can be created.

      Returns void

      canvas.destroy();
      // Create a new canvas
      const newCanvas = new TVG.Canvas('#canvas');

      This method should be called when you're done with a canvas to free memory. It's particularly important in single-page applications where canvases may be created and destroyed frequently.