劳资的第一个html5动画 - 虽然简单但很爽
2010-07-15 22:02
316 查看
--
Player.js
-- CircleAnimation.js
-- CircleTrajectory.js
测试页面
刚刚写好, 非常简单的动画, 以后会慢慢改进的。第一次手工写出动画,小兴奋
HOHO
~~
Player.js
if (!(window.Html5Animation)) { window.Html5Animation = {}; } Html5Animation.Player = function (animation) { this.animation = animation; this.framePerSec = animation.framePerSec; this.intervalTime = 1000/this.framePerSec; //millisecond this.currentFrameCount = 0; this.playing = false; } Html5Animation.Player.prototype = { play: function () { if (this.animation && !(this.playing)) { this.playing = true; this.doPlay(); } }, //private doPlay: function () { var self = this; this.timeoutCode = setTimeout(function () { self.clearScreen(); self.animation.draw(self.currentFrameCount); self.currentFrameCount++; self.doPlay(); }, this.intervalTime); }, pause: function () { if (this.currentFrameCount > 0 && this.playing) { this.playing = false; clearTimeout(this.timeoutCode); } }, stop: function () { this.playing = false; clearTimeout(this.timeoutCode); this.currentFrameCount = 0; this.clearScreen(); }, //private clearScreen: function () { this.animation.clear(); } }
-- CircleAnimation.js
if (!(window.Html5Animation)) { window.Html5Animation = {}; } (function () { Html5Animation.CircleAnimation = function (canvas) { this.canvas = canvas; this.context = canvas.getContext("2d"); } var TRAJECTORY = Html5Animation.CircleAnimation.TRAJECTORY = Html5Animation.CircleTrajectory; Html5Animation.CircleAnimation.prototype = { framePerSec: 12, draw: function (frameCount) { var radius = this.getRadius(frameCount); var centerCoordinate = this.getCenterCoordinate(frameCount); var centerX = centerCoordinate.x; var centerY= centerCoordinate.y; var strokeStyle = this.getStrokeStyle(frameCount); this.context.beginPath(); this.context.arc(centerX, centerY, radius, 0, Math.PI * 2, false); this.context.closePath(); this.context.strokeStyle = strokeStyle; this.context.stroke(); }, getRadius: function (frameCount) { return TRAJECTORY.getRadius(frameCount); }, /** * Return an object {x:[number], y:[number]} */ getCenterCoordinate: function (frameCount) { return TRAJECTORY.getCenterCoordinate(frameCount); }, getStrokeStyle: function (frameCount) { return "#000"; }, clear: function () { var w = this.canvas.width; var h = this.canvas.height ; this.context.clearRect(0, 0, w, h); } } })();
-- CircleTrajectory.js
if (!(window.Html5Animation)) { window.Html5Animation = {}; } Html5Animation.CircleTrajectory = { getRadius: function (time) { var x = time%(2*Math.PI); return Math.sin(x)+20; }, /** * Return an object {x:[number], y:[number]} */ getCenterCoordinate: function (time) { var x = time; var y = time*1.5; return { x: x, y: y }; } }
测试页面
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>My Page</title> <mce:script type="text/javascript" src="CircleTrajectory.js" mce_src="CircleTrajectory.js"></mce:script> <mce:script type="text/javascript" src="CircleAnimation.js" mce_src="CircleAnimation.js"></mce:script> <mce:script type="text/javascript" src="Player.js" mce_src="Player.js"></mce:script> <mce:script type="text/javascript"><!-- var player; function play() { if (!player) { var canvas = document.getElementById("myCanvas"); var ca = new Html5Animation.CircleAnimation(canvas); player = new Html5Animation.Player(ca); } player.play(); } // --></mce:script> </head> <body> <canvas id="myCanvas" width="300" height="225"></canvas> <input type="button" onclick="play()" value="Play"/> <input type="button" onclick="player.pause()" value="Pause"/> <input type="button" onclick="player.stop()" value="Stop"/> </body> </html>
刚刚写好, 非常简单的动画, 以后会慢慢改进的。第一次手工写出动画,小兴奋
HOHO
~~
相关文章推荐
- 基于html5websocket java实现简单通讯(虽然通了但是...你懂得)
- Delphi 与 DirectX 之 DelphiX(8): 第一个简单动画
- HTML5 Cavans(12) 简单动画:反弹
- [Mugeda HTML5技术教程之3] Hello World: 第一个Mugeda动画
- 自己写的HTML5 canvas动画制作函数,非常简单易懂
- HTML5 Cavans(10) 简单动画:摆动
- 我的第一个简单的html5 页面
- HTML5 Cavans(11) 简单动画:圆周运动
- 基于HTML5的简单游戏动画Demo_持续更新中
- [Mugeda HTML5技术教程之3] Hello World: 第一个Mugeda动画
- HTML5 Cavans(11) 简单动画:圆周运动
- 很有趣的flash动画,虽然简单,但是表达的手法却实在而幽默。
- HTML5 Cavans(12) 简单动画:反弹
- 基于HTML5的简单游戏动画Demo
- 第一个QML简单动画
- HTML5 Cavans(10) 简单动画:摆动
- 巧用HTML5给按钮背景设计不同的动画简单实例
- Html5的CSS3的transition实现简单动画效果
- 基于HTML5的简单游戏动画Demo_update3
- 基于HTML5的简单游戏动画Demo_update1