-
Notifications
You must be signed in to change notification settings - Fork 30
/
index.html
68 lines (57 loc) · 2.24 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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Hermes Demo</title>
<style>
.ball {
width: 100px;
height: 100px;
border-radius: 50%;
background-color: red;
position: fixed;
top: calc(50% - 50px);
left: calc(50% - 50px);
}
</style>
</head>
<body>
<h1>Hermes Demo</h1>
<a href="https://github.com/arnellebalane/hermes">https://github.com/arnellebalane/hermes</a>
<h2>Open this page in multiple tabs.</h2>
<button id="others">Send data to other tabs</button>
<button id="all">Send data to all tabs (including self)</button>
<p class="ball"></p>
<ul></ul>
<script src="dist/hermes.min.js"></script>
<script>
const othersBtn = document.querySelector('#others');
const allBtn = document.querySelector('#all');
const ul = document.querySelector('ul');
const ball = document.querySelector('.ball');
othersBtn.onclick = function() { hermes.send('event', new Date()); };
allBtn.onclick = function() { hermes.send('event', new Date(), true); };
hermes.on('event', function(data) {
const li = document.createElement('li');
li.textContent = data;
ul.appendChild(li);
});
hermes.on('moveball', function(data) {
ball.style.top = 'calc(' + (data.y * 100) + '% - 50px)';
ball.style.left = 'calc(' + (data.x * 100) + '% - 50px)';
});
let dragging = false;
ball.addEventListener('pointerdown', function(e) { dragging = true});
document.addEventListener('pointerup', function(e) { dragging = false });
document.addEventListener('pointermove', function(e) {
if (dragging) {
const data = {
x: e.pageX / window.innerWidth,
y: e.pageY / window.innerHeight
};
hermes.send('moveball', data, true);
}
});
</script>
</body>
</html>