-
Notifications
You must be signed in to change notification settings - Fork 40
/
touchAdjustment-tables.html
186 lines (157 loc) · 4.51 KB
/
touchAdjustment-tables.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
175
176
177
178
179
180
181
182
183
184
185
<!DOCTYPE=HTML>
<html>
<meta name=viewport content='width=device-width,minimum-scale=1'>
<title>Touch adjustment demo</title>
<style>
#container {
display: inline-block;
position: relative;
}
#log {
white-space: pre;
}
#canvas
{
position: absolute;
top: 0;
left: 0;
border: 1px solid #000;
pointer-events: none;
}
#content
{
padding: 10px;
}
#hover:hover
{
color: red;
}
table, th, td
{
border-collapse:collapse;
border:1px solid black;
}
th, td
{
padding:5px;
}
</style>
<body id="body" onLoad = "setup()">
<h1>Touch adjustment</h1>
<div id="container">
<canvas id="canvas"></canvas>
<div id="content">
Here is some text <a href='#' id='link'>with a link</a><br>
And here is a superscript<sup><a href='#' id='superscript'>[1]</a></sup><br>
<span id='hover'>This text</span> has a hover effect<br>
Here's some text <span id='tabindex' tabindex=0>with a tabindex</span><br>
<span id='mousehandler'>This line has a mousedown event handler</span><br>
<table>
<caption>Monthly savings</caption>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$50</td>
</tr>
</table>
</div>
</div>
<div id="log"></div>
<script>
var dotRadius = 3;
function setup() {
function $(name) {
return document.getElementById(name);
}
function getPosition(node) {
var r = node.getBoundingClientRect();
return {
x: r.left + document.body.scrollLeft,
y: r.top + document.body.scrollTop
}
}
function createHandlers(opt_params) {
var target = $('container');
var top, left, bottom, right;
function onTouchEvent(e) {
if (e.touches.length > 1 || (e.type=='touchend' && e.touches.length > 0))
return; // ignore multi-touch
var id = e.target.id;
var canvas = $('canvas');
var touch = e.changedTouches[0];
var ctx = canvas.getContext('2d');
if (e.type == 'touchstart') {
top = Infinity;
left = Infinity;
bottom = -Infinity;
right = -Infinity;
$('log').textContent = '';
}
if (e.type != 'touchend') {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
var radiusX = touch.webkitRadiusX;
var radiusY = touch.webkitRadiusY;
var drawTouchBorder = radiusX > 1 || radiusY > 1;
if (radiusX > 1 && radiusY == 1)
radiusY = radiusX; // only one radius supported
var pt = getPosition(canvas);
var offsetX = touch.pageX - pt.x;
var offsetY = touch.pageY - pt.y;
if (drawTouchBorder) {
ctx.beginPath();
ctx.strokeStyle = '#00FF00';
ctx.lineWidth = 3;
top = Math.min(top, offsetY - radiusY);
left = Math.min(left, offsetX - radiusX);
bottom = Math.max(bottom, offsetY + radiusY);
right = Math.max(right, offsetX + radiusX);
ctx.rect(left, top, right - left, bottom - top);
ctx.stroke();
}
if (e.type == 'touchend') {
ctx.fillStyle = '#FF0000';
ctx.beginPath();
ctx.arc(offsetX, offsetY, dotRadius, 0, 2*Math.PI, true);
ctx.fill();
}
var msg = e.type + ': ' + Math.round(touch.pageX) + ', ' + Math.round(touch.pageY) +
' (' + document.elementFromPoint(touch.pageX, touch.pageY).id +') radius=' +
Math.round(touch.webkitRadiusX) + 'x' + Math.round(touch.webkitRadiusY) + '\n';
$('log').textContent += msg;
}
function onClick(e) {
var id = e.target.nodeName;
if (e.target.id)
id += '#' + e.target.id;
var canvas = $('canvas');
var ctx = canvas.getContext('2d');
$('log').textContent += 'click: ' + e.pageX + ', ' + e.pageY + ' (' + id + ')\n';
var pt = getPosition(canvas);
var offsetX = e.pageX - pt.x;
var offsetY = e.pageY - pt.y;
ctx.fillStyle = '#0000FF';
ctx.beginPath();
ctx.arc(offsetX, offsetY, dotRadius, 0, 2*Math.PI, true);
ctx.fill();
}
target.addEventListener('touchstart', onTouchEvent);
target.addEventListener('touchend', onTouchEvent);
target.addEventListener('touchmove', onTouchEvent);
target.addEventListener('click', onClick);
$('mousehandler').addEventListener('mousedown', function() {});
$('canvas').width = $('content').clientWidth;
$('canvas').height = $('content').clientHeight;
}
createHandlers();
}
</script>
</body>
</html>