|
169 | 169 | // 連接 mqtt server
|
170 | 170 | let webduinoBroadcastor;
|
171 | 171 | if (!webduinoBroadcastor) {
|
172 |
| - webduinoBroadcastor = new webduino.module.mqttClient(); |
173 |
| - await webduinoBroadcastor.connect(); |
| 172 | + webduinoBroadcastor = new window.mqttClient(); |
| 173 | + try { |
| 174 | + await webduinoBroadcastor.connect(); |
| 175 | + } catch (err) { |
| 176 | + console.error(err); |
| 177 | + } |
174 | 178 | }
|
175 | 179 | // 發送 mqtt 訊號
|
176 |
| - const mqttPush = async function (topic, msg) { |
| 180 | + const mqttPush = function (topic, msg) { |
177 | 181 | if (topic == list.topic1) {
|
178 | 182 | webduinoBroadcastor.send({
|
179 | 183 | topic: topic,
|
|
249 | 253 | drag();
|
250 | 254 | function drag() {
|
251 | 255 | let drag = false;
|
252 |
| - let mx, my, dx, dy; |
253 |
| - const move = function (evt) { |
| 256 | + let mx, my, dx, dy, carSize; |
| 257 | + |
| 258 | + function move(evt) { |
254 | 259 | let touches = evt.changedTouches;
|
255 | 260 | if (touches) {
|
256 | 261 | mx = ~~touches[0].pageX;
|
257 | 262 | my = ~~touches[0].pageY;
|
258 | 263 | } else {
|
259 |
| - mx = event.pageX; |
260 |
| - my = event.pageY; |
| 264 | + mx = evt.pageX; |
| 265 | + my = evt.pageY; |
261 | 266 | }
|
262 | 267 | if (drag) {
|
263 | 268 | kebbi.style.left = mx - dx + 'px';
|
264 | 269 | kebbi.style.top = my - dy + 'px';
|
265 | 270 | }
|
266 | 271 | if (kebbi.classList.contains('target')) {
|
267 |
| - let kx = kebbi.offsetLeft; |
268 |
| - let ky = kebbi.offsetTop; |
269 |
| - if (kx < ww * 0.2) { |
| 272 | + const kx = kebbi.offsetLeft; // 車子 left |
| 273 | + const ky = kebbi.offsetTop; // 車子 top |
| 274 | + const carSize = getCarSize(); |
| 275 | + |
| 276 | + // 注意,小車一開始的中心點 y 座標,不等於畫面的中心點 y 座標。 |
| 277 | + // 邊界在中心的周圍 1/3 小車寬/高的距離 |
| 278 | + const kxCenter = kx + carSize.width/2; // 車子中心點 x 座標 |
| 279 | + const kyCenter = ky + carSize.height/2; // 車子中心點 y 座標 |
| 280 | + const leftSide = ww * 0.5 - carSize.width/3; // 左邊界,小於這個值,判定車子移動到左邊 |
| 281 | + const rightSide = ww * 0.5 + carSize.width/3; // 右邊界,大於這個值,判定車子移動到右邊 |
| 282 | + const topSide = oy + carSize.height/6; // 上邊界,小於這個值,判定車子移動到上面 |
| 283 | + const bottomSide = oy + 5 * carSize.height/6; // 下邊界,小於這個值,判定車子移動到下面 |
| 284 | + |
| 285 | + if (kxCenter < leftSide) { |
270 | 286 | if (!send.left) {
|
271 | 287 | sendCheck('left');
|
272 | 288 | mqttPush(list.topic1, list.kebbiLeft);
|
273 | 289 | }
|
274 |
| - } else if (kx > ww * 0.5) { |
| 290 | + } else if (kxCenter > rightSide) { |
275 | 291 | if (!send.right) {
|
276 | 292 | sendCheck('right');
|
277 | 293 | mqttPush(list.topic1, list.kebbiRight);
|
278 | 294 | }
|
279 | 295 | } else {
|
280 |
| - if (ky < wh * 0.2) { |
| 296 | + if (kyCenter < topSide) { |
281 | 297 | if (!send.top) {
|
282 | 298 | sendCheck('top');
|
283 | 299 | mqttPush(list.topic1, list.kebbiTop);
|
284 | 300 | }
|
285 |
| - } else if (ky > wh * 0.4) { |
| 301 | + } else if (kyCenter > bottomSide) { |
286 | 302 | if (!send.bottom) {
|
287 | 303 | sendCheck('bottom');
|
288 | 304 | mqttPush(list.topic1, list.kebbiBottom);
|
|
295 | 311 | }
|
296 | 312 | }
|
297 | 313 | }
|
298 |
| - }; |
299 |
| - const target = function (evt) { |
| 314 | + } |
| 315 | + |
| 316 | + function target(evt) { |
300 | 317 | evt.preventDefault();
|
301 | 318 | kebbi.classList.remove('reset');
|
302 | 319 | let touches = evt.changedTouches;
|
|
308 | 325 | dx = mx - kebbi.offsetLeft;
|
309 | 326 | dy = my - kebbi.offsetTop;
|
310 | 327 | kebbi.classList.add('target');
|
311 |
| - }; |
312 |
| - document.addEventListener('mousemove', move); |
313 |
| - kebbi.addEventListener('touchmove', move); |
| 328 | + updateCarSize(); |
| 329 | + sendCheck('center'); // 設定初始狀態為 center |
| 330 | + } |
314 | 331 |
|
315 | 332 | function reset() {
|
316 | 333 | if (send.top || send.bottom || send.left || send.right) {
|
|
325 | 342 | kebbi.style.top = `${oy}px`;
|
326 | 343 | }
|
327 | 344 |
|
328 |
| - document.addEventListener('mouseup', reset); |
329 |
| - kebbi.addEventListener('touchend', reset); |
| 345 | + function updateCarSize() { |
| 346 | + const car = document.getElementById('svgKebbi'); |
| 347 | + carSize = { |
| 348 | + width: car.offsetWidth, |
| 349 | + height: car.offsetHeight, |
| 350 | + }; |
| 351 | + } |
| 352 | + |
| 353 | + function getCarSize() { |
| 354 | + return carSize; |
| 355 | + } |
330 | 356 |
|
| 357 | + document.addEventListener('mousemove', move); |
| 358 | + document.addEventListener('mouseup', reset); |
331 | 359 | kebbi.addEventListener('mousedown', target);
|
| 360 | + |
| 361 | + kebbi.addEventListener('touchmove', move); |
| 362 | + kebbi.addEventListener('touchend', reset); |
332 | 363 | kebbi.addEventListener('touchstart', target);
|
333 | 364 | }
|
334 | 365 |
|
|
0 commit comments