简体   繁体   English

保存在html画布或网页中所做的更改

[英]Save changes made in a html canvas or web page

I have an HTML canvas on which a user draws some shapes. 我有一个HTML画布,用户可以在其上绘制一些形状。 Now suppose after drawing some shapes the user types some new url and goes to that url. 现在,假设在绘制了一些形状之后,用户键入一些新的URL并转到该URL。 When the user presses the back button of browser and comes back to my page, all of his shapes drawn earlier are gone. 当用户按下浏览器的后退按钮并返回到我的页面时,先前绘制的所有形状都消失了。

What I want is to keep those shapes some where so that when user comes back he can resume with his previous shapes. 我想要的是将这些形状保留在某个位置,以便用户回来时可以恢复以前的形状。 Any Idea? 任何想法?

You need to save what the user is drawing in objects, and then serialize them to JSON . 您需要保存用户在对象中绘制的内容,然后将其序列化为JSON When it's serialized you can store the JSON using the localstorage, in a cookie or in a database. 序列化后,您可以使用localstorage将JSON存储在cookie或数据库中。

Using localStorage you can save the data with: 使用localStorage可以通过以下方式保存数据:

try {
    localStorage.setItem(“jsonName”, jsonString);
} exception(e) {
    // do something
}

and later read it with: 然后阅读:

var jsonString = localStorage.getItem(“jsonName”);

but you need a browser that has support for localStorage. 但是您需要一个支持localStorage的浏览器。

There is also compatibility issues, that you need to care of, see DOM storage . 还有一些您需要注意的兼容性问题,请参阅DOM storage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM