用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - javascript代码库

js下雪效果

2013-01-31 作者: 小蜜锋举报

[javascript]代码库

<!DOCTYPE HTML>
 
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<title>雪花</title>
<style type="text/css">
* {
    margin:0px;
    padding:0px;
}
body, html {
    width:100%;
    height:100%;
    overflow:hidden;
}
</style>
</head>
 
<body>
<canvas id="mycanvas"></canvas>
<script>
        var myCanvas = document.getElementById('mycanvas');
        var cxt = myCanvas.getContext('2d');
        var snowcount = 30; /// 显示数量
        var index = 0;
 
        imageList = {}; /// 图片列表
        imageSource = ['bg.jpg', 'snow3.png']; /// 图片源文件类表
        var snowlist = [];
 
 
        /// 初始化
        function init() {
            /// 画布
            myCanvas.width = document.body.offsetWidth;
            myCanvas.height = document.body.offsetHeight;
            // 加载图片
            loadImages();
        }
         
        /// 加载图片
        function loadImages() {
            for (var i in imageSource) {
                imageList[i] = new Image();
                imageList[i].src = imageSource[i];
                 
                imageList[i].onload = function () {
                    index++;
                    if (imageSource.length >= index++) {
                        callImages();
                    }
                }
            }
        }
         
        function callImages() {
            for (var i = 0; i < snowcount; i++) {
                snowlist.push(new snow());
            }
             
            setInterval(function () {
                cxt.clearRect(0, 0, myCanvas.width, myCanvas.height);
                fillBackground();
                 
                for (var i in snowlist) {
                    snowlist[i].Go();
 
                    if (snowlist[i].Y >= myCanvas.height) {
                        snowlist[i] = new snow();
                    }
                }
            }, 1);
        }
         
        function fillBackground() {
            cxt.drawImage(imageList[0], 0, 0, imageList[0].width, imageList[0].height, 0,0, myCanvas.width, myCanvas.height);
        }
 
        /// 雪
        function snow()
        {
            this.X = parseInt(Math.random() * myCanvas.width);
            this.Y = 0;
            this.Size = parseInt(Math.random() * 71);
            this.Transparency = 10;
            this.Speed = parseInt(Math.random()*(1-10+1)+10);
        }
 
        /// 动起来
        snow.prototype.Go = function () {
            this.Y  = this.Y + this.Speed;
            cxt.drawImage(imageList[1], 0, 0, imageList[1].width, imageList[1].height, this.X, this.Y , imageList[1].width - this.Size , imageList[1].height - this.Size);
        }
 
        init();
    </script>
</body>
</html>

[代码运行效果截图]


js下雪效果

[源代码打包下载]




网友评论    (发表评论)

共4 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...