JSFiddle - React, Tailwind, and code Playground
by dhaval_123
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js""></script>
<div class="wrapper" style="backgroundColor='green';display:block">
<div class="t-shirt">
<canvas id="c" width="500" height="400"></canvas>
<input type="button" id="saveImg" value="Save Image"/>
</div>
<div class="myText" style="float:right">
<input type="text" id="logo"/>
<input type="button" id="loadText" value="Load Text"/>
<input type="button" id="left" value="left"/>
<input type="button" id="right" value="right"/>
<input type="button" id="top" value="top"/>
<input type="button" id="bottom" value="bottom" />
</div>
</div>
JavaScript
var canvas = new fabric.Canvas('c');
var scaleFactor=0.4
canvas.backgroundColor = 'yellow';
canvas.renderAll();
var myImg = 'http://izy.urweb.eu/files/tshirt.jpg';
fabric.Image.fromURL(myImg, function(myImg) {
var img1 = myImg.scale(scaleFactor).set({ left: 0, top: 0 });
var text = new fabric.Text('the_text_sample\nand more', {
fontFamily: 'Arial',
fontSize:20,
});
text.set("top",myImg.height*scaleFactor-myImg.height*scaleFactor+150);
text.set("left",myImg.width*scaleFactor/2-text.width/2);
var group = new fabric.Group([ img1,text ], { left: 10, top: 10 });
canvas.add(group);
console.log(canvas._objects[0]._objects[1].text);
});
$('#saveImg').on('click',saveImg);
function saveImg = () => {
console.log('export image');
var image = new Image(),
canvass = document.createElements('canvas'),
ctxs = canvass.getContext('2d');
image.src = 'https://i.stack.imgur.com/I4jXc.png';
image.onload = () => {
ctxs.drawImage(image,
70, 20, // Start at 70/20 pixels from the left and the top of the image (crop),
50, 50, // "Get" a `50 * 50` (w * h) area from the source image (crop),
0, 0, // Place the result at 0, 0 in the canvas,
100, 100); // With as width / height: 100 * 100 (scale)
window.open(canvass.toDataURL('png'));
}
}