Skip to content

Commit f95d2f2

Browse files
committed
fix touch
1 parent 1dd5e95 commit f95d2f2

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

02-Archery-Game/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@
9191

9292
<span>Draw back an arrow and launch it!</span>
9393
<script src='https://cdnjs.cloudflare.com/ajax/libs/gsap/1.19.1/TweenMax.min.js'></script>
94-
<script src="//s3-us-west-2.amazonaws.com/s.cdpn.io/16327/MorphSVGPlugin.min.js">
95-
// <script src='MorphSVGPlugin.min.js'></script>
94+
<script src="//s3-us-west-2.amazonaws.com/s.cdpn.io/16327/MorphSVGPlugin.min.js"></script>
95+
<!---- <script src='MorphSVGPlugin.min.js'> </script>-->
9696
<script src="script.js"></script>
9797
</body>
9898

02-Archery-Game/script.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ var pivot = {
2525

2626
// 判断当前设备是否为触摸屏
2727
function isTouchDevice() {
28-
return window.matchMedia("(pointer: coarse)").matches;
28+
return window.matchMedia("(pointer: coarse)").matches;
2929
}
3030

3131
// 计算屏幕中心
3232
function getScreenCenter() {
33-
return {
34-
x: window.innerWidth / 2,
35-
y: window.innerHeight / 2
36-
};
33+
return {
34+
x: window.innerWidth / 2,
35+
y: window.innerHeight / 2
36+
};
3737
}
3838

3939
// // 设置 pivot 点
@@ -56,7 +56,8 @@ aim({
5656
clientY: 300
5757
});
5858

59-
59+
//set up start touch
60+
window.addEventListener("touchstart", draw)
6061

6162
// set up start drag event
6263
window.addEventListener("mousedown", draw);
@@ -75,14 +76,14 @@ function draw(e) {
7576
}
7677

7778
function aim(e) {
78-
try{e.preventDefault();}catch(e){}
79+
try { e.preventDefault(); } catch (e) { }
7980
// get mouse position in relation to svg position and scale
8081
var point = getMouseSVG(e);
8182
point.x = Math.min(point.x, pivot.x - 7);
8283
point.y = Math.max(point.y, pivot.y + 7);
8384
var dx = point.x - pivot.x;
8485
var dy = point.y - pivot.y;
85-
86+
8687
// Make it more difficult by adding random angle each time
8788
var angle = Math.atan2(dy, dx) + randomAngle;
8889
var bowAngle = angle - Math.PI;
@@ -118,7 +119,7 @@ function aim(e) {
118119
attr: {
119120
d: "M100,250c" + offset.x + "," + offset.y + "," + (arcWidth - offset.x) + "," + (offset.y + 50) + "," + arcWidth + ",50"
120121
},
121-
autoAlpha: distance/60
122+
autoAlpha: distance / 60
122123
});
123124

124125
}
@@ -226,8 +227,15 @@ function showMessage(selector) {
226227

227228
function getMouseSVG(e) {
228229
// normalize mouse position within svg coordinates
229-
cursor.x = e.clientX;
230-
cursor.y = e.clientY;
230+
if (e.touches) {//如果是触摸
231+
cursor.x = e.touches[0].pageX;
232+
cursor.y = e.touches[0].pageY;
233+
} else {//如果是鼠标
234+
cursor.x = e.clientX;
235+
cursor.y = e.clientY;
236+
}
237+
238+
231239
return cursor.matrixTransform(svg.getScreenCTM().inverse());
232240
}
233241

0 commit comments

Comments
 (0)