2013-10-16 15:57:30 +02:00
function Progressbar ( ) {
this . currProgress = 0 ; // default val
this . progressbarFGImg = new Image ( ) ;
this . progressbarFGImgSrc = "img/progress_fg.png" ;
this . progressbarBGImg = new Image ( ) ;
this . progressbarBGImgSrc = "img/progress_bg.png" ;
this . progressWidth = 93 ;
this . progressHeight = 82 ;
this . quartPI = . 5 * Math . PI ;
this . twoPI = 2 * Math . PI ;
2013-10-23 18:10:45 +02:00
// To make the progressbar start with a minimal amount of 'progress'
// so that you can visually see that there is progress
this . progressPadding = Math . PI * . 1 ;
2013-10-16 15:57:30 +02:00
this . $canvas ;
this . canvas ;
this . context ;
2013-10-16 23:11:06 +02:00
this . $container ;
2013-10-16 15:57:30 +02:00
this . isInitted = false ;
2013-10-16 23:11:06 +02:00
this . enabled = true ;
this . init = function ( targCanvas , targCanvasContainer ) {
2013-10-16 15:57:30 +02:00
console . log ( "Thermometer.init()" ) ;
2013-10-16 23:11:06 +02:00
this . $container = targCanvasContainer ;
2013-10-16 15:57:30 +02:00
this . $canvas = targCanvas ;
this . canvas = this . $canvas [ 0 ] ;
this . context = this . canvas . getContext ( '2d' ) ;
var self = this ;
this . progressbarBGImg . onload = function ( ) {
console . log ( "progressbarBGImg img loaded" ) ;
// self.isInitted = true;
// self.update(self.currentTemperature, self.targetTemperature);
self . progressbarFGImg . onload = function ( ) {
console . log ( "progressbarFGImg img loaded" ) ;
self . isInitted = true ;
self . update ( 0 , 100 ) ;
} ;
self . progressbarFGImg . src = self . progressbarFGImgSrc ;
} ;
this . progressbarBGImg . src = this . progressbarBGImgSrc ;
}
this . update = function ( part , total ) {
2013-10-18 19:11:10 +02:00
//console.log("Progressbar.update(" + part + "," + total + ")");
2013-10-16 15:57:30 +02:00
var pct = part / total ;
if ( this . isInitted ) {
if ( part == undefined ) part = 0 ;
if ( total == undefined ) total = 100 ; // prevent divide by zero
var progress = part / total ;
progress = Math . min ( progress , 1.0 ) ;
progress = Math . max ( progress , 0 ) ;
2013-10-18 19:11:10 +02:00
//console.log("progressbar >> f:update() >> progress: " + progress);
2013-10-16 15:57:30 +02:00
// clear
this . context . clearRect ( 0 , 0 , this . canvas . width , this . canvas . height ) ;
this . context . drawImage ( this . progressbarBGImg , 0 , 0 ) ;
this . context . font = "7pt sans-serif" ;
// draw the progressbar foreground's clipping path
this . context . save ( ) ;
this . context . beginPath ( ) ;
this . context . moveTo ( 45 , 45 ) ;
this . context . lineTo ( 45 , 0 ) ;
2013-10-23 18:10:45 +02:00
this . context . arc ( 45 , 45 , 45 , - this . quartPI , - this . quartPI + this . progressPadding + ( progress * ( this . twoPI - this . progressPadding ) ) , false ) ; // circle bottom of thermometer
2013-10-16 15:57:30 +02:00
this . context . lineTo ( 45 , 45 ) ;
this . context . clip ( ) ;
this . context . drawImage ( this . progressbarFGImg , 0 , 0 ) ;
this . context . restore ( ) ;
if ( debugMode ) {
this . context . fillStyle = '#222' ;
this . context . strokeStyle = '#fff' ;
this . context . lineWidth = 3 ;
this . context . textAlign = "center" ;
this . context . strokeText ( part + " / " + total , 45 , 45 , 90 ) ;
this . context . fillText ( part + " / " + total , 45 , 45 , 90 ) ;
}
} else {
console . log ( "Progressbar.setTemperature() -> thermometer not initialized!" ) ;
}
}
2013-10-16 23:11:06 +02:00
this . show = function ( ) {
this . $container . addClass ( "progressbarAppear" ) ;
// this.$container.show();
this . enabled = true ;
}
this . hide = function ( ) {
this . $container . removeClass ( "progressbarAppear" ) ;
// this.$container.hide();
this . enabled = false ;
}
2013-10-16 15:57:30 +02:00
}