The snow animation effect is implemented using canvas canvas in HTML5, which involves knowledge and computation of curve motion in physics.
Index.html
<! DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0, User-scalable=no " name=" Viewport "> <meta content="yes" name="Apple-mobile-web-app-capable" > <meta content="Black" name=" Apple-mobile-web-app-status-bar-style "> <meta content="Telephone=no" name="Format-detection"> <meta content="Email=no" name="Format-detection"> <title>Snow</title> <link rel="stylesheet" href="Css/main.css"> </head><body> <canvas id="Canvas"></Canvas> <script src="Js/snow.js"></script> <script>window.addeventlistener (' load ', function(){this. Snow = new
Snow (); //Initialize the Snow object and start the Snow animation snow.init (). Start (); }); </script></body></html>
Main.css
HTML,Body{ width: % ; height: % ; Overflow: hidden ; margin: 0 ; padding: 0 ; background-color: #000 ; font-family: Microsoft Black, Chinese fine black, blackbody ;}
Snow.js
( function(exports, undefined){' use strict ';varDocument = Exports.document; function Snow(){ This. colors = [' #fff ']; This. balls = []; This. winddirection =-1; This. Ballradius =3; This. Ballsperframe =2; This. TimeInterval = +; This. Winddirectionchangedinterval = the; This. Accumulativetime =0;return This; }; Exports. Snow = snow; Snow.prototype = {init: function(args){ for(varPinchargs) { 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(){ varBalls = This. Balls, Len = balls.length, i =0, cnt =0; for(; i<len;i++) {balls[i].x + = BALLS[I].VX * This. winddirection; Balls[i].y + = Balls[i].vy; Balls[i].vy + = BALLS[I].G * BALLS[I].T; balls[i].t + = This. timeinterval;if(Balls[i].y- This. Ballradius < This. canvasheight) {balls[cnt++] = balls[i]; } } while(len>cnt) {Balls.pop (); len--; }}, Addballs: function(){ varBall, i =0, Len = This. ballsperframe, _this = This; for(; i<len;i++) {ball = {x:Math. POW (-1,Math. Ceil (Math. Random () * +)) *Math. Floor (Math. Random () * _this.canvaswidth *1.5), Y:Math. Floor (Math. Random () * This. Ballradius) *-1G:0.00005, VX:1+Math. Floor (Math. Random () *2), VY:2+Math. Floor (Math. Random () *5), T:0, Color: _this.colors[Math. Floor (Math. Random () * _this.colors.length)]} This. Balls.push (ball); }}, Render: function(){ varCXT = This. Context, I =0, Len = This. balls.length; Cxt.clearrect (0,0, This. Canvaswidth, This. canvasheight); for(; i<len;i++) {Cxt.fillstyle = This. Balls[i].color; Cxt.beginpath (); Cxt.arc ( This. balls[i].x, This. BALLS[I].Y, This. Ballradius,0,2*Math. Pitrue); Cxt.closepath (); Cxt.fill (); }}, pause: function(){Cleartimeout ( This. timer); This. Timer =NULL; }, Resume: function(){ This. Start.call ( This); }, clear: function(){Cleartimeout ( This. timer); This. Timer =NULL; This. Context.clearrect (0,0, This. Canvaswidth, This. canvasheight); }}) (window);
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Use JavaScript and canvas for snow-animated effects