筆記十九(基本動畫——速度實例)

本節筆記中引用的js文件具體參見前幾節筆記。
速度實例中主要理解速度向量概念。
自此節筆記開始,代碼段:

if (!window.requestAnimationFrame) {
            window.requestAnimationFrame = (window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(callback){
                return window.setTimeout(callback,1000/60);
            })
        };

寫入utils.js文件裏。

速度實例1:文件名:velocity1.html。

<!DOCTYPE html>
<html>
<head>
    <meta charset = "utf-8">
    <title>速度實例1</title>
<style>
#canvas{background-color: #99cc33;}
</style>
</head>
<body>
<canvas id="canvas" width="400" height="400"></canvas>
<script type="text/javascript" src="ball.js"></script>
<script type="text/javascript" src="utils.js"></script>
<script type="text/javascript">
    window.onload = function(){
        var canvas = document.getElementById('canvas'),
            context = canvas.getContext("2d"),
            ball = new Ball(),
            angle = 45,
            speed = 1;
        ball.x = 50;
        ball.y = 100;
        (function drawFrame(){
            window.requestAnimationFrame(drawFrame,canvas);
            context.clearRect(0,0,canvas.width,canvas.height);
            var radius = angle*Math.PI/180,
                vx = Math.cos(radius)*speed,
                vy = Math.sin(radius)*speed;
            ball.x += vx; 
            ball.y += vy;
            ball.draw(context);
        }());
    }
</script>
</body>
</html>

效果圖:
實例1

文件名:velocity2.html。

<!DOCTYPE html>
<html>
<head>
    <meta charset = "utf-8">
    <title>速度實例2</title>
<style>
#canvas{background-color: #99cc33;}
</style>
</head>
<body>
<canvas id="canvas" width="400" height="400"></canvas>
<script type="text/javascript" src="arrow.js"></script>
<script type="text/javascript" src="utils.js"></script>
<script type="text/javascript">
    window.onload = function(){
        var canvas = document.getElementById('canvas'),
            context = canvas.getContext("2d"),
            mouse = utils.captureMouse(canvas),
            arrow = new Arrow(),
            speed = 3;
        (function drawFrame(){
            window.requestAnimationFrame(drawFrame,canvas);
            context.clearRect(0,0,canvas.width,canvas.height);
            var dx = mouse.x - arrow.x,
                dy = mouse.y - arrow.y,
                angle = Math.atan2(dy,dx),
                vx = Math.cos(angle)*speed,
                vy = Math.sin(angle)*speed;
            arrow.rotation = angle;
            arrow.x += vx;
            arrow.y += vy;
            arrow.draw(context); 
        }());
    }
</script>
</body>
</html>

效果圖:
實例2

文件名:velocity3.html。

<!DOCTYPE html>
<html>
<head>
    <meta charset = "utf-8">
    <title>速度實例3</title>
<style>
#canvas{background-color: #99cc33;}
</style>
</head>
<body>
<canvas id="canvas" width="400" height="400"></canvas>
<script type="text/javascript" src="arrow.js"></script>
<script type="text/javascript" src="utils.js"></script>
<script type="text/javascript">
    window.onload = function(){
        var canvas = document.getElementById('canvas'),
            context = canvas.getContext("2d"),
            arrow = new Arrow(),
            vr = 3;
        arrow.x = canvas.width/2;
        arrow.y = canvas.height/2;
        (function drawFrame(){
            window.requestAnimationFrame(drawFrame,canvas);
            context.clearRect(0,0,canvas.width,canvas.height);
            arrow.rotation += vr*Math.PI/180;  
            arrow.draw(context); 
        }());
    }
</script>
</body>
</html>

效果圖:
實例3

參見《HTML5+Javascript動畫基礎》。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章