search
HomeWeb Front-endH5 TutorialHTML5游戏开发 之 循环的控制(1)

       1) 动画绘制的利器:RequestAnimationFrame

       RequestAnimationFrame,绝对的大名鼎鼎。相信做HTML5的同学们,都听到过这个函数。再重复一下它的好处:
游戏页面在不被显示的时候,RequestAnimationFrame暂停执行,不会占用CPU时间
RequestAnimaitonFrame会将JS产生的动画以及CSS产生的动画,放到同一个Repait和Reflow的循环中。

       对于第二点,很少有人提及,但是非常重要,如果没有RequestAnimationFrame,而用setTimeout,很有可能在每次绘制的时候,JS产生的动画和CSS产生的动画,没有同时发生,相信这个肯定不是你想要的结果。对于第二点,有点晦涩,不太容易懂,让我们更进一步解释一下,这就要从浏览器的渲染流程说起。大体上,浏览器的渲染流程有以下四步:

       更新Dom的结构:每次你执行JS改变Dom结构,或者修改CSS相关属性,都会对Dom结构进行改变。现在HTML5中,多了Canvas元素,对于Canvas对象,执行某个操作,比如画一条线,也属于对Dom结构的改变。但是Canvas有个比较特殊的操作,比如对Canvas对象执行getImageData操作,会强制浏览器立即跳到第四步,将渲染好的浏览器窗口,绘制到屏幕。
渲染每个元素:在所有JS和CSS执行完毕之后,浏览器按照要求,开始对每个元素进行渲染。

  • 将所有元素渲染到窗口:按照窗口大小的要求,将所有的元素,绘制到一个平面上。

  • 通过操作系统窗口管理器,将渲染好的窗口,输出到屏幕。

       通常,执行完第四步,称为一帧。目前,大部分的显示器,都会将显示控制在每秒六十帧,浏览器处于优化的目的,通常的显示频率也不会超过六十帧。

       讲到这里,大约可以清楚一点,如果不用RequestAnimationFrame,而是用传统的SetTimeout,很难要求浏览器将同一次SetTimeout里面执行的Dom或者CSS操作放到同一帧中,也就会随机的出现,某个JS操作的动画和CSS或者Canvas的动画,不能同步。因为随机的,每次行为不一致,相信这是所有开发者都不愿意碰到的情况。没有开发人员怕Bug,但是害怕的是Bug不可以重现。

       2) 计算游戏的帧率(FPS)

       衡量游戏性能的重要指标就是帧率(FPS),因为浏览器的实现不一,有些浏览器出于优化的目的,没有严格的按照第一部分介绍的四步,有些时候,在某一帧没有完成的时候,就开始执行下一帧,所以理论上,很难严格的记录浏览器显示的帧率。如果你可以有个高速的摄像机,对着屏幕拍摄,是可以严格的记录屏幕显示的帧率,但是相信这个方法,很难被大规模使用。

       目前也有一些第三方类库,可以帮助你衡量游戏的帧率,比如比较著名的https://github.com/mrdoob/stats.js,但是这个是非常不严格的,以StatsJS为代表的类库,是利用很衡量setTimeout或者setInterval每秒钟被执行的次数或者时间,来衡量帧率的。但是seTimeout函数被执行的次数和时间,和RequestAnimationFrame没有特别严格的对应关系,只可以作为参考。或者,简单一点说,每一帧被渲染的过程中,setTimetout函数很有可能被执行一次或者两次,甚至更多次。

       理论上,为了监控RequestAnimationFrame帧率,需要开发者hook RequestAnimaitonFrame这个函数,在函数每次执行完毕的时候,执行Canvas的getImageData操作,强制浏览器渲染本次RequestAnimationFrame的所有操作,计算两次渲染操作的时间差,从而得出帧率。

       幸运的是,Firefox浏览器已经开发了得到帧率的接口,可以省去很多周折。比如window.mozPaintCount这个接口,可以直接告诉开发者,浏览器渲染的帧率。

(未完待续)

接上文:HTML5 游戏开发 之 资源加载篇(2)  

HTML5 游戏开发 之 资源加载篇(1)


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What Does H5 Refer To? Exploring the ContextWhat Does H5 Refer To? Exploring the ContextApr 12, 2025 am 12:03 AM

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

H5: Tools, Frameworks, and Best PracticesH5: Tools, Frameworks, and Best PracticesApr 11, 2025 am 12:11 AM

The tools and frameworks that need to be mastered in H5 development include Vue.js, React and Webpack. 1.Vue.js is suitable for building user interfaces and supports component development. 2.React optimizes page rendering through virtual DOM, suitable for complex applications. 3.Webpack is used for module packaging and optimize resource loading.

The Legacy of HTML5: Understanding H5 in the PresentThe Legacy of HTML5: Understanding H5 in the PresentApr 10, 2025 am 09:28 AM

HTML5hassignificantlytransformedwebdevelopmentbyintroducingsemanticelements,enhancingmultimediasupport,andimprovingperformance.1)ItmadewebsitesmoreaccessibleandSEO-friendlywithsemanticelementslike,,and.2)HTML5introducednativeandtags,eliminatingthenee

H5 Code: Accessibility and Semantic HTMLH5 Code: Accessibility and Semantic HTMLApr 09, 2025 am 12:05 AM

H5 improves web page accessibility and SEO effects through semantic elements and ARIA attributes. 1. Use, etc. to organize the content structure and improve SEO. 2. ARIA attributes such as aria-label enhance accessibility, and assistive technology users can use web pages smoothly.

Is h5 same as HTML5?Is h5 same as HTML5?Apr 08, 2025 am 12:16 AM

"h5" and "HTML5" are the same in most cases, but they may have different meanings in certain specific scenarios. 1. "HTML5" is a W3C-defined standard that contains new tags and APIs. 2. "h5" is usually the abbreviation of HTML5, but in mobile development, it may refer to a framework based on HTML5. Understanding these differences helps to use these terms accurately in your project.

What is the function of H5?What is the function of H5?Apr 07, 2025 am 12:10 AM

H5, or HTML5, is the fifth version of HTML. It provides developers with a stronger tool set, making it easier to create complex web applications. The core functions of H5 include: 1) elements that allow drawing graphics and animations on web pages; 2) semantic tags such as, etc. to make the web page structure clear and conducive to SEO optimization; 3) new APIs such as GeolocationAPI support location-based services; 4) Cross-browser compatibility needs to be ensured through compatibility testing and Polyfill library.

How to do h5 linkHow to do h5 linkApr 06, 2025 pm 12:39 PM

How to create an H5 link? Determine the link target: Get the URL of the H5 page or application. Create HTML anchors: Use the <a> tag to create an anchor and specify the link target URL. Set link properties (optional): Set target, title, and onclick properties as needed. Add to webpage: Add HTML anchor code to the webpage where you want the link to appear.

How to solve the h5 compatibility problemHow to solve the h5 compatibility problemApr 06, 2025 pm 12:36 PM

Solutions to H5 compatibility issues include: using responsive design that allows web pages to adjust layouts according to screen size. Use cross-browser testing tools to test compatibility before release. Use Polyfill to provide support for new APIs for older browsers. Follow web standards and use effective code and best practices. Use CSS preprocessors to simplify CSS code and improve readability. Optimize images, reduce web page size and speed up loading. Enable HTTPS to ensure the security of the website.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.