将画布中的图像保存到本地存储

将画布中的图像保存到本地存储

本文介绍了将画布中的图像保存到本地存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我画布上的画圈保存到本地存储中,但我根本无法让它工作。

I'm trying to save my drawn circles on my canvas to the localstorage, but I just can't get it to work, at all.

到目前为止这是我在JS中的保存代码。

So far this is my save-code in JS.

function saveCanvas() {
var canvas = document.getElementById("imgCanvas"),
    doomslayer = canvas.toDataURL();

 if (typeof (localStorage) !== "undefined") {

    localStorage.setItem('imgCanvas', doomslayer);
 } else {
    document.getElementById("save").innerHTML.dataURL = "Local Storage not supported";
   }
}

function loadCanvas() {

var image = localStorage.getItem('imgCanvas');

document.getElementById('imgCanvas').src = image;
}

现在终于开始工作吧!

Got it to work now, finally!Working fiddle :)

推荐答案

您必须将保存的数据网址加载到图像中,然后使用drawImage将其加载到画布上

You must load the saved data url into an Image and then use drawImage to load it onto the canvas

IE限制:localStorage仅在提供的页面上可用(本地执行无效!)。

IE limitation: localStorage is only available on a served page (local execution won't work!).

// save to localStorage

localStorage.setItem("imgCanvas",canvas.toDataURL());

// reload from localStorage

var img=new Image();
img.onload=function(){
    context.drawImage(img,0,0);
}
img.src=localStorage.getItem("imgCanvas");

这篇关于将画布中的图像保存到本地存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 14:13