javascript - How save image with canvas in fabric.js -
i creating application t-shirt customization in have put canvas on image using css, problem saving image canvas. todataurl
gives part of canvas area, want whole image. there other solutions on stack overflow not solve problem.
hello, have create image object (tshirt) text object holds message.
- to , load image fabric.image.fromurl() function , inside function , create text object going show tshirt message. so, image , text belong group object.
- every time want load new text , call loadtext function , change text object.
- i added 4 buttons in order manupulate up/down/left/right text .
you can export canvas + image+ text function saveimg(), on jsfiddle security message tained canvases. happens because on example load image domain , code runs on domain, can run code on web application no problem @ all.
that code :
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); }); $('#loadtext').on('click',loadtext); $('#saveimg').on('click',saveimg); function loadtext(){ console.log($('#logo').val()); canvas._objects[0]._objects[1].text = $('#logo').val(); canvas.renderall(); } function saveimg(){ console.log('export image'); if (!fabric.canvas.supports('todataurl')) { alert('this browser doesn\'t provide means serialize canvas image'); } else { window.open(canvas.todataurl('png')); } } $('#left').on('click',function(){ canvas._objects[0]._objects[1].set('left',canvas._objects[0]._objects[1].left-1); canvas.renderall(); }) $('#right').on('click',function(){ canvas._objects[0]._objects[1].set('left',canvas._objects[0]._objects[1].left+1); canvas.renderall(); }) $('#top').on('click',function(){ canvas._objects[0]._objects[1].set('top',canvas._objects[0]._objects[1].top-1); canvas.renderall(); }) $('#bottom').on('click',function(){ canvas._objects[0]._objects[1].set('top',canvas._objects[0]._objects[1].top+1); canvas.renderall(); })
that jsfiddle example: http://jsfiddle.net/tornado1979/zrazuhcq/1/
hope helps, luck.
Comments
Post a Comment