动手学Javascript(3)——HTML5模拟下雪

时间:2022-11-22 09:37:22

马上就是圣诞节,基于HTML5的Canvas写个网页动画,祝大家圣诞节快乐。

在该网页中动画中,用简单的模型模拟下雪的场景,其截屏效果如下图所示。如果你想看动画的动态效果,也可以到网页http://jsfiddle.net/zhedahht/2uD5x/1/ 查看。

动手学Javascript(3)——HTML5模拟下雪

雪花被简化成一个白色的圆形,并按照正弦曲线缓慢降落。这个模型虽然简单,但效果基本满足卡通动画的要求。代码如下:

$(document).ready(function () {
    makeSnow("christmasSnow", "./Image/Christmas.jpg");
});

function makeSnow(canvasId, imagePath) {
    var christmasSnow = new ChristmasSnow(canvasId, imagePath);
    
    renderAndUpdate.christmasSnow = christmasSnow;
    setInterval(renderAndUpdate, 15);
}

function renderAndUpdate() {
    renderAndUpdate.christmasSnow.render();
    renderAndUpdate.christmasSnow.update();
}

function ChristmasSnow(canvasId, imagePath) {
    var snowElement = document.getElementById(canvasId);
    this.canvasContext = snowElement.getContext("2d");
    
    this.width = snowElement.clientWidth;
    this.heigth = snowElement.clientHeight;
    
    this.image = initImage(imagePath);
    this.snow = initSnow(this.width, this.heigth);
}

function initImage(imagePath) {
    var image = new Image();
    image.src = imagePath;
    return image;
}

function initSnow(width, height) {
    var minRasius = 3,
        maxRadius = 10,
        minSpeedY = 1,
        maxSpeedY = 3,
        speedX = 0.05,
        minAlpha = 0.5,
        maxAlpha = 1.0,
        minMoveX = 4,
        maxMoveX = 18;
    var snowSettings = new SnowSettings(minRasius, maxRadius, width, height, minSpeedY, maxSpeedY, speedX, minAlpha, maxAlpha, minMoveX, maxMoveX);
    
    var snow = [];
    var snowNumber = 200;
    for(var i = 0; i < snowNumber; ++i) {
        snow[i] = new Snow(snowSettings);
    }
    
    return snow;
}

ChristmasSnow.prototype.render = function() {
    // render background image
    this.canvasContext.drawImage(this.image, 0, 0);
    
    // render snow
    for(var i = 0; i < this.snow.length; ++i) {
        this.snow[i].render(this.canvasContext);
    }
}

ChristmasSnow.prototype.update = function() {
    for(var i = 0; i < this.snow.length; ++i) {
        this.snow[i].update();
    }
}

function SnowSettings(minRadius, maxRadius, maxX, maxY, minSpeedY, maxSpeedY, speedX, minAlpha, maxAlpha, minMoveX, maxMoveX) {
    this.minRadius = minRadius;
    this.maxRadius = maxRadius;
    this.maxX = maxX;
    this.maxY = maxY;
    this.speedX = speedX;
    this.minSpeedY = minSpeedY;
    this.maxSpeedY = maxSpeedY;
    this.minAlpha = minAlpha;
    this.maxAlpha = maxAlpha;
    this.minMoveX = minMoveX;
    this.maxMoveX = maxMoveX;
}

function Snow(snowSettings) {
    this.snowSettings = snowSettings;
    
    this.radius = randomInRange(snowSettings.minRadius, snowSettings.maxRadius);
    this.initialX = Math.random() * snowSettings.maxX;
    this.y = Math.random() * snowSettings.maxY;
    this.speedY = randomInRange(snowSettings.minSpeedY, snowSettings.maxSpeedY);
    this.speedX = snowSettings.speedX;
    this.alpha = randomInRange(snowSettings.minAlpha, snowSettings.maxAlpha);
    this.angle = Math.random(Math.PI * 2);
    this.x = this.initialX + Math.sin(this.angle);
    this.moveX = randomInRange(snowSettings.minMoveX, snowSettings.maxMoveX);
}

Snow.prototype.render = function(canvasContext) {
    canvasContext.fillStyle = "rgba(255, 255, 255, " + this.alpha + ")";
    canvasContext.beginPath();
    canvasContext.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true);
    canvasContext.closePath();
    canvasContext.fill(); 
}

Snow.prototype.update = function() {
    this.y += this.speedY;
    if (this.y > this.snowSettings.maxY) {
        this.y -= this.snowSettings.maxY;
    }
    
    this.angle += this.speedX;
    if (this.angle > Math.PI * 2) {
        this.angle -= Math.PI * 2;
    }

    this.x = this.initialX + this.moveX * Math.sin(this.angle);
}

function randomInRange(min, max) {
    var random = Math.random() * (max - min) + min;
    return random;
}

如果您感兴趣,可以到如下链接下载源代码:

http://download.csdn.net/detail/haitaohe/6757563