ncase.me-trust/js/core/Scratcher.js

101 lines
2.1 KiB
JavaScript
Raw Normal View History

2017-07-13 15:41:43 +00:00
Loader.addToManifest(Loader.manifest,{
scratch_in: "assets/sounds/scratch_in.mp3",
scratch_out: "assets/sounds/scratch_out.mp3"
});
2017-07-10 15:38:20 +00:00
(function(exports){
var Scratcher = {};
exports.Scratcher = Scratcher;
2017-07-13 15:07:33 +00:00
Scratcher.isTransitioning = false;
2017-07-12 13:42:17 +00:00
Scratcher.scratch = function(gotoID){
2017-07-10 15:38:20 +00:00
2017-07-13 15:07:33 +00:00
if(Scratcher.isTransitioning) return;
Scratcher.isTransitioning = true;
2017-07-10 15:38:20 +00:00
var dom = $("#scratcher");
dom.style.display = "block";
2017-07-20 18:45:11 +00:00
dom.className = "scratcher";
2017-07-10 15:38:20 +00:00
2017-07-13 15:07:33 +00:00
var width = $("#main").clientWidth;
var height = $("#main").clientHeight;
dom.style.width = width+"px";
dom.style.height = height+"px";
dom.style.left = -width/2+"px";
dom.style.top = -height/2+"px";
2017-07-15 02:29:31 +00:00
Scratcher.scratchAnim(dom, true)
2017-07-10 15:38:20 +00:00
.then(function(){
2017-07-12 13:42:17 +00:00
if(gotoID){
publish("slideshow/goto", [gotoID]);
}else{
publish("slideshow/next");
}
2017-07-10 15:38:20 +00:00
})
.then(function(){
2017-07-15 02:29:31 +00:00
return Scratcher.scratchAnim(dom, false);
2017-07-10 15:38:20 +00:00
})
.then(function(){
dom.style.display = "none";
2017-07-13 15:07:33 +00:00
Scratcher.isTransitioning = false;
2017-07-10 15:38:20 +00:00
});
};
subscribe("slideshow/scratch", Scratcher.scratch);
2017-07-15 02:29:31 +00:00
Scratcher.scratchAnim = function(dom, scratchIn){
2017-07-13 15:41:43 +00:00
2017-07-10 15:38:20 +00:00
var deferred = Q.defer();
var frame = 0;
var interval = setInterval(function(){
frame++;
if(frame>19){
clearInterval(interval);
deferred.resolve();
}else{
2017-07-15 02:29:31 +00:00
dom.style.backgroundPosition = (scratchIn?0:-100)+"% "+(frame*-100)+"%";
2017-07-10 15:38:20 +00:00
}
},40);
2017-07-13 15:41:43 +00:00
setTimeout(function(){
var sound = scratchIn ? Loader.sounds.scratch_in : Loader.sounds.scratch_out;
2017-07-20 22:16:35 +00:00
sound.play();
2017-07-13 15:41:43 +00:00
},100);
2017-07-10 15:38:20 +00:00
return deferred.promise;
2017-07-13 15:41:43 +00:00
2017-07-10 15:38:20 +00:00
};
2017-07-15 02:29:31 +00:00
Scratcher.smallScratch = function(x,y,width,height,_onChange,_onComplete){
// Create DOM
var scratcher = document.createElement("div");
scratcher.style.left = x+"px";
scratcher.style.top = y+"px";
scratcher.style.width = width+"px";
scratcher.style.height = height+"px";
scratcher.className = "scratcher";
scratcher.style.display = "block";
slideshow.dom.appendChild(scratcher);
// Animate!
Scratcher.scratchAnim(scratcher, true)
.then(function(){
2017-07-17 22:32:02 +00:00
if(_onChange) _onChange();
2017-07-15 02:29:31 +00:00
})
.then(function(){
return Scratcher.scratchAnim(scratcher, false);
})
.then(function(){
slideshow.dom.removeChild(scratcher); // Destroy DOM
2017-07-17 22:32:02 +00:00
if(_onComplete) _onComplete();
2017-07-15 02:29:31 +00:00
});
2017-07-10 15:38:20 +00:00
};
2017-07-15 02:29:31 +00:00
})(window);