我的HTML5画布中有一个图像背景.
var canvas = new fabric.Canvas('#canvas1'); canvas.setBackgroundImage( 'http://fabricjs.com/assets/jail_cell_bars.png', canvas.renderAll.bind(canvas) );
如何将画布的尺寸(宽度,高度)设置为背景图像的尺寸?
谢谢.
我认为上面的答案不使用fabric.js,它使用普通画布.
当您使用fabric.js及其Fabric.Canvas类时,代码应为:
image.onLoad = function() { var fabric_canvas = new fabric.Canvas('canvas1'); fabric_canvas.setDimensions({width:image.width, height:image.height}); };
就我而言,
var canvas = new fabric.Canvas("test_fabric"); canvas.setDimensions({width:800, height:200});
结果是:
canvas.width和canvas.style.width都已设置.