-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathTouches.html
174 lines (132 loc) · 4.13 KB
/
Touches.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0" />
<title></title>
<style type="text/css">
* {
-webkit-touch-callout: none; /* prevent callout to copy image, etc when tap to hold */
-webkit-text-size-adjust: none; /* prevent webkit from resizing text to fit */
/* make transparent link selection, adjust last value opacity 0 to 1.0 */
-webkit-tap-highlight-color: rgba(0,0,0,0);
-webkit-user-select: none; /* prevent copy paste, to allow, change 'none' to 'text' */
-webkit-tap-highlight-color: rgba(0,0,0,0);
}
body {
background-color: #000000;
margin: 0px;
}
canvas {
background-color:#111133;
display:block;
position:absolute;
}
.container {
width:auto;
text-align:center;
background-color:#ff0000;
}
</style>
</head>
<body onload = "init()">
<script src="../libs/Vector2.js"></script>
<script>
var canvas,
c, // c is the canvas' context 2D
container;
setupCanvas();
var mouseX, mouseY,
// is this running in a touch capable environment?
touchable = 'createTouch' in document,
touches = []; // array of touch vectors
setInterval(draw, 1000/35);
if(touchable) {
canvas.addEventListener( 'touchstart', onTouchStart, false );
canvas.addEventListener( 'touchmove', onTouchMove, false );
canvas.addEventListener( 'touchend', onTouchEnd, false );
window.onorientationchange = resetCanvas;
window.onresize = resetCanvas;
} else {
canvas.addEventListener( 'mousemove', onMouseMove, false );
}
function resetCanvas (e) {
// resize the canvas - but remember - this clears the canvas too.
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
//make sure we scroll to the top left.
window.scrollTo(0,0);
}
function init(){
}
function draw() {
c.clearRect(0,0,canvas.width, canvas.height);
if(touchable) {
for(var i=0; i<touches.length; i++)
{
var touch = touches[i];
c.beginPath();
c.fillStyle = "white";
c.fillText("touch id : "+touch.identifier+" x:"+touch.clientX+" y:"+touch.clientY, touch.clientX+30, touch.clientY-30);
c.beginPath();
c.strokeStyle = "cyan";
c.lineWidth = "6";
c.arc(touch.clientX, touch.clientY, 40, 0, Math.PI*2, true);
c.stroke();
}
} else {
c.fillStyle = "white";
c.fillText("mouse : "+mouseX+", "+mouseY, mouseX, mouseY);
}
//c.fillText("hello", 0,0);
}
/*
* Touch event (e) properties :
* e.touches: Array of touch objects for every finger currently touching the screen
* e.targetTouches: Array of touch objects for every finger touching the screen that
* originally touched down on the DOM object the transmitted the event.
* e.changedTouches Array of touch objects for touches that are changed for this event.
* I'm not sure if this would ever be a list of more than one, but would
* be bad to assume.
*
* Touch objects :
*
* identifier: An identifying number, unique to each touch event
* target: DOM object that broadcast the event
* clientX: X coordinate of touch relative to the viewport (excludes scroll offset)
* clientY: Y coordinate of touch relative to the viewport (excludes scroll offset)
* screenX: Relative to the screen
* screenY: Relative to the screen
* pageX: Relative to the full page (includes scrolling)
* pageY: Relative to the full page (includes scrolling)
*/
function onTouchStart(e) {
touches = e.touches;
}
function onTouchMove(e) {
// Prevent the browser from doing its default thing (scroll, zoom)
e.preventDefault();
touches = e.touches;
}
function onTouchEnd(e) {
touches = e.touches;
}
function onMouseMove(event) {
mouseX = event.offsetX;
mouseY = event.offsetY;
}
function setupCanvas() {
canvas = document.createElement( 'canvas' );
c = canvas.getContext( '2d' );
container = document.createElement( 'div' );
container.className = "container";
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
document.body.appendChild( container );
container.appendChild(canvas);
c.strokeStyle = "#ffffff";
c.lineWidth =2;
}
</script>
</body>
</html>