|
182 | 182 | // 連接 mqtt server
|
183 | 183 | let webduinoBroadcastor;
|
184 | 184 | if (!webduinoBroadcastor) {
|
185 |
| - webduinoBroadcastor = new webduino.module.mqttClient(); |
186 |
| - await webduinoBroadcastor.connect(); |
| 185 | + webduinoBroadcastor = new window.mqttClient(); |
| 186 | + try { |
| 187 | + await webduinoBroadcastor.connect(); |
| 188 | + } catch (err) { |
| 189 | + console.error(err); |
| 190 | + } |
187 | 191 | }
|
188 | 192 | // 發送 mqtt 訊號
|
189 |
| - const mqttPush = async function (topic, msg) { |
| 193 | + const mqttPush = function (topic, msg) { |
190 | 194 | if (topic == list.topic1) {
|
191 | 195 | webduinoBroadcastor.send({
|
192 | 196 | topic: topic,
|
|
264 | 268 | drag();
|
265 | 269 | function drag() {
|
266 | 270 | let drag = false;
|
267 |
| - let mx, my, dx, dy; |
268 |
| - const move = function (evt) { |
| 271 | + let mx, my, dx, dy, carSize; |
| 272 | + |
| 273 | + function move(evt) { |
269 | 274 | let touches = evt.changedTouches;
|
270 | 275 | if (touches) {
|
271 | 276 | mx = ~~touches[0].pageX;
|
272 | 277 | my = ~~touches[0].pageY;
|
273 | 278 | } else {
|
274 |
| - mx = event.pageX; |
275 |
| - my = event.pageY; |
| 279 | + mx = evt.pageX; |
| 280 | + my = evt.pageY; |
276 | 281 | }
|
277 | 282 | if (drag) {
|
278 | 283 | kebbi.style.left = mx - dx + 'px';
|
279 | 284 | kebbi.style.top = my - dy + 'px';
|
280 | 285 | }
|
281 | 286 | if (kebbi.classList.contains('target')) {
|
282 |
| - let kx = kebbi.offsetLeft; |
283 |
| - let ky = kebbi.offsetTop; |
284 |
| - if (kx < ww * 0.2) { |
| 287 | + const kx = kebbi.offsetLeft; // 車子 left |
| 288 | + const ky = kebbi.offsetTop; // 車子 top |
| 289 | + const carSize = getCarSize(); |
| 290 | + |
| 291 | + // 注意,小車一開始的中心點 y 座標,不等於畫面的中心點 y 座標。 |
| 292 | + // 邊界在中心的周圍 1/3 小車寬/高的距離 |
| 293 | + const kxCenter = kx + carSize.width/2; // 車子中心點 x 座標 |
| 294 | + const kyCenter = ky + carSize.height/2; // 車子中心點 y 座標 |
| 295 | + const leftSide = ww * 0.5 - carSize.width/3; // 左邊界,小於這個值,判定車子移動到左邊 |
| 296 | + const rightSide = ww * 0.5 + carSize.width/3; // 右邊界,大於這個值,判定車子移動到右邊 |
| 297 | + const topSide = oy + carSize.height/6; // 上邊界,小於這個值,判定車子移動到上面 |
| 298 | + const bottomSide = oy + 5 * carSize.height/6; // 下邊界,小於這個值,判定車子移動到下面 |
| 299 | + |
| 300 | + if (kxCenter < leftSide) { |
285 | 301 | if (!send.left) {
|
286 | 302 | sendCheck('left');
|
287 | 303 | mqttPush(list.topic1, list.kebbiLeft);
|
288 | 304 | }
|
289 |
| - } else if (kx > ww * 0.5) { |
| 305 | + } else if (kxCenter > rightSide) { |
290 | 306 | if (!send.right) {
|
291 | 307 | sendCheck('right');
|
292 | 308 | mqttPush(list.topic1, list.kebbiRight);
|
293 | 309 | }
|
294 | 310 | } else {
|
295 |
| - if (ky < wh * 0.2) { |
| 311 | + if (kyCenter < topSide) { |
296 | 312 | if (!send.top) {
|
297 | 313 | sendCheck('top');
|
298 | 314 | mqttPush(list.topic1, list.kebbiTop);
|
299 | 315 | }
|
300 |
| - } else if (ky > wh * 0.4) { |
| 316 | + } else if (kyCenter > bottomSide) { |
301 | 317 | if (!send.bottom) {
|
302 | 318 | sendCheck('bottom');
|
303 | 319 | mqttPush(list.topic1, list.kebbiBottom);
|
|
310 | 326 | }
|
311 | 327 | }
|
312 | 328 | }
|
313 |
| - }; |
314 |
| - const target = function (evt) { |
| 329 | + } |
| 330 | + |
| 331 | + function target(evt) { |
315 | 332 | evt.preventDefault();
|
316 | 333 | kebbi.classList.remove('reset');
|
317 | 334 | let touches = evt.changedTouches;
|
|
323 | 340 | dx = mx - kebbi.offsetLeft;
|
324 | 341 | dy = my - kebbi.offsetTop;
|
325 | 342 | kebbi.classList.add('target');
|
326 |
| - }; |
327 |
| - document.addEventListener('mousemove', move); |
328 |
| - kebbi.addEventListener('touchmove', move); |
| 343 | + updateCarSize(); |
| 344 | + sendCheck('center'); // 設定初始狀態為 center |
| 345 | + } |
329 | 346 |
|
330 | 347 | function reset() {
|
331 | 348 | if (send.top || send.bottom || send.left || send.right) {
|
|
340 | 357 | kebbi.style.top = `${oy}px`;
|
341 | 358 | }
|
342 | 359 |
|
343 |
| - document.addEventListener('mouseup', reset); |
344 |
| - kebbi.addEventListener('touchend', reset); |
| 360 | + function updateCarSize() { |
| 361 | + const car = document.getElementById('svgKebbi'); |
| 362 | + carSize = { |
| 363 | + width: car.offsetWidth, |
| 364 | + height: car.offsetHeight, |
| 365 | + }; |
| 366 | + } |
| 367 | + |
| 368 | + function getCarSize() { |
| 369 | + return carSize; |
| 370 | + } |
345 | 371 |
|
| 372 | + document.addEventListener('mousemove', move); |
| 373 | + document.addEventListener('mouseup', reset); |
346 | 374 | kebbi.addEventListener('mousedown', target);
|
| 375 | + |
| 376 | + kebbi.addEventListener('touchmove', move); |
| 377 | + kebbi.addEventListener('touchend', reset); |
347 | 378 | kebbi.addEventListener('touchstart', target);
|
348 | 379 | }
|
349 | 380 |
|
|
0 commit comments