使用html5

时间:2016-01-19 08:59:49

标签: javascript html5 twitter-bootstrap canvas fabricjs

我实现了将画布转换为图像,但是当我设置背景图像时,它将无法正常工作。 我使用布料js画布,但每当我设置背景图像,然后画布不转换图像。设置背景图像后,我点击我的点击!!最后的形象应该来了。  我有一个JFFiddle。 https://jsfiddle.net/varunPes/vb1weL93/

HTML:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.min.js"></script>
 <div class="col-sm-12">                    
      <div class="text-center" style="font-size:25;margin-top:17px;margin-bottom: 30px;"><strong>Customize T-Shirt From Here</strong></div>
             <input type="file" id="file">            
             <br/>
              <canvas id="canvas" width="750" height="550" style="border: 1px solid black;  box-shadow: rgba(0,0,0,0.8) 0 0 10px;"></canvas>
              <a href='' id='txt' target="_blank">Click Me!!</a><br />
              <img id="preview" />
  </div>

Java脚本:

 var canvas = new fabric.Canvas('canvas');

            // initialize fabric canvas and assign to global windows object for debug         

            canvas.setBackgroundImage('https://c1.staticflickr.com/5/4051/4510967899_293f0dd5ac_z.jpg', canvas.renderAll.bind(canvas), {
            });



            document.getElementById('file').addEventListener("change", function (e) {
                var file = e.target.files[0];
                var reader = new FileReader();
                console.log("reader   " + reader);
                reader.onload = function (f) {

                    var data = f.target.result;
                    fabric.Image.fromURL(data, function (img) {
                        var oImg = img.set({left: 70, top: 100, width: 250, height: 200, angle: 0}).scale(0.9);
                        canvas.add(oImg).renderAll();
                        canvas.setActiveObject(oImg);
                    });
                };
                reader.readAsDataURL(file);
            });

            document.querySelector('#txt').onclick = function (e) {
                e.preventDefault();
                canvas.deactivateAll().renderAll();                
                document.querySelector('#preview').src = canvas.toDataURL();
            };

1 个答案:

答案 0 :(得分:3)

帕特里克埃文斯告诉你,你有一个cross-origin issue

幸运的是,您在问题中指出的图片是通过发送正确标头以通过跨域使用的域提供的。

要实现此目的,您必须设置crossOrigin方法的setBackgroundImage()参数:

&#13;
&#13;
var canvas = new fabric.Canvas('canvas');

canvas.setBackgroundImage('https://c1.staticflickr.com/5/4051/4510967899_293f0dd5ac_z.jpg', 
    canvas.renderAll.bind(canvas),
    // here set the crossOrigin attribute
    {crossOrigin: 'anonymous'}
    );



document.getElementById('file').addEventListener("change", function(e) {
  var file = e.target.files[0];
  var reader = new FileReader();
  console.log("reader   " + reader);
  reader.onload = function(f) {

    var data = f.target.result;
    fabric.Image.fromURL(data, function(img) {
      var oImg = img.set({
        left: 70,
        top: 100,
        width: 250,
        height: 200,
        angle: 0
      }).scale(0.9);
      canvas.add(oImg).renderAll();
      canvas.setActiveObject(oImg);
    });
  };
  reader.readAsDataURL(file);
});

document.querySelector('#txt').onclick = function(e) {
  e.preventDefault();
  canvas.deactivateAll().renderAll();
  document.querySelector('#preview').src = canvas.toDataURL();
};
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.min.js"></script>
<div class="col-sm-12">
  <div class="text-center" style="font-size:25;margin-top:17px;margin-bottom: 30px;"><strong>Customize T-Shirt From Here</strong>
  </div>
  <input type="file" id="file">
  <br/>
  <canvas id="canvas" width="750" height="550" style="border: 1px solid black;  box-shadow: rgba(0,0,0,0.8) 0 0 10px;"></canvas>
  <a href='' id='txt' target="_blank">Click Me!!</a>
  <br />
  <img id="preview" />
</div>
&#13;
&#13;
&#13;

请注意,它在没有背景图像的情况下工作,因为从FileAPI提供的图像不受跨源限制。