-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnixie.js
57 lines (57 loc) · 1.56 KB
/
nixie.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Nixie{
constructor(canvas,digits){
this.c=canvas;
this.ctx=this.c.getContext("2d");
this.numDigits=digits;
this.__val=0;
this.digits=[];
var that = this;
for(var i=0;i<11;i++){
var m = new Image();
m.src="img/"+i+".jpg";
this.digits.push(m);
m.onload=function(){
that.draw(0);
}
}
this.ctx.fillRect(0,0,this.c.width,this.c.height);
}
draw(i){
var that=this;
if(i>0){
setTimeout(function(){that.draw(i-1)},50);
this.ctx.fillStyle = "rgba(0, 0, 0, 0.4)";
}
else{
this.ctx.fillStyle = "rgba(0, 0, 0, 1)";
}
this.ctx.globalAlpha = 1;
this.ctx.globalCompositeOperation = "normal";
this.ctx.fillRect(0,0,this.c.width,this.c.height);
if(i>0)this.ctx.globalCompositeOperation = "lighter";
if(i>0)this.ctx.globalAlpha = 0.5;
var number = this.__val,
output = [],
sNumber = number.toString();
for(var i=0;i<sNumber.length;i+=1){
output.push(+sNumber.charAt(i));
}
for(var i=output.length-1;i>-1;i--){
var x=this.c.width-this.c.width/(this.numDigits)*(output.length-i);
var y=(this.c.height-this.c.width/(this.numDigits)*1.94)/2;
this.ctx.drawImage(this.digits[output[i]],x,y,this.c.width/(this.numDigits),this.c.width/(this.numDigits)*1.94)
}
for(var i=0;i<this.numDigits-output.length;i++){
var x=this.c.width/(this.numDigits)*(i);
var y=(this.c.height-this.c.width/(this.numDigits)*1.94)/2;
this.ctx.drawImage(this.digits[10],x,y,this.c.width/(this.numDigits),this.c.width/(this.numDigits)*1.94)
}
}
get value(){
return this.__val;
}
set value(v){
this.__val=v;
this.draw(5);
}
}