Easing effect implementation program in javascript_javascript skills
There are four common types of animations, let’s introduce them:
linear: linear animation, that is, constant speed
easeIn: speed from small to large, that is, fade in
easeOut: speed from large to small, that is, fade out
easeInOut: The speed is from small to large at the beginning, and the speed is from large to small at the end, that is, fade in and fade out
In fact, when it comes to easing, we have to mention Robert Penner, who invented the N-multiple easing formula. For example,
Let me explain it:
Suppose the current change amount is X, then
t / d = X / c, so X = c * t / d, and then Let’s look at a slightly more complicated one:
This has a fade-in effect, which means that when the animation starts, the value changes from small to large.
You can find that the only difference between the two is t / d and (t /= d) * t. I just said that t / d is a ratio of >=0 && <=1, temporarily named a, And (t /= d) * t is equivalent to Math.pow(a, 2).
Why square it?
1. First of all, a >= Math.pow(a, 2) is certain
2. Every time the function is called, the ratio of t / d also becomes larger at a constant speed, for example The first call is 0.1 (square 0.01), the second call is 0.2 (square 0.04), etc. Then the 10th call must be 1, right. At this time c * 1 b, the animation ends here Ended
3. Point 2 proves that the smaller the ratio, the smaller the change in value, and the larger the ratio, the greater the change in value. If square is not used but cubed, the fade-in effect will be more obvious. .
The style, structure and public functions are as follows:
First, start with the simplest: set the start position, end position and step size, each time Increase the fixed value until the termination condition
function run() {
if((iLeft = parseInt(getStyle( drag,"left"))) < end){
drag.style.left = iLeft step "px"; 🎜> }
var timer = setInterval(run, 20);
The above method is constant speed, and the distance of each movement is fixed. Let’s look at another implementation method:
The code is as follows:
var timer = null;
var begin = 0, end = 400;
var drag = $("drag");
function run() {
if(( iLeft = parseInt(getStyle(drag,"left"))) < end){
var step = Math.ceil((400 - iLeft)/7);
drag.style.left = iLeft step " px";
>
The above method is to calculate the step length of the displacement based on the distance between the current position and the target.
There is a tween class in flash that specifically handles easing. The code converted into javascript is:
var Tween = {
Linear: function(t,b,c,d){ return c*t/d b; },
Quad: {
easeIn: function(t,b,c,d){
return c*(t/=d)*t b;
},
easeOut: function(t,b,c,d){
return -c *(t/=d)*(t-2) b;
},
easeInOut: function(t,b,c,d){
if ((t/=d/2) < 1) return c/2*t*t b;
return -c/2 * ((--t)*(t-2) - 1) b;
}
},
Cubic: {
easeIn: function(t,b,c,d){
return c*(t/=d)*t*t b;
},
easeOut: function(t,b,c,d){
return c*((t=t/d-1)*t*t 1) b;
},
easeInOut: function(t,b,c,d){
if ((t/=d/2) < 1) return c/2*t*t*t b;
return c/2*((t-=2)*t*t 2) b;
}
},
Quart: {
easeIn: function(t,b,c,d){
return c*(t/=d)*t*t*t b;
},
easeOut: function(t,b,c,d){
return -c * ((t=t/d-1)*t*t*t - 1) b;
},
easeInOut: function(t,b,c,d){
if ((t/=d/2) < 1) return c/2*t*t*t*t b;
return -c/2 * ((t-=2)*t*t*t - 2) b;
}
},
Quint: {
easeIn: function(t,b,c,d){
return c*(t/=d)*t*t*t*t b;
},
easeOut: function(t,b,c,d){
return c*((t=t/d-1)*t*t*t*t 1) b;
},
easeInOut: function(t,b,c,d){
if ((t/=d/2) < 1) return c/2*t*t*t*t*t b;
return c/2*((t-=2)*t*t*t*t 2) b;
}
},
Sine: {
easeIn: function(t,b,c,d){
return -c * Math.cos(t/d * (Math.PI/2)) c b;
},
easeOut: function(t,b,c,d){
return c * Math.sin(t/d * (Math.PI/2)) b;
},
easeInOut: function(t,b,c,d){
return -c/2 * (Math.cos(Math.PI*t/d) - 1) b;
}
},
Expo: {
easeIn: function(t,b,c,d){
return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) b;
},
easeOut: function(t,b,c,d){
return (t==d) ? b c : c * (-Math.pow(2, -10 * t/d) 1) b;
},
easeInOut: function(t,b,c,d){
if (t==0) return b;
if (t==d) return b c;
if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) b;
return c/2 * (-Math.pow(2, -10 * --t) 2) b;
}
},
Circ: {
easeIn: function(t,b,c,d){
return -c * (Math.sqrt(1 - (t/=d)*t) - 1) b;
},
easeOut: function(t,b,c,d){
return c * Math.sqrt(1 - (t=t/d-1)*t) b;
},
easeInOut: function(t,b,c,d){
if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) b;
return c/2 * (Math.sqrt(1 - (t-=2)*t) 1) b;
}
},
Elastic: {
easeIn: function(t,b,c,d,a,p){
if (t==0) return b; if ((t/=d)==1) return b c; if (!p) p=d*.3;
if (!a || a < Math.abs(c)) { a=c; var s=p/4; }
else var s = p/(2*Math.PI) * Math.asin (c/a);
return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) b;
},
easeOut: function(t,b,c,d,a,p){
if (t==0) return b; if ((t/=d)==1) return b c; if (!p) p=d*.3;
if (!a || a < Math.abs(c)) { a=c; var s=p/4; }
else var s = p/(2*Math.PI) * Math.asin (c/a);
return (a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) c b);
},
easeInOut: function(t,b,c,d,a,p){
if (t==0) return b; if ((t/=d/2)==2) return b c; if (!p) p=d*(.3*1.5);
if (!a || a < Math.abs(c)) { a=c; var s=p/4; }
else var s = p/(2*Math.PI) * Math.asin (c/a);
if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) b;
return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 c b;
}
},
Back: {
easeIn: function(t,b,c,d,s){
if (s == undefined) s = 1.70158;
return c*(t/=d)*t*((s 1)*t - s) b;
},
easeOut: function(t,b,c,d,s){
if (s == undefined) s = 1.70158;
return c*((t=t/d-1)*t*((s 1)*t s) 1) b;
},
easeInOut: function(t,b,c,d,s){
if (s == undefined) s = 1.70158;
if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525)) 1)*t - s)) b;
return c/2*((t-=2)*t*(((s*=(1.525)) 1)*t s) 2) b;
}
},
Bounce: {
easeIn: function(t,b,c,d){
return c - Tween.Bounce.easeOut(d-t, 0, c, d) b;
},
easeOut: function(t,b,c,d){
if ((t/=d) < (1/2.75)) {
return c*(7.5625*t*t) b ;
} else if (t < (2/2.75)) {
return c*(7.5625*(t-=(1.5/2.75))*t .75) b;
} else if (t < (2.5/2.75)) {
return c*(7.5625*(t-=(2.25/2.75))*t .9375) b;
} else {
return c*( 7.5625*(t-=(2.625/2.75))*t .984375) b;
}
},
easeInOut: function(t,b,c,d){
if (t < d/2) return Tween.Bounce.easeIn(t*2, 0, c, d) * .5 b;
else return Tween.Bounce.easeOut(t*2-d, 0, c, d ) * .5 c*.5 b;
}
}
}
Each easing method corresponds to 3 types
easeIn: starting from 0 Easing of acceleration;
easeOut: easing of decelerating to 0;
easeInOut: easing of accelerating from 0 in the first half and decelerating to 0 in the second half.
Parameter description:
t: current Time
b: Initial value
c: Change amount
d: Duration
Calling method:
var timer = null;
var b=0 ,c=400,d=100,t=0;
var drag = $("drag");
function run() {
drag.style.left = Math.ceil(Tween.Circ .easeInOut(t,b,c,d)) "px";
if(t
}else{
clearInterval(timer);
}
}
var timer = setInterval(run, 20);

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

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



Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

This tutorial will explain how to create pie, ring, and bubble charts using Chart.js. Previously, we have learned four chart types of Chart.js: line chart and bar chart (tutorial 2), as well as radar chart and polar region chart (tutorial 3). Create pie and ring charts Pie charts and ring charts are ideal for showing the proportions of a whole that is divided into different parts. For example, a pie chart can be used to show the percentage of male lions, female lions and young lions in a safari, or the percentage of votes that different candidates receive in the election. Pie charts are only suitable for comparing single parameters or datasets. It should be noted that the pie chart cannot draw entities with zero value because the angle of the fan in the pie chart depends on the numerical size of the data point. This means any entity with zero proportion

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...
