javascript 使用 Fabric.js 添加图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31627639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding images with Fabric.js
提问by Devin
I'm new to JavaScript and am working with this code using Fabric.js to create multiple images with the click of a button. 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.
我是 JavaScript 新手,正在使用 Fabric.js 处理此代码,只需单击一个按钮即可创建多个图像。我从另一篇文章中找到了以下允许形状的代码,但我似乎无法弄清楚如何成功制作这些图像。
<!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>
采纳答案by Theo Itzaris
you can easily add any image that you have already uploaded on a url like this:
您可以轻松添加已上传到 url 的任何图像,如下所示:
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.
希望有帮助,祝你好运。
回答by Hong Yi
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>