多边形和路径哪个更轻?
我正在渲染由 600 多个在笛卡尔平面上对齐的 SVG 元素组成的地图。我需要它们是单独的元素,因为我希望它们单独响应鼠标事件等。
我的问题是:为了应用大量转换,例如“翻译”(更改其位置),哪个选项是“更轻” “到浏览器?
渲染像这样的六边形的多边形:
<polygon points="43.301270189221924,55 43.301270189221924,65 51.96152422706631,70 60.6217782649107,65 60.6217782649107,55 51.96152422706631,50"></polygon>
...或者将它们创建为像这样的路径:
<path d="M43.301270189221924,55L43.301270189221924,65L51.96152422706631,70L60.6217782649107,65L60.6217782649107,55L51.96152422706631,50Z"></path>
I am rendering a map composed by 600+ SVG elements aligned in a cartesian plane. I need them to be separate elements because I want them to individually respond to mouse events, etc.
My question is: for the purpose of applying a lot of transformations like "translate" (changing their positions) for example, which option is "lighter" to browsers?
Rendering polygons like this hexagon:
<polygon points="43.301270189221924,55 43.301270189221924,65 51.96152422706631,70 60.6217782649107,65 60.6217782649107,55 51.96152422706631,50"></polygon>
... or creating them as paths like this one:
<path d="M43.301270189221924,55L43.301270189221924,65L51.96152422706631,70L60.6217782649107,65L60.6217782649107,55L51.96152422706631,50Z"></path>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我怀疑会有很大的差异,但如果有的话,我希望多边形会稍微快一点,因为它是专门用于多边形的。
事实上,在运行两个分析脚本(见下文)后,我的上述评估似乎是正确的。在所有浏览器中,多边形都比路径快一点,但差异并不显着。所以我怀疑你真的需要担心这个。幸运的是,无论如何,
polygon
听起来都是合乎逻辑的选择。分析
path
:对于
polygon
也是如此:I doubt there's going to be much difference, but if there is any, I'd expect
polygon
to be marginally faster, since it's specifically meant for, you know, polygons.In fact, after running two profiling scripts (see below), my above assessment appears correct. Polygons are a little bit faster than paths in all browsers, but the difference is hardly significant. So I doubt you really need to worry about this. Luckily,
polygon
sounds like the logical choice anyway.Profiling
path
:And the same thing for
polygon
:最终将我的评论更改为答案...
我不太了解 SVG 细节,但是...我认为转换本身将花费相同的时间,因为它只是改变存储在内存中的点的值。即使不是,额外的“重量”可能会被渲染是需要最多资源这一事实所抵消。请参阅http://en.wikipedia.org/wiki/File:10-simplex_t03。 svg 是一个包含大量 SVG 元素的示例。
无论如何,如果没有显着的性能差异,那么我也同意使用多边形标签,但不仅仅是因为语义。它可以防止您意外地使六边形弯曲,并且语法更简单。
Ended up changing my comment to an answer...
I don't know very much about SVG detail-wise, but... I would assume the transformation itself would take the same amount of time, as it'd just be changing the values of the points stored in memory. Even if not, the additional "heaviness" would likely be vastly outweighed by the fact that rendering is what requires the most resources out of anything else. See http://en.wikipedia.org/wiki/File:10-simplex_t03.svg for an example with lots and lots of SVG elements.
Anyway, if it is the case that there's no significant performance difference, then I too would agree on going for the polygon tag, but not just because of semantics. It would prevent you from accidentally making the hexagon curvy, and the syntax is simpler.