H5&CSS Basic —— 035. Web Animation Points (Not translated)
»
001. Free HTTPS Certificate
002. About The Column
003. Visible Area of Browser
004. Why ReactJs
005. Prohibit mobile scaling
006. EM in H5
007. Space width in page
008. H5 Special Characters
009. H5 Restricting Input
010. H5 Font
011. nginx redirection configuration
012. Text Indentation
013. Flex Layout
014. Gradient Borders
015. Common DOM Operations
016. Media Query
017. CSS Selectors
018. CSS alternate lines color
019. fetch
020. JS Attributes Operation
021. scroll div
022. JS for Loop
023. Prohibit Text Selection
024. Precision Controled Float Output
025. Ionic Full Screen
026. SVG Circle
027. SVG Ellipse
028. SVG Arc
029. SVG Elliptical Sector
030. Using WebAssembly
031. JS Checklist checkbox Operation
032. SVG Bezier Curve
033. Threejs Material
034. Luminous Image Styles
035. Web Animation Points
036. CSS Gradient Fonts
037. SVG Rectangle
038. Development Ability for Remuneration
039. Close Browser Default Style for Input
040. Jet Tail Particle Animation
041. Prohibit Desktop Browser Scaling JS
042. Desktop Full Screen Web CSS
043. Display images in self scaling aspect ratio div
044. Bi-side Text Alignment Styles
045. Single line ellipsis
046. Multi-line ellipsis
047. Font Transformation Styles
048. Remove default style of a tag
049. Chinese Characters: Full Width Space
050. DIV lays on DIV
051. Calculating 1rem Pixel Values in JS
052. JS Operations of DOM Parent Nodes and Adjacent Nodes
053. Absolute Coordinates of DOM Elements in JS
054. ToolTips Div
055. Height and Width in JS
056. Nginx configuration for stream media
057. Scroll Bar Style
058. Common Methods for JS Array Operations
059. SVG Fill Style Definition
060. SVG Stroke Style Definition
061. SVG Drawing Lines
062. CSS Background Images Splicing
063. WebSocket
064. JS Calculate DPI
065. GIF Opacity or not
066. Nginx solved CORS problem
067. Look At JsonP before publish
068. Center and Middle Content in Div
069. CSS background image size keeping ratio
070. CSS Frame Animation
071. CSS Same Width Font Display
072. Package
    所有的图形程序都有UI线程,也就是主线程。浏览器中的网页,只有一个线程那就是UI线程。它负责网页图形、文字的绘制以及用户交互事件的处理。比如:用户点击了一个按钮,它就会生成一个点击事件并且传递给对应的事件处理函数来运行用户指定的代码从而实现相应的事件处理功能。

    当用户代码调用setTimeout()的时候,UI线程就在UI循环中加入一个时间判断标记,每次UI循环都判断一次时间间隔是否已经到达。如果到达就运行setTimeout()中指定的函数。

    所以,setTimout()或者setInterval()的时间都是不准确的,要等待UI线程来判断时间间隔是否已经到达。
    
    所以,setTimout()或者setInterval()中,每次运行用户代码的等待都会比用户传入的时间间隔参数长。
    
    如果UI线程一直在处理需要长时间处理的循环,那么setTimeout()的函数调用就会被延后。如果有多个setTimeout(),那么依次判断依次延后。


    在这里要讲的是,网页动画也是帧动画,它的画面的动的形成在于一帧一帧图像的快速绘制和显示。
    
    在网页动画中,如果有多个短时间的setInterval(),短时间的setInterval()就很容易被长时间的事件所阻塞,比如被下载3D模型文件的事件所阻塞。
    
    当被阻塞的setInterval()动画被唤醒时,页面的动画就会不按设定的时间间隔来显示。

    所以,在网页中如果用不同时间间隔的setInterval()定时器来实现动画,尽量把页面上所有的动画放入一个setInterval()函数中,通过使用倍数于时间间隔的方式实现不同的动画。
    
    当然,最好的方法是:setTimout()和requestAnimationFrame()函数一起使用,不论是SVG的动画还是threejs的动画。

    requestAnimationFrame()函数传入的是一个动画控制函数。在调用requestAnimationFrame()之后,UI线程会立马运行传入该函数的动画控制函数。

    使用requestAnimationFrame()控制帧率的方法为:

window.onload=()=>{
        //启动渲染动画
        requestAnimationFrame(animate);//浏览器自带函数:requestAnimationFrame,请求动画帧
}
//计算与上一帧之间的时间间隔(毫秒)
function getDelta(){
        if(this.lastTimestamp==null){
                this.lastTimestamp=new Date().getTime();
                return 0.0;
        }else{
                var currentTimestamp=new Date().getTime();
                var delta = 1.0 * (currentTimestamp - this.lastTimestamp);
                this.lastTimestamp=currentTimestamp;
                return delta;
        }
}
var frameCount=0;
var totalDelta=0.0;
function animate(){//绘制动画帧自定义函数
        const delta = getDelta();//自上一次调用getDelta()以来的时间流失。
        totalDelta+=delta;//时间流逝计入总间隔时间(毫秒)
        if(totalDelta>=1000.0/60){
                //总间隔时间的计时已达到或者已超过指定帧率的帧间隔时间,则渲染场景
                //帧率上限:60帧/秒
                ++frameCount;
                drawH5();//渲染函数,即帧画面绘制函数
                totalDelta=getDelta();//渲染结束。本次渲染时间的流逝计入新的帧间隔时间的计时
                requestAnimationFrame(animate);//请求新的渲染
        }else{
                //总时间间隔还未达到指定帧率的帧间隔时间
                const delta = getDelta();//自上一次调用getDelta()以来的时间流失。
                totalDelta+=delta;//时间流逝计入总间隔时间
                setTimeout(()=>{
                        requestAnimationFrame(animate);
                },1000.0/60-totalDelta);//等待时间差后再次请求渲染
        }
}

    在这个动画显示函数中,当完成一次动画绘制后,会立马请求渲染函数animate。
    
    再次进入animate时,程序判断时间是否已经到达每秒60帧的渲染时间,如果已经到达或者已经超过,则直接渲染;如果还没有到达,则等待还需要等待的时间后再请求渲染。

    
    ————www.v-signon.com学习者共勉
«
--Alex.Zhang
--www.v-signon.com Learningers Co-Encouraged
Back
Personal Art: www.up-task.com Unit: Individual
中文 Русский 京ICP备19038994号-2
If the content on this website infringes upon your any rights, please contact me at 1307776259@qq.com for removal