1
我有一個內部透明的圖像。它被設置爲CSS的背景。我可以將圖像添加到畫布上,但希望一次只能添加一張圖像,以便在畫面的透明部分後面添加圖像。我該如何做到這一點?先謝謝你!如何使用Fabric.JS將我的電腦中的圖像添加到已經設置的圖像後面?
var id = document.getElementById('rectangle') && rectangle ||
document.getElementById('anotherCanvas') && anotherCanvas;
var canvas = new fabric.Canvas(id);
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({
width: 125,
height: 170
})
canvas.add(oImg).renderAll();
var a = canvas.setActiveObject(oImg);
var dataURL = canvas.toDataURL({
format: 'png',
quality: 1
});
});
};
reader.readAsDataURL(file);
});
canvas {
border: 1px solid #808080;
border-radius: 1px;
}
#rectangle {
background: url(http://i.imgur.com/0Mt6ho1.png);
background-size: contain;
background-repeat: no-repeat;
}
<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="rectangle" width="637" height="412"></canvas>
這地方在正確的位置,但不是框架背後 –
見更新後 – Observer
感謝您的意見和歡呼聲! –