Add interpolation for synced animation frames - much smoother

alternate-visualiser
William Toohey 10 years ago
parent d79e0d36e2
commit 75b591ad3b
  1. 25
      js/HuesCanvas.js
  2. 2
      js/HuesCore.js

@ -34,7 +34,7 @@ function HuesCanvas(element, aContext, core) {
this.animTimeout = null; this.animTimeout = null;
this.animFrame = null; this.animFrame = null;
this.animSync = false; // for synced anims this.lastBeat = 0;
// set later // set later
this.blurDecay = null; this.blurDecay = null;
@ -243,19 +243,28 @@ HuesCanvas.prototype.setImage = function(image) {
} }
} }
HuesCanvas.prototype.beat = function() {
this.lastBeat = this.aContext.currentTime;
}
HuesCanvas.prototype.syncAnim = function() { HuesCanvas.prototype.syncAnim = function() {
var song = this.core.currentSong; var song = this.core.currentSong;
if(!song) { // fallback to default if(!song) { // fallback to default
return; return;
} }
// This loops A-OK because the core's beatIndex never rolls over for a new loop var index = this.core.beatIndex;
var beatLoc = (this.core.beatIndex / song.charsPerBeat) % this.image.beatsPerAnim; // When animation has more frames than song has beats, or part thereof
this.animFrame = Math.floor(this.image.bitmaps.length * (beatLoc / this.image.beatsPerAnim)); if(this.lastBeat && this.core.beatLength) {
// for build var interp = (this.aContext.currentTime - this.lastBeat) / this.core.beatLength;
if(this.animFrame < 0) { index += Math.min(interp, 1);
this.animFrame += this.image.bitmaps.length;
} }
this.animSync = true; // This loops A-OK because the core's beatIndex never rolls over for a new loop
var beatLoc = (index / song.charsPerBeat) % this.image.beatsPerAnim;
var aLen = this.image.bitmaps.length;
this.animFrame = Math.floor(aLen * (beatLoc / this.image.beatsPerAnim));
// Because negative mods are different in JS
this.animFrame = ((this.animFrame % aLen) + aLen) % aLen;
} }
HuesCanvas.prototype.setColour = function(colour, isFade) { HuesCanvas.prototype.setColour = function(colour, isFade) {

@ -264,7 +264,6 @@ HuesCore.prototype.songDataUpdated = function() {
} }
HuesCore.prototype.resetAudio = function() { HuesCore.prototype.resetAudio = function() {
this.samplePosition = 0;
this.beatIndex = 0; this.beatIndex = 0;
this.position = 0; this.position = 0;
this.songDataUpdated(); this.songDataUpdated();
@ -346,6 +345,7 @@ HuesCore.prototype.getBeat = function(index) {
HuesCore.prototype.beater = function(beat) { HuesCore.prototype.beater = function(beat) {
this.userInterface.beat(); this.userInterface.beat();
this.renderer.beat();
switch(beat) { switch(beat) {
case 'X': case 'X':
case 'x': case 'x':

Loading…
Cancel
Save