简体   繁体   English

如何从较小的画布上获得高分辨率图像?

[英]How can i get high resolution image from smaller canvas?

I am using one canvas in my web app and it's actual height and width are 500px. 我在网络应用程序中使用了一个画布,它的实际高度和宽度均为500px。 I am showing this canvas on screen as 500px square but i want image exported from this canvas as 1600px square. 我在屏幕上以500像素见方显示此画布,但我希望从此画布中以1600像素见方导出图像。 I have tried below code with no luck. 我尝试下面的代码没有运气。

canvas.width = 1600;
canvas.style.width = 500;

Any help will be appreciated. 任何帮助将不胜感激。

HTML HTML

<canvas width="1600px" height="1600px" > </canvas>

CSS CSS

canvas{
   position :absolute;
   transform:scale(0.3125);
   left:-500px; //adjust
   top:-350px; //adjust
}

Use transform:scale() to adjust size of your canvas 使用transform:scale()调整画布的大小

Now 1600 * 1600 will be the actual size of your canvas, so you can directly export images from your canvas 现在1600 * 1600将是画布的实际大小,因此您可以直接从画布中导出图像

But in view it show as 500px * 500px beacuse it's scaled down, it dose not affect the image quality while exporting 但是在视图中它显示为500px * 500px,因为它被缩小了,在导出时不会影响图像质量

Honest answer: you can't. 诚实的回答:你不能。

If you did, then you'd have found a way to losslessly compress data with less than 1/9th of the original size, and without any encoding, which is unarguably impossible. 如果这样做的话,您将找到一种无损压缩数据的方法,该方法可以压缩原始大小的不到1/9的数据,并且不进行任何编码,这无疑是不可能的。

What you can do is scale it up in a way that it at least doesn't get blurry. 您可以做的是按至少不会模糊的方式放大它。 To do that, you need the final image to be an integer multiple of the previous canvas, so the browser won't apply anti-aliasing. 为此,您需要最终图像是前一个画布的整数倍,因此浏览器将不会应用抗锯齿。 Or if you want to use your own copying formula with putImageData that would get rid of anti-aliasing, you'll still get various incongruences and it would be very slow 或者,如果您想将自己的复制公式与putImageData一起使用,这将消除抗锯齿,那么您仍然会遇到各种不一致的情况,而且速度会很慢

In your case, the closest you could get is 1500x1500 ( 3*500x3*500 ). 在您的情况下,您可以获得的最接近值是1500x1500(3 * 500x3 * 500)。 If your point was to process an image, you're not in luck, but if you just want to display something good enough, you can resort to various other tricks such as centering the canvas and using properties like box-shadow to make it clear that it's separate from the rest of the screen 如果您要处理图像,那么就不走运了,但是,如果您只想显示足够好的图像,则可以采用其他各种技巧,例如将画布居中以及使用诸如box-shadow类的属性来使其清晰它与屏幕的其余部分是分开的

You can have the canvas display at 500px while still having a resolution of 1600px. 您可以将画布显示为500px,同时仍具有1600px的分辨率。 Display size and resolution are independent. 显示尺寸和分辨率是独立的。 For resolution you set the canvas width and height properties. 为了获得分辨率,您可以设置画布的width和height属性。 For display size you set the canvas style width and height properties. 对于显示尺寸,您可以设置画布样式的 width和height属性。

// create a canvas or get it from the page
var canvas = document.createElement("canvas");
// set the resolution (number of pixels)
canvas.width = canvas.height = 1600;
// set the display size
canvas.style.width = canvas.style.height = "500px";
// get the rendering context
var ctx = canvas.getContext("2d");

To get the rendering to match the display size you need to scale up all rendering. 为了使渲染与显示尺寸匹配,您需要按比例放大所有渲染。 You can do this by setting the transform scale to the canvas resolution divided by the display size 您可以通过将变换比例设置为画布分辨率除以显示尺寸来完成此操作

var scale = 1600 / 500; // get the scale that matches display size 
ctx.setTransform(scale,0,0,scale,0,0);

Now when you render to the canvas you use the screen size coordinates. 现在,当您渲染到画布上时,您将使用屏幕尺寸坐标。

ctx.fillRect(0,0,500,500); // fill all of the canvas.
ctx.fillStyle = "red";  // draw a red circle 100 display pixels in size.
ctx.beginPath();
ctx.arc(250,250,100,0,Math.PI * 2);
ctx.fill();

When you then save the canvas, what ever method you use as long as it is not screen capture the saved canvas will be 1600 by 1600 and all the rendering will be correctly positions and proportional 然后,当您保存画布时,只要不进行屏幕捕获,所用的任何方法都将是1600 x 1600,并且所有渲染都将正确定位并成比例

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

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