JSFiddle - React, Tailwind, and code Playground

by Vijay Gujar

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/4.0.0-beta.5/fabric.js"></script>
<button id='btnClip' onclick="clip()" >
Clip
</button>
<canvas id="canvas" width="500" height="500"></canvas>

JavaScript

var canvas = new fabric.Canvas('canvas');
var strokeWidth = 8;
var r1 = new fabric.Rect({
	left: 0,
  top: 0,
  width: 70,
  height: 50,
  strokeWidth: strokeWidth,
  stroke: '#ff00ff',
  fill: 'transparent'
})



var t1 = new fabric.Textbox('Hello', {
    width: 100,
    height: 100,
    top: 0,
    left: 0,
    fontSize: 50,
    textAlign: 'left'
});


var g = new fabric.Group([r1,t1],{
	left: 0,
  top: 0
  
})
var mainGroup = new fabric.Group([g],{
	left: 0,
  top: 0
})

var mainGroupParent = new fabric.Group([mainGroup],{
	left: 0,
  top: 0
})

//canvas.add(r1);
canvas.add(mainGroupParent);
//canvas.add(mainGroupParent);
/* mainGroup.left = 10;
mainGroup.top = 25; */



canvas.renderAll();

function clip() {
//console.log(mainGroup.left)
	t1.clipPath = new fabric.Rect({
  	left: -r1.width/2 - strokeWidth,
    top: -r1.height/2 - strokeWidth,
    width: r1.width-strokeWidth,
    height: r1.height,
    //absolutePositioned: true,
    
  })
  
  mainGroupParent.dirty = true;
 // console.log(this.r1, t1.clipPath)
 
  
  
  canvas.renderAll();
}