-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
132 lines (116 loc) · 4.08 KB
/
app.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var alternate=0;
function prober(){
//checks if somethign changed
if(alternate=1){
r=255;g=51;b=102;
alternate-=1;
}else{
alterna
r=51;g=255;b=102;
alternate+=1;
}
playbulbCandle.setFlashingColor(r, g, b).then(onColorChanged);
alert('ran');
setTimeout(prober,5000);
}
document.querySelector('#connect').addEventListener('click', event => {
document.querySelector('#state').classList.add('connecting');
playbulbCandle.connect()
.then(() => {
console.log(playbulbCandle.device);
document.querySelector('#state').classList.remove('connecting');
document.querySelector('#state').classList.add('connected');
/* active so trigger what you want */
prober();
return playbulbCandle.getDeviceName().then(handleDeviceName)
.then(() => playbulbCandle.getBatteryLevel().then(handleBatteryLevel));
})
.catch(error => {
console.error('Argh!', error);
});
});
function handleDeviceName(deviceName) {
document.querySelector('#deviceName').value = deviceName;
}
function handleBatteryLevel(batteryLevel) { //
document.querySelector('#batteryLevel').textContent = batteryLevel + '%';
}
function changeColor() {
var effect = document.querySelector('[name="effectSwitch"]:checked').id;
switch(effect) {
case 'noEffect':
playbulbCandle.setColor(r, g, b).then(onColorChanged);
break;
case 'candleEffect':
playbulbCandle.setCandleEffectColor(r, g, b).then(onColorChanged);
break;
case 'flashing':
playbulbCandle.setFlashingColor(r, g, b).then(onColorChanged);
break;
case 'pulse':
playbulbCandle.setPulseColor(r, g, b).then(onColorChanged);
break;
case 'rainbow':
playbulbCandle.setRainbow().then(onColorChanged);
break;
case 'rainbowFade':
playbulbCandle.setRainbowFade().then(onColorChanged);
break;
}
}
document.querySelector('#deviceName').addEventListener('input', event => {
playbulbCandle.setDeviceName(event.target.value)
.then(() => {
console.log('Device name changed to ' + event.target.value);
})
.catch(error => {
console.error('Argh!', error);
});
});
var r = g = b = 255;
function onColorChanged(rgb) {
if (rgb) {
console.log('Color changed to ' + rgb);
alert('Color changed to ' + rgb);
r = rgb[0]; g = rgb[1]; b = rgb[2];
} else {
console.log('Color changed');
}
}
var img = new Image();
img.src = 'color-wheel.png';
img.onload = function() {
var canvas = document.querySelector('canvas');
var context = canvas.getContext('2d');
canvas.width = 300 * devicePixelRatio;
canvas.height = 300 * devicePixelRatio;
canvas.style.width = "300px";
canvas.style.height = "300px";
canvas.addEventListener('click', function(evt) {
// Refresh canvas in case user zooms and devicePixelRatio changes.
canvas.width = 300 * devicePixelRatio;
canvas.height = 300 * devicePixelRatio;
context.drawImage(img, 0, 0, canvas.width, canvas.height);
var rect = canvas.getBoundingClientRect();
var x = Math.round((evt.clientX - rect.left) * devicePixelRatio);
var y = Math.round((evt.clientY - rect.top) * devicePixelRatio);
var data = context.getImageData(0, 0, canvas.width, canvas.height).data;
r = data[((canvas.width * y) + x) * 4];
g = data[((canvas.width * y) + x) * 4 + 1];
b = data[((canvas.width * y) + x) * 4 + 2];
changeColor();
context.beginPath();
context.arc(x, y + 2, 10 * devicePixelRatio, 0, 2 * Math.PI, false);
context.shadowColor = '#333';
context.shadowBlur = 4 * devicePixelRatio;
context.fillStyle = 'white';
context.fill();
});
context.drawImage(img, 0, 0, canvas.width, canvas.height);
}
document.querySelector('#noEffect').addEventListener('click', changeColor);
document.querySelector('#candleEffect').addEventListener('click', changeColor);
document.querySelector('#flashing').addEventListener('click', changeColor);
document.querySelector('#pulse').addEventListener('click', changeColor);
document.querySelector('#rainbow').addEventListener('click', changeColor);
document.querySelector('#rainbowFade').addEventListener('click', changeColor);