简体   繁体   English

当在许多矩形上使用背景时,Raphael JS变得非常慢

[英]Raphael JS becomes really slow when using background on a lot of rectangles

I have a big set of small rectangles (4K-5K) and I want to use the sprite technique to give them the background, so I have only one image (otherwise the number of requests would kill the server). 我有一大套小矩形(4K-5K),并且我想使用Sprite技术为它们提供背景,所以我只有一个图像(否则请求的数量会杀死服务器)。

If I give them a coloured background, the map takes only a few seconds to build. 如果我给他们提供彩色背景,则只需几秒钟即可构建地图。

I tried to create a rect, apply a translation to it to get a different part of the background image assigned and that works. 我试图创建一个rect,对其进行翻译,以获取分配的背景图像的不同部分,并且可以正常工作。 But when I add even only 10 of them it is incredibly slow. 但是,即使我只添加10个,它的速度也非常慢。

Am I doing something wrong? 难道我做错了什么?

I had a similar problem, trying to generate a hexagonal map and then fill the hexagons with sprites like forest, water, grassland. 我有一个类似的问题,试图生成一个六边形图,然后用森林,水,草地之类的精灵填充六边形。 When I fill them with a solid color it takes just two seconds to render 10K elements. 当我用纯色填充它们时,仅需两秒钟即可渲染10K元素。

If I try to use fill: url('image.png'); 如果我尝试使用fill: url('image.png'); , it takes longer to draw just 180 hexagons and crashes the browser on 10K. ,仅绘制180个六边形就会花费更长的时间,并使浏览器崩溃10K。

As far as I can tell, the problem is that Raphael JS creates a separate texture definition for each element and then applies it as the fill. 据我所知,问题是Raphael JS为每个元素创建了一个单独的纹理定义,然后将其用作填充。 This means that for 10K elements you have 10K texture definitions that point to the same image. 这意味着对于10K元素,您具有指向同一图像的10K纹理定义。 This is bad. 这不好。

Update: 更新:

I ended up ditching SVG for this kind of usage. 我最终放弃了这种用法的SVG。 It's way faster to just create HTML elements and set the backgrounds with CSS. 仅创建HTML元素并使用CSS设置背景的方法会更快。 You can combine them with SVG as a layer on top for example. 例如,您可以将它们与SVG结合在一起作为顶层。 However you are working with quite a lot of elements. 但是,您正在处理很多元素。

I solved my problem by dynamically loading only the elements I need, because all 10K don't need to be in the viewport at the same time. 我通过仅动态加载所需元素来解决了我的问题,因为所有10K都不需要同时位于视口中。

In your case that is not really an option. 在您的情况下,这不是真正的选择。 A good alternative would be using Canvas to draw the elements. 一个很好的选择是使用“画布”绘制元素。 From my experiments it will be faster than SVG at drawing this many although if you are using some animations, they will be harder to implement than with the RaphaelJS library. 根据我的实验,绘制这么多动画的速度将比SVG快,尽管如果您使用某些动画,则与RaphaelJS库相比,实现起来会更加困难。

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

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