使用JavaScript和Canvas實現下雪動畫效果

來源:互聯網
上載者:User

使用JavaScript和Canvas實現下雪動畫效果

該下雪動畫效果使用了HTML5中Canvas畫布實現,其中涉及了物理學中曲線運動的相關知識與運算。

index.html

<script src=js/snow.js></script><script> window.addEventListener('load', function(){ this.snow = new Snow(); // 初始化snow對象並開始下雪動畫 snow.init().start(); }); </script>

main.css

html, body{    width: 100%;    height: 100%;    overflow: hidden;    margin: 0;    padding: 0;    background-color: #000;    font-family: 微軟雅黑, 華文細黑, 黑體;}

Snow.js

(function(exports, undefined){    'use strict';    var document = exports.document;    function Snow(){        this.colors = ['#fff'];        this.balls = [];        this.windDirection = -1;        this.ballRadius = 3;        this.ballsPerFrame = 2;        this.timeInterval = 40;        this.windDirectionChangedInterval = 5000;        this.accumulativeTime = 0;        return this;    };    exports.Snow = Snow;    Snow.prototype = {        init: function(args){            for(var p in args){                this[p] = args[p];            }            this.canvas = this.canvas || document.querySelector('#canvas');            this.context = this.context || this.canvas.getContext('2d');            this.canvasWidth = this.canvasWidth || document.body.offsetWidth || document.body.clientWidth;            this.canvasHeight = this.canvasHeight || document.body.offsetHeight || document.body.clientHeight;            this.canvas.width = this.canvasWidth;            this.canvas.height = this.canvasHeight;            return this;        },        start: function(){            this.timer = this.timer || setTimeout(this.frame.bind(this), this.timeInterval);            return this;        },        frame: function(){            this.accumulativeTime += this.timeInterval;            (this.accumulativeTime % this.windDirectionChangedInterval < this.timeInterval) && (this.windDirection *= -1);            this.render.call(this);            this.update.call(this);            this.timer = null;            this.timer = setTimeout(this.frame.bind(this), this.timeInterval);        },        update: function(){            this.addBalls.call(this);            this.updateBalls.call(this);        },        updateBalls: function(){            var balls = this.balls,                len = balls.length,                i = 0,                cnt = 0;            for(;icnt){                balls.pop();                len--;            }        },        addBalls: function(){            var ball,                i = 0,                len = this.ballsPerFrame,                _this = this;            for(;i

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.