Hi!
I am a beginner...
I simply want to know how do a local storage for my drawing in the canvas. Here is my html code. If you click on draw, a rectangle is drawn in the canvas. But how do I make the web browser remember the drawing when I update or load them website again using localstorage?
Code:
<!doctype html>
<html>
<head>
<title>Uppgift 3</title>
<script>
function draw(){
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.fillStyle="#FF0000";
ctx.fillRect(0,0,150,75);
}
</script>
</head>
<body >
<canvas id="myCanvas" width="200" height="100"></canvas>
<input type="button" value="draw" onclick="draw()"/>
</body>
</html>
Your help really appreciated!
you can redraw it by following the same steps, or you can grab a copy of the pixel data using toDataURL(). save that data URL to localStorage[]. later load it into an img tag from localStorage[], and use drawImage() to paint the img tag into the canvas.
Bookmarks