meicy
2015-05-03 17:48
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>html5 localStorage实现图片存储</title>
<script>
var src='images/key.png';
function set(key){
var img=document.createElement('img');
img.addEventListener('load',function(){
var imgCanvas=document.creatElement('canvas');
imgContext=imgCanvas.getContext('2d');
//确保canvas元素的大小和图片尺寸一致
imgCanvas.width=this.width;
imgCanvas.height=this.height;
//渲染图片到canvas中
imgContext.drawImage(this,0,0,this.widht,this.height);
//用data url的形式取出
var imgAsDataURL=imgCanvas.toDataURL('images/png');
//保存到本地存储中
try{
localStorage.setItem(key,imgAsDataURL);
}
catch(e){
console.log('Storage failed:'+e)
}
},false);
img.src=src
}
function get(key){
var srcStr=localStorage.getItem(key);
var imgobj=document.createElement('img');
imgobj.src=srcStr;
document.body.appendChild(imgobj);
}
</script>
</head>
<body>
</body>
</html>
var imgCanvas=document.creatElement('canvas');
写错了
document.createElement
咦,正好拿来改一改
哥,你应该贴出报的错,不然怎么回答你呀
HTML5存储
55331 学习 · 109 问题
相似问题