我進入了一個問題,我試圖從cookie加載我的畫布數據,但是當我加載數據時發生以下錯誤。HTML從Cookie加載畫布數據
Uncaught TypeError: Failed to execute 'putImageData' on 'CanvasRenderingContext2D': No function was found that matched the signature provided.
什麼,我試圖做的就是當另一按鈕被按下我想讀,然後重新繪製在畫布上該數據按下一個按鈕,然後帆布數據保存到一個cookie。
if (document.getElementById('hdnSwitchButton').getAttribute("Value") == 0) {
canvas.style.zIndex = "-2";
} else if (document.getElementById('hdnSwitchButton').getAttribute("Value") == 1) {
canvas.style.zIndex = "0";
canvasRead = getCookie("canvas");
if (!canvasRead) {
alert('Error: Cannot read canvas!');
} else {
context.putImageData(canvasRead, 0, 0);
}
}
這是我initiliase代碼而讀取cookie並上線時出現錯誤:
context.putImageData(canvasRead, 0, 0);
當保存一個cookie,這是我的代碼:
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i].trim();
if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
}
return "";
}
function BrowserButtonClick() {
document.getElementById('hdnSwitchButton').setAttribute('Value', 0);
canvasSave = context.getImageData(0, 0, aWidth, aHeight);
document.cookie = "canvas=" + canvasSave;
}
和我HTML畫布代碼如下:
<div class="canvas">
<canvas id = "DrawingCanvas">
<p>Unfortunately, your browser is currently unsupported by our web application. We are sorry for the inconvenience. Please use one of the supported browsers listed below.</p>
<p>Supported browsers: <a href="http://www.google.com/intl/en_uk/chrome/browser/">Chrome</a>, <a href="http://www.opera.com">Opera</a>, <a href="http://www.mozilla.com">Firefox</a>, <a href="http://www.apple.com/safari">Safari</a>, and <a href="http://www.konqueror.org">Konqueror</a>.</p>
</canvas>
</div>
我想知道的是爲什麼發生這種情況,以及如何從cookie中加載我的畫布,沒有任何問題或更好的加載畫布數據的更有效的方式。