简体   繁体   English

JS canvas 大数据缩放和平移(100k-1M 对象)

[英]JS canvas zoom and pan with large data (100k-1M objects)

I want to implement fast and high-res zooming and panning on my Canvas. As you may already read in the title, I have a lot of data, so using CanvasRenderingContext2D.scale() and CanvasRenderingContext2D.translate() with redrawing isn't an option, because it is way to laggy.我想在我的 Canvas 上实现快速和高分辨率的缩放和平移。正如您可能已经在标题中读到的那样,我有很多数据,所以使用CanvasRenderingContext2D.scale()CanvasRenderingContext2D.translate()重绘不是一个选项,因为它很慢。 I already tried using Images, but I want to see all the data-points while zooming and panning.我已经尝试过使用图像,但我想在缩放和平移时查看所有数据点。 Drawing only the data-points that are visible is a good start, but this makes the zooming and panning only possible on a very close zoom, but I actually want to be able to zoom out until I can see all the data points.只绘制可见的数据点是一个好的开始,但这使得缩放和平移只能在非常接近的缩放下进行,但我实际上希望能够缩小直到我可以看到所有数据点。 The data points are drawn with circles and are very close together if that helps you in any way.数据点用圆圈绘制,如果这对您有任何帮助,它们会非常靠近。 I appreciate every suggestion on how to handle this.我感谢有关如何处理此问题的每条建议。

I think You should try one of these:我认为您应该尝试以下方法之一:

  1. WebGL (it is something line OpenGL for web browesers) WebGL(对于 web 浏览器,它是 OpenGL 行)
    materials: khronos group web page repo: GitHub材料: khronos 组 web 页面回购: GitHub
  2. three.js materials: three.js web page repo: GitHub three.js 材料: three.js web 页面回购: GitHub

I sugested them because they are able to work with 2D also (not only with 3D) and they have tools to work with big textures.我向他们求助是因为他们也能够处理 2D(不仅是 3D)并且他们有处理大纹理的工具。 You can create static camera which will just change the distance to the surface (with texture) - zoom.您可以创建 static 相机,它只会改变到表面的距离(带纹理)- 缩放。

With Three.js there is a way to load bitmaps progressively with mipmap chains or with "Prefiltered, Mipmapped Radiance Environment Map: PMREMGenerator .使用 Three.js 可以使用 mipmap 链或“预过滤的 Mipmapped 辐射环境 Map: PMREMGenerator ”逐步加载位图。

An alternative approach that worked for me was having a (hidden) canvas for drawing and a viewport canvas the user could see.另一种对我有用的方法是有一个(隐藏的)canvas 用于绘图和一个用户可以看到的视口 canvas。 Drawing on the hidden canvas allows you to draw a bigger image, and then simply update the viewport by redrawing the canvas to view only the middle part (you can use viewportContext.drawImage(sourceCanvas, -canvasWidth / 2, -canvasHeight / 2) to draw from one canvas to another).在隐藏的 canvas 上绘制允许您绘制更大的图像,然后通过重绘 canvas 来更新视口以仅查看中间部分(您可以使用viewportContext.drawImage(sourceCanvas, -canvasWidth / 2, -canvasHeight / 2)来从一个 canvas 绘制到另一个)。

To implement zooming and panning, I would simply store the image from the source canvas at the beginning (on mouseDown or scroll) and redraw it according to new mouse position/scroll (simply translating it or changing its dimensions will work) to the source canvas.要实现缩放和平移,我会简单地将源 canvas 中的图像存储在开头(在 mouseDown 或滚动上)并根据新的鼠标位置/滚动(只需平移它或更改其尺寸即可)将其重新绘制到源 canvas .

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

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