整体改变画布尺寸的实现
<pre><code class="language-javascript">function GetCanvasAtResoution(newWidth){
if(newWidth<100 || newWidth>1200) return;
if(canvas.width != newWidth){
var scaleMultiplier = newWidth / canvas.width;
var objects = canvas.getObjects();
for(var i in objects){
objects[i].scaleX = objects[i].scaleX * scaleMultiplier;
objects[i].scaleY = objects[i].scaleY * scaleMultiplier;
objects[i].left = objects[i].left * scaleMultiplier;
objects[i].top = objects[i].top * scaleMultiplier;
objects[i].setCoords();
}
canvas.setWidth(canvas.getWidth()* scaleMultiplier);
canvas.setHeight(canvas.getHeight()* scaleMultiplier);
canvas.renderAll();
canvas.calcOffset();
}
canvas.includeDefaultValues = false
let json = canvas.toJSON()
console.table(JSON.stringify(json))
return canvas.toDataURL();
}</code></pre>