canvas:用于绘制图像(经过脚本,一般是 JavaScript)。元素自己并无绘制能力(它仅仅是图形的容器) 您必须使用脚原本完成实际的绘图任务.
getContext() 方法可返回一个对象,该对象提供了用于在画布上绘图的方法和属性。
经常使用属性:
fillStyle: 设置或返回用于填充绘画的颜色、渐变或模式
strokeStyle: 设置或返回用于笔触的颜色、渐变或模式
shadowColor: 设置或返回用于阴影的颜色
shadowBlur:设置或返回用于阴影的模糊级别
font: 设置或返回文本内容的当前字体属性
textAlign: 设置或返回文本内容的当前对齐方式
经常使用方法:
createLinearGradient() 建立线性渐变(用在画布内容上)
createPattern() 在指定的方向上重复指定的元素
lineWidth 设置或返回当前的线条宽度
rect() 建立矩形
fillRect() 绘制“被填充”的矩形
strokeRect() 绘制矩形(无填充)
clearRect() 在给定的矩形内清除指定的像素
fill() 填充当前绘图(路径)
stroke() 绘制已定义的路径
beginPath() 起始一条路径,或重置当前路径
moveTo() 把路径移动到画布中的指定点,不建立线条
closePath() 建立从当前点回到起始点的路径
lineTo() 添加一个新点,而后在画布中建立从该点到最后指定点的线条
quadraticCurveTo() 建立二次贝塞尔曲线
bezierCurveTo() 建立三次方贝塞尔曲线
arc() 建立弧/曲线(用于建立圆形或部分圆)
scale() 缩放当前绘图至更大或更小
rotate() 旋转当前绘图
translate() 从新映射画布上的 (0,0) 位置
fillText() 在画布上绘制“被填充的”文本
strokeText() 在画布上绘制文本(无填充)
createImageData() 建立新的、空白的 ImageData 对象
getImageData() 返回 ImageData 对象,该对象为画布上指定的矩形复制像素数据
save() 保存当前环境的状态
restore() 返回以前保存过的路径状态和属性
-------------------
利用画布和javascript咱们能够实现不少动画效果,虽然css+javascript也能够实现动画的效果,可是css+javascript执行效率没有画布和javascript高,因为css+javascript每执行一次,都会刷新一次页面,页面整个内容会加载一次。可是画布+javascript不会,每次都是重新加载画布的内容,真个页面的布局不会打乱。因此从执行效率来讲,仍是画布和javascript有优点。javascript
下面是用画布和javascript结合作的一个动画效果:css
<!DOCTYPE html> | |
<html> | |
<head lang="en"> | |
<meta charset="UTF-8"> | |
<title></title> | |
<style> | |
*{margin: 0;padding: 0} | |
body{ | |
text-align: center; | |
} | |
canvas{ | |
margin: 20px auto; | |
border: 1px solid; | |
} | |
audio{ | |
position: absolute; | |
top: 100px; | |
left: 950px; | |
} | |
</style> | |
</head> | |
<body> | |
<canvas id="myCanvas" width="900" height="600"></canvas> | |
<audio src="12345.mp3" controls="controls" autoplay="autoplay">你的浏览器不支持音频播放</audio> | |
</body> | |
<script> | |
var c=document.getElementById("myCanvas"); | |
var cxt=c.getContext("2d"); | |
back(); | |
id1=setTimeout("head()",10); | |
id2=setTimeout("body()",1000); | |
id3=setTimeout("hand()",2000); | |
id4= setTimeout("foot()",3000); | |
id5=setTimeout("feeler()",4000); | |
id6=setTimeout("shan()",6000); | |
function back(){ | |
cxt.fillStyle="black"; | |
cxt.beginPath(); | |
cxt.fillRect(250,50,400,500); | |
cxt.closePath(); | |
} | |
function head(){ | |
cxt.fillStyle="#5acc42"; | |
cxt.beginPath(); | |
cxt.arc(450,200,100,Math.PI,2*Math.PI); | |
cxt.closePath(); | |
cxt.fill(); | |
cxt.fillStyle="black"; | |
cxt.beginPath(); | |
cxt.arc(410,150,12,0,2*Math.PI); | |
cxt.arc(490,150,12,0,2*Math.PI); | |
cxt.closePath(); | |
cxt.fill(); | |
} | |
function body(){ | |
cxt.fillStyle="#5acc42"; | |
cxt.beginPath(); | |
cxt.fillRect(350,220,200,180); | |
cxt.closePath(); | |
cxt.beginPath(); | |
cxt.fillRect(370,400,160,20); | |
cxt.closePath(); | |
cxt.beginPath(); | |
cxt.arc(370,400,20,0,2*Math.PI); | |
cxt.arc(530,400,20,0,2*Math.PI); | |
cxt.closePath(); | |
cxt.fill(); | |
} | |
function hand(){ | |
cxt.fillStyle="#5acc42"; | |
cxt.beginPath(); | |
cxt.fillRect(290,250,40,100); | |
cxt.fillRect(570,250,40,100); | |
cxt.closePath(); | |
cxt.beginPath(); | |
cxt.arc(310,250,20,0,2*Math.PI); | |
cxt.arc(310,350,20,0,2*Math.PI); | |
cxt.closePath(); | |
cxt.fill(); | |
cxt.beginPath(); | |
cxt.arc(590,250,20,0,2*Math.PI); | |
cxt.arc(590,350,20,0,2*Math.PI); | |
cxt.closePath(); | |
cxt.fill(); | |
} | |
function foot(){ | |
cxt.fillStyle="#5acc42"; | |
cxt.beginPath(); | |
cxt.fillRect(380,419,40,80); | |
cxt.fillRect(480,419,40,80); | |
cxt.closePath(); | |
cxt.beginPath(); | |
cxt.arc(400,500,20,0,2*Math.PI); | |
cxt.closePath(); | |
cxt.fill(); | |
cxt.beginPath(); | |
cxt.arc(500,500,20,0,2*Math.PI); | |
cxt.closePath(); | |
cxt.fill(); | |
} | |
function feeler(){ | |
cxt.fillStyle="#5acc42"; | |
cxt.beginPath(); | |
cxt.save(); | |
cxt.translate(390,75); | |
cxt.rotate(-30*Math.PI/180); | |
cxt.fillRect(-20,0,8,40); | |
cxt.closePath(); | |
cxt.beginPath(); | |
cxt.arc(-16,0,4,0,2*Math.PI); | |
cxt.closePath(); | |
cxt.restore(); | |
cxt.fill(); | |
cxt.beginPath(); | |
cxt.save(); | |
cxt.translate(500,75); | |
cxt.rotate(30*Math.PI/180); | |
cxt.fillRect(20,-5,8,40); | |
cxt.closePath(); | |
cxt.beginPath(); | |
cxt.arc(24,-5,4,0,2*Math.PI); | |
cxt.closePath(); | |
cxt.restore(); | |
cxt.fill(); | |
} | |
function shan(){ | |
cxt.clearRect(250,50,400,500); | |
back(); | |
clearTimeout(id1); | |
clearTimeout(id2); | |
clearTimeout(id3); | |
clearTimeout(id4); | |
clearTimeout(id5); | |
clearTimeout(id6); | |
setTimeout("shan1()",500); | |
} | |
function shan1(){ | |
clearTimeout(id1); | |
clearTimeout(id2); | |
clearTimeout(id3); | |
clearTimeout(id4); | |
clearTimeout(id5); | |
clearTimeout(id6); | |
head(); | |
body(); | |
hand(); | |
foot(); | |
feeler(); | |
setTimeout("shan()",1000); | |
} | |
</script> | |
</html> |