天天看點

canvas實作星星特效

效果描述:

當滑鼠移動到圖檔上方時,圖檔上放會出現星星,并且星星随即運動;當滑鼠移開圖像時,星星漸漸消失。

效果圖如下:

canvas實作星星特效

主要實作代碼

index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<body>
    <div>
        <canvas id="canvas" width="800" height="600"></canvas>
    </div>

    <script type="text/javascript" src="js/commonFunctions.js"></script>
    <script type="text/javascript" src="js/main.js"></script>
    <script type="text/javascript" src="js/stars.js"></script>

</body>
</html>
           

main.js

var can;
var ctx;
var w;
var h;
// 女孩的圖檔
var girlPic = new Image();
// 星星圖檔
var starPic = new Image();
// 上次重新整理時間
var lastTime;
//重新整理時間間隔
var deltaTime;
var life = ;
var num = ;
var stars = [];
var switchy;
function init(){
    can = document.getElementById("canvas");
    ctx = can.getContext("2d");
    w=can.width;
    h=can.height;
    document.addEventListener("mousemove",mousemove,false); 

    // 加載圖檔
    girlPic.src = "src/girl.jpg";
    starPic.src="src/star.png";

    for (var i = ; i < num; i++) {
        var obj = new starObj();
        stars.push(obj);
        stars[i].init();
    }
    lastTime = Date.now();
    gameloop();
}
document.body.onload = init;

function drawBackground(){
    ctx.fillstyle = "#393550";
    ctx.fillRect(,,w,h);
}
function gameloop(){
    // 循環調用函數,時間不确定
    window.requestAnimFrame(gameloop);
    var now = Date.now();
    deltaTime = now - lastTime;
    lastTime = now;
    // console.log(deltaTime);
    drawBackground();
    drawGirl();
    drawStars();
    aliveUpdate();
}

function drawGirl(){
    ctx.drawImage(girlPic,,,,);
}
function mousemove(e){
    if (e.offsetX || e.layerX) {
        var px = e.offsetX == undefined ? e.layerX :e.offsetX;
        var py = e.offsetY == undefined ? e.layerY :e.offsetY;
        // console.log(px);

        if (px > && px < && py > && py < ) {
            switchy = true;
        }else{
            switchy = false;
        }
        // console.log(switchy);

    }
}
           

stars.js

// 星星類
var starObj = function(){
    this.x ;
    this.y ;
    this.picNo;
    this.timer;
    this.xSpeed;
    this.ySpeed;
}
// 初始化方法
starObj.prototype.init = function(){
    this.x= + Math.random()*;
    this.y= + Math.random()*;
    this.xSpeed = Math.random()*-;
    this.ySpeed = Math.random()*-;

    this.picNo = Math.floor(Math.random()*);
    this.timer=;
}
starObj.prototype.update = function(){
    this.timer += deltaTime;
    this.x += this.xSpeed *  * deltaTime;
    this.y += this.ySpeed *  * deltaTime;

    if (this.x < || this.x >) {
        this.init();
        return;
    }
    if (this.y < || this.y >) {
        this.init();
        return;
    }
    if (this.timer > ) {
        this.picNo += ;
        this.picNo %= ;
        this.timer = ;
    } 

}
starObj.prototype.draw = function(){
    ctx.save();
    ctx.globalAlpha = life;
    ctx.drawImage(starPic,*this.picNo,,,,this.x,this.y,,);
    ctx.restore();
}
function drawStars(){
    for (var i = ; i < num; i++) {
        stars[i].draw();
        stars[i].update();
    }
}
function aliveUpdate(){
    if (switchy) {
        life += *deltaTime*;
        if (life>) {
            life = ;
        }
    }else{
        life -= *deltaTime*;
        if (life<) {
            life = ;
        }
    }
}
           

commonFunctions.js

window.requestAnimFrame = (function() {
    return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
        function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element) {
            return window.setTimeout(callback,  / );
        };
})();
           

這是小編在通過慕課學習視訊時根據老師講解敲的代碼,供大家參考學習!