画布动画停止播放时,点击home键(切换应用程序)在移动设备上
canvas animation stops playing when click homebutton (switch apps) on mobile device
我在移动设备/平板电脑上的画布动画有问题。动画运行得很好,直到我点击"home"或"switchapp"按钮,然后改变设备的"旋转"。当我切换回具有画布动画的浏览器窗口时,它被冻结并且不会再次播放。
这里是小提琴:jsfiddle.net/nLmjchqv/
是否有可能再次运行动画(如重新启动画布)?是否有可能检测应用程序切换或home按钮点击在JavaScript?
谢谢。
您可以监听pageshow
和pagehide
事件(参见MDN)
例如,您可以在pagehide
上调用cancelAnimationFrame
,然后在pageshow
上再次启动requestAnimationFrame
的动画
谢谢Christoph,
听了你的劝告,我终于找到了一个解决办法。
<canvas id="myCanvas" width="578" height="200"></canvas>
<script>
//raf polyfill
(function() {
var lastTime = 0;
var vendors = ['webkit', 'moz'];
for(var x = 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) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!window.cancelAnimationFrame)
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}());
function drawRectangle(myRectangle, context) {
context.beginPath();
context.rect(myRectangle.x, myRectangle.y, myRectangle.width, myRectangle.height);
context.fillStyle = '#8ED6FF';
context.fill();
context.lineWidth = myRectangle.borderWidth;
context.strokeStyle = 'black';
context.stroke();
}
var rafId;
function animate() {
if (myRectangle.dir == 'right') {
myRectangle.x += 5;
}
if (myRectangle.dir == 'right' && myRectangle.x >= canvas.width - myRectangle.width) {
myRectangle.dir = 'left';
}
if (myRectangle.dir == 'left' && myRectangle.x <= 0) {
myRectangle.dir = 'right';
}
if (myRectangle.dir == 'left') {
myRectangle.x -= 5;
}
// clear
context.clearRect(0, 0, canvas.width, canvas.height);
drawRectangle(myRectangle, context);
// request new frame
rafId = requestAnimationFrame(animate);
}
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var myRectangle = {
x: 0,
y: 75,
width: 100,
height: 50,
borderWidth: 5,
dir: 'right'
};
animate();
var supportsOrientationChange = "onorientationchange" in window,
orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";
window.addEventListener(orientationEvent, function() {
cancelAnimationFrame(rafId);
canvas = document.getElementById("myCanvas");
var cw = canvas.width;
var ch = canvas.height;
canvas.outerHTML = "";
delete canvas;
canvas = document.createElement('canvas');
canvas.id = "myCanvas";
canvas.width = cw;
canvas.height = ch;
document.body.appendChild(canvas);
context = canvas.getContext('2d');
rafId = requestAnimationFrame(animate);
}, false);
</script>
这里有一个小提琴:http://jsfiddle.net/cszkmsLs/
相关文章:
- Canvas Html5绘图应用程序,移动画布会导致重大问题
- 如何减少在移动网络应用程序上播放声音的延迟
- Meteor移动应用程序-嵌入式数据库
- 如何在telerik剑道移动应用程序中按下刷新数据源的按钮
- 移动登录应用程序
- jquery移动应用程序无法使用jquryMobile框架JS和css文件的相对路径
- 离子框架移动应用程序性能问题
- 在移动设备上按路径或名称获取二进制图像(Ionic / Ng cordova 应用程序)
- 移动照片库应用程序
- 尝试在移动优先应用程序中从 SQL 数据库获取数据时出错
- 我想在混合移动应用程序中使用Sqlite插件(Cordova)创建一个示例项目
- 地理位置不适用于苹果移动网络应用程序功能
- 移动网络应用程序:在多分辨率设备中使用同一组图像
- 在移动应用程序上滚动iframe会在某个时刻跳到页面顶部
- 在加载移动应用程序页面时调用Javascript函数
- Facebook在跨平台移动应用程序中的集成
- jquery.mobile-1.4.5.js:26未捕获类型错误:无法设置属性'移动'react应用程序
- 在web应用程序ASP.net中拖放和移动图像
- 将Facebook应用程序移动到新服务器,现在它坏了
- 正在将AngularJS应用程序移动到pushState