Recently, I want to create a web page and put some demos I made during my HTML5 learning process into a collection. However, if I just made a web page and sorted all the demos one by one, it would be too ugly. I thought that since I learned canvas, I would like to make a small opening animation on the browser.
After thinking about the effect of the opening animation for a while, I decided to use particles because they thought they were quite fun. I still remember the first technical blog post I wrote about particle-based text images: particle-based text images. At that time, I only made linear motion and added 3D effects by the way. The formula is simple. So I want to make the opening animation more dynamic.
First DEMO: http://2.axescanvas.sinaapp.com/demoHome/index.html
Is the effect more dynamic than linear motion? And it's really easy. Don't forget the questions in this blog post. It's a great pleasure to have a little bit of formulas. To make such an effect, we only use junior high school .. Or the formula of physical knowledge, acceleration, and deceleration in high school. So it is indeed a small formula. The landlord is very fond of tossing some cool things. Although he may not be able to work at ordinary times, this pleasure is really fascinating. In addition, doing so can also enhance the Thinking Ability of programming.
Let's talk about theme. Let's briefly explain the principles ~~~
The core code of particle motion is as follows:
- update:function(time){
- this.x += this.vx*time;
- this.y += this.vy*time;
-
- if(!this.globleDown&&this.y>0){
- var yc = this.toy - this.y;
- var xc = this.tox - this.x;
-
- this.jl = Math.sqrt(xc*xc+yc*yc);
-
- var za = 20;
-
- var ax = za*(xc/this.jl),
- ay = za*(yc/this.jl),
- vx = (this.vx+ax*time)*0.97,
- vy = (this.vy+ay*time)*0.97;
-
- this.vx = vx;
- this.vy = vy;
-
- }else {
- var gravity = 9.8;
- var vy = this.vy+gravity*time;
-
- if(this.y>canvas.height){
- vy = -vy*0.7;
- }
-
- this.vy = vy;
- }
- },
There are two States in total for particles: free falling and suction. Let's leave it empty. Paste the attributes of the particle before the suction:
- var Dot = function(x,y,vx,vy,tox,toy,color){
- this.x=x;
- this.y=y;
- this.vx=vx;
- this.vy=vy;
- this.nextox = tox;
- this.nextoy = toy;
- this.color = color;
- this.visible = true;
- this.globleDown = false;
- this.setEnd(tox , toy);
- }
-
- setEnd:function(tox , toy){
- this.tox = tox;
- this.toy = toy;
- var yc = this.toy - this.y;
- var xc = this.tox - this.x;
- },
X, y is the position of the particle, vx is the horizontal velocity of the particle, vy is the vertical velocity of the particle, and nexttox doesn't matter if you know it, just save the variable temporarily. Tox and toy are the destination positions of the particles.
First, give all particles a destination, which will be explained below. That is, the place where the particle will arrive, and then define a variable za as the acceleration. If the specific value is used, the approximate parameter will be displayed in multiple tests. I set it to 20, it's almost the same. Za is the acceleration of the link between the particle and the destination. Therefore, we can obtain the horizontal acceleration and vertical acceleration of the particle through the position of the particle and the location of the destination through a simple trigonometric function, this section
- var ax = za*(xc/this.jl),
- ay = za*(yc/this.jl),
With the horizontal acceleration and vertical acceleration, it is simpler to directly calculate the increment of the horizontal speed and vertical speed, thus changing the value of the horizontal speed and vertical speed.
- vx = (this.vx+ax*time)*0.97,
- vy = (this.vy+ay*time)*0.97;
The particle slows down only when 0.97 is used to simulate energy loss. Time is the time difference between each frame.
After calculating the speed, update the particle position.
- this.x += this.vx*time;
- this.y += this.vy*time;
Because the direction of the link between the particle and the destination is constantly changing during the flight, the horizontal acceleration and vertical acceleration of the particle must be re-calculated for each frame.
That's how motion works. Is it easy.
After the motion principle is completed, let's take a look at the specific implementation of the animation above: Initialize the animation and draw the desired word or image on an off-screen canvas, then, use the getImageData method to obtain the pixels of the canvas. Then, use a loop to find out the areas drawn from the canvas, because the data value in imageData is an rgba array, therefore, we determine that the last value, that is, the transparency is greater than 128, that is, the area that has been drawn. Then obtain the xy value of the region. To prevent page freezing caused by too many particle objects, we will limit the number of particles. The x and y values increase by 2 each time when pixels are taken, this reduces the number of particles.
- This. osCanvas = document. createElement ("canvas ");
- Var osCtx = this. osCanvas. getContext ("2d ");
-
- This. osCanvas. width = 1000;
- This. osCanvas. height = 150;
-
- OsCtx. textAlign = "center ";
- OsCtx. textBaseline = "middle ";
- OsCtx. font = "70px, bold ";
- OsCtx. fillStyle = "# 1D181F"
- OsCtx. fillText ("WelCome", this. osCanvas. width/2, this. osCanvas. height/2-40 );
- OsCtx. fillText ("To wAxes 'home", this. osCanvas. width/2, this. osCanvas. height/2 + 40 );
- Var bigImageData = osCtx. getImageData (0, 0, this. osCanvas. width, this. osCanvas. height );
-
- Dots = [];
-
- For (var x = 0; x <bigImageData. width; x + = 2 ){
- For (var y = 0; y <bigImageData. height; y + = 2 ){
- Var I = (y * bigImageData. width + x) * 4;
- If (bigImageData. data [I + 3]> 128 ){
- Var dot = new Dot (
- Math. random ()> 0.5? Math. random () * 20 + 10: Math. random () * 20 + canvas. width-40,
- -Math. random () * canvas. height * 2,
- 0,
- 0,
- X + (canvas. width/2-this.osCanvas.width/2 ),
- Y + (canvas. height/2-this.osCanvas.height/2 ),
- "Rgba (" + bigImageData. data [I] + "," + bigImageData. data [I + 1] + "," + bigImageData. data [I + 2] + ", 1 )"
- );
- Dot. setEnd (canvas. width/2, canvas. height/2)
- Dots. push (dot );
- }
- }
- }
After obtaining the position xy value of the particle through the loop, the position is assigned to the particle to become the destination of the particle. Then, the animation starts to make the text image particle effect.