Home >Web Front-end >JS Tutorial >How to create a canvas with a background image using FabricJS?
In this article, we will create a canvas with a background image using FabricJS. There are two methods in FabricJS to change the background image of the canvas.
The first way is to use the Canvas class itself and pass backgroundImage.
The second method is to use the setBackgroundColor method. Let's understand them through an example.
In the first method, we will use the Canvas class itself by passing it in the second parameter of the classbackgroundImage.
new fabric.Canvas(element: HTMLElement|String, {backgroundImage: fabric.Image}: Object)
Element - This parameter is
Options (optional) - This parameter is an object that provides additional customization of the canvas, backgroundImage is one of them, it will help us customize the background image. backgroundImageUse fabric.Image as the value of the specified canvas background image.
Check out the following example that demonstrates how to create a Canvas with a background image using FabricJS. Since FabricJS works on top of the Canvas API, we will create an HTML element using the We can also use the setBackgroundImage method provided in the Canvas class after creating the canvas. Let's take a closer look at how to achieve this. image - This parameter accepts fabric.Image or referenced String The URL of the image we want to set the background to. callback - This parameter takes a callback function that is called when the image is loaded and set as the background. Options (optional): - This parameter is the object in which we can specify the background image options. We can change the opacity, enlarge or reduce the image, etc. First, we assign the image URL to a variable and use it as the first parameter. In the second parameter we will bind the canvas using renderAll() method after setting the background image as shown in the code below -<!DOCTYPE html>
<html>
<head>
<!-- Adding the Fabric JS Library-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script>
</head>
<body>
<h2> Creating a Canvas with a Background Image in Fabric.js </h2>
<canvas id="canvas"> </canvas>
<script>
//Initiate a canvas instance
var canvas = new fabric.Canvas("canvas", {
backgroundImage: "https://www.tutorialspoint.com/tools/images/logo.png",
});
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
Method 2: Use the setBackgroundImage method
Syntax
canvas.setBackgroundImage(image: fabric.Image | String, callback: function, options: Object)
Parameters
Example 2
<!DOCTYPE html>
<html>
<head>
<!-- Adding the Fabric JS Library-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script>
</head>
<body>
<h2> Creating a Canvas with a Background Image in Fabric.js </h2>
<p> Here we have used the setBackgroundImage method. </p>
<canvas id="canvas"> </canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
// Image URL
var imageURL = "https://www.tutorialspoint.com/tools/images/logo.png";
canvas.setBackgroundImage(imageURL, canvas.renderAll.bind(canvas), {
backgroundImageOpacity: 1,
originX: "left",
originY: "top",
top: 90,
left: 70,
scaleX: 1.1,
scaleY: 1.1,
});
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
The above is the detailed content of How to create a canvas with a background image using FabricJS?. For more information, please follow other related articles on the PHP Chinese website!