主要思想是借助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>
~~~
**运行效果如下:**
![](https://box.kancloud.cn/2016-05-17_573adb3533d7c.jpg)
- 前言
- HTML5 组件Canvas实现图像灰度化
- HTML5 Canvas组件绘制太极图案
- HTML5 组件Canvas实现电子钟
- 将HTML5 Canvas的内容保存为图片
- 纯JavaScript实现HTML5 Canvas六种特效滤镜
- 提高HTML5 Canvas性能的技巧
- HTML5 Canvas平移,放缩,旋转演示
- HTML5 Canvas鼠标与键盘事件
- HTML5 Canvas渐进填充与透明
- HTML5 Canvas动画效果演示
- HTML5 Canvas 填充与描边(Fill And Stroke)
- HTML5 Canvas阴影用法演示
- HTML5 Canvas自定义圆角矩形与虚线(Rounded Rectangle and Dash Line)
- HTML5 Canvas中实现绘制一个像素宽的细线