The combination optimization of high frequency event and requestanimationframe in Mobile terminal touchmove

Source: Internet
Author: User

The operation of the most high-frequency memory consumption of mobile is the touchmove and scroll events need microscopic optimization;

Here we tell the Touchmove;touchmove event occurs very frequently, will be faster than the screen refresh rate, resulting in invalid rendering and repainting;

Number of frames – display device The usual refresh rate is usually 50~60hz–1000ms/60≈16.6ms (1 ms optimization means 6% performance improvement)

The browser's rendering of each frame needs to be done within 16 milliseconds (1 seconds/60 = 16.66 milliseconds), and if this time limit is exceeded, the rendering of the page will have a jank effect, which is often said; we need to do the right rendering at the right time;

Drag and drop will write, first look at the effect;

Probably understand Timeline look at the rendering of the old version as follows

Look at the frame pattern rendering situation;

Those unnecessary moves do not need to be done; there is no need to calculate the extra event object within 16.6 milliseconds;

About frame mode:

Attach Source code:
1 functiondrag (Element) {2         3         varStartx=0,4Starty=0,5ticking=false,6 RAF,7Doc=document;8             9Element.addeventlistener ("Touchstart",function(e) {Ten               One              A                   vare=e| |window.event, -Touchs = E.touches[0]; -E.preventdefault ();//some of the low-end Android touch events cause the Touchend event to begin with E.preventdefault (); the                                            //text A ipnut textarea except for a few labels -                                             //, in addition, custom mobile Touchstart touchend combination of hover events, it is recommended not to add this, otherwise the page cannot scroll -                                             //touchmove start to add otherwise shake a bit, in order to Touchmove, and then normal especially earlier Samsung series comes with browser -                                                +                 -Startx=parseint (touchs.pagex-(element.lefts| | 0)); +Starty=parseint (touchs.pagey-(element.tops| | 0)); A                    atDoc.addeventlistener ("Touchmove", UPDATE,false); -Doc.addeventlistener ("Touchend", End,false); -                              -},false); -          -          in          -          to          +         varUpdate=function(e) { -               the                vare=e| |window.event; *                if(E.touches.length > 1 | | e.scale && e.scale!== 1)return; $ E.preventdefault ();Panax Notoginseng                 -                //Cancelanimationframe (RAF); the                if(!ticking) { +  A                varTouchs = E.changedtouches[0]; the                     +                  //1 Touch Move First -Element.lefts = Touchs.pagex-StartX; $Element.tops = Touchs.pagey-Starty; $                     -                  //2 Give requestanimationframe update location -                 //raf=requestanimationframe (function () {Draw ();}); theraf=Requestanimationframe (draw); - Wuyi                 } the                -ticking =true; Wu            }; -          About           $           -           -          vardraw=function  (){        -ticking =false; A                   varNowleft=parseint (element.lefts);//Sliding distance touchmove time, if the addition of resistance, there may be small jitter; I think it should be the mobile side of the support of the 0.5px for the sake of; the transformation of parseint is a bit farfetched; +                   varNowtop=parseint (Element.tops);//Distance to Slide the                       -Element.style.webkittransform=element.style.transform = "Translate3d (" + Nowleft + "px," + nowtop + "px,0px)"; $                      the               }; the                     the         varEnd=function(){ the                      varendleft= parseint (element.lefts);//Distance to Slide -                      varendtop= parseint (element.tops);//Distance to Slide in                      the                      //element.style.webkittransform=element.style.transform = "translate (" + endleft+ "px," + endtop + "px)"; the      AboutDoc.removeeventlistener ("Touchmove", UPDATE,false); theDoc.removeeventlistener ("Touchend", End,false); the                         //Cancelanimationframe (RAF); the                           +             } -                  the      };Bayi      
Note the point:compatible with Requestanimationframe
;(function(){varlasttime=0;varvendors=["MS", "Moz", "WebKit", "O"]; for(varX=0;X<VENDORS.LENGTH&&!WINDOW.REQUESTANIMATIONFRAME;++X) {window.requestanimationframe=window[ vendors[x]+ "Requestanimationframe"];window.cancelanimationframe=window[vendors[x]+ "CancelAnimationFrame"]| | window[vendors[x]+ "Cancelrequestanimationframe"]}if(!window.requestanimationframe) {window.requestanimationframe=function(callback,element) {varCurrtime=NewDate (). GetTime ();varTimetocall=math.max (0,16-(currtime-lasttime));varId=window.settimeout (function() {callback (Currtime+timetocall)},timetocall); lasttime=currtime+timetocall;returnID}}if(!window.cancelanimationframe) {window.cancelanimationframe=function(ID) {cleartimeout (ID)}}} ());
Requestanimationframe Simple animation library Requestanimationframe throttle function
  var rafthrottle=function (FN) {//touchmove  scroll throttle     var ticking=false;     var update=function () {         ticking = false;    Fn&&fn.apply (this,arguments); } function Requesttick () {       if (!ticking) {         requestanimationframe (update);        }       ticking = true;     } Requesttick ();};

  

Reference website:

Google Developer, very professional: HTTPS://DEVELOPERS.GOOGLE.COM/WEB/FUNDAMENTALS/GETTING-STARTED/?HL=ZH-CN need FQ;

Micro-analysis of Web front-end performance optimization http://velocity.oreilly.com.cn/2013/ppts/16_ms_optimization--web_front-end_performance_optimization.pdf

Mobile-Performance tuning: Ttps://speakerdeck.com/baofen14787/yi-dong-duan-xing-neng-diao-you-ji-16msyou-hua

Summary: Do more things, you have to tidy up to;

Mobile Scroll,touchmove events are used or more, there is time to be carefully optimized, although the feeling is very microscopic, and even feel that the optimization is almost impossible to see, but you go to optimize, or spend a lot of time;

Requestanimationframe is a sharp weapon on the mobile side; The animation tries to use it or the CSS3 of the set.

The combination optimization of high frequency event and requestanimationframe in Mobile terminal touchmove

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.