简体   繁体   English

缩放画布后如何在画布中保存原始尺寸的图像

[英]How to save an image in its original size in a canvas after scaled the canvas

I have a canvas used to photo editing. 我有一个用于照片编辑的画布。 I want to save the photos in Full HD. 我想以全高清格式保存照片。 Canvas size is well below this resolution for viewing on small screen. 画布大小远低于此分辨率,可以在小屏幕上查看。 So I apply a scaling on canvas to see the whole picture. 因此,我在画布上应用了缩放比例以查看整个图片。

How can I save after editing, the image visible in the canvas in Full HD? 编辑后如何保存全高清画布中可见的图像?

Example: Edit an image size QHD (2560x1440) in a canvas size SVGA (800x600) and produce a picture Full HD (1920x1080) 示例:在画布尺寸SVGA(800x600)中编辑图像尺寸QHD(2560x1440)并生成全高清(1920x1080)图片

The size you set with the width and height properties will be the image's final size. 使用width和height属性设置的大小将是图像的最终大小。

canvas.width = 1920;              // actual size given with integer values
canvas.height = 1080;

If you need to scale down while on screen you need to use CSS (one of those special cases): 如果需要在屏幕上缩小比例,则需要使用CSS(特殊情况之一):

canvas.style.width = '960px';     // show at 50% on screen
canvas.style.height = '540px';

Alternatively have your actual image as an off-screen canvas and copy regions from that to the on-screen canvas depending on scale etc. 或者,将您的实际图像作为屏幕外画布,并根据比例等将其区域复制到屏幕上画布。

If you edit your image at a lower than the actual resolution then the point of HD etc. is gone as you need to scale up a lower resolution image to a bigger one which will make it appear more blurry due to the introduced interpolation. 如果您以低于实际分辨率的速度编辑图像,那么HD等的点就消失了,因为您需要将较低分辨率的图像放大到较大的图像,由于引入了插值,这会使图像显得更加模糊。 Always work at the actual target resolution when working with digital images (or video). 使用数字图像(或视频)时,请始终以实际目标分辨率工作。

Then use toDataURL() to get the image: 然后使用toDataURL()获取图像:

var dataUri = canvas.toDataURL(); // saves a PNG at 1920x1080

for JPEG: 对于JPEG:

var dataUri = canvas.toDataURL('image/jpeg', 0.8);  // last = quality

Just be aware of that your mouse positions will have to be scaled the opposite way as they are relative to the canvas element and not its bitmap. 请注意,您的鼠标位置将必须以相反的方式缩放,因为它们相对于canvas元素而不是其位图。

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

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