Skip to content

Commit cfa15d2

Browse files
committed
fix: 🐛 fixed draging area to trigger event and refactor
1 parent 60af901 commit cfa15d2

File tree

5 files changed

+150
-22
lines changed

5 files changed

+150
-22
lines changed

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

index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -495,8 +495,8 @@ <h4>儲存成功</h4>
495495
</div>
496496
</div>
497497

498-
<script src="https://webbit.webduino.io/blockly/dist/lib/webduino-all.min.js"></script>
499-
<script src="https://webbit.webduino.io/blockly/custom-blocks/radio/mqttClient.min.js"></script>
498+
<script src="js/paho-mqtt-min.js"></script>
499+
<script src="js/mqttClient.min.js"></script>
500500
<script src="js/clipboard.min.js "></script>
501501
<script src="js/firebase.js"></script>
502502
<script src="js/main.js"></script>

js/main.js

+51-20
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,15 @@
169169
// 連接 mqtt server
170170
let webduinoBroadcastor;
171171
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+
}
174178
}
175179
// 發送 mqtt 訊號
176-
const mqttPush = async function (topic, msg) {
180+
const mqttPush = function (topic, msg) {
177181
if (topic == list.topic1) {
178182
webduinoBroadcastor.send({
179183
topic: topic,
@@ -249,40 +253,52 @@
249253
drag();
250254
function drag() {
251255
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) {
254259
let touches = evt.changedTouches;
255260
if (touches) {
256261
mx = ~~touches[0].pageX;
257262
my = ~~touches[0].pageY;
258263
} else {
259-
mx = event.pageX;
260-
my = event.pageY;
264+
mx = evt.pageX;
265+
my = evt.pageY;
261266
}
262267
if (drag) {
263268
kebbi.style.left = mx - dx + 'px';
264269
kebbi.style.top = my - dy + 'px';
265270
}
266271
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) {
270286
if (!send.left) {
271287
sendCheck('left');
272288
mqttPush(list.topic1, list.kebbiLeft);
273289
}
274-
} else if (kx > ww * 0.5) {
290+
} else if (kxCenter > rightSide) {
275291
if (!send.right) {
276292
sendCheck('right');
277293
mqttPush(list.topic1, list.kebbiRight);
278294
}
279295
} else {
280-
if (ky < wh * 0.2) {
296+
if (kyCenter < topSide) {
281297
if (!send.top) {
282298
sendCheck('top');
283299
mqttPush(list.topic1, list.kebbiTop);
284300
}
285-
} else if (ky > wh * 0.4) {
301+
} else if (kyCenter > bottomSide) {
286302
if (!send.bottom) {
287303
sendCheck('bottom');
288304
mqttPush(list.topic1, list.kebbiBottom);
@@ -295,8 +311,9 @@
295311
}
296312
}
297313
}
298-
};
299-
const target = function (evt) {
314+
}
315+
316+
function target(evt) {
300317
evt.preventDefault();
301318
kebbi.classList.remove('reset');
302319
let touches = evt.changedTouches;
@@ -308,9 +325,9 @@
308325
dx = mx - kebbi.offsetLeft;
309326
dy = my - kebbi.offsetTop;
310327
kebbi.classList.add('target');
311-
};
312-
document.addEventListener('mousemove', move);
313-
kebbi.addEventListener('touchmove', move);
328+
updateCarSize();
329+
sendCheck('center'); // 設定初始狀態為 center
330+
}
314331

315332
function reset() {
316333
if (send.top || send.bottom || send.left || send.right) {
@@ -325,10 +342,24 @@
325342
kebbi.style.top = `${oy}px`;
326343
}
327344

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

357+
document.addEventListener('mousemove', move);
358+
document.addEventListener('mouseup', reset);
331359
kebbi.addEventListener('mousedown', target);
360+
361+
kebbi.addEventListener('touchmove', move);
362+
kebbi.addEventListener('touchend', reset);
332363
kebbi.addEventListener('touchstart', target);
333364
}
334365

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)