第27章 canvas保存文件

html

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript" src="27.js"></script>
<style type="text/css">
body{ margin:0; padding:0;}
</style>
</head>
<body onLoad="draw('canvas')">
<canvas id="canvas" width='400' height="300"></canvas>
</body>
</html>

 

js

// JavaScript Document
function draw(id){
    var canvas = document.getElementById(id);
    var context = canvas.getContext('2d');
    context.fillStyle = "green";
    context.fillRect(0,0,400,300);
    window.location = canvas.toDataURL('image/png');
}

 

效果图:
第27章 canvas保存文件_第1张图片
 

 

 

你可能感兴趣的:(html5)