如何使用Fabric.js将图像上传到画布?

时间:2014-02-11 23:57:46

标签: javascript jquery html5 html5-canvas fabricjs

我想要创建一个流程,用户可以上传他们的图像,然后在浏览器中使用Fabric.js在Canvas中使用一些按钮编辑它们-wich使用Fabric.js添加一些效果 - 。

我不能让它发挥作用。我的HTML是:

<form id="uploadImg" runat="server">
  <input type="file" id="uploadedImg"/>
</form>
<canvas id="canvas"></canvas>

Javascript如下:

var canvas = new fabric.Canvas('canvas');
canvas.setHeight(480);
canvas.setWidth(640);

$('#uploadedImg').change(function () {
    var imgData = $(this).files[0];
    fabric.util.loadImage(imgData, function(img) {
        var oImg = new fabric.Image(img);
        oImg.scale(0.2).set({
            left: 100,
            top: 100,
        });
    canvas.add(oImg);
    });
}); 

4 个答案:

答案 0 :(得分:20)

document.getElementById('imgLoader').onchange = function handleImage(e) {
var reader = new FileReader();
  reader.onload = function (event){
    var imgObj = new Image();
    imgObj.src = event.target.result;
    imgObj.onload = function () {
      var image = new fabric.Image(imgObj);
      image.set({
            angle: 0,
            padding: 10,
            cornersize:10,
            height:110,
            width:110,
      });
      canvas.centerObject(image);
      canvas.add(image);
      canvas.renderAll();
    }
  }
  reader.readAsDataURL(e.target.files[0]);
}

答案 1 :(得分:11)

您可以按照面料js上传多个图片,也可以将其拖放到画布中的另一个位置。

&#13;
&#13;
var canvas = new fabric.Canvas('canvas');
document.getElementById('file').addEventListener("change", function (e) {
  var file = e.target.files[0];
  var reader = new FileReader();
  reader.onload = function (f) {
    var data = f.target.result;                    
    fabric.Image.fromURL(data, function (img) {
      var oImg = img.set({left: 0, top: 0, angle: 0,width:100, height:100}).scale(0.9);
      canvas.add(oImg).renderAll();
      var a = canvas.setActiveObject(oImg);
      var dataURL = canvas.toDataURL({format: 'png', quality: 0.8});
    });
  };
  reader.readAsDataURL(file);
});
&#13;
canvas{
  border: 1px solid black;
}


Include it like this:
&#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>
<input type="file" id="file"><br />
<canvas id="canvas" width="450" height="450"></canvas>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

 var canvas = new fabric.Canvas('main-canvas');
        canvas.selection = false;
        var imgElement = document.getElementById("img11");

        var imgInstance = new fabric.Image(imgElement, {
            angle: 0,
            position: 'absolute',
            opacity: 1,
            originX: 'left',
            originY:'top',
            meetOrSlice:'slice',

        });
        imgInstance.selectable = false;
        canvas.add(imgInstance);
        canvas.renderAll();

答案 3 :(得分:0)

这是一个老问题,这里的一些现有答案似乎已被破坏。下面是一个工作示例,说明如何使用文件输入将图像添加到 Fabric.js 画布,然后将其缩放以适合画布。

此示例适用于库的当前版本。

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

document.getElementById("uploader").onchange = function(e) {
  var reader = new FileReader();
  reader.onload = function(e) {
    var image = new Image();
    image.src = e.target.result;
    image.onload = function() {
      var img = new fabric.Image(image);
      img.set({
        left: 100,
        top: 60
      });
      img.scaleToWidth(200);
      canvas.add(img).setActiveObject(img).renderAll();
    }
  }
  reader.readAsDataURL(e.target.files[0]);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/4.5.0/fabric.min.js"></script>
<input id="uploader" type="file"/>
<canvas id="canvas" width="400" height="300" style="border:1px solid black"></canvas>