forked from WebBluetoothCG/demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
149 lines (127 loc) · 4.45 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var r = g = b = 255; // White by default.
document.querySelector('#connect').addEventListener('click', function() {
document.querySelector('#state').classList.add('connecting');
playbulbCandle.connect()
.then(() => {
document.querySelector('#state').classList.remove('connecting');
document.querySelector('#state').classList.add('connected');
return playbulbCandle.getDeviceName().then(handleDeviceName)
.then(() => playbulbCandle.getBatteryLevel().then(handleBatteryLevel));
})
.catch(error => {
// TODO: Replace with toast when snackbar lands.
console.error('Argh!', error);
});
});
function handleDeviceName(deviceName) {
document.querySelector('#deviceName').value = deviceName;
}
function handleBatteryLevel(batteryLevel) {
document.querySelector('#batteryLevel').textContent = batteryLevel + '%';
}
/* Device name */
document.querySelector('#deviceName').addEventListener('input', function() {
playbulbCandle.setDeviceName(this.value)
.catch(error => {
console.error('Argh!', error);
});
});
/* Color picker */
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', draw);
canvas.addEventListener('touchmove', function(evt) {
evt.preventDefault();
draw(evt.targetTouches[0]);
});
function draw(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);
}
/* Color effects */
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);
var colorChanging = false;
function changeColor() {
if (colorChanging) {
return;
}
colorChanging = true;
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;
}
}
function onColorChanged(rgb) {
if (rgb) {
console.log('Color changed to ' + rgb);
r = rgb[0];
g = rgb[1];
b = rgb[2];
} else {
console.log('Color changed');
}
colorChanging = false;
}
window.addEventListener('unhandledrejection', function() {
colorChanging = false;
});
window.onload = function() {
var connect = document.getElementById("connect");
var no_bt = document.getElementById("no-bluetooth");
if (navigator.bluetooth == undefined) {
console.log("No navigator.bluetooth found.");
connect.style.display = "none";
no_bt.style.display = "block";
} else {
connect.style.display = "block";
no_bt.style.display = "none";
}
screen.orientation.lock('portrait').catch(e => e);
};