Canvas Study Notes: a little bit of formula, great pleasure (1)

Source: Internet
Author: User

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:

 
 
  1. update:function(time){  
  2.             this.x += this.vx*time;  
  3.             this.y += this.vy*time;  
  4.  
  5.             if(!this.globleDown&&this.y>0){  
  6.                 var yc = this.toy - this.y;  
  7.                 var xc = this.tox - this.x;  
  8.  
  9.                 this.jl = Math.sqrt(xc*xc+yc*yc);  
  10.  
  11.                 var za = 20;  
  12.  
  13.                 var ax = za*(xc/this.jl),  
  14.                     ay = za*(yc/this.jl),  
  15.                     vx = (this.vx+ax*time)*0.97,  
  16.                     vy = (this.vy+ay*time)*0.97;  
  17.  
  18.                 this.vx = vx;  
  19.                 this.vy = vy;  
  20.  
  21.             }else {  
  22.                 var gravity = 9.8;  
  23.                 var vy = this.vy+gravity*time;  
  24.  
  25.                 if(this.y>canvas.height){  
  26.                     vy = -vy*0.7;  
  27.                 }  
  28.  
  29.                 this.vy = vy;  
  30.             }  
  31.         }, 

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:

 
 
  1. var Dot = function(x,y,vx,vy,tox,toy,color){  
  2.         this.x=x;  
  3.         this.y=y;  
  4.         this.vx=vx;  
  5.         this.vy=vy;  
  6.         this.nextox = tox;  
  7.         this.nextoy = toy;  
  8.         this.color = color;  
  9.         this.visible = true;  
  10.         this.globleDown = false;  
  11.         this.setEnd(tox , toy);  
  12.     }  
  13.  
  14. setEnd:function(tox , toy){  
  15.             this.tox = tox;  
  16.             this.toy = toy;  
  17.             var yc = this.toy - this.y;  
  18.             var xc = this.tox - this.x;  
  19.         }, 

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

 
 
  1. var ax = za*(xc/this.jl),  
  2.  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.

 
 
  1. vx = (this.vx+ax*time)*0.97,  
  2. 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.

 
 
  1. this.x += this.vx*time;  
  2. 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.

 
 
  1. This. osCanvas = document. createElement ("canvas ");
  2. Var osCtx = this. osCanvas. getContext ("2d ");
  3.  
  4. This. osCanvas. width = 1000;
  5. This. osCanvas. height = 150;
  6.  
  7. OsCtx. textAlign = "center ";
  8. OsCtx. textBaseline = "middle ";
  9. OsCtx. font = "70px, bold ";
  10. OsCtx. fillStyle = "# 1D181F"
  11. OsCtx. fillText ("WelCome", this. osCanvas. width/2, this. osCanvas. height/2-40 );
  12. OsCtx. fillText ("To wAxes 'home", this. osCanvas. width/2, this. osCanvas. height/2 + 40 );
  13. Var bigImageData = osCtx. getImageData (0, 0, this. osCanvas. width, this. osCanvas. height );
  14.  
  15. Dots = [];
  16.  
  17. For (var x = 0; x <bigImageData. width; x + = 2 ){
  18. For (var y = 0; y <bigImageData. height; y + = 2 ){
  19. Var I = (y * bigImageData. width + x) * 4;
  20. If (bigImageData. data [I + 3]> 128 ){
  21. Var dot = new Dot (
  22. Math. random ()> 0.5? Math. random () * 20 + 10: Math. random () * 20 + canvas. width-40,
  23. -Math. random () * canvas. height * 2,
  24. 0,
  25. 0,
  26. X + (canvas. width/2-this.osCanvas.width/2 ),
  27. Y + (canvas. height/2-this.osCanvas.height/2 ),
  28. "Rgba (" + bigImageData. data [I] + "," + bigImageData. data [I + 1] + "," + bigImageData. data [I + 2] + ", 1 )"
  29. );
  30. Dot. setEnd (canvas. width/2, canvas. height/2)
  31. Dots. push (dot );
  32. }
  33. }
  34. }

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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.