0

我正在尝试在 canvas 上编写撤消/重做功能。我正在使用 fabric-js 来绘制对象。撤消?重做工作正常,但问题是在调用撤消/重做功能后,如果我单击画布元素变得不可见。我尝试了 canvas.renderAll() 仍然遇到问题。

撤消步骤::

  1. 在绘制元素之前,我保存canvas.toDataURl()在一个变量数组中。

    2.当单击撤消按钮时,我使用结构的清除方法来清除画布,然后使用 Previous `toDataUrl() 获取要在画布上绘制的变量数组。

代码 ::

     function undoDrawOnCanvas() {
        console.log('inside undodrawn');
        // If we have some restore points
        if (restorePoints.length > 0) {
            // Create a new Image object

            var oImg = new Image();
            // When the image object is fully loaded in the memory...
            oImg.onload = function() {

                //Saving point for Redo
                c_redo=document.getElementById("design_text").toDataURL("image/png");

                canvas.clear();

                // Get the canvas context
                var canvasContext = document.getElementById("design_text").getContext("2d");
                // and draw the image (restore point) on the canvas. That would overwrite anything
                // already drawn on the canvas, which will basically restore it to a previous point.
                canvasContext.drawImage(oImg, 0, 0);
            }
            // The source of the image, is the last restoration point
            oImg.src = restorePoints.pop();

        }
    }  
4

0 回答 0