【发布时间】:2013-03-11 12:11:25
【问题描述】:
我想使用 JavaScript 来存储(以某种方式记住)画布元素的当前状态(显示的图像),然后稍后恢复它。
var cvSave; //Global variable to save ImageData
function function1(){
//some stuff wich involes putting an image into a canvas
cvSave = context.getImageData(0,0,viewport.width, viewport.height);
// All the variables are existing and I use the context to put Stuff into the canvas which works fine
}
function isCalledLater(){
var canvas = document.getElementById('cv');
var ctx = canvas.getContext('2d'); //Get canvas and context
ctx.putImageData(cvSave,0,0); //So this should restore the canvas to what I save earlier, right?
}
但是当第二个函数被调用时,它只会将画布变成白色,并且不会将它恢复到我认为我在 cvSave 中保存的内容。
我希望这是客户端,我想将其恢复到我多次保存的状态。
在恢复画布后也很重要(我一开始忘记了),我想使用 Processingjs 在恢复图像上绘制,然后我希望能够再次执行此操作。
感谢您的帮助。
【问题讨论】:
-
您是否忘记定义一个在 function1 命名上下文中使用的全局变量?