-
Notifications
You must be signed in to change notification settings - Fork 6
/
script.js
134 lines (111 loc) · 3.06 KB
/
script.js
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
// Request frames function
window.requestAnimFrame = (function (callback) {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
function (callback) {
window.setTimeout(callback, 1000 / 60)
}
})()
// Create image and set to default if invalid input provided
var image = new Image()
image.src = prompt('Enter an image URL')
image.onerror = function () {
image.src = './default.svg'
}
// Ensure canvas shape has same dimensions as image
image.onload = function () {
rect.width = image.width
rect.height = image.height
}
// Hide and append image
image.style.display = 'none'
document.body.appendChild(image)
// Set favicon
document.getElementById('favicon').setAttribute('href', image.src)
// Draw image on canvas
function showImage (rect, context) {
context.drawImage(image, rect.x, rect.y)
}
// Movement
var direction = 'se'
function display (rect, canvas, context) {
var speed = 5
// If image hits top
if (rect.y <= 0) {
if (direction === 'ne') {
direction = 'se'
} else if (direction === 'nw') {
direction = 'sw'
}
// If image hits bottom
} else if (rect.y >= canvas.height - rect.height) {
if (direction === 'se') {
direction = 'ne'
} else if (direction === 'sw') {
direction = 'nw'
}
// If image hits left
} else if (rect.x <= 0) {
if (direction === 'nw') {
direction = 'ne'
} else if (direction === 'sw') {
direction = 'se'
}
// If image hits right
} else if (rect.x >= canvas.width - rect.width) {
if (direction === 'ne') {
direction = 'nw'
} else if (direction === 'se') {
direction = 'sw'
}
}
// Now to define what the directions actually mean
switch(direction) {
case 'ne':
rect.x += speed
rect.y -= speed
break
case 'nw':
rect.x -= speed
rect.y -= speed
break
case 'se':
rect.x += speed
rect.y += speed
break
case 'sw':
rect.x -= speed
rect.y += speed
break
default:
// This shouldn't ever happen
}
// Clear canvas
context.clearRect(0, 0, canvas.width, canvas.height)
showImage(rect, context)
// Request another frame
requestAnimFrame(function () {
display(rect, canvas, context)
})
}
// Get canvas and context
var canvas = document.getElementById('canvas')
var context = canvas.getContext('2d')
// Change canvas dimensions when browser window is resized
function resize (canvas) {
canvas.height = window.innerHeight
canvas.width = window.innerWidth
}
resize(canvas)
window.onresize = function () {
resize(canvas)
}
// Define canvas rectangle
var rect = {
x: Math.floor(Math.random() * window.innerWidth) + 1,
y: Math.floor(Math.random() * window.innerHeight) + 1,
width: image.width,
height: image.height
}
// Start animation
showImage(rect, context)
display(rect, canvas, context)