尽管每个canvas对象都是一副位图,但它并不是HTML的img元素,所以用户不能对其进行某些操作,比如不能在canvas上通过右击鼠标然后将其保存到磁盘,也不能将其拖动到桌面上,不过canvas的API提供了一个叫做toDataURL()的方法,该方法所返回的引用,指向了某个给定canvas元素的数据地址,可以将img元素的src属性设置为这个数据地址。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Clock</title>
<style>
body {
background: #dddddd;
}
#canvas {
position: absolute;
left: 10px;
top: 1.5em;
margin: 20px;
border: thin solid #aaaaaa;
}
#snapshotImageElement {
position: absolute;
left: 10px;
top: 1.5em;
margin: 20px;
border: thin solid #aaaaaa;
}
</style>
</head>
<body>
<div id='controls'>
<input id='snapshotButton' type='button' value='