Web Front-end
H5 Tutorial
Tutorial on using Canvas in HTML5 combined with formulas to draw particle motion_html5 tutorial skillsTutorial on using Canvas in HTML5 combined with formulas to draw particle motion_html5 tutorial skills
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:
- 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;
- },
- 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){
- .y; .x;
- },
-
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
JavaScript CodeCopy content to clipboard- var ax = za*(xc/this.jl),
- ay = za*(yc/this.jl),
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
XML/HTML CodeCopy content to clipboard- vx = (this.vx ax*time)*0.97,
- vy = (this.vy ay*time)*0.97;
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.
XML/HTML CodeCopy content to clipboard- this.x = this.vx*time;
- this.y = this.vy*time;
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.
XML/HTML CodeCopy content to clipboard- 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;xbigImageData.width;x =2){
- for(var y=0;ybigImageData.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);
- }
- }
- }
通过循环获取到粒子的位置xy值后,把位置赋给粒子,成为粒子的目的地。然后动画开始,就可以做出文字图片粒子化的效果了。
H5: How It Enhances User Experience on the WebApr 19, 2025 am 12:08 AMH5 improves web user experience with multimedia support, offline storage and performance optimization. 1) Multimedia support: H5 and elements simplify development and improve user experience. 2) Offline storage: WebStorage and IndexedDB allow offline use to improve the experience. 3) Performance optimization: WebWorkers and elements optimize performance to reduce bandwidth consumption.
Deconstructing H5 Code: Tags, Elements, and AttributesApr 18, 2025 am 12:06 AMHTML5 code consists of tags, elements and attributes: 1. The tag defines the content type and is surrounded by angle brackets, such as. 2. Elements are composed of start tags, contents and end tags, such as contents. 3. Attributes define key-value pairs in the start tag, enhance functions, such as. These are the basic units for building web structure.
Understanding H5 Code: The Fundamentals of HTML5Apr 17, 2025 am 12:08 AMHTML5 is a key technology for building modern web pages, providing many new elements and features. 1. HTML5 introduces semantic elements such as, , etc., which enhances web page structure and SEO. 2. Support multimedia elements and embed media without plug-ins. 3. Forms enhance new input types and verification properties, simplifying the verification process. 4. Offer offline and local storage functions to improve web page performance and user experience.
H5 Code: Best Practices for Web DevelopersApr 16, 2025 am 12:14 AMBest practices for H5 code include: 1. Use correct DOCTYPE declarations and character encoding; 2. Use semantic tags; 3. Reduce HTTP requests; 4. Use asynchronous loading; 5. Optimize images. These practices can improve the efficiency, maintainability and user experience of web pages.
H5: The Evolution of Web Standards and TechnologiesApr 15, 2025 am 12:12 AMWeb standards and technologies have evolved from HTML4, CSS2 and simple JavaScript to date and have undergone significant developments. 1) HTML5 introduces APIs such as Canvas and WebStorage, which enhances the complexity and interactivity of web applications. 2) CSS3 adds animation and transition functions to make the page more effective. 3) JavaScript improves development efficiency and code readability through modern syntax of Node.js and ES6, such as arrow functions and classes. These changes have promoted the development of performance optimization and best practices of web applications.
Is H5 a Shorthand for HTML5? Exploring the DetailsApr 14, 2025 am 12:05 AMH5 is not just the abbreviation of HTML5, it represents a wider modern web development technology ecosystem: 1. H5 includes HTML5, CSS3, JavaScript and related APIs and technologies; 2. It provides a richer, interactive and smooth user experience, and can run seamlessly on multiple devices; 3. Using the H5 technology stack, you can create responsive web pages and complex interactive functions.
H5 and HTML5: Commonly Used Terms in Web DevelopmentApr 13, 2025 am 12:01 AMH5 and HTML5 refer to the same thing, namely HTML5. HTML5 is the fifth version of HTML, bringing new features such as semantic tags, multimedia support, canvas and graphics, offline storage and local storage, improving the expressiveness and interactivity of web pages.
What Does H5 Refer To? Exploring the ContextApr 12, 2025 am 12:03 AMH5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use

Atom editor mac version download
The most popular open source editor

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





