-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
vibration.html
118 lines (102 loc) · 3.19 KB
/
vibration.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
<!DOCTYPE html>
<html>
<head>
<title>Joypad.js vibration example</title>
<meta charset="UTF-8" />
<style>
html,
body {
margin: 0;
padding: 0;
position: relative;
font-family: sans-serif;
}
.info {
position: absolute;
right: 0;
color: #fff;
background-color: #222;
padding: 15px;
z-index: 1;
}
.info>h2 {
font-weight: 400;
font-size: 1em;
margin: 0;
}
.info>p {
font-weight: 300;
font-size: .85em;
margin: 25px auto 0;
}
#vibrate {
background-color: #547ae2;
color: #fff;
font-weight: 300;
border-radius: 3px;
text-align: center;
width: 80px;
cursor: pointer;
position: absolute;
top: 180px;
padding: 20px 80px;
left: calc(50% - 120px);
}
#vibrate:hover {
background-color: #1c41a7;
}
</style>
</head>
<body>
<div class="info">
<h2 id="heading"></h2>
<p id="message"></p>
</div>
<div id="vibrate">Vibrate</div>
<script type="module">
import '../dist/joypad.min.js';
let heading = document.getElementById('heading');
let message = document.getElementById('message');
let vibrateButton = document.getElementById('vibrate');
function resetInfo(e) {
heading.innerText = 'No controller connected!';
message.innerText = 'Please connect a controller and press any key to start.';
};
function updateInfo(e) {
const { gamepad } = e;
heading.innerText = 'Controller connected!';
message.innerText = gamepad.id + '\n\n' + 'Press the blue button on the screen to make the connected controllers vibrate.';
};
function vibrateController() {
const connectedControllers = window.joypad.instances;
if (Object.keys(connectedControllers).length) {
Object.keys(connectedControllers).forEach(controller => {
const gamepadInstance = connectedControllers[controller];
const options = {
startDelay: 0,
duration: 2000,
weakMagnitude: 1,
strongMagnitude: 1
};
console.log(gamepadInstance);
// These options override the global vibration settings
joypad.vibrate(gamepadInstance, options);
});
}
};
resetInfo();
vibrateButton.addEventListener('click', vibrateController);
joypad.set({
// Global vibration settings
vibration: {
startDelay: 500,
duration: 3000,
weakMagnitude: 1,
strongMagnitude: 1
}
});
joypad.on('connect', e => updateInfo(e));
joypad.on('disconnect', e => resetInfo(e));
</script>
</body>
</html>