Fabric.js provides viewport manipulation capabilities through the viewportTransform property, allowing developers to control how the entire canvas appears to users.
The viewportTransform property leverages native canvas transformation functionality, essentially wrapping the canvas 2D API's transform() method. According to the official documentation:
viewportTransform: Array - The transformation (a Canvas 2D API transform matrix) which focuses the viewport
Common use cases for modifying viewportTransform include canvas panning and zooming features.
Understanding viewportTransform Structure
The viewportTransform property is an array containing six elements with a default value of [1, 0, 0, 1, 0, 0]. Each index represents a specific transformation component:
[0]: Horizontal scaling (X-axis)[1]: Horizontal skewing (X-axis)[2]: Vertical skewing (Y-axis)[3]: Vertical scaling (Y-axis)[4]: Horizontal translation (X-axis)[5]: Vertical translation (Y-axis)
This arrangement follows the standard 2D transformation matrix pattern used in computer graphics and linear algebra.
For clarity, we can represent these parameters as: viewportTransform[a, b, c, d, e, f]
Setting Up the Demo Environment
To demonstrate the various transformations, here's a basic canvas setup with sample objects:
<canvas id="demoCanvas" width="600" height="600" style="border: 1px solid #ccc;"></canvas>
<script>
// Initialize canvas instance
const canvasInstance = new fabric.Canvas('demoCanvas', {
width: 400,
height: 400
})
// Create rectangle object
const rectangle = new fabric.Rect({
top: 10,
left: 10,
width: 80,
height: 80,
fill: 'lightcoral'
})
// Create triangle object
const polygon = new fabric.Triangle({
top: 100,
left: 100,
width: 80,
height: 100,
fill: 'lightblue'
})
// Create text element
const label = new fabric.Text('Sample Text', {
top: 240,
left: 100
})
// Add all objects to canvas
canvasInstance.add(rectangle, polygon, label)
</script>
Scaling Transformations
To scale the viewport, modify indices 0 and 3 of the viewportTransform array.
Horizontal scaling example:
// Double the horizontal scale
canvasInstance.viewportTransform[0] = 2
Vertical scaling example:
// Reduce vertical scale by half
canvasInstance.viewportTransform[3] = 0.5
Skewing Transformations
Skew transformations affect the angle of displayed content:
Horizontal skew:
// Apply positive horizontal skew
canvasInstance.viewportTransform[1] = 0.1
Negative horizontal skew:
// Apply negative horizontal skew
canvasInstance.viewportTransform[1] = -0.1
Vertical skew:
// Apply vertical skew
canvasInstance.viewportTransform[2] = 0.2
Translation (Panning)
Trasnlation moves the entire canvas view without rotating or scaling:
Horizontal translation:
// Move canvas view 10 units right
canvasInstance.viewportTransform[4] = 10
Vertical translation:
// Move canvas view 10 units down
canvasInstance.viewportTransform[5] = 10
Using setViewportTransform Method
Fabric.js also provides the setViewportTransform method as an alternative approach. This method accepts an aray parameter with the same meaning as the viewportTransform property.
// Set complete transformation matrix at once
const transformationMatrix = [1.5, 0, 0, 1.5, 20, 20];
canvasInstance.setViewportTransform(transformationMatrix);
The key difference is that viewportTransform is a property you can modify directly, while setViewportTransform is a method that replaces the entire transformation matrix.