HTML5的canvas绘制动态时钟,1秒刷新一次canvas,个人觉得有性能的问题,如果不这么做的话,有什么好的方法?
回复内容:
个人觉得没有性能的问题 不会有任何性能问题,要理解canvas是比较底层的API,它需要手工去做一些底层的工作,让canvas里的东西动起来就需要不停的重绘,刚接触canvas的人会容易产生这样的误解,觉得这样会有性能问题传统的DOM动画并不是没有这个重绘过程的,想要DOM元素动起来,一样需要经历重排版和重渲染,只不过这些浏览器的排版引擎在底层已经自动替你完成了,实际上浏览器重构整个渲染树的开销是非常非常巨大的,这也是造成DOM动画性能低下的原因,在canvas里可以让里面的数千个图形不断运动,依然保持流畅,而如果一个页面中有数十个DOM一起运动就已经开始卡了 不会吧,许多HTML5游戏引擎都是冲着60fps去的,做个打飞机之类的游戏也完全没有压力,时钟这种东西就更不至于造成性能问题了,更何况你是1fps。先做个实验试试吧。 一秒刷新都有性能问题的机器就别跑什么HTML5了。 对于CANVAS绘制性能提高问题,可以考虑使用“双缓冲”技巧。
CANVAS图像传统绘制是这个样子的:
(1)在可见画布上画出一个圆。
(2)在可见画布上画出一个三角。
(3)在可见画布上画出一个方块。
“双缓冲”技巧则是这个样子:
(1)在非可见画布上画出一个圆。
(2)在非可见画布上画出一个三角。
(3)在非可见画布上画出一个方块。
(4)将非可见画布上的内容一次性画到可见画布上。
也就是说,在浏览器上,使用双缓冲技术进行canvas对象的绘制只会对浏览器进行一次渲染。就理论而言,在需要绘制的东西非常多的时候,双缓冲技术可以有效减少页面闪烁次数。
之前做过实验,在canvas画布中有若干个圆做无规则碰撞运动,设定两个对照组,一个用传统方法绘制圆,另一个则是使用双缓冲技巧。以下是实验记录:
第一次对比:
两组都是900个圆在CANVAS中进行无规则运动并相互碰撞,两组都运行流畅
第二次对比:
两组都是1100个圆在CANVAS中进行无规则运动并相互碰撞,两组都运行流畅
第四次对比:
两组都是2100个圆在CANVAS中进行无规则运动并相互碰撞,两组都开始出现掉帧现象
第五次对比:
两组都是2500个圆在CANVAS中进行无规则运动并相互碰撞,两组都有较为明显的卡顿现象,仍然没有明显的性能差别
第六次对比:
两组都是3000个圆在CANVAS中进行无规则运动并相互碰撞,两组都卡顿,仍然没有明显的性能差别
第七次对比:
两组都是3500个圆在CANVAS中进行无规则运动并相互碰撞,两组都很卡顿,仍然没有明显的性能差别
当时的出的结论是:双缓冲技巧对于HTML5 CANVAS性能提升效果并不明显。
现在想来,其实还有一种可能:浏览器把性能都用在计算那几千个撞来撞去的圆的速度、角度变化上去了,而“大量绘制图像”的实验目的并没有得到体现。
另外,所谓“卡顿程度”在当时是使用肉眼判断的,这又是另一个不严谨之处。
此技巧并不复杂,我想题主还是可以一试的。

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

H5 page production refers to the creation of cross-platform compatible web pages using technologies such as HTML5, CSS3 and JavaScript. Its core lies in the browser's parsing code, rendering structure, style and interactive functions. Common technologies include animation effects, responsive design, and data interaction. To avoid errors, developers should be debugged; performance optimization and best practices include image format optimization, request reduction and code specifications, etc. to improve loading speed and code quality.

Running the H5 project requires the following steps: installing necessary tools such as web server, Node.js, development tools, etc. Build a development environment, create project folders, initialize projects, and write code. Start the development server and run the command using the command line. Preview the project in your browser and enter the development server URL. Publish projects, optimize code, deploy projects, and set up web server configuration.

The steps to create an H5 click icon include: preparing a square source image in the image editing software. Add interactivity in the H5 editor and set the click event. Create a hotspot that covers the entire icon. Set the action of click events, such as jumping to the page or triggering animation. Export H5 documents as HTML, CSS, and JavaScript files. Deploy the exported files to a website or other platform.

H5 is not a standalone programming language, but a collection of HTML5, CSS3 and JavaScript for building modern web applications. 1. HTML5 defines the web page structure and content, and provides new tags and APIs. 2. CSS3 controls style and layout, and introduces new features such as animation. 3. JavaScript implements dynamic interaction and enhances functions through DOM operations and asynchronous requests.

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

H5 pop-up window creation steps: 1. Determine the triggering method (click, time, exit, scroll); 2. Design content (title, text, action button); 3. Set style (size, color, font, background); 4. Implement code (HTML, CSS, JavaScript); 5. Test and deployment.

H5 (HTML5) is suitable for lightweight applications, such as marketing campaign pages, product display pages and corporate promotion micro-websites. Its advantages lie in cross-platformity and rich interactivity, but its limitations lie in complex interactions and animations, local resource access and offline capabilities.

"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.
