最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How save image with canvas in fabric.js - Stack Overflow

programmeradmin1浏览0评论

I am creating an application for T-shirt customization in which I have put the canvas on image using CSS, but the problem is saving that image as canvas. toDataURL just gives part of the canvas area, but I want the whole image. There are other solutions on Stack Overflow but they do not solve this problem.

I am creating an application for T-shirt customization in which I have put the canvas on image using CSS, but the problem is saving that image as canvas. toDataURL just gives part of the canvas area, but I want the whole image. There are other solutions on Stack Overflow but they do not solve this problem.

Share Improve this question edited Aug 17, 2016 at 15:03 Don't Panic 41.8k11 gold badges68 silver badges85 bronze badges asked Jul 27, 2015 at 12:03 Bhupendra JadejaBhupendra Jadeja 2981 gold badge6 silver badges17 bronze badges 2
  • 1 draw the whole on an a canvas? or first draw the background on a new canvas then the canvas on this new one. – Kaiido Commented Jul 27, 2015 at 14:43
  • it is image on which i have place the canvas – Bhupendra Jadeja Commented Jul 28, 2015 at 4:57
Add a ment  | 

1 Answer 1

Reset to default 9

Hello, you have to create an image object (tshirt) with a text object that holds the message.

  1. to do that , load the image with fabric.Image.fromURL() function and inside the function , also create a text object that is going to show the tshirt message. so, your image and text belong to a group object.
  2. every time you want to load new text , you call the loadText function and you change the text object.
  3. i also added 4 buttons in order to manupulate up/down/left/right the text .
  4. you can export the canvas + image+ text into the function saveImg(), but on the jsfiddle you will get a security message for Tained canvases. that happens because on the example i load the image from another domain and the code runs on another domain, you can run that code on your web application with no problem at all.

  5. that is the 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 to serialize canvas to an 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();
    })
    
  6. that is the jsfiddle example: http://jsfiddle/tornado1979/zrazuhcq/1/

hope helps, good luck.

发布评论

评论列表(0)

  1. 暂无评论