Make Canvas use more callbacks

master
William Toohey 10 years ago
parent c306f30203
commit b4a278f630
  1. 33
      js/HuesCanvas.js
  2. 25
      js/HuesCore.js

@ -25,6 +25,9 @@
function HuesCanvas(element, aContext, core) { function HuesCanvas(element, aContext, core) {
'use strict'; 'use strict';
this.aContext = aContext; this.aContext = aContext;
core.addEventListener("newimage", this.setImage.bind(this));
core.addEventListener("newcolour", this.setColour.bind(this));
core.addEventListener("beat", this.beat.bind(this));
this.core = core; this.core = core;
this.needsRedraw = false; this.needsRedraw = false;
@ -64,7 +67,7 @@ function HuesCanvas(element, aContext, core) {
this.setBlurQuality("high"); this.setBlurQuality("high");
this.setBlurDecay("fast"); this.setBlurDecay("fast");
this.canvas = document.getElementById(element).getContext("2d"); this.canvas = document.getElementById(element).getContext("2d");
window.addEventListener('resize', this.resizeHandler(this)); window.addEventListener('resize', this.resize.bind(this));
this.resize(); this.resize();
this.snowing = false; this.snowing = false;
@ -74,12 +77,15 @@ function HuesCanvas(element, aContext, core) {
this.snowflakes = []; this.snowflakes = [];
this.animating = true; this.animating = true;
requestAnimationFrame(this.getAnimLoop()); requestAnimationFrame(this.animationLoop.bind(this));
} }
HuesCanvas.prototype.resizeHandler = function(that) { HuesCanvas.prototype.settingsUpdated = function() {
return function() {that.resize();}; this.setSmartAlign(localStorage["smartAlign"]);
}; this.setBlurAmount(localStorage["blurAmount"]);
this.setBlurDecay(localStorage["blurDecay"]);
this.setBlurQuality(localStorage["blurQuality"]);
}
HuesCanvas.prototype.resize = function() { HuesCanvas.prototype.resize = function() {
// height is constant 720px, we expand width to suit // height is constant 720px, we expand width to suit
@ -180,11 +186,6 @@ HuesCanvas.prototype.intToHex = function(num) {
return '#' + ("00000"+num.toString(16)).slice(-6); return '#' + ("00000"+num.toString(16)).slice(-6);
}; };
HuesCanvas.prototype.getAnimLoop = function() {
var that = this;
return function() {that.animationLoop();};
};
HuesCanvas.prototype.animationLoop = function() { HuesCanvas.prototype.animationLoop = function() {
if (this.colourFade) { if (this.colourFade) {
var delta = this.aContext.currentTime - this.colourFadeStart; var delta = this.aContext.currentTime - this.colourFadeStart;
@ -244,11 +245,14 @@ HuesCanvas.prototype.animationLoop = function() {
this.drawSnow(); this.drawSnow();
} }
if(this.animating) { if(this.animating) {
requestAnimationFrame(this.getAnimLoop()); requestAnimationFrame(this.animationLoop.bind(this));
} }
}; };
HuesCanvas.prototype.setImage = function(image) { HuesCanvas.prototype.setImage = function(image) {
if(this.image == image) {
return;
}
this.needsRedraw = true; this.needsRedraw = true;
this.image = image; this.image = image;
// Null images don't need anything interesting done to them // Null images don't need anything interesting done to them
@ -293,11 +297,14 @@ HuesCanvas.prototype.syncAnim = function() {
}; };
HuesCanvas.prototype.setColour = function(colour, isFade) { HuesCanvas.prototype.setColour = function(colour, isFade) {
if(colour.c == this.colour) {
return;
}
if(isFade) { if(isFade) {
this.newColour = colour; this.newColour = colour.c;
} else { } else {
this.stopFade(); this.stopFade();
this.colour = colour; this.colour = colour.c;
} }
this.needsRedraw = true; this.needsRedraw = true;
}; };

@ -63,10 +63,11 @@ function HuesCore(defaults) {
* Image object is passed. * Image object is passed.
*/ */
newimage : [], newimage : [],
/* callback newimage(image) /* callback newcolour(colour, isFade)
* *
* Called on colour change. * Called on colour change.
* Colour object is passed. * colour: colour object.
* isFade: if the colour is fading from the previous value
*/ */
newcolour : [], newcolour : [],
/* callback newmode(mode) /* callback newmode(mode)
@ -99,7 +100,12 @@ function HuesCore(defaults) {
* Called when the song actually begins to play, not just when the * Called when the song actually begins to play, not just when the
* new song processing begins * new song processing begins
*/ */
songstarted : [] songstarted : [],
/* callback settingsupdated()
*
* Called when settings are updated and should be re-read from localStorage
*/
settingsupdated : []
}; };
var that = this; var that = this;
@ -473,7 +479,6 @@ HuesCore.prototype.setImage = function(index) {
this.imageIndex = -1; this.imageIndex = -1;
this.lastImageArray = []; this.lastImageArray = [];
} }
this.renderer.setImage(this.currentImage);
this.callEventListeners("newimage", this.currentImage); this.callEventListeners("newimage", this.currentImage);
}; };
@ -518,8 +523,7 @@ HuesCore.prototype.randomColour = function(isFade) {
HuesCore.prototype.setColour = function(index, isFade) { HuesCore.prototype.setColour = function(index, isFade) {
this.colourIndex = index; this.colourIndex = index;
var colour = this.colours[this.colourIndex]; var colour = this.colours[this.colourIndex];
this.renderer.setColour(colour.c, isFade); this.callEventListeners("newcolour", colour, isFade);
this.callEventListeners("newcolour", colour);
}; };
HuesCore.prototype.getBeat = function(index) { HuesCore.prototype.getBeat = function(index) {
@ -532,7 +536,6 @@ HuesCore.prototype.getBeat = function(index) {
HuesCore.prototype.beater = function(beat) { HuesCore.prototype.beater = function(beat) {
this.callEventListeners("beat", this.getBeatString(), this.getSafeBeatIndex()); this.callEventListeners("beat", this.getBeatString(), this.getSafeBeatIndex());
this.renderer.beat();
switch(beat) { switch(beat) {
case 'X': case 'X':
case 'x': case 'x':
@ -579,7 +582,6 @@ HuesCore.prototype.beater = function(beat) {
break; break;
} }
} }
this.renderer.stopFade();
this.renderer.doColourFade(fadeLen * this.beatLength); this.renderer.doColourFade(fadeLen * this.beatLength);
this.randomColour(true); this.randomColour(true);
break; break;
@ -665,16 +667,13 @@ HuesCore.prototype.changeUI = function(index) {
this.callEventListeners("newmode", this.isFullAuto); this.callEventListeners("newmode", this.isFullAuto);
this.callEventListeners("newsong", this.currentSong); this.callEventListeners("newsong", this.currentSong);
this.callEventListeners("newimage", this.currentImage); this.callEventListeners("newimage", this.currentImage);
this.callEventListeners("newcolour", this.colours[this.colourIndex]); this.callEventListeners("newcolour", this.colours[this.colourIndex], false);
this.callEventListeners("beat", this.getBeatString(), this.getSafeBeatIndex()); this.callEventListeners("beat", this.getBeatString(), this.getSafeBeatIndex());
} }
}; };
HuesCore.prototype.settingsUpdated = function() { HuesCore.prototype.settingsUpdated = function() {
this.renderer.setSmartAlign(localStorage["smartAlign"]); this.callEventListeners("settingsupdated");
this.renderer.setBlurAmount(localStorage["blurAmount"]);
this.renderer.setBlurDecay(localStorage["blurDecay"]);
this.renderer.setBlurQuality(localStorage["blurQuality"]);
switch (localStorage["currentUI"]) { switch (localStorage["currentUI"]) {
case "retro": case "retro":
this.changeUI(0); this.changeUI(0);

Loading…
Cancel
Save