Skip to content

Commit 14afa24

Browse files
authored
Merge pull request #2 from shengyuan/bug/4686
Bug/4686
2 parents 2f3ccc0 + d4d26d9 commit 14afa24

File tree

5 files changed

+150
-22
lines changed

5 files changed

+150
-22
lines changed

Diff for: CHANGELOG.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
8+
## 2022.2.25
9+
10+
### Fixed
11+
12+
- 調整小車拖拉時的觸發事件範圍,調整為小車的 1/3 寬/高。
13+
- mqtt 斷線時,自動重連
14+
15+
### Changed
16+
17+
- 拿掉 webduinojs 的部份
18+
- 獨立的 mqttClient.js

Diff for: index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,8 @@ <h4>儲存成功</h4>
499499
</div>
500500
</div>
501501

502-
<script src="https://webbit.webduino.io/blockly/dist/lib/webduino-all.min.js"></script>
503-
<script src="https://webbit.webduino.io/blockly/custom-blocks/radio/mqttClient.min.js"></script>
502+
<script src="js/paho-mqtt-min.js"></script>
503+
<script src="js/mqttClient.min.js"></script>
504504
<script src="js/clipboard.min.js "></script>
505505
<script src="js/firebase.js"></script>
506506
<script src="js/main.js"></script>

Diff for: js/main.js

+51-20
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,15 @@
182182
// 連接 mqtt server
183183
let webduinoBroadcastor;
184184
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+
}
187191
}
188192
// 發送 mqtt 訊號
189-
const mqttPush = async function (topic, msg) {
193+
const mqttPush = function (topic, msg) {
190194
if (topic == list.topic1) {
191195
webduinoBroadcastor.send({
192196
topic: topic,
@@ -264,40 +268,52 @@
264268
drag();
265269
function drag() {
266270
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) {
269274
let touches = evt.changedTouches;
270275
if (touches) {
271276
mx = ~~touches[0].pageX;
272277
my = ~~touches[0].pageY;
273278
} else {
274-
mx = event.pageX;
275-
my = event.pageY;
279+
mx = evt.pageX;
280+
my = evt.pageY;
276281
}
277282
if (drag) {
278283
kebbi.style.left = mx - dx + 'px';
279284
kebbi.style.top = my - dy + 'px';
280285
}
281286
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) {
285301
if (!send.left) {
286302
sendCheck('left');
287303
mqttPush(list.topic1, list.kebbiLeft);
288304
}
289-
} else if (kx > ww * 0.5) {
305+
} else if (kxCenter > rightSide) {
290306
if (!send.right) {
291307
sendCheck('right');
292308
mqttPush(list.topic1, list.kebbiRight);
293309
}
294310
} else {
295-
if (ky < wh * 0.2) {
311+
if (kyCenter < topSide) {
296312
if (!send.top) {
297313
sendCheck('top');
298314
mqttPush(list.topic1, list.kebbiTop);
299315
}
300-
} else if (ky > wh * 0.4) {
316+
} else if (kyCenter > bottomSide) {
301317
if (!send.bottom) {
302318
sendCheck('bottom');
303319
mqttPush(list.topic1, list.kebbiBottom);
@@ -310,8 +326,9 @@
310326
}
311327
}
312328
}
313-
};
314-
const target = function (evt) {
329+
}
330+
331+
function target(evt) {
315332
evt.preventDefault();
316333
kebbi.classList.remove('reset');
317334
let touches = evt.changedTouches;
@@ -323,9 +340,9 @@
323340
dx = mx - kebbi.offsetLeft;
324341
dy = my - kebbi.offsetTop;
325342
kebbi.classList.add('target');
326-
};
327-
document.addEventListener('mousemove', move);
328-
kebbi.addEventListener('touchmove', move);
343+
updateCarSize();
344+
sendCheck('center'); // 設定初始狀態為 center
345+
}
329346

330347
function reset() {
331348
if (send.top || send.bottom || send.left || send.right) {
@@ -340,10 +357,24 @@
340357
kebbi.style.top = `${oy}px`;
341358
}
342359

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+
}
345371

372+
document.addEventListener('mousemove', move);
373+
document.addEventListener('mouseup', reset);
346374
kebbi.addEventListener('mousedown', target);
375+
376+
kebbi.addEventListener('touchmove', move);
377+
kebbi.addEventListener('touchend', reset);
347378
kebbi.addEventListener('touchstart', target);
348379
}
349380

Diff for: js/mqttClient.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)