


HTML5 canvas realizes the special effect of falling snowflakes_html5 tutorial skills
I have seen many displays on the Internet showing the effect of html5 snowflakes flying, which is indeed very fascinating. I believe that everyone is as excited as me and is also very curious and wants to study how to implement the code; although these can be downloaded from many places Source code, but I don’t know other people’s ideas and analysis of difficulties when making such animations.
I just happened to study a lot these days, and I am taking advantage of the time now to dissect it step by step from demand analysis, knowledge points, and programming. If I try to make a fool of myself in front of you, please don’t laugh.
The final rendering is as follows:
Picture 1
1. Demand analysis
1. Round snowflake
The snowflake shape in this example uses a circle
2. The number of snowflakes is fixed
Carefully observe the number of white snowflakes according to Figure 1. During the falling process, the number of snowflakes in the entire picture should be fixed. This requirement needs to be obtained through our observation and analysis. This is consistent with the scene we see in real life where snowflakes are flying all over the sky.
3. The size of snowflakes is inconsistent
Each snowflake has a different size, which means the radius of the snowflake is random. This is consistent with the scene where we see snowflakes flying all over the sky in real life.
4. The position of the snowflakes is moving
Snowflakes are falling, and naturally their positions are also moving.
2. Knowledge points
1. Use Html5 Canvas JavaScript to draw a circle - forming a circular snowflake
In Html5, you need to use Canvas and use JavaScript to draw a circle to form a circular snowflake - arc(x, y, r, start, stop);
2. Random numbers—generate circular snowflakes with different radii and coordinates
In this example, when the web page is loaded for the first time, a certain number of snowflakes with different radii and positions need to be generated, so the radius and coordinates are random numbers; while the snowflakes are falling, their radius remains unchanged and the coordinates are within a certain range. changes, so the coordinates are also random numbers at this time——Math.random()
3. Programming
1. Preparation
Put a canvas and set the background color of the entire body to black
HTML code:
- <canvas id="mycanvas" >
- Your browser does not support canvas
- canvas>
CSS code:
- * {
- margin: 0;
- padding: 0;
- }
- #mycanvas {
- background: black; }
Note: canvas has an initialized height and width by default, so you don’t have to worry about it
The JavaScript code is as follows:
- //Get mycanvas canvas
- var can = document.getElementById("mycanvas");
- var ctx = can.getContext("2d");
- //Canvas width
- var wid = window.innerWidth;
- //Canvas height
- var hei = window.innerHeight;
- can.width=wid;
- can.height=hei;
The effect at this time is as follows:
3. Initialization generates a fixed number of snowflakes
According to our above demand analysis and interpretation of knowledge points, first of all, the number of snowflakes is fixed, so we need to define a variable var snow = 100; here it is assumed that the number of snowflakes is 100;
When generating snowflakes, the radius and position of each snowflake are different. We regard each snowflake as an object, then the properties of this object include: radius, coordinates (X, Y), then a snowflake object can be written as var snowOject={x:1,y:10,r:5}, here represents a circular snowflake with coordinates (1,10) and radius 5; in this example, since the radius and coordinates are random numbers, Math is used .random() generates radius and coordinates (X, Y) for 100 snowflakes respectively;
Then we have 100 snowflakes here, so in order to facilitate subsequent operations, we use an array to save these 100 snowflake objects.
The JavaScript code is as follows:
- //Number of snowflakes
- var snow = 100;
- //Snowflake coordinates, radius
- var arr = []; //Save the coordinates and radius of each circle
- for (var i = 0; i < snow; i ) {
- arr.push({
- x: Math.random() * wid,
- y: Math.random() * hei,
- r: Math.random() * 10 1
- })
- }
4. Draw snowflakes
We have generated 100 snowflake radii and coordinates (X, Y) above. The following is to use canvas to draw snowflakes (here, we draw circles). Here we define a function
The JavaScript code is as follows:
- //Drawing snowflakes
- function DrawSnow() {
- ctx.fillStyle="white";
- ctx.beginPath();
- for (var i = 0; i < snow; i ) {
- var p = arr[i];
- ctx.moveTo(p.x,p.y);
- ctx.arc(p.x,p.y,p.r,0,2*Math.PI,false);
- }
- ctx.fill();
- ctx.closePath();
Then call the DrawSnow() function, the effect is as follows:
You can try to refresh the webpage multiple times to see if snowflakes of different sizes and positions will be generated (normally it is possible). Once you do this, you are close to the final effect
Note: Since 100 circles need to be drawn here, the drawing start coordinates are redefined every time a circle is drawn: ctx.moveTo(p.x,p.y); otherwise strange effects will occur. If you don’t believe it, try it
5. Snowflakes fluttering
We have drawn 100 snowflakes above. Unfortunately, we can only see the changing effect by refreshing the web page, but what we need to achieve is that the snowflakes keep moving.
First we need to use the setInterval function to continuously redraw the snowflakes. The interval here is 50 milliseconds: setInterval(DrawSnow,50);
At the same time, the coordinates (X, Y) of each snowflake need to be constantly changed (within a certain range). The snowflakes here fall from the upper left to the lower right, so the X and Y coordinate values of each snowflake are in Keep increasing, then we use a function SnowFall() to define the snowflakes falling rules
The function code is as follows:
- //雪花飘落
- function SnowFall() {
- for (var i = 0; i < snow; i ) {
- var p = arr[i];
- p.y = Math.random() * 2 1;
- if (p.y > hei) {
- p.y = 0;
- }
- p.x = Math.random() * 2 1;
- if (p.x > wid) {
- p.x = 0;
- "white-space:pre"> }
- }
- }
然后将该函数放入DrawSnow()执行,注意:我们每隔50毫毛重画雪花,必须擦除画布,所以DrawSnow()函数体内必须在前面执行clearRect()函数,即:ctx.clearRect(0, 0, wid, hei);
此时DrawSnow函数定义如下:
- //画雪花
- function DrawSnow() {
- ctx.clearRect(0, 0, wid, hei);
- ctx.fillStyle = "white";
- ctx.beginPath();
- for (var i = 0; i < snow; i ) {
- var p = arr[i];
- ctx.moveTo(p.x, p.y);
- ctx.arc(p.x, p.y, p.r, 0, 2 * Math.PI, false);
- }
- ctx.fill();
- SnowFall();
- ctx.closePath();
- }
最后执行setInterval(DrawSnow, 50);
OK,经过我们上述步骤,小伙伴们是否已经对整个过程及技术实现很清晰了。
完整代码如下(大家可以直接复制到自己项目中执行,测试下效果):
- >
- <html>
- <head>
- <meta charset="utf-8" />
- <title>title>
- <script src="js/jquery-1.8.3.min.js">script>
- <style type="text/css">
- * {
- margin: 0;
- padding: 0;
- }
- #mycanvas {
- background: black;
- }
- style>
- head>
- <body>
- <canvas id="mycanvas" >
- Your browser does not support canvas
- canvas>
- <script>
- //Get mycanvas canvas
- var can = document.getElementById("mycanvas");
- var ctx = can.getContext("2d");
- //Canvas width
- var wid = window.innerWidth;
- //Canvas height
- var hei = window.innerHeight;
- can.width = wid;
- can.height = hei;
- //Number of snowflakes
- var snow = 100;
- //Snowflake coordinates, radius
- var arr = []; //Save the coordinates and radius of each circle
- for (var i = 0; i < snow; i ) {
- arr.push({
- x: Math.random() * wid,
- y: Math.random() * hei,
- r: Math.random() * 10 1
- })
- }
- //画雪花
- function DrawSnow() {
- ctx.clearRect(0, 0, wid, hei);
- ctx.fillStyle = "white";
- ctx.beginPath();
- for (var i = 0; i < snow; i ) {
- var p = arr[i];
- ctx.moveTo(p.x, p.y);
- ctx.arc(p.x, p.y, p.r, 0, 2 * Math.PI, false);
- }
- ctx.fill();
- SnowFall();
- ctx.closePath();
- }
- //雪꽃飘落
- 기능 SnowFall() {
- for (var i = 0; i < 눈; i ) {
- var p = arr[i];
- p.y = Math.random() * 2 1;
- if (p.y > hei) {
- p.y = 0;
- }
- p.x = Math.random() * 2 1;
- if (p.x > wid) {
- p.x = 0;
- }
- }
- }
- setInterval(DrawSnow, 50);
- 스크립트>
- 본체>
- html>
好了,今天分享就到这里,希望对大家的文习有所帮助。
원문:http://www.cnblogs.com/tangyifeng/p/5253629.html

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.
