简体   繁体   English

使用Fabric.js添加图像

[英]Adding images with Fabric.js

I'm new to JavaScript and am working with this code using Fabric.js to create multiple images with the click of a button. 我是JavaScript的新手,正在使用Fabric.js处理这段代码,只需点击一下按钮即可创建多个图像。 I've found the following code from another post which allows for shapes, but I can't seem to figure out how to successfully make those images instead. 我从另一个允许形状的帖子中找到了以下代码,但我似乎无法弄清楚如何成功制作这些图像。

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="fabric.min.js"></script>
<script type="text/javascript">
jQuery(function($){
var canvas = new fabric.Canvas('c');

$("#addCircle").click(function(){
       canvas.add(new fabric.Circle({
    radius: 20, fill: 'green', left: 100, top: 100
 }));
  });

});

</script>
</head>
<body>
<div id="wrapper">
<div id="editor">
<input type="button" id="addCircle" value="Add Circle"></div>
<canvas id="c" width="300" height="300"></canvas>
</div>

</body>
</html>

you can easily add any image that you have already uploaded on a url like this: 您可以轻松添加您已经上传到网址上的任何图片,如下所示:

this is the js snippet: 这是js片段:

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

canvas.backgroundColor = 'yellow';
canvas.renderAll();
var myImg = 'http://www.logowik.com/uploads/images/511_android.jpg';


$('#addImage').on('click',addImg);

function addImg(){
    fabric.Image.fromURL(myImg, function(oImg) {
        var l = Math.random() * (500 - 0) + 0;
        var t = Math.random() * (500 - 0) + 0;                
            oImg.scale(0.2);
        oImg.set({'left':l});
                  oImg.set({'top':t});
            canvas.add(oImg);
        });
}

here you can find a live example on jsfiddle: http://jsfiddle.net/tornado1979/1awwv3eh/1/ press the button and import images on random positions. 在这里你可以找到关于jsfiddle的实例: http//jsfiddle.net/tornado1979/1awwv3eh/1/按下按钮并在随机位置导入图像。

hope helps, good luck. 希望有所帮助,祝你好运。

This is how you can use HTML5 file API to read image from your local computer and render in the fabric.js canvas: 这是您可以使用HTML5文件API从本地计算机读取图像并在fabric.js画布中呈现的方法:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-2.1.4.js"></script>
<script src="Scripts/fabric.min.js"></script>
<script type="text/javascript"> 

    var myAppModule = (function () {
        var outObj = {};

        var file, fileReader, img;
        var cCanvas, cImg;

        var init = function (newFile, newFileReader) {
            file = newFile;
            fileReader = newFileReader;
        };

        var onloadImage = function () {
            cCanvas = new fabric.Canvas('myCanvas');
            cCanvas.setWidth(img.width);
            cCanvas.setHeight(img.height);

            cImg = new fabric.Image(img, {
                left: 0,
                top: 0,
                angle: 0
            });

            cCanvas.add(cImg);
        };

        var onloadFile = function (e) {
            img = new Image();
            img.onload = onloadImage;
            img.src = fileReader.result;
        };

        outObj.init = init;
        outObj.OnloadFile = onloadFile;

        return outObj;
    })();

    function handleFileSelect(evt) {
        var files = evt.target.files;
        var output = [];
        for (var i = 0, f; f = files[i]; i++) {

            if (!f.type.match('image.*')) {
                continue;
            }

            var reader = new FileReader();

            myAppModule.init(f, reader);

            reader.onload = myAppModule.OnloadFile;

            reader.readAsDataURL(f);

        }
    }

    $(document).ready(function () {
        document.getElementById('selectFile').addEventListener('change', handleFileSelect, false);

    });
</script>
</head>
<body>
<canvas id="myCanvas">

</canvas>

<form id="form1">

    <input type="file" id="selectFile" name="selectFile"/>

</form>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM