我有下面的代码在给定的时间间隔内闪烁 Canvas ,我怎样才能让它在给定的赫兹(每秒次数)上闪烁
var startTime;
var interval;
var index = 0;
var colors = ['black', 'white'];
var id = null;
function animate(time) {
id = requestAnimationFrame(animate);
if (!startTime) { startTime = time; }
var elapsed = time - startTime;
if (elapsed > interval) {
startTime = time;
canvas.style.backgroundColor = colors[index];
if (++index > colors.length) { index = 0; }
}
}
function startBlink() {
interval = parseFloat(document.getElementById('freqBlink').value);
requestAnimationFrame(animate);
}
最佳答案
- 频率单位是赫兹(秒^-1)
- 间隔单位为毫秒
然后:
interval = 1000 / frequency
关于javascript - JS requestAnimationFrame 在给定的赫兹上闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61165361/