Home > Web Front-end > JS Tutorial > How to set properties of an image object from options using FabricJS?

How to set properties of an image object from options using FabricJS?

王林
Release: 2023-08-23 08:01:04
forward
1544 people have browsed it

How to set properties of an image object from options using FabricJS?

In this tutorial we will learn how to set the properties of an image object from the options

Use FabricJS. We can create an Image object by creating an instance of fabric.Image Since it is one of the basic elements of FabricJS, we can also easily modify it by customizing it Apply properties such as angle, opacity, etc. in order to set the properties of the image object from Options, we use the setOptions method.

grammar

setOptions(options: Object)
Copy after login

parameter

  • options (optional) − This parameter is an Object that provides additional Customize our object. Use this parameter origin, stroke width and many other things Other properties related to the image object can be changed.

Default appearance of picture objects

The Chinese translation of

Example

is:

Example

Let's look at a code example to see how the Image object is displayed when setOptions appears.

Method not used.

<!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>Default appearance of image object</h2>
   <p>You can see the default appearance of Image object</p>
   <canvas id="canvas"></canvas>
   <img  src="https://www.tutorialspoint.com/images/logo.png" id="img1"   style="max-width:90%" / alt="How to set properties of an image object from options using FabricJS?" >
   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
      
      // Initiating the image element
      var imageElement = document.getElementById("img1");
      
      // Initiate an Image object
      var image = new fabric.Image(imageElement);
      
      // Add it to the canvas
      canvas.add(image);
   </script>
</body>
</html>
Copy after login

Use setOptionsMethod

The Chinese translation of

Example

is:

Example

In this example, we use the setOptions method to set the properties of the image

options. Here, we have applied a 4px stroke of color “green” and re-adjusted the options. Here we have applied a 4px stroke with the color "green" and resized the Set the position of the Image object to a top value of 30 and a left value of 100
<!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>Using setOptions method</h2>
   <p>
      You can see that the object's properties have been set using setOptions method
   </p>
   <canvas id="canvas"></canvas>
   <img  src="https://www.tutorialspoint.com/images/logo.png" id="img1"   style="max-width:90%" / alt="How to set properties of an image object from options using FabricJS?" >
   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
      
      // Initiating the image element
      var imageElement = document.getElementById("img1");
      
      // Initiate an Image object
      var image = new fabric.Image(imageElement);
      
      // Using setOptions method
      image.setOptions({
         left: 100,
         top: 30,
         stroke: "green",
         strokeWidth: 4,
      });
      
      // Add it to the canvas
      canvas.add(image);
   </script>
</body>
</html>
Copy after login

The above is the detailed content of How to set properties of an image object from options using FabricJS?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template