Skip to main content

Translation

Move shapes using the x and y props on the Shape component.
import { SwCanvas, Shape, Rect } from 'react-thorvg-fiber';

<SwCanvas width={200} height={200}>
  <Shape fill={[59, 130, 246, 255]} x={40} y={40}>
    <Rect x={0} y={0} width={60} height={60} />
  </Shape>
  <Shape fill={[234, 88, 12, 255]} x={120} y={120}>
    <Rect x={0} y={0} width={60} height={60} />
  </Shape>
</SwCanvas>
The x and y props translate the entire shape. The first square appears at (40, 40) and the second at (120, 120).

Rotation

Rotate shapes around their origin using the rotation prop (in degrees).
import { SwCanvas, Shape, Rect } from 'react-thorvg-fiber';

<SwCanvas width={200} height={200}>
  <Shape fill={[234, 88, 12, 255]} rotation={45}>
    <Rect x={50} y={50} width={80} height={80} />
  </Shape>
</SwCanvas>
Rotates the rectangle 45 degrees around the top-left corner of the canvas (0, 0).

Scale

Scale shapes using the scaleX and scaleY props.
import { SwCanvas, Shape, Circle } from 'react-thorvg-fiber';

<SwCanvas width={200} height={200}>
  <Shape fill={[59, 130, 246, 255]} scaleX={1.5} scaleY={1.5}>
    <Circle x={100} y={100} radius={40} />
  </Shape>
</SwCanvas>
Scales the circle uniformly by 1.5x in both directions.

Combined Transforms

Combine translation, rotation, and scaling for complex transformations.
import { SwCanvas, Shape, Rect } from 'react-thorvg-fiber';

<SwCanvas width={200} height={200}>
  <Shape
    fill={[34, 197, 94, 255]}
    x={100}
    y={100}
    rotation={30}
    scaleX={1.2}
    scaleY={1.2}
  >
    <Rect x={-40} y={-40} width={80} height={80} />
  </Shape>
</SwCanvas>
This example:
  1. Translates the shape to (100, 100)
  2. Scales it by 1.2x
  3. Rotates it 30 degrees
Transforms are applied in this order: scale, rotation, then translation.

Transform Origin

Control the transform origin point by carefully positioning your geometry.
import { SwCanvas, Shape, Rect } from 'react-thorvg-fiber';

<SwCanvas width={200} height={200}>
  <Shape fill={[234, 88, 12, 255]} rotation={45}>
    <Rect x={80} y={80} width={60} height={60} />
  </Shape>
</SwCanvas>
Rotates around the top-left corner of the canvas (0, 0).

Animated Transforms

Transforms can be animated using React state or animation libraries.
import { SwCanvas, Shape, Rect } from 'react-thorvg-fiber';
import { useState, useEffect } from 'react';

function RotatingSquare() {
  const [rotation, setRotation] = useState(0);
  
  useEffect(() => {
    const interval = setInterval(() => {
      setRotation(r => (r + 1) % 360);
    }, 16);
    return () => clearInterval(interval);
  }, []);
  
  return (
    <SwCanvas width={200} height={200}>
      <Shape fill={[59, 130, 246, 255]} x={100} y={100} rotation={rotation}>
        <Rect x={-40} y={-40} width={80} height={80} />
      </Shape>
    </SwCanvas>
  );
}
This creates a continuously rotating square by updating the rotation prop every frame.

Negative Scales

Use negative scale values to flip shapes.
import { SwCanvas, Shape, Rect } from 'react-thorvg-fiber';

<SwCanvas width={200} height={200}>
  <Shape fill={[234, 88, 12, 255]} x={100} scaleX={-1}>
    <Rect x={0} y={50} width={80} height={60} />
  </Shape>
</SwCanvas>
Flips the rectangle horizontally.

Build docs developers (and LLMs) love