你也許看到很多html5很炫的動畫,自己也很想寫一個,這個需要一步一步的積累,一步一步的學,不能著急,很高興終於改用fps的方式畫圖了,靜態學地差不多了,呵呵,下面是一個簡單的圖形代碼,每秒執行60次,每次圓移動3個像素記錄下圓形的軌跡碰到臨界點就改變方向很簡單直接上源碼,又有這個程式依賴canvas庫,自己也可以開發一個,很簡單的庫,把一些常用的canvas功能做簡單的封裝就ok了。這個得方式很好記錄了我的學習過程,~_~ 高興呵呵
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="lib/canvas/canvas-1.2.dev.js">
</script>
<title>測試Canvas 方法</title>
</head>
<body style="padding:0 0 0 0; margin:0 0 0 0;">
<div style="width:100%;height:100%; padding:0 0 0 0; margin:0 0 0 0; font-size:50px;">
<canvas style="background-color:#ffffff; width:500px; height:400px; padding:0 0 0 0; margin:0 0 0 0;"id="ctMs">
</canvas>
</div>
<script type="text/javascript">
var cvs = Canvas("ctMs", 0);
var i = 0;
cvs.clear();
cvs.fillStyle('rgba(0, 0, 255, 0.5)');
cvs.fillRect(150, 100, 100, 50); // ALIGN.LEFT.TOP is the default alignment
cvs.strokeStyle('#333333');
cvs.grid(40, 50);
var boss = {
x: 17,
y: 32,
isTr: false,
isTl: false
}
cvs.createLayer("sp", 100, function(){
if (boss.x >= 480) {
boss.isTr = true;
}
if (boss.x <= 0) {
boss.isTr = false;
}
if (boss.isTr) {
boss.x--;
}
else {
boss.x++;
}
if (boss.y >= 300) {
boss.isTl = true;
}
if (boss.y <= 1) {
boss.isTl = false;
}
if (boss.isTl) {
boss.y--;
}
else {
boss.y++;
}
//this.clear();
this.fillColor('#331555');
this.fillCircle(boss.x, boss.y, 15);
});
cvs.onKeyPress = function(keyCode, character, isCtrlPressed, isShiftPressed, isAltPressed){
switch (keyCode) {
case 97:
boss.x -= 3;
break;
case 119:
boss.y += 3;
break;
case 115:
boss.y -= 3;
break;
case 100:
boss.x += 3;
break;
}
}
</script>
</body>
</html>
奉上