How can i change the rotation icon in fabricjs

前端 未结 7 2011
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-03 03:45

Please guide me to modify Fabricjs to add custom icon for Rotation.

I get some of the answers but it is not working fine.

Please let me know the code to chan

7条回答
  •  我在风中等你
    2021-01-03 04:23

    With the release of v4.0 in August of 2020, Fabric.js now includes a complete control customization system which makes styling the rotate control quite easy.

    var canvas = new fabric.Canvas('c');
    // set your icon here as a base64 string
    var rotateIcon = "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGhlaWdodD0iMjQiIHZpZXdCb3g9IjAgMCAyNCAyNCIgd2lkdGg9IjI0Ij48cGF0aCBkPSJNMCAwaDI0djI0SDB6IiBmaWxsPSJub25lIi8+PHBhdGggZD0iTTEyIDZ2M2w0LTQtNC00djNjLTQuNDIgMC04IDMuNTgtOCA4IDAgMS41Ny40NiAzLjAzIDEuMjQgNC4yNkw2LjcgMTQuOGMtLjQ1LS44My0uNy0xLjc5LS43LTIuOCAwLTMuMzEgMi42OS02IDYtNnptNi43NiAxLjc0TDE3LjMgOS4yYy40NC44NC43IDEuNzkuNyAyLjggMCAzLjMxLTIuNjkgNi02IDZ2LTNsLTQgNCA0IDR2LTNjNC40MiAwIDgtMy41OCA4LTggMC0xLjU3LS40Ni0zLjAzLTEuMjQtNC4yNnoiLz48L3N2Zz4=";
    var img = document.createElement('img');
    img.src = rotateIcon;
    
    // here's where your custom rotation control is defined
    // by changing the values you can customize the location, size, look, and behavior of the control
    fabric.Object.prototype.controls.mtr = new fabric.Control({
      x: 0,
      y: -0.5,
      offsetY: -40,
      cursorStyle: 'crosshair',
      actionHandler: fabric.controlsUtils.rotationWithSnapping,
      actionName: 'rotate',
      render: renderIcon,
      cornerSize: 28,
      withConnection: true
    });
    
    // here's where the render action for the control is defined
    function renderIcon(ctx, left, top, styleOverride, fabricObject) {
      var size = this.cornerSize;
      ctx.save();
      ctx.translate(left, top);
      ctx.rotate(fabric.util.degreesToRadians(fabricObject.angle));
      ctx.drawImage(img, -size / 2, -size / 2, size, size);
      ctx.restore();
    }
    
    var rect = new fabric.Rect({
      left: 50,
      top: 70,
      fill: 'red',
      width: 200,
      height: 100,
      objectCaching: false,
      stroke: 'blue',
      strokeWidth: 4,
    });
    
    canvas.add(rect);
    canvas.setActiveObject(rect);
    
    

提交回复
热议问题