forked from hehos/javascriptAdvanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2DDataUrlExample01.htm
More file actions
executable file
·44 lines (36 loc) · 1.69 KB
/
Copy path2DDataUrlExample01.htm
File metadata and controls
executable file
·44 lines (36 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<!DOCTYPE html>
<html>
<head>
<title>Canvas Fill Rect Example</title>
</head>
<body>
<canvas id="drawing" width="200" height="200">Your browser doesn't support the canvas tag.</canvas>
<input type="button" value="Export" id="export-btn" >
<script type="text/javascript">
window.onload = function(){
var drawing = document.getElementById("drawing"),
btn = document.getElementById("export-btn");
//make sure <canvas> is completely supported
if (drawing.getContext){
var context = drawing.getContext("2d");
//draw a red rectangle
context.fillStyle = "#ff0000";
context.fillRect(10, 10, 50, 50);
//draw a blue rectangle that's semi-transparent
context.fillStyle = "rgba(0,0,255,0.5)";
context.fillRect(30, 30, 50, 50);
}
btn.onclick = function(){
//get data URI of the image
var imgURI = drawing.toDataURL();
//display the image
var image = document.createElement("img");
image.src = imgURI;
document.body.appendChild(image);
};
};
</script>
<p>This example based on <a href="http://developer.mozilla.org/en/docs/Canvas_tutorial:Basic_usage">Mozilla's documentation</a></p>
<p>Click <strong>Export</strong> to export the image to an <code>img</code> element. Then, you can right-click and save the image locally.</p>
</body>
</html>