Recently, I want to make a web page and put some of the DEMOs I made during the process of learning HTML5 to make a collection. However, it would be too ugly to just make a web page and arrange all the DEMOs one by one. I just thought, since I have learned canvas, let’s play around with the browser and make a small opening animation.
After thinking about the effect of the opening animation for a while, I decided to use particles because I think particles are more fun. I still remember that the first technical blog post I wrote was about particleizing text and pictures: Particleizing text and pictures. At that time, I only did linear motion, and added a little 3D effect. The exercise formula is simple. So I just wanted to make this opening animation more dynamic.
Go to DEMO first: http://2.axescanvas.sinaapp.com/demoHome/index.html
Is the effect more dynamic than linear motion? And it’s really simple, don’t forget the title of this blog post, a little formula, a big fun. To achieve such an effect, all we need is our junior high school. . Or the physics knowledge in high school, the formulas of accelerated motion and decelerated motion. So it's really the little drop formula. The original poster likes to mess around with some cool things. Although he may not use them at work, the fun is really fascinating. Moreover, doing this can also strengthen your programming thinking skills.
Without further ado, let’s get to the topic. Let me briefly explain the principle~~~
The core code of particle motion is just this:
x and y are the positions of the particles, vx is the horizontal velocity of the particles, and vy is the vertical velocity of the particles. It doesn’t matter whether you know nexttox or the like, they are just temporary variables. tox, and toy are the destination locations of the particles.
First, give all particles a destination, which will be discussed below. That is to say, you want the particle to reach the place, and then define a variable za as the acceleration. If you want to know the specific value, you will get the approximate parameters through more tests. I set it to 20, and it feels about the same. za is the acceleration of the line between the particle and the destination. Therefore, we can calculate the horizontal acceleration and vertical acceleration of the particle through the position of the particle and the position of the destination through simple trigonometric functions. This paragraph
After having horizontal acceleration and vertical acceleration, the next step is even simpler. Calculate the increment of horizontal speed and vertical speed directly, thereby changing the values of horizontal speed and vertical speed
The reason why it is multiplied by 0.97 is to simulate energy loss so that the particles will slow down. time is the time difference between each frame
After calculating the velocity, just update the particle position.
Because the direction of the connection between the particle and the destination is constantly changing during flight, the horizontal acceleration and vertical acceleration of the particle must be recalculated every frame.
This is the principle of movement, isn’t it very simple?
Now that we have talked about the principle of motion, let’s talk about the specific implementation of the above animation: animation initialization, draw the desired words or pictures on an off-screen canvas, and then obtain the pixels of the off-screen canvas through the getImageData method. . Then use a loop to find the drawn area in the off-screen canvas. Because the data value in imageData is an rgba array, we judge that the last value, that is, the transparency is greater than 128, means that the area has been drawn. Then get the xy value of the area. In order to prevent too many particle objects from causing page lag, we limit the number of particles. When taking pixels, the x value and y value increase by 2 each time, thereby reducing the number of particles.
通过循环获取到粒子的位置xy值后,把位置赋给粒子,成为粒子的目的地。然后动画开始,就可以做出文字图片粒子化的效果了。