将HTML5 Canvas的内容保存为图片借助toDataURL实现
程序员文章站
2023-11-21 13:02:46
将HTML5 Canvas的内容保存为图片主要思想是借助Canvas自己的API - toDataURL()来实现,具体实现如下,感兴趣的朋友可以参考下哈,希望对你有所帮助... 13-05-20...
主要思想是借助canvas自己的api - todataurl()来实现,整个实现
html + javascript的代码很简单。
<html>
<meta http-equiv="x-ua-compatible" content="chrome=1">
<head>
<script>
window.onload = function() {
draw();
var savebutton = document.getelementbyid("saveimagebtn");
bindbuttonevent(savebutton, "click", saveimageinfo);
var dlbutton = document.getelementbyid("downloadimagebtn");
bindbuttonevent(dlbutton, "click", saveaslocalimage);
};
function draw(){
var canvas = document.getelementbyid("thecanvas");
var ctx = canvas.getcontext("2d");
ctx.fillstyle = "rgba(125, 46, 138, 0.5)";
ctx.fillrect(25,25,100,100);
ctx.fillstyle = "rgba( 0, 146, 38, 0.5)";
ctx.fillrect(58, 74, 125, 100);
ctx.fillstyle = "rgba( 0, 0, 0, 1)"; // black color
ctx.filltext("gloomyfish - demo", 50, 50);
}
function bindbuttonevent(element, type, handler)
{
if(element.addeventlistener) {
element.addeventlistener(type, handler, false);
} else {
element.attachevent('on'+type, handler);
}
}
function saveimageinfo ()
{
var mycanvas = document.getelementbyid("thecanvas");
var image = mycanvas.todataurl("image/png");
var w=window.open('about:blank','image from canvas');
w.document.write("<img src='"+image+"' alt='from canvas'/>");
}
function saveaslocalimage () {
var mycanvas = document.getelementbyid("thecanvas");
// here is the most important part because if you dont replace you will get a dom 18 exception.
// var image = mycanvas.todataurl("image/png").replace("image/png", "image/octet-stream;content-disposition: attachment;filename=foobar.png");
var image = mycanvas.todataurl("image/png").replace("image/png", "image/octet-stream");
window.location.href=image; // it will save locally
}
</script>
</head>
<body bgcolor="#e6e6fa">
<div>
<canvas width=200 height=200 id="thecanvas"></canvas>
<button id="saveimagebtn">save image</button>
<button id="downloadimagebtn">download image</button>
</div>
</body>
</html>
运行效果如下:
html + javascript的代码很简单。
复制代码
代码如下:<html>
<meta http-equiv="x-ua-compatible" content="chrome=1">
<head>
<script>
window.onload = function() {
draw();
var savebutton = document.getelementbyid("saveimagebtn");
bindbuttonevent(savebutton, "click", saveimageinfo);
var dlbutton = document.getelementbyid("downloadimagebtn");
bindbuttonevent(dlbutton, "click", saveaslocalimage);
};
function draw(){
var canvas = document.getelementbyid("thecanvas");
var ctx = canvas.getcontext("2d");
ctx.fillstyle = "rgba(125, 46, 138, 0.5)";
ctx.fillrect(25,25,100,100);
ctx.fillstyle = "rgba( 0, 146, 38, 0.5)";
ctx.fillrect(58, 74, 125, 100);
ctx.fillstyle = "rgba( 0, 0, 0, 1)"; // black color
ctx.filltext("gloomyfish - demo", 50, 50);
}
function bindbuttonevent(element, type, handler)
{
if(element.addeventlistener) {
element.addeventlistener(type, handler, false);
} else {
element.attachevent('on'+type, handler);
}
}
function saveimageinfo ()
{
var mycanvas = document.getelementbyid("thecanvas");
var image = mycanvas.todataurl("image/png");
var w=window.open('about:blank','image from canvas');
w.document.write("<img src='"+image+"' alt='from canvas'/>");
}
function saveaslocalimage () {
var mycanvas = document.getelementbyid("thecanvas");
// here is the most important part because if you dont replace you will get a dom 18 exception.
// var image = mycanvas.todataurl("image/png").replace("image/png", "image/octet-stream;content-disposition: attachment;filename=foobar.png");
var image = mycanvas.todataurl("image/png").replace("image/png", "image/octet-stream");
window.location.href=image; // it will save locally
}
</script>
</head>
<body bgcolor="#e6e6fa">
<div>
<canvas width=200 height=200 id="thecanvas"></canvas>
<button id="saveimagebtn">save image</button>
<button id="downloadimagebtn">download image</button>
</div>
</body>
</html>
运行效果如下:
上一篇: HTML5视频支持检测(检查浏览器是否支持视频播放)
下一篇: 下一代汽车:移动互联战场的新主角