-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes: New timeline generation mechanism, normalized to 24 hours
- Loading branch information
Showing
5 changed files
with
252 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ system_DATA = \ | |
style.css \ | ||
texture.jpg \ | ||
xml-background.js \ | ||
xml2json.js \ | ||
$(NULL) | ||
|
||
CLEANFILES = \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,193 +1,157 @@ | ||
// starttime | state1 | transition | state2 | transition | state3 | transition | ||
// 07.00 | 1 | 2 | 1 | 2 | 1 | 2 | ||
// | 0 | 1 | 3 | 4 | 6 | 7 | ||
|
||
// handlers | ||
window.handle = { | ||
var handle = { | ||
timeout : null | ||
} | ||
|
||
var XmlBackground = function() { | ||
/* | ||
* Use singleton pattern to maintain a single background sequence items handler object | ||
*/ | ||
if (XmlBackground.prototype.instance) { | ||
return XmlBackground.prototype.instance; | ||
} | ||
XmlBackground.prototype.instance = this; | ||
}; | ||
|
||
XmlBackground.prototype.setFile = function(file) { | ||
|
||
// set file and load it | ||
this.file = file; | ||
this.load(); | ||
|
||
}; | ||
|
||
XmlBackground.prototype.load = function() { | ||
|
||
// loading file from local disk | ||
if (!this.file) { | ||
return; | ||
} | ||
var XmlBg = function(){}; | ||
XmlBg.prototype.load = function(file){ | ||
var self = this; | ||
|
||
$.ajax({ | ||
url: self.file | ||
}).done(function(data) { | ||
self.$data = $(data); | ||
self.run(); | ||
url: file, | ||
cache: false, | ||
dataType : "text", | ||
processData : false, | ||
}) | ||
.done( function (data) { | ||
self.parse (data); | ||
}); | ||
}; | ||
} | ||
|
||
XmlBackground.prototype.getItem = function() { | ||
XmlBg.prototype.parse = function (data) { | ||
var seq = $.xml2json(data); | ||
|
||
// get current date | ||
var now = new Date(); | ||
var acc = 0; | ||
var i = this.$data.children().length - 1; | ||
var delta = (now - this.startTime) / 1000; | ||
if (seq.hasOwnProperty("background")) { | ||
var bg = seq.background; | ||
|
||
var self = this; | ||
var startTime = bg.starttime; | ||
|
||
while (i > 1) { | ||
this.startTick = new Date(); | ||
this.startTick.setHours(startTime.hour); | ||
this.startTick.setMinutes(startTime.minute); | ||
this.startTick.setSeconds(startTime.second); | ||
|
||
var item = this.$data.children()[i]; | ||
var timeline = []; | ||
|
||
if (item.nodeName.toLowerCase() != "starttime" && delta >= (item.delta) ) { | ||
|
||
var hasNext = i < (this.$data.children().length - 1); | ||
var nextItem; | ||
var when = 0; | ||
var initialOpacity = 1.0; | ||
for (var i = 0; i < bg.static.length; i++) { | ||
bg.static[i].type = "static"; | ||
bg.transition[i].type = "transition"; | ||
timeline.push(bg.static[i]); | ||
timeline.push(bg.transition[i]); | ||
} | ||
|
||
if (hasNext) { | ||
for (var i = 0; i < timeline.length; i++) { | ||
if (i == 0) { | ||
timeline[i].span = 0; | ||
} | ||
else { | ||
timeline[i].span = timeline[i - 1].span + parseInt(timeline[i - 1].duration); | ||
} | ||
|
||
} | ||
|
||
nextItem = this.$data.children()[ i + 1 ]; | ||
this.timeline = timeline; | ||
this.where(); | ||
} | ||
} | ||
|
||
// when is in seconds | ||
when = nextItem.delta - delta; | ||
initialOpacity = 1 - (when/nextItem.duration); | ||
//"00.00 - 23" | ||
//"07.00 - 06.00" | ||
// | ||
// where are we? (search for a time-frame inside the timeline) | ||
// | ||
XmlBg.prototype.where = function(){ | ||
|
||
if (nextItem.nodeName.toLowerCase() == "transition") { | ||
var now = new Date(); | ||
var delta = (now - this.startTick).valueOf()/1000; | ||
var frame; | ||
|
||
// handle next item | ||
self.transitionDuration = when; | ||
if (delta < 0) { | ||
delta = 24 + (delta / 3600); | ||
delta *= 3600; | ||
} | ||
|
||
// background - to | ||
self.handleItem(nextItem); | ||
var timeline = this.timeline; | ||
var i = timeline.length; | ||
|
||
// need hacks on this. The initial opacity should reflect current setup | ||
$("#bg").css("opacity", initialOpacity); | ||
$("#overlay").css("opacity", 1 - initialOpacity); | ||
while (i--) { | ||
var frame = timeline[i]; | ||
if (frame.span <= delta) { | ||
// we got the frame here; | ||
this.handle(frame, frame.duration - (delta - frame.span)); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
$("#bg").css("-webkit-transition-property", "opacity"); | ||
$("#bg").css("-webkit-transition-duration", self.transitionDuration + "s"); | ||
$("#bg").css("-webkit-transition-timing-function", "linear"); | ||
$("#bg").css("opacity", 1.0); | ||
XmlBg.prototype.handle = function (frame, next){ | ||
|
||
// overlay - from | ||
$("#overlay").css("-webkit-transition-property", "opacity"); | ||
var self = this; | ||
|
||
// especially this line | ||
$("#overlay").css("-webkit-transition-duration", self.transitionDuration + "s"); | ||
$("#overlay").css("-webkit-transition-timing-function", "linear"); | ||
$("#overlay").css("opacity", 0.0); | ||
} | ||
else { | ||
if (frame.type == "static") { | ||
|
||
// handle item | ||
self.handleItem(item); | ||
} | ||
|
||
} else { | ||
// handle current item | ||
self.handleItem(item); | ||
} | ||
// do render image | ||
$("#overlay").css("opacity", 0.0); | ||
$("#bg").css("background-image", "url(" + frame.file + ")"); | ||
|
||
if (nextItem && when) { | ||
} else { | ||
|
||
if (window.handle.timeout) { | ||
clearTimeout(window.handle.timeout); | ||
} | ||
|
||
window.handle.timeout = setTimeout(function(){ | ||
self.getItem(); | ||
}, when * 1000); | ||
// do css animation | ||
$("#overlay").css("background-image", "url(" + frame.from + ")"); | ||
$("#bg").css("background-image", "url(" + frame.to.replace("bright-day", "good-night") + ")"); | ||
|
||
} | ||
var initialOpacity = next/frame.duration; | ||
|
||
// break the loop | ||
break; | ||
} | ||
$("#overlay").css("opacity", initialOpacity); | ||
$("#bg").css("opacity", 1 - initialOpacity); | ||
|
||
i--; | ||
} | ||
} | ||
self.next = next; | ||
|
||
XmlBackground.prototype.handleItem = function(item) { | ||
|
||
var file; | ||
// next tick to set webkit transition | ||
setTimeout(function(){ | ||
|
||
$("#overlay").css("-webkit-transition-property", "opacity"); | ||
$("#overlay").css("-webkit-transition-duration", self.next + "s"); | ||
$("#overlay").css("-webkit-transition-timing-function", "linear"); | ||
$("#overlay").css("opacity", 0.0); | ||
|
||
if (item.nodeName.toLowerCase() == "static") { | ||
$("#bg").css("-webkit-transition-property", "opacity"); | ||
$("#bg").css("-webkit-transition-duration", self.next + "s"); | ||
$("#bg").css("-webkit-transition-timing-function", "linear"); | ||
$("#bg").css("opacity", 1.0); | ||
|
||
file = item.querySelector("file").textContent; | ||
$("#bg").css("background-image", "url(" + file + ")"); | ||
}, 1); | ||
} | ||
|
||
} else { | ||
|
||
overlay = item.querySelector("from").textContent; | ||
file = item.querySelector("to").textContent; | ||
clearTimeout(window.handle.timeout); | ||
|
||
$("#overlay").css("background-image", "url(" + overlay + ")"); | ||
$("#bg").css("background-image", "url(" + file + ")"); | ||
window.handle.timeout = setTimeout(function(){ | ||
self.where(); | ||
}, next * 1000); | ||
|
||
} | ||
console.log("handle"); | ||
} | ||
|
||
XmlBackground.prototype.reset = function() { | ||
XmlBg.prototype.reset = function (){ | ||
|
||
// reset timeout | ||
if (window.handle.timeout) { | ||
clearTimeout(window.handle.timeout); | ||
} | ||
clearTimeout(window.handle.timeout); | ||
|
||
// reset transition | ||
$("#overlay").css("transition", "none"); | ||
$("#bg").css("transition", "none"); | ||
} | ||
$("#overlay").css("-webkit-transition-property", "none"); | ||
$("#overlay").css("-webkit-transition-duration", "none"); | ||
|
||
XmlBackground.prototype.run = function() { | ||
|
||
// start time | ||
var $startTime = this.$data.find("starttime"); | ||
$("#bg").css("-webkit-transition-property", "none"); | ||
$("#bg").css("-webkit-transition-duration", "none"); | ||
|
||
// new Date(); | ||
this.startTime = new Date(); | ||
this.startTime.setHours($startTime.find("hour").text()); | ||
this.startTime.setMinutes($startTime.find("minute").text()); | ||
this.startTime.setSeconds($startTime.find("second").text()); | ||
|
||
// accumulation | ||
var acc = 0; | ||
|
||
// Populate deltas of each item | ||
for (var i = 0; i < this.$data.children().length; i ++) { | ||
|
||
// item children | ||
var item = this.$data.children()[i]; | ||
|
||
// if not starttime element | ||
if (item.nodeName.toLowerCase() != "starttime") { | ||
|
||
// set delta | ||
item.delta = item.delta || 0; | ||
item.i = i; | ||
item.duration = parseInt(item.querySelector("duration").textContent); | ||
|
||
item.delta += (acc + item.duration); | ||
acc = item.delta; | ||
} | ||
} | ||
setTimeout(function(){ | ||
$("#bg").css("opacity", 1.0); | ||
$("#overlay").css("opacity", 0.0); | ||
}, 1); | ||
} | ||
|
||
// | ||
this.getItem(); | ||
}; | ||
$(function(){ | ||
var xmlBg = new XmlBg(); | ||
window.xmlBg = xmlBg; | ||
}); |
Oops, something went wrong.