从间隔到请求动画帧
From interval to requestAnimationFrame
重温我的一个旧实验。我正在使用间隔来旋转元素。现在我想将间隔更改为 rAF。我尝试重写了几次Rotation.start
方法,但我没有成功。由于我是这个requestAnimationFrame
的新手,我不知道还能尝试什么。
相关JS代码:
function Rotation(mass) {
this.mass = mass;
this.angle = 0;
this.velocity = 1;
this.fps = 1000/25; // for interval
this.handler = null;
this.rotate = function () {
// when called from rAF "this" refers to window, method fails
this.angle = (this.angle + this.velocity) % 360;
var transform = "rotate(" + this.angle + "deg)";
this.mass.elm.style.webkitTransform = transform;
};
this.start = function () {
var rotation = this; // this = Rotation
rotation.handler = setInterval(function () {
// inside the interval "this" refers to window
// that's why we set rotation to "this" at the beginning
// so we can access the Rotation obj
rotation.rotate();
}, rotation.fps);
/* requestAnimationFrame(rotation.rotate); */
};
}
小提琴(完整代码)
可以使用 bind()
方法绑定this
的值
function Rotation(mass) {
this.mass = mass;
this.angle = 0;
this.velocity = 1;
this.handler = null;
this.rotate = function () {
this.angle = (this.angle + this.velocity) % 360;
var transform = "rotate(" + this.angle + "deg)"
this.mass.elm.style.webkitTransform = transform;
//call bind on this.rotate so we can properly associate this
window.requestAnimationFrame(this.rotate.bind(this));
};
this.start = function () {
//then just start the animation by using this.rotate()
this.rotate();
};
}
工作 JSFiddle: http://jsfiddle.net/gvr16mcL/
相关文章:
- 画布:逐像素绘制图像并请求动画帧计时
- 将对象函数传递给请求动画帧时丢失对象引用
- 防止请求动画帧一直运行
- 取消请求动画设置时间后的帧
- 如果 Ajax 请求太快,jQuery 动画将无法正常工作
- 从间隔到请求动画帧
- Javascript move 元素与鼠标移动事件 60 FPS 请求动画帧
- 如何控制动画速度(请求动画帧)
- 请求动画帧范围更改为窗口
- 反应.js - 在请求动画帧上呈现
- 如何通过请求动画帧从其新实例中移动形状
- 从对象引用请求动画帧时出错
- HTML5 画布撕裂与请求动画帧
- 请求返回结果的动画帧
- 请求 iPhone 上的动画帧
- 当 ajax 请求速度时不显示动画.js
- 有关请求动画帧的问题
- 如何使用请求动画帧暂停和重新启动秒表计时器
- 请求动画帧不适用于小于 1 的十进制数
- 不同速度的动画请求AnimFrame javascript