forked from yoannmoinet/nipplejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
297 lines (290 loc) · 11.8 KB
/
index.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>NippleJS</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="http://yoannmoinet.github.io/nipplejs/stylesheets/styles.css" media="screen" title="no title" charset="utf-8">
<link rel="stylesheet" href="http://yoannmoinet.github.io/nipplejs/stylesheets/github-light.css" media="screen" title="no title" charset="utf-8">
<style>
body {
margin: 0;
padding: 0;
margin-top: 20px;
max-width: 1024px;
margin-left: auto;
margin-right: auto;
}
#buttons {
position: relative;
border: solid 1px #aaa;
display: inline-block;
width: 100%;
border-radius: 10px;
height: 50px;
overflow: hidden;
margin-bottom: 20px;
box-sizing: border-box;
}
.button {
display: inline-block;
background: #ccc;
color: black;
height: 50px;
width: 33%;
margin: 0;
margin-right: -3px;
text-align: center;
line-height: 45px;
font-size: 25px;
cursor: pointer;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.button:hover {
background: #eee;
}
.button:active {
background: #ddd;
}
.button.active {
color: white;
background: #888;
}
.button:nth-child(2) {
width: 34%;
border-left: solid 1px #aaa;
border-right: solid 1px #aaa;
}
.highlight {
display: none;
}
.highlight.active {
display: block;
}
.zone {
display: none;
position: absolute;
width: 100%;
height: 100%;
left: 0;
}
.zone.active {
display: block;
}
.zone > h1 {
position: absolute;
padding: 10px 10px;
margin: 0;
color: white;
right: 0;
bottom: 0;
}
.zone.dynamic {
background: rgba(0, 0, 255, 0.1);
}
.zone.semi {
background: rgba(255, 255, 255, 0.1);
}
.zone.static {
background: rgba(255, 0, 0, 0.1);
}
</style>
</head>
<body>
<p>First thing first. A demo. Executed with this configuration : </p>
<div class="dynamic active highlight highlight-javascript">
<pre>
<span class="pl-k"><</span>script src<span class="pl-k">=</span><span class="pl-s"><span class="pl-pds">"</span>./dist/nipplejs.min.js<span class="pl-pds">"</span></span><span class="pl-k">></</span>script<span class="pl-k">></span>
<span class="pl-k"><</span>script<span class="pl-k">></span>
<span class="pl-k">var</span> dynamic <span class="pl-k">=</span> nipplejs.create({
zone: document.getElementById('dynamic'),
color: 'blue'
});
<span class="pl-k"></</span>script<span class="pl-k">></span></pre>
</div>
<div class="semi highlight highlight-javascript">
<pre>
<span class="pl-k"><</span>script src<span class="pl-k">=</span><span class="pl-s"><span class="pl-pds">"</span>./dist/nipplejs.min.js<span class="pl-pds">"</span></span><span class="pl-k">></</span>script<span class="pl-k">></span>
<span class="pl-k"><</span>script<span class="pl-k">></span>
<span class="pl-k">var</span> semi <span class="pl-k">=</span> nipplejs.create({
zone: document.getElementById('semi'),
mode: 'semi',
catchDistance: 150,
color: 'white'
});
<span class="pl-k"></</span>script<span class="pl-k">></span></pre>
</div>
<div class="static highlight highlight-javascript">
<pre>
<span class="pl-k"><</span>script src<span class="pl-k">=</span><span class="pl-s"><span class="pl-pds">"</span>./dist/nipplejs.min.js<span class="pl-pds">"</span></span><span class="pl-k">></</span>script<span class="pl-k">></span>
<span class="pl-k"><</span>script<span class="pl-k">></span>
<span class="pl-k">var</span> static <span class="pl-k">=</span> nipplejs.create({
zone: document.getElementById('static'),
mode: 'static',
position: {left: '50%', top: '50%'},
color: 'red'
});
<span class="pl-k"></</span>script<span class="pl-k">></span></pre>
</div>
<p>Choose the mode you want to test.</p>
<div id="buttons">
<div data-type="dynamic" class="active button dynamic">dynamic</div>
<div data-type="semi" class="button semi">semi</div>
<div data-type="static" class="button static">static</div>
</div>
<div id="zone_joystick">
<div id="debug">
<ul>
<li class="position">
position :
<ul>
<li class="x">x : <span class='data'></span></li>
<li class="y">y : <span class='data'></span></li>
</ul>
</li>
<li class="force">force : <span class='data'></span></li>
<li class="pressure">pressure : <span class='data'></span></li>
<li class="distance">distance : <span class='data'></span></li>
<li class="angle">
angle :
<ul>
<li class="radian">radian : <span class='data'></span></li>
<li class="degree">degree : <span class='data'></span></li>
</ul>
</li>
<li class="direction">
direction :
<ul>
<li class="x">x : <span class='data'></span></li>
<li class="y">y : <span class='data'></span></li>
<li class="angle">angle : <span class='data'></span></li>
</ul>
</li>
</ul>
<div class="dump"></div>
</div>
<div class="zone dynamic active"><h1>dynamic</h1></div>
<div class="zone semi"><h1>semi</h1></div>
<div class="zone static"><h1>static</h1></div>
</div>
<br/>
<div>
<a href="https://codepen.io/YoannM/full/gapmMG" target="_blank">> Open the demo in a new window.</a>
</div>
<script src="../dist/nipplejs.js" charset="utf-8"></script>
<script>
var s = function (sel) { return document.querySelector(sel); };
var sId = function (sel) { return document.getElementById(sel); };
var removeClass = function (el, clss) {
el.className = el.className.replace(new RegExp('\\b' + clss + ' ?\\b', 'g'), '');
}
var joysticks = {
dynamic: {
zone: s('.zone.dynamic'),
color: 'blue',
multitouch: true
},
semi: {
zone: s('.zone.semi'),
mode: 'semi',
catchDistance: 150,
color: 'white'
},
static: {
zone: s('.zone.static'),
mode: 'static',
position: {left: '50%', top: '50%'},
color: 'red'
}
};
var joystick;
// Get debug elements and map them
var elDebug = sId('debug');
var elDump = elDebug.querySelector('.dump');
var els = {
position: {
x: elDebug.querySelector('.position .x .data'),
y: elDebug.querySelector('.position .y .data')
},
force: elDebug.querySelector('.force .data'),
pressure: elDebug.querySelector('.pressure .data'),
distance: elDebug.querySelector('.distance .data'),
angle: {
radian: elDebug.querySelector('.angle .radian .data'),
degree: elDebug.querySelector('.angle .degree .data')
},
direction: {
x: elDebug.querySelector('.direction .x .data'),
y: elDebug.querySelector('.direction .y .data'),
angle: elDebug.querySelector('.direction .angle .data')
}
};
sId('buttons').onclick = createNipple;
createNipple('dynamic');
function bindNipple () {
joystick.on('start end', function (evt, data) {
dump(evt.type);
debug(data);
}).on('move', function (evt, data) {
debug(data);
}).on('dir:up plain:up dir:left plain:left dir:down ' +
'plain:down dir:right plain:right',
function (evt, data) {
dump(evt.type);
}
).on('pressure', function (evt, data) {
debug({pressure: data});
});
}
function createNipple (evt) {
var type = typeof evt === 'string' ?
evt : evt.target.getAttribute('data-type');
if (joystick) {
joystick.destroy();
}
removeClass(s('.zone.active'), 'active');
removeClass(s('.button.active'), 'active');
removeClass(s('.highlight.active'), 'active');
s('.highlight.' + type).className += ' active';
s('.button.' + type).className += ' active';
s('.zone.' + type).className += ' active';
joystick = nipplejs.create(joysticks[type]);
bindNipple();
}
// Print data into elements
function debug (obj) {
function parseObj(sub, el) {
for (var i in sub) {
if (typeof sub[i] === 'object' && el) {
parseObj(sub[i], el[i]);
} else if (el && el[i]) {
el[i].innerHTML = sub[i];
}
}
}
setTimeout(function () {
parseObj(obj, els);
}, 0);
}
var nbEvents = 0;
// Dump data
function dump (evt) {
setTimeout(function () {
if (elDump.children.length > 4) {
elDump.removeChild(elDump.firstChild);
}
var newEvent = document.createElement('div');
newEvent.innerHTML = '#' + nbEvents + ' : <span class="data">' +
evt + '</span>';
elDump.appendChild(newEvent);
nbEvents += 1;
}, 0);
}
</script>
</body>
</html>