This repository has been archived by the owner on Jul 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
snake.html
66 lines (58 loc) · 2 KB
/
snake.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
body {
background-color: #ffffff;
margin: 0px;
cursor: none;
overflow: hidden;
}
</style>
<meta name="viewport" content="width=device-width, user-scalable=no" />
</head>
<body>
<script type="text/javascript">
/**
* Return parameter `name` from the URI's query string.
*/
function getParameterByName(name) {
var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
var imageWidthHalf, imageHeightHalf;
var canvas = document.createElement( 'canvas' );
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
canvas.style.display = 'block';
document.body.appendChild( canvas );
var context = canvas.getContext( '2d' );
var image = document.createElement( 'img' );
image.addEventListener('load', function() {
imageWidthHalf = Math.floor( this.width / 2 );
imageHeightHalf = Math.floor( this.height / 2 );
document.addEventListener( 'mousemove', onMouseEvent, false );
document.addEventListener( 'touchstart', onTouchEvent, false );
document.addEventListener( 'touchmove', onTouchEvent, false );
}, false );
var param = getParameterByName("img");
if (param) {
image.src = "img/" + param + ".png";
}
else {
image.src = "img/blern.png";
}
function onMouseEvent( event ) {
context.drawImage( image, event.clientX - imageWidthHalf, event.clientY - imageHeightHalf );
}
function onTouchEvent( event ) {
event.preventDefault();
for ( var i = 0; i < event.touches.length; i++ ) {
context.drawImage( image, event.touches[i].pageX - imageWidthHalf, event.touches[i].pageY - imageHeightHalf );
}
}
</script>
</body>
</html>