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

    Class Scene

    Scene class for hierarchical grouping of Paint objects

    // Grouping shapes in a scene
    const scene = new TVG.Scene();

    const background = new TVG.Shape();
    background.appendRect(0, 0, 800, 600).fill(240, 240, 240, 255);

    const circle = new TVG.Shape();
    circle.appendCircle(100, 100, 50).fill(255, 100, 100, 255);

    scene.add(background);
    scene.add(circle);
    canvas.add(scene);
    // Scene transformations affect all children
    const scene = new TVG.Scene();

    for (let i = 0; i < 5; i++) {
    const shape = new TVG.Shape();
    shape.appendRect(i * 60, 100, 50, 50)
    .fill(100 + i * 30, 150, 255 - i * 30, 255);
    scene.add(shape);
    }

    // Transform entire group
    scene.translate(200, 200).rotate(30);
    canvas.add(scene);

    Hierarchy (View Summary)

    Index
    • get id(): number

      The ID of this paint object. IDs are used to identify paint objects within a picture's scene tree. Assign a string to generate a hash ID from the name, or a number to set directly.

      Returns number

    • set id(value: string | number): void

      Parameters

      • value: string | number

      Returns void

    • get ptr(): number

      Gets the WASM pointer for this object Returns 0 if object has been disposed (error handled by global handler)

      Returns number

    • get isDisposed(): boolean

      Check if this object has been disposed

      Returns boolean

    • Cleanup function to be implemented by subclasses

      Parameters

      • ptr: number

        The WASM pointer to clean up

      Returns void

    • Translate the paint by (x, y)

      Parameters

      • x: number
      • y: number

      Returns this

    • Rotate the paint by angle (in degrees)

      Parameters

      • angle: number

      Returns this

    • Scale the paint by factor

      Parameters

      • factor: number

      Returns this

    • Set the blending method for this paint. Blending determines how this paint is combined with the content below it.

      Parameters

      Returns this

      The Paint instance for method chaining

      const scene = new TVG.Scene();
      scene.blend(BlendMethod.Add);

      const shape = new TVG.Shape();
      shape.appendCircle(100, 100, 50, 50);
      shape.fill(255, 0, 0, 255);
      shape.blend(BlendMethod.Multiply);
    • Get or set the opacity (0 to 255)

      Returns number

      When setting, returns the Paint instance for method chaining. When getting, returns the opacity value (0-255).

      // Set opacity to 50% (half transparent)
      shape.opacity(128);

      // Set to fully opaque
      shape.opacity(255);

      // Get current opacity value
      const currentOpacity = shape.opacity(); // returns 0-255
    • Get or set the opacity (0 to 255)

      Parameters

      • value: number

        The opacity value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque.

      Returns this

      When setting, returns the Paint instance for method chaining. When getting, returns the opacity value (0-255).

      // Set opacity to 50% (half transparent)
      shape.opacity(128);

      // Set to fully opaque
      shape.opacity(255);

      // Get current opacity value
      const currentOpacity = shape.opacity(); // returns 0-255
    • Get the axis-aligned bounding box (AABB) of this paint

      Returns Bounds

    • Get the oriented bounding box (OBB) of this paint as 4 corner points

      Parameters

      • options: { oriented: true }

        Options object with oriented flag

      Returns Point[]

    • Duplicates the object.

      Creates a new object and sets its all properties as in the original object.

      Type Parameters

      • T extends Paint

        The Paint subclass type of the created object

      Returns T

      The created object

    • Applies a custom transformation matrix to the paint.

      This method allows you to apply complex transformations that combine translation, rotation, scaling, and skewing in a single operation. The matrix is multiplied with any existing transformations.

      Parameters

      • matrix: Matrix

        A 3x3 transformation matrix

      Returns this

      The Paint instance for method chaining

      // Apply a combined transformation
      const shape = new TVG.Shape();
      shape.appendRect(0, 0, 100, 100);

      // Create a matrix for: scale(2, 1.5) + rotate(45deg) + translate(100, 50)
      const rad = (45 * Math.PI) / 180;
      const cos = Math.cos(rad);
      const sin = Math.sin(rad);

      shape.transform({
      e11: 2 * cos, e12: -2 * sin, e13: 100,
      e21: 1.5 * sin, e22: 1.5 * cos, e23: 50,
      e31: 0, e32: 0, e33: 1
      });
      // Create a skew transformation
      const shape = new TVG.Shape();
      shape.appendRect(0, 0, 100, 100);

      // Skew in X direction
      shape.transform({
      e11: 1, e12: 0.5, e13: 0,
      e21: 0, e22: 1, e23: 0,
      e31: 0, e32: 0, e33: 1
      });
    • Sets a clipping path for this paint object.

      The clipping path restricts the area where the paint will be rendered. Only the parts of the paint that overlap with the clipper shape will be visible.

      Parameters

      • clipper: Paint

        A Paint object (typically a Shape) to use as the clipping path

      Returns this

      The Paint instance for method chaining

      const circle = new TVG.Shape();
      circle.appendCircle(150, 150, 100);

      const rect = new TVG.Shape();
      rect.appendRect(0, 0, 300, 300)
      .fill(255, 0, 0, 255)
      .clip(circle);

      canvas.add(rect);
    • Sets a masking target object and the masking method.

      The masking restricts the transparency of the source paint using the target paint.

      Parameters

      • target: Paint

        A Paint object to use as the masking target

      • method: MaskMethod

        The method used to mask the source object with the target

      Returns this

      The Paint instance for method chaining

      const mask = new TVG.Shape();
      mask.appendCircle(200, 200, 125);
      mask.fill(255, 255, 255);

      const shape = new TVG.Shape();
      shape.appendRect(0, 0, 400, 400)
      .fill(255, 0, 0, 255)
      .mask(mask, MaskMethod.Alpha);

      canvas.add(shape);
    • Checks whether the given rectangular region intersects the filled area of the paint.

      Useful for hit-testing, such as detecting whether a click or touch landed on a painted region. The paint must have been updated by a Canvas beforehand — typically after the canvas has been drawn and synchronized.

      Parameters

      • x: number

        The x-coordinate of the region's top-left corner

      • y: number

        The y-coordinate of the region's top-left corner

      • width: number

        The width of the region. Must be greater than 0

      • height: number

        The height of the region. Must be greater than 0

      • visibleOnly: boolean = false

        If true, hidden paints are excluded from the test (default: false)

      Returns boolean

      true if any part of the region intersects the filled area, false otherwise

      To test a single point, set width and height to 1. This test does not account for the results of blending or masking.

      visible

      const shape = new TVG.Shape();
      shape.appendRect(100, 100, 200, 200);
      canvas.add(shape).render();

      // Check if shape intersects with a region
      if (shape.intersects(150, 150, 100, 100)) {
      console.log('Shape intersects with region');
      }

      // Hit-test a single point, ignoring hidden paints
      if (shape.intersects(event.offsetX, event.offsetY, 1, 1, true)) {
      console.log('Clicked a visible part of the shape');
      }
    • Create a new instance of this paint type with the given pointer Must be implemented by subclasses

      Parameters

      • ptr: number

      Returns Scene

    • Remove paint(s) from the scene If no paint is provided, removes all paints

      Parameters

      Returns this

    • Clear all paints from the scene (alias for remove())

      Returns this

    • Reset all previously applied scene effects, restoring the scene to its original state.

      Returns this

      The Scene instance for method chaining

      const scene = new TVG.Scene();
      scene.dropShadow(128, 128, 128, 200, 45, 5, 2, 60);
      scene.resetEffects(); // Remove all effects
    • Apply a Gaussian blur effect to the scene.

      Parameters

      • sigma: number

        Blur intensity (> 0)

      • direction: number = 0

        Blur direction: 0 (both), 1 (horizontal), 2 (vertical)

      • border: number = 0

        Reserved for border handling. Currently ignored.

      • quality: number = 75

        Blur quality (0-100)

      Returns this

      The Scene instance for method chaining

      const scene = new TVG.Scene();
      scene.add(shape1);
      scene.add(shape2);
      scene.gaussianBlur(1.5, 0, 0, 75); // Apply blur to entire scene
    • Beta

      Apply a centered linear motion blur effect to the scene.

      The blur is spread evenly on both sides of the content along the given angle. The scene transform applies to the blur vector.

      Parameters

      • distance: number

        Total blur length in scene-local units (>= 0)

      • angle: number = 0

        Blur angle in degrees, measured clockwise from the positive x-axis. Default: 0

      • quality: number = 75

        Blur quality (0-100)

      Returns this

      The Scene instance for method chaining

      const scene = new TVG.Scene();
      scene.add(shape);
      scene.motionBlur(30, 45, 75); // 30px blur along the 45° direction
    • Apply a drop shadow effect with Gaussian blur filter to the scene.

      Parameters

      • r: number

        Red component (0-255)

      • g: number

        Green component (0-255)

      • b: number

        Blue component (0-255)

      • a: number

        Alpha/opacity (0-255)

      • angle: number

        Shadow angle in degrees (0-360)

      • distance: number

        Shadow distance/offset

      • sigma: number

        Blur intensity for the shadow (> 0)

      • quality: number = 60

        Blur quality (0-100)

      Returns this

      The Scene instance for method chaining

      const scene = new TVG.Scene();
      scene.add(shape);
      // Add gray drop shadow at 45° angle, 5px distance, 2px blur
      scene.dropShadow(128, 128, 128, 200, 45, 5, 2, 60);
    • Override the scene content color with a given fill color.

      Parameters

      • r: number

        Red component (0-255)

      • g: number

        Green component (0-255)

      • b: number

        Blue component (0-255)

      • a: number

        Alpha/opacity (0-255)

      Returns this

      The Scene instance for method chaining

      const scene = new TVG.Scene();
      scene.add(shape1);
      scene.add(shape2);
      scene.fillEffect(255, 0, 0, 128); // Fill entire scene with semi-transparent red
    • Apply a tint effect to the scene using black and white color parameters.

      Parameters

      • blackR: number

        Black tint red component (0-255)

      • blackG: number

        Black tint green component (0-255)

      • blackB: number

        Black tint blue component (0-255)

      • whiteR: number

        White tint red component (0-255)

      • whiteG: number

        White tint green component (0-255)

      • whiteB: number

        White tint blue component (0-255)

      • intensity: number

        Tint intensity (0-100)

      Returns this

      The Scene instance for method chaining

      const scene = new TVG.Scene();
      scene.add(picture);
      // Apply sepia-like tint
      scene.tint(112, 66, 20, 255, 236, 184, 50);
    • Apply a tritone color effect to the scene using three color parameters for shadows, midtones, and highlights. A blending factor determines the mix between the original color and the tritone colors.

      Parameters

      • shadowR: number

        Shadow red component (0-255)

      • shadowG: number

        Shadow green component (0-255)

      • shadowB: number

        Shadow blue component (0-255)

      • midtoneR: number

        Midtone red component (0-255)

      • midtoneG: number

        Midtone green component (0-255)

      • midtoneB: number

        Midtone blue component (0-255)

      • highlightR: number

        Highlight red component (0-255)

      • highlightG: number

        Highlight green component (0-255)

      • highlightB: number

        Highlight blue component (0-255)

      • blend: number

        Blend factor (0-255)

      Returns this

      The Scene instance for method chaining

      const scene = new TVG.Scene();
      scene.add(picture);
      // Apply tritone: dark blue shadows, gray midtones, yellow highlights
      scene.tritone(0, 0, 128, 128, 128, 128, 255, 255, 0, 128);