Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to keep the image at the back of the HTML5 canvas when moving elements with fabric.js?
When working with fabric.js, you might want to keep a background image fixed while allowing other elements to move freely on top. By default, fabric.js may reorder objects during interactions, potentially bringing background elements to the front.
The Solution: preserveObjectStacking
To maintain the proper layering order, use the preserveObjectStacking property when creating your fabric.js canvas:
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/4.5.0/fabric.min.js"></script>
</head>
<body>
<canvas id="c" width="400" height="300"></canvas>
<script>
// Create canvas with preserveObjectStacking enabled
var canvas = new fabric.Canvas('c', {
preserveObjectStacking: true
});
// Add a background image
fabric.Image.fromURL('https://via.placeholder.com/400x300/cccccc/666666?text=Background', function(img) {
img.set({
left: 0,
top: 0,
selectable: false
});
canvas.add(img);
canvas.sendToBack(img); // Ensure it stays at the back
});
// Add a movable rectangle on top
var rect = new fabric.Rect({
left: 100,
top: 100,
fill: 'red',
width: 80,
height: 60
});
canvas.add(rect);
// Add a movable circle
var circle = new fabric.Circle({
left: 200,
top: 150,
fill: 'blue',
radius: 40
});
canvas.add(circle);
</script>
</body>
</html>
How It Works
The preserveObjectStacking property prevents fabric.js from automatically reordering objects when they are selected or moved. This ensures that:
- Background images remain at the bottom layer
- Moving objects don't accidentally jump behind background elements
- The visual hierarchy is maintained during interactions
Additional Background Control
For even better control over background images, you can also use fabric.js's built-in background methods:
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/4.5.0/fabric.min.js"></script>
</head>
<body>
<canvas id="c2" width="400" height="300"></canvas>
<script>
var canvas = new fabric.Canvas('c2', {
preserveObjectStacking: true
});
// Set background image using setBackgroundImage
canvas.setBackgroundImage('https://via.placeholder.com/400x300/eeeeee/999999?text=Fixed+Background', canvas.renderAll.bind(canvas));
// Add movable shapes
var triangle = new fabric.Triangle({
left: 150,
top: 100,
fill: 'green',
width: 60,
height: 60
});
canvas.add(triangle);
</script>
</body>
</html>
Key Benefits
| Approach | Background Fixed? | Selectable Background? |
|---|---|---|
preserveObjectStacking: true |
Yes | Can be disabled |
setBackgroundImage() |
Yes | No |
Conclusion
Using preserveObjectStacking: true ensures background images stay behind movable elements in fabric.js. For completely fixed backgrounds, consider using setBackgroundImage() instead.
