简体   繁体   English

如何在html5,canvas,javascript中将图像缩放到更大?

[英]How to scale an image to larger in html5, canvas, javascript?

I know this might sound newbie but there are many answers out there that just doesn't do it. 我知道这听起来像是新手,但是有很多答案是不可行的。

I have a .png image with size 10,000 pixel in height and want to scale it to 30,000 pixel in height. 我有一个.png图像,高度为10,000像素,并希望将其缩放到30,000像素。 It can just be a simple stretch to the height. 可以简单地拉伸到高处。

  img = new Image(800, 30000);
  img.src = path;

But when it comes out, it doesn't scale the image at all. 但是,一旦显示出来,它根本不会缩放图像。 It seems to be expanding an image at anything greater than 10,000 pixel as invisible pixel. 它似乎在将大于10,000像素的图像扩展为不可见像素。

Does another know how to do it properly? 另一个人知道如何正确地做吗?

Any advice will be grateful. 任何建议将不胜感激。 Thanks. 谢谢。

You will have problems doing this. 您在执行此操作时会遇到问题。 The only reason you would want to scale it to that size is if you want to print it. 您想要将其缩放到该尺寸的唯一原因是要打印它。 Of you need to print it then you will need another solution. 您需要打印它,然后您将需要另一种解决方案。

Reality The fact is that you can only ever see as much as the display can present, so creating an image this large is a waste of resources and a battle against the limits of the browsers. 现实情况是,您只能看到尽可能多的显示内容,因此创建如此大的图像既浪费资源,又与浏览器的极限作斗争。

Solution. 解。 Load the image as usual but do not add it to the DOM. 照常加载图像,但不要将其添加到DOM。 Add a div element with the height set to 30000px, this will supply a scroll bar. 添加高度设置为30000px的div元素,这将提供滚动条。 Then create a canvas with height and width set to window.innerWidth and window.innerHeight and add the CSS style { position:fixed; top:0px; right:0px; } 然后创建一个高度和宽度分别设置为window.innerWidthwindow.innerHeight的画布,并添加CSS样式{ position:fixed; top:0px; right:0px; } { position:fixed; top:0px; right:0px; } { position:fixed; top:0px; right:0px; } to that canvas this will fix the position of the canvas but still give a scroll bar via the background div. { position:fixed; top:0px; right:0px; }可以固定画布的位置,但仍通过背景div给出滚动条。

Use requestAnimationFrame to call your function that checks for updated scroll position and if changed render the appropriate part of the image on the canvas. 使用requestAnimationFrame调用您的函数,该函数检查更新的滚动位置,如果更改,则在画布上渲染图像的适当部分。

Using this method will allow it to appear that the image is very large, yet you only ever display what can be seen. 使用此方法将使其看起来非常大,但是您只能显示可见的图像。 If you chose you can implement your own scroll and with a little fancy math you can make the image as large as the universe. 如果选择,则可以实现自己的滚动,并通过一些简单的数学运算就可以使图像与Universe一样大。 Think of google maps at max zoom what is the size of that? 想想最大缩放的Google地图的大小是多少? it does not matter because you can only ever see part of it so they only ever render the part you can see. 没关系,因为您只能看到其中的一部分,因此它们只能渲染您可以看到的部分。

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

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