多边形和路径哪个更轻?

发布于 2024-11-27 19:05:33 字数 554 浏览 0 评论 0原文

我正在渲染由 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿哥们儿 2024-12-04 19:05:33

我怀疑会有很大的差异,但如果有的话,我希望多边形会稍微快一点,因为它是专门用于多边形的。

事实上,在运行两个分析脚本(见下文)后,我的上述评估似乎是正确的。在所有浏览器中,多边形都比路径快一点,但差异并不显着。所以我怀疑你真的需要担心这个。幸运的是,无论如何,polygon 听起来都是合乎逻辑的选择。

分析path

<svg xmlns="http://www.w3.org/2000/svg">
    <g id="paths"></g>

    <text x="20" y="20" id="out"></text>

    <script><![CDATA[
    var d = "M43.301270189221924,55 L43.301270189221924,65 L51.96152422706631,70 L60.6217782649107,65 L60.6217782649107,55 L51.96152422706631,50 Z";

    var paths = document.getElementById('paths');
    var path = document.createElementNS("http://www.w3.org/2000/svg", 'path');
    path.setAttribute('d', d);

    var start = new Date();
    for (var i = 0; i < 20000; i++)
    {
        var el = path.cloneNode(true);
        el.setAttribute('transform', 'translate(' + ((i * 20) % 1000) + ',' + ((i / 50) * 20) + ')');
        paths.appendChild(el);
    }

    setTimeout(function() {
        document.getElementById('out').textContent += 'Path: ' + (new Date() - start) + 'ms';
        // paths.parentNode.removeChild(paths);
    }, 10);

    ]]>
    </script>
</svg>

对于polygon也是如此:

<svg xmlns="http://www.w3.org/2000/svg">
    <g id="polygons"></g>

    <text x="20" y="20" id="out"></text>

    <script><![CDATA[
    var points = "43.301270189221924,55 43.301270189221924,65 51.96152422706631,70 60.6217782649107,65 60.6217782649107,55 51.96152422706631,50";

    var polygons = document.getElementById('polygons');
    var polygon = document.createElementNS("http://www.w3.org/2000/svg", 'polygon');
    polygon.setAttribute('points', points);

    var start = new Date();
    for (var i = 0; i < 20000; i++)
    {
        var el = polygon.cloneNode(true);
        el.setAttribute('transform', 'translate(' + ((i * 20) % 1000) + ',' + ((i / 50) * 20) + ')');
        polygons.appendChild(el);
    }

    setTimeout(function(){
        document.getElementById('out').textContent += 'Polygons: ' + (new Date() - start) + 'ms';
        // polygons.parentNode.removeChild(polygons);
    }, 10);

    ]]>
    </script>
</svg>

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:

<svg xmlns="http://www.w3.org/2000/svg">
    <g id="paths"></g>

    <text x="20" y="20" id="out"></text>

    <script><![CDATA[
    var d = "M43.301270189221924,55 L43.301270189221924,65 L51.96152422706631,70 L60.6217782649107,65 L60.6217782649107,55 L51.96152422706631,50 Z";

    var paths = document.getElementById('paths');
    var path = document.createElementNS("http://www.w3.org/2000/svg", 'path');
    path.setAttribute('d', d);

    var start = new Date();
    for (var i = 0; i < 20000; i++)
    {
        var el = path.cloneNode(true);
        el.setAttribute('transform', 'translate(' + ((i * 20) % 1000) + ',' + ((i / 50) * 20) + ')');
        paths.appendChild(el);
    }

    setTimeout(function() {
        document.getElementById('out').textContent += 'Path: ' + (new Date() - start) + 'ms';
        // paths.parentNode.removeChild(paths);
    }, 10);

    ]]>
    </script>
</svg>

And the same thing for polygon:

<svg xmlns="http://www.w3.org/2000/svg">
    <g id="polygons"></g>

    <text x="20" y="20" id="out"></text>

    <script><![CDATA[
    var points = "43.301270189221924,55 43.301270189221924,65 51.96152422706631,70 60.6217782649107,65 60.6217782649107,55 51.96152422706631,50";

    var polygons = document.getElementById('polygons');
    var polygon = document.createElementNS("http://www.w3.org/2000/svg", 'polygon');
    polygon.setAttribute('points', points);

    var start = new Date();
    for (var i = 0; i < 20000; i++)
    {
        var el = polygon.cloneNode(true);
        el.setAttribute('transform', 'translate(' + ((i * 20) % 1000) + ',' + ((i / 50) * 20) + ')');
        polygons.appendChild(el);
    }

    setTimeout(function(){
        document.getElementById('out').textContent += 'Polygons: ' + (new Date() - start) + 'ms';
        // polygons.parentNode.removeChild(polygons);
    }, 10);

    ]]>
    </script>
</svg>
习惯成性 2024-12-04 19:05:33

最终将我的评论更改为答案...

我不太了解 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文