requestanimationframe

小程序Canvas性能优化实战

我只是一个虾纸丫 提交于 2019-12-12 20:25:21
以下内容转载自totoro的文章《小程序Canvas性能优化实战!》 作者:totoro 链接: https://blog.totoroxiao.com/canvas-perf-mini/ 来源: https://blog.totoroxiao.com/ 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 腾讯位置服务基于微信提供的小程序插件能力,专注于(围绕)地图功能,打造一系列小程序插件,可以帮助开发者简单、快速的构建小程序,是您实现地图功能的最佳伙伴。目前微信小程序插件提供 路线规划、地铁图、地图选点 等服务! 案例背景 需求: 在小程序中使用canvas组件绘制地铁图,地铁图包括地铁线路、站点图标、线及站点名称文字,绘制元素为线、圆、图片、文字。 支持拖动平移和双指缩放。 问题: 小程序中的canvas性能有限,特别在交互的过程中不断触发重绘会引发严重卡顿。 基本实现 在不考虑优化的情况下,先说说如何实现绘制和交互。 数据格式 首先看看数据,服务返回的数据中每个元素都是独立的,包括该元素的样式及坐标 // 线路数据 lineData = { path: [x0, y0, x1, y1, ...], strokeColor, strokeWidth } // 站点数据:分为普通站点和换乘站点 // 普通站点绘制简单圆形 stationData = { x,

小程序Canvas性能优化实战

天大地大妈咪最大 提交于 2019-12-12 20:15:55
以下内容转载自totoro的文章《小程序Canvas性能优化实战!》 作者:totoro 链接: https://blog.totoroxiao.com/canvas-perf-mini/ 来源: https://blog.totoroxiao.com/ 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 腾讯位置服务基于微信提供的小程序插件能力,专注于(围绕)地图功能,打造一系列小程序插件,可以帮助开发者简单、快速的构建小程序,是您实现地图功能的最佳伙伴。目前微信小程序插件提供 路线规划、地铁图、地图选点 等服务! 案例背景 需求: 在小程序中使用canvas组件绘制地铁图,地铁图包括地铁线路、站点图标、线及站点名称文字,绘制元素为线、圆、图片、文字。 支持拖动平移和双指缩放。 问题: 小程序中的canvas性能有限,特别在交互的过程中不断触发重绘会引发严重卡顿。 基本实现 在不考虑优化的情况下,先说说如何实现绘制和交互。 数据格式 首先看看数据,服务返回的数据中每个元素都是独立的,包括该元素的样式及坐标 // 线路数据 lineData = { path: [x0, y0, x1, y1, ...], strokeColor, strokeWidth } // 站点数据:分为普通站点和换乘站点 // 普通站点绘制简单圆形 stationData = { x,

Should I use window.webkitRequestAnimationFrame instead of setInterval?

喜欢而已 提交于 2019-12-12 10:15:22
问题 I am developing a game in JavaScript and I am confused on which one I should use - window.webkitRequestAnimationFrame or setInterval for moving my game characters. 回答1: you can use window.webkitRequestAnimationFrame instead of setInterval var counter = 0; var requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame; var cancelAnimationFrame = window

Smooth Animation in HTML5 Canvas [duplicate]

笑着哭i 提交于 2019-12-12 03:43:31
问题 This question already has answers here : get a smooth animation for a canvas game (3 answers) Closed 3 years ago . So I was creating a game on the canvas in HTML and Javascript. I wanted to make some kind of flappy bird-ish game but when I press a key, the animation of the player looks really stuttering. Take a look: body { overflow: hidden; } <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="Style.css"/> </head> <body onload="startgame()"> <canvas id="canvas"></canvas> <script>

Web browser paint rate with multiple animations on the page

半世苍凉 提交于 2019-12-12 02:53:39
问题 We can use requestAnimationFrame in order to paint a frame in our browser. However, what happens if we have multiple animations on the same page? If for example we have 3 custom marquees and some 2 fading effects, since each effect has it's own requestAnimationFrame doesn't it mean that we are actually painting the browser 5 times each frame request? Moreover, if I limit my text-scrollers FPS to be at 30FPS but my fading effects are running with 45FPS doesn't it mean that in a span of 1

cycle through animations on click

自古美人都是妖i 提交于 2019-12-12 00:12:26
问题 starting with a single image, I have three animations I want it to cycle through each time the image is clicked. I created this switch: function switcher() { // counts to 4 then resets to 0 x = x+1 if (x>3) {x = 0} // Fire OFF if (x == 0) { // animation code ? } // Fire LARGE if (x == 1) { // animation code ? } // Fire MEDIUM if (x == 2) { // animation code ? } // Fire SMALL if (x == 3) { // animation code ? }} 1 static state + 3 animated states. The animation I have uses Paul Irish's

When does requestAnimationFrame fire?

落花浮王杯 提交于 2019-12-11 19:55:17
问题 It seems that requestAnimationFrame only works after first run of js code is done: https://jsfiddle.net/pn91zmpc/ var n = 0; function sleep(ms) { var date = new Date(); var curDate = null; do { curDate = new Date(); } while (curDate - date < ms); } function refresh() { var body = document.getElementsByTagName("body")[0]; body.textContent = n; } n=0; window.requestAnimationFrame(refresh); n = n+1; sleep(2000); n = n+1; sleep(2000); n = n+1; I expected to see a countdown. Instead, I only see

different speed of animation requestAnimFrame javascript

余生长醉 提交于 2019-12-11 15:35:14
问题 I have a problem with window.requestAnimFrame. It works in different ways in FF,Chrome and in IE9,Opera. For example this http://jsfiddle.net/vZP3u/2/ (tacked from another question about requestAnimFrame). It shows about 60 fps (I guess should be 30) in FF 9.0.1 and Chrome 16.0.912.77 m. But It works fine in IE9 and Opera. Have you any idea how to fix this? 回答1: Opera and IE do not support requestAnimationFrame yet. So your code runs at 33ms setTimeout fallback. More ontopic: http://paulirish

requestAnimationFrame attached to App object not Window

依然范特西╮ 提交于 2019-12-11 11:05:45
问题 I have set up the following jsFiddle http://jsfiddle.net/ZbhQY/4/ Im using a small requestAnimationFrame polyfill and returning the result to window.reqeuestAnimFrame this all works fine and all calls to this function work as expected. If i change this to return to game.reqeuestAnimFrame and replace all calls to window.reqeuestAnimFrame to the new game.reqeuestAnimFrame it no longer works. Could someone please explain why this is the case and what i could do in order to make this work? Thanks

how to replace setInterval by requestAnimationFrame

旧巷老猫 提交于 2019-12-10 20:25:30
问题 Here is my situation, I need to speed up the function running time, so setInterval is not a wise choose, right? Since it will cost at least 4ms for each time. So, may I change setInterval function to requestAnimationFrame, but I don't quite understand how the requestAnimationFrame works. For example // some code here var interval = setInterval(doSomething, 10) var progress = 0 function doSomething(){ if (progress != 100){ // do some thing here }else{ clearInterval(interval) } } and how can I