ami*_*pta 16 javascript css svg canvas fabricjs
我想增加我的svg图像的高度和宽度,与画布的高度和宽度相同,使它看起来像画布的背景图像.当我按下"设置背景"按钮时,一个svg图像将从我的目录设置为画布.我想动态地将此图像缩放到画布高度和宽度.
预期产出:我想要这个
<h1>canvas</h1>
<canvas style="left: -300px; border: 1px dotted;" height="385" width="400" id="c"></canvas>
<input type="button" id="svg3" value="set background" />
Run Code Online (Sandbox Code Playgroud)
$(document).ready(function(){
var canvas = new fabric.Canvas('c');
var colorSet="red";
$("#svg3").click(function(){
fabric.loadSVGFromURL('http://upload.wikimedia.org/wikipedia/en/c/cd/-Islandshreyfingin.svg', function (objects, options) {
var shape = fabric.util.groupSVGElements(objects, options);
shape.set({
left: 150,
top:200,
//height: 700,
//width: 700,
scaleX: .35,
scaleY:.35
});
if (shape.isSameColor && shape.isSameColor() || !shape.paths) {
shape.setFill(colorSet);
} else if (shape.paths) {
for (var i = 0; i < shape.paths.length; i++) {
shape.paths[i].setFill(colorSet);
}
}
canvas.add(shape);
canvas.renderAll();
});
});
});
Run Code Online (Sandbox Code Playgroud)
这是我的FIDDLE演示.
有人知道怎么做吗?
Ser*_*hiv 21
你知道你的画布宽度和高度.所以这将有效:
shape.set({
top: canvas.height/2,
left: canvas.width/2,
scaleY: canvas.height / shape.height,
scaleX: canvas.width / shape.width
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
25754 次 |
最近记录: |