【发布时间】:2016-01-14 18:26:49
【问题描述】:
问题
我正在尝试为画布的帧计时以与后台播放的音频同步。
当我使用window.requestAnimationFrame 时,您会在一秒钟内在不同的浏览器/计算机上获得不稳定的帧数。我还尝试了setInterval(function(){ //code }, 1);,它比window.requestAnimationFrame 更接近完美,但它仍然没有正确完成工作。
我的代码是这样工作的。
setInterval 每毫秒触发一次,然后检查在 x 时间应该生成哪些对象,它还清除和更新画布。此外,我使用毫秒来计算对象的速度,以便它们在正确的 x 时间到达画布上的某个位置。
有没有更好的方法?
我将在下面列出我的代码,以及我当前的 CodePen 的链接,以便您可以看到它的运行情况。 (点击画布区开始动画背景有音乐要同步,但点击后才会开始播放)
Sonar Song on CodePen
var Queues = [
[1, '#FFFFFF', 'line', true, [20,20], [80,80], 500, 950],
[2, '#FFFFFF', 'line', true, [80,80], [55,20], 950, 1200]
];
var playing = false;
$(document).click(function(){
if(playing == false) {
var audio = document.getElementById("audio");
audio.currentTime = 0;
audio.play();
apparatus();
playing = true;
} else {
var audio = document.getElementById("audio");
audio.pause();
playing = false;
$('body canvas').remove();
}
});
function apparatus() {
var canvas, context, toggle, time = 0, vidtime = 0;
var points = [];
init();
function init() {
canvas = document.createElement( 'canvas' );
context = canvas.getContext( '2d' );
canvas.width = $(document).width();
canvas.height = $(document).height();
document.body.appendChild( canvas );
}
var point = function(options) {
this.position = {}, this.end_position = {}, this.distance = {}, this.velocity = {}, this.time = {};
points.push(this);
// TIMING
this.time.start = options[6];
this.time.end = options[7];
// VECTORS
this.position.x = options[4][0] * (canvas.width / 100);
this.position.y = options[4][1] * (canvas.height / 100);
this.end_position.x = options[5][0] * (canvas.width / 100);
this.end_position.y = options[5][1] * (canvas.height / 100);
this.distance.x = Math.abs(this.position.x - this.end_position.x);
this.distance.y = Math.abs(this.position.y - this.end_position.y);
if(this.position.x > this.end_position.x) {
this.velocity.x = -Math.abs(this.distance.x / (this.time.end - this.time.start));
} else {
this.velocity.x = Math.abs(this.distance.x / (this.time.end - this.time.start));
}
if(this.position.y > this.end_position.y) {
this.velocity.y = -Math.abs(this.distance.y / (this.time.end - this.time.start));
} else {
this.velocity.y = Math.abs(this.distance.y / (this.time.end - this.time.start));
}
// STYLING
this.style = options[2];
this.color = options[1];
//-- STYLING / STYLE TYPES
if(this.style == 'line') {
}
}
point.prototype.draw = function() {
this.position.x += this.velocity.x;
this.position.y += this.velocity.y;
context.fillStyle = this.color;
context.beginPath();
context.arc( this.position.x, this.position.y, 10, 0, Math.PI * 2, true );
context.closePath();
context.fill();
}
setInterval(function(){
time++;
console.log(time);
context.fillStyle = '#000000';
context.fillRect(0, 0, $(document).width(), $(document).height());
for(var i = 0; i < Queues.length; i++) {
if(time == Queues[i][6]) {
new point(Queues[i]);
}
if(time == Queues[i][7]) {
console.log('la');
}
}
for(var i = 0; i <= points.length; i++) {
if(points[i] != null) {
points[i].draw();
}
}
}, 1);
}
【问题讨论】:
标签: javascript jquery html canvas