Propiedad de rotación centrada en la imagen de Fabric.js

Fabric.js es una biblioteca de javascript que se utiliza para trabajar con lienzo. La imagen del lienzo pertenece a la clase de fabric.js que se utiliza para crear instancias de imágenes. La imagen del lienzo significa que la imagen es móvil y se puede estirar según los requisitos. La propiedad centeredRotation de la imagen se usa para rotar la imagen del lienzo a lo largo del centro, es decir, el origen.

Enfoque: primero importe la biblioteca fabric.js. Después de importar la biblioteca, cree un bloque de lienzo en la etiqueta del cuerpo que contendrá la imagen. Después de esto, inicialice una instancia de Canvas y la clase de imagen proporcionada por Fabric.JS y establezca la propiedad centeredRotation primero en falso y luego en verdadero e intente rotar la imagen con la ayuda de los controles. Luego renderice la instancia de la imagen en el lienzo y centre el elemento.

Sintaxis:

fabric.Image(image, {
    centeredRotation:Boolean
});

Parámetros: La función anterior toma dos parámetros como se mencionó anteriormente y se describe a continuación:

  • image: Este parámetro toma la imagen.
  • centeredRotation: este parámetro define si habilitar o deshabilitar la rotación centrada de la imagen del lienzo.

Ejemplo 1: este ejemplo usa FabricJS para habilitar la rotación centrada de la imagen del lienzo. La rotación de la imagen es junto con el origen como se muestra en el siguiente ejemplo.

<!DOCTYPE html> 
<html> 
  
<head> 
    <!-- Adding the FabricJS library --> 
    <script src= 
"https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js"> 
    </script> 
</head> 
  
<body> 
    <h1 style="color: green;">GeeksforGeeks</h1>
    <b>Fabric.js | Image centeredRotation Property </b>  
       
    <canvas id="canvas" width="300" height="250"
        style="border:2px solid #000000"> 
    </canvas> 
  
    <img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20200327230544/g4gicon.png" 
        width="100" height="100" id="my-image">
    <br>
  
    <button onclick="func()">Clickme</button>
  
    <script> 
        // Create the instance of canvas object
        var canvas = new fabric.Canvas("canvas"); 
  
        // Getting the image
        var img= document.getElementById('my-image');
  
        // Creating the image instance 
        var imgInstance = new fabric.Image(img, {
            centeredRotation:true
        });
  
        // Rendering the image to canvas
        canvas.add(imgInstance);
        canvas.centerObject(imgInstance);
    </script> 
</body> 
  
</html>

Producción:

Ejemplo 2:

<!DOCTYPE html> 
<html> 
  
<head> 
    <!-- Adding the FabricJS library --> 
    <script src= 
"https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js"> 
    </script> 
</head> 
  
<body> 
    <h1 style="color: green;">GeeksforGeeks</h1> 
    <b>Fabric.js | Image centeredRotation Property </b> 
  
    <canvas id="canvas" width="300" height="300"
        style="border:2px solid #000000"> 
    </canvas> 
  
    <img src =
"https://www.geeksforgeeks.org/wp-content/uploads/gfg_200X200-1.png" 
        width="100" height="100" id="my-image"
        style="display:none">
    <br>
  
    <button onclick="func()">Clickme</button>
  
    <script> 
        // Create the instance of canvas object
        var canvas = new fabric.Canvas("canvas"); 
  
        // Getting the image
        var img= document.getElementById('my-image');
  
        // Creating the image instance 
        var imgInstance = new fabric.Image(img, {
            centeredRotation:false
        });
  
        // Rendering the image to canvas
        canvas.add(imgInstance);
  
        func=()=>{
            var imgInstance = new fabric.Image(img, {
                centeredRotation:true
            });
  
            // Rendering the image to canvas
            canvas.add(imgInstance);
            canvas.clear();
            canvas.add(imgInstance);
        }
    </script> 
</body> 
  
</html>

Producción:

Publicación traducida automáticamente

Artículo escrito por TARuN y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *