如何通过拖放来重新排序dom中的canvas元素?

发布于 2024-12-25 09:50:43 字数 728 浏览 1 评论 0原文

我想允许对类似 Photoshop 的图层进行拖放重新排序。

假设我在页面中有 3 个画布元素,id 为 a、b 和 c:

<canvas id="a"></canvas>
<canvas id="b"></canvas>
<canvas id="c"></canvas>

c 是“顶层”层,因为它是最后定义的,并且它们绝对位于彼此之上;如何将元素重新排列为以下顺序:c、a、b,其中 c 现在位于其他层的后面?

<canvas id="c"></canvas>
<canvas id="a"></canvas>
<canvas id="b"></canvas>

我想通过检测列表的拖放排序来触发排序,如下所示:

 <ul id="layers">
    <li>Layer C</li>
    <li>Layer B</li>
    <li>Layer A</li>
 </ul>

我可能可以使用 jQuery ui sort 来允许对列表进行重新排序。使画布 DOM 顺序反映列表顺序的最简单方法是什么? (镜像有点相反,因为顶部有 C 的列表映射到底部有 C 的画布。

I want to allow drag-and-drop re-ordering of photoshop-like layers.

Assuming I have 3 canvas elements in the page with ids a, b and c:

<canvas id="a"></canvas>
<canvas id="b"></canvas>
<canvas id="c"></canvas>

c is the layer "on-top" because it is defined last, and they are positioned absolutely on top of each other; how can I rearrange the elements into the order: c, a, b, where c is now behind the other layers?

<canvas id="c"></canvas>
<canvas id="a"></canvas>
<canvas id="b"></canvas>

I want to trigger the sorting by detecting drag-and-drop sorting of a list like this:

 <ul id="layers">
    <li>Layer C</li>
    <li>Layer B</li>
    <li>Layer A</li>
 </ul>

I can probably use jQuery ui sort to allow re-ordering of the list. What is the easiest way to make the canvases DOM order mirror the list order? (the mirroring is somewhat in reverse because the list with C on-top maps to canvas having C on bottom.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

无需解释 2025-01-01 09:50:43

调整 z 分数绝对是正确的选择。我使用了一种样式来处理我关心的所有元素。这是我最近的一个项目的一些片段。

像这样的:

// brings the focus of the selected table on the canvas to the front
function adjustFocus(event, ui) {
    // clear any old focuses
    $("#table-canvas").find(".table-view").each(function (index) {
        $(this).removeClass("table-view-focus");
    });

    // add the focus to the current table
    $(this).addClass("table-view-focus");
}

类很简单:

.table-view-focus { z-index: 10; }

调整焦点被称为可拖动的开始事件:

// make this view draggable
        tableView.draggable({
            containment: "#table-canvas",
            handle: "h3",
            start: adjustFocus,
            drag: tableViewDrag
        });

Adjusting the z-score is definitely the way to go. I used a style to handle this for all the elements I care about. Here are some snippets from a recent project of mine.

Something like this:

// brings the focus of the selected table on the canvas to the front
function adjustFocus(event, ui) {
    // clear any old focuses
    $("#table-canvas").find(".table-view").each(function (index) {
        $(this).removeClass("table-view-focus");
    });

    // add the focus to the current table
    $(this).addClass("table-view-focus");
}

The class is simply:

.table-view-focus { z-index: 10; }

Adjust focus is called as the start event for draggable:

// make this view draggable
        tableView.draggable({
            containment: "#table-canvas",
            handle: "h3",
            start: adjustFocus,
            drag: tableViewDrag
        });
绳情 2025-01-01 09:50:43

我相信你必须改变画布的 z 顺序,而不仅仅是改变它们的顺序,还是我错了?无论如何,我建议这样:

$("#layers").sortable({
    update: function(event, ui) {
        $("canvas").detach(); // Removes all canvases
        $("#layers li").each(function() {
            // Reinsert them in the same order of your list items
            $("#" + $(this).data("canvas")).appendTo(whereYourCanvasesWere);
        });
    }
}).disableSelection();

在你的 HTML 中:

<ul id="layers">
    <li data-canvas="c">Layer C</li>
    <li data-canvas="b">Layer B</li>
    <li data-canvas="a">Layer A</li>
</ul>

编辑:哦,如果你想颠倒它们的放置顺序,只需使用 prependTo 而不是 appendTo

I believe you have to change the z-order of the canvases instead of just altering their order, or am I mistaken? Anyway, I'd suggest something like this:

$("#layers").sortable({
    update: function(event, ui) {
        $("canvas").detach(); // Removes all canvases
        $("#layers li").each(function() {
            // Reinsert them in the same order of your list items
            $("#" + $(this).data("canvas")).appendTo(whereYourCanvasesWere);
        });
    }
}).disableSelection();

And in your HTML:

<ul id="layers">
    <li data-canvas="c">Layer C</li>
    <li data-canvas="b">Layer B</li>
    <li data-canvas="a">Layer A</li>
</ul>

Edit: oh, and if you want to invert the order they are placed, just use prependTo instead of appendTo.

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