简体   繁体   English

fabric.js缩放整个图像的最简单方法

[英]fabric.js the easiest way to scale whole image

i use fabric.js on the client side to allow user to create the image in the browser, then fabric.js serializes the image to json and send to the node server. 我在客户端使用fabric.js允许用户在浏览器中创建图像,然后fabric.js将图像序列化为json并发送到节点服务器。 then the backend deserializes JSON string to an object and save the image locally as a file. 然后后端将JSON字符串反序列化为对象,然后将图像本地保存为文件。

The question is: 问题是:

The result image has to be large, for example 3000x3000px, now i use <canvas width="600" height="600"></canvas> User uploads the background image(3000x3000) into the canvas, then i scale it this way: 结果图像必须很大,例如3000x3000px,现在我使用<canvas width="600" height="600"></canvas>用户将背景图像(3000x3000)上传到画布中,然后按这种方式缩放:

fabric.Image.fromURL(data, function (img) {
            img.scale(0.2);             
            canvas.add(img).renderAll();            
        });

and it fills whole the canvas. 它充满了整个画布。

Then user adds text onto the canvas, for example offset x,y: 100,200 and clicks the submit button. 然后,用户将文本添加到画布上,例如偏移量x,y:100,200,然后单击“提交”按钮。 The server has to unscale the 600x600 image into 3Kx3K, also unscale the text and recalculate the offset. 服务器必须将600x600图像缩放为3Kx3K,还需要缩放文本并重新计算偏移量。 Is there the easiest way to manipulate with scaling ? 是否有最简单的缩放方法?

Try to keep the image at scale 1. Detect how much the image should be scaled, and scale the whole canvas instead: 尝试使图像保持缩放比例1.检测应缩放图像的数量,然后缩放整个画布:

canvas.setZoom(0.2)

When you are in the node environment, as soon as you loaded the image back, set the canvas dimensions to the image dimensions. 在节点环境中时,将图像重新加载回原位后,请将画布尺寸设置为图像尺寸。

Zoom is not exported to json so you do not have to change anyhing. 缩放不会导出到json,因此您无需更改任何内容。

You should get a canvas as big as the image and all the proportions saved. 您应该获得与图像一样大的画布,并保存所有比例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM