简体   繁体   English

在 HTML 画布上显示许多高分辨率图像(地图平铺)

[英]Displaying many high-resolution images on an HTML canvas (map tiling)

I'm using three.js to display a globe.我正在使用three.js 来显示一个地球仪。 At first, the image is low-quality, and as a user zooms in, the images become higher quality.起初,图像是低质量的,随着用户放大,图像变得更高质量。 This is done using tiling .这是使用平铺完成的。 Each tile is 256px x 256px.每个图块为 256px x 256px。 For the lowest zoom, there are only a couple tiles, and for the largest, there are thousands.对于最低的缩放,只有几个图块,对于最大的,有数千个图块。

The issue is that the images are still low quality, even at the highest zoom.问题是图像质量仍然很低,即使在最高变焦下。 I think this is because of the canvas I'm using.我认为这是因为我使用的画布。 It's 2000px x 1000px.它是 2000 像素 x 1000 像素。 Even if I increase this canvas, the image at its highest quality is 92160px x 46080px, which is too large of a canvas to render in most browsers.即使我增加了这个画布,最高质量的图像也是 92160 像素 x 46080 像素,这对于大多数浏览器来说都太大了。

What approach can I use to display tiles at high quality, but not have a huge canvas?我可以使用什么方法以高质量显示图块,但没有巨大的画布? Is using a canvas the right approach?使用画布是正确的方法吗? Thanks!谢谢!

I figured out a way of doing it, though I'm not sure it's a great approach.我想出了一种方法,虽然我不确定这是一个很好的方法。 For every tile that needs to be displayed, I create a sphere.对于需要显示的每个图块,我创建了一个球体。 I make the sphere match the size of the tile on the map using phiStart , phiLength , thetaStart , thetaLength .我使用phiStartphiLengththetaStartthetaLength使球体与地图上图块的大小相匹配。 Then I create a 256px x 256px canvas, put the tile image on the canvas, and put the canvas on the tile.然后我创建一个 256px x 256px 的画布,将平铺图像放在画布上,然后将画布放在平铺上。 Essentially I'm placing parts of spheres on top of a low-resolution sphere, which is the sphere that loads when the page is loaded.本质上,我将球体的一部分放置在低分辨率球体的顶部,该球体是页面加载时加载的球体。

I calculated the four variables by taking a whole sphere (which is 2*Pi for each variable), then multiplying by the percentage of the sphere I want to fill.我通过取一个完整的球体(每个变量为2*Pi ),然后乘以我想要填充的球体的百分比来计算四个变量。 Here is the four variables:这是四个变量:

// `sphereCanvasWidth` is the width of the low-resolution sphere's canvas
// `canvasX` is the top left pixel of where you want to draw on the canvas
phiStart = (2 * Pi) * (canvasX / sphereCanvasWidth)
// `numPixelsTileX` is the number of pixels the canvas should cover
phiLength = (2 * Pi) * (numPixelsTileX / sphereCanvasWidth)
// `canvasY` is the bottom left pixel of where you want to draw on the canvas
thetaStart = (2 * Pi) * (canvasY / sphereCanvasWidth)
// `numPixelsTileY` is the number of pixels the canvas should cover
phiLength = (2 * Pi) * (numPixelsTileY / sphereCanvasWidth)

For example, if I want to fill the top left 1/4 of the sphere, it'd be:例如,如果我想填充球体左上角的 1/4,它会是:

phiStart = 0
phiLength = (2 * Pi) * .25
thetaStart = 0
thetaLength = (2 * Pi) * .25

The three.js documentation has a sphere you can test the variables on . Three.js 文档有一个领域,您可以在 上测试变量 Here's what the example above generates:这是上面的示例生成的内容:

在此处输入图片说明

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

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