首页 \ 问答 \ 如何避免在页面刷新时刷新html5画布(How to avoid html5 canvas to be refreshed on page refresh)

如何避免在页面刷新时刷新html5画布(How to avoid html5 canvas to be refreshed on page refresh)

在一个网页中,我有一个html5画布,需要花费大量时间进行初始化,一旦准备就绪,我想避免刷新它。 目前在页面刷新时,画布将从头开始重新加载,从而导致漫长的等待时间。

有没有办法,甚至是解决方法(“未记录的画布属性或保存到本地存储技巧”),以避免这种情况?


In a webpage i have a html5 canvas who takes a lot of time to initialize and once it is ready i would like to avoid refreshing it. Currently on page refresh the canvas is reloaded form scratch introducing a long waiting time.

Is there a way, even a workaround (a "non documented canvas attribute or a save to local storage trick"), to avoid this?


原文:https://stackoverflow.com/questions/43465242
更新时间:2023-07-25 09:07

最满意答案

此函数将删除复制其上方单元格内容的单元格内容。 它不会合并它们( rowspan / colspan ),但它会将它们清空。

function emptyDittoCells(table) {
    if (!(table instanceof jQuery && table.is("table")))
        throw "bad parameter";

    table.find('tr').each(function(rowElement) {
        var row = $(rowElement);
        var previousRow = $(rowElement).prev('tr');
        if (!previousRow.length) return;

        row.each(function(cellElement, index) {
            var cell = $(cellElement);
            var previousCell = previousRow.children("td:nth-child(" + index + ")"); 

            while (previousCell.data('ditto')) {
                previousCell = previousCell.data('ditto');
            }

            if (hasSameContents(cell, previousCell)) {
                cell.empty()
                    .data('ditto', previousCell);
            }
        });
    });
}

function hasSameContents(a, b) {
    return (a.text() == b.text()); // naive but usable in the simple case
}

未经测试和一点DOM密集,但我认为它会工作。 除了可能的语法错误(缺少密括号),我最关心的是td:nth-child()选择器。 这可以更好地写成$(previousRow.children('td').get(index))


This function will remove the contents of cells which duplicate the content of the cell above them. It won't merge them (à la rowspan / colspan), but it will empty them.

function emptyDittoCells(table) {
    if (!(table instanceof jQuery && table.is("table")))
        throw "bad parameter";

    table.find('tr').each(function(rowElement) {
        var row = $(rowElement);
        var previousRow = $(rowElement).prev('tr');
        if (!previousRow.length) return;

        row.each(function(cellElement, index) {
            var cell = $(cellElement);
            var previousCell = previousRow.children("td:nth-child(" + index + ")"); 

            while (previousCell.data('ditto')) {
                previousCell = previousCell.data('ditto');
            }

            if (hasSameContents(cell, previousCell)) {
                cell.empty()
                    .data('ditto', previousCell);
            }
        });
    });
}

function hasSameContents(a, b) {
    return (a.text() == b.text()); // naive but usable in the simple case
}

untested and a little DOM-intensive, but I think it'll work. Besides possible syntax errors (missing close-parentheses), I'm mostly concerned about that td:nth-child() selector. That could be written more robustly as $(previousRow.children('td').get(index)).

相关问答

更多
  • 这是我做的工作: function createTableHeader(table, headers, alignment) { if (headers.length > 0) { var thead = document.createElement('thead'); table.appendChild(thead); var tr = document.createElement('tr'); for (var i = 0; i <= headers.length - ...
  • 使用sort()来排序tr元素的数组。 您可以在排序功能中获取元素类并设置每个元素的排列。 $("table tbody tr").sort(function (a, b){ return $("td", b).attr("class") < $("td", a).attr("class") ? 1 : -1; }).appendTo('table tbody');