-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
41 lines (34 loc) · 1.16 KB
/
index.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
module.exports = CanvasBattery = function(element, charge) {
this.element = element;
this.context = element.getContext('2d');
this.charge = (typeof charge == 'number') ? charge : 0;
this.drawWalls();
if(this.charge) this.drawCharge(this.charge);
return this;
};
CanvasBattery.prototype.drawWalls = function() {
this.context.beginPath();
this.context.rect(5, 5, 180, 90);
this.context.lineWidth = 10;
this.context.strokeStyle = 'black';
this.context.stroke();
this.context.beginPath();
this.context.rect(190, 40, 10, 20);
this.context.fillStyle = 'black';
this.context.fill();
this.context.stroke();
};
CanvasBattery.prototype.drawCharge = function(charge) {
this.context.beginPath();
this.context.rect(10, 10, 170 * (charge/100), 80);
this.context.fillStyle = 'rgb('+ Math.floor((1-(charge/100))*255) + ',' + Math.floor((charge/100)*255) + ',0)';
this.context.fill();
};
CanvasBattery.prototype.clearCharge = function() {
this.context.clearRect(10, 10, 170, 80);
};
CanvasBattery.prototype.update = function(charge) {
this.charge = (typeof charge == 'number') ? charge : this.charge;
this.clearCharge();
this.drawCharge(this.charge);
};