-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
114 lines (100 loc) · 3.37 KB
/
script.js
File metadata and controls
114 lines (100 loc) · 3.37 KB
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
var backgrounds = new Array();
var characters = new Array();
var sounds = new Array();
var indexB = 0;
var indexC = 0;
window.addEventListener("keydown", switchBackground, false);
(function($){
$.extend({
playSound: function(){
return $("<embed src='"+arguments[0]+".mp3' hidden='true' autostart='true' loop='false' class='playSound'>" + "<audio autoplay='autoplay' style='display:none;' controls='controls'><source src='"+arguments[0]+".mp3' /><source src='"+arguments[0]+".ogg' /></audio>").appendTo('body');
}
});
})(jQuery);
$(function () {
$('body').click(function (e) {
var x = e.pageX - 170 + 'px';
var y = e.pageY - 150 + 'px';
var charIndex = navigateC();
var img = $(characters[charIndex]);
var div = $('<div>').css({
"position": "absolute",
"left": x,
"top": y
});
animateDiv($(div));
div.append(img);
$(document.body).append(div);
$.playSound(sounds[indexC]);
});
});
function makeNewPosition() {
var h = $(window).height() - 200;
var w = $(window).width() - 200;
var nh = Math.floor(Math.random() * h);
var nw = Math.floor(Math.random() * w);
return [nh, nw];
}
function animateDiv($target) {
var newq = makeNewPosition();
var oldq = $target.offset();
var speed = calcSpeed([oldq.top, oldq.left], newq);
$target.animate({ top: newq[0], left: newq[1] }, speed, function () {
animateDiv($target);
});
};
function calcSpeed(prev, next) {
var x = Math.abs(prev[1] - next[1]);
var y = Math.abs(prev[0] - next[0]);
var greatest = x > y ? x : y;
var speedModifier = 0.1;
var speed = Math.ceil(greatest / speedModifier);
return speed;
}
function navigateB(){
indexB += 1;
if (indexB >= backgrounds.length){
indexB = indexB - backgrounds.length;
}
if (indexB < 0){
indexB = backgrounds.length + indexB;
}
return backgrounds[indexB];
}
function switchBackground(){
if (document.body){
document.body.style.backgroundImage = 'url('+ navigateB() +')';
}
return !(e.keyCode == 32);
}
function navigateC(){
indexC = Math.round(Math.random() * (characters.length - 1))
return indexC;
}
function loadArray(){
backgrounds[0] = "resources/background1.jpg";
backgrounds[1] = "resources/background2.jpg";
backgrounds[2] = "resources/background3.jpg";
backgrounds[3] = "resources/background4.jpg";
characters[0] = '<img src="resources/character1.png"/>';
characters[1] = '<img src="resources/character2.png"/>';
characters[2] = '<img src="resources/character3.png"/>';
characters[3] = '<img src="resources/character4.png"/>';
characters[4] = '<img src="resources/character5.png"/>';
characters[5] = '<img src="resources/character6.png"/>';
characters[6] = '<img src="resources/character7.png"/>';
characters[7] = '<img src="resources/character8.png"/>';
characters[8] = '<img src="resources/character9.png"/>';
characters[9] = '<img src="resources/character10.png"/>';
sounds[0] = "resources/sound1";
sounds[1] = "resources/sound2";
sounds[2] = "resources/sound3";
sounds[3] = "resources/sound4";
sounds[4] = "resources/sound5";
sounds[5] = "resources/sound6";
sounds[6] = "resources/sound7";
sounds[7] = "resources/sound8";
sounds[8] = "resources/sound9";
sounds[9] = "resources/sound10";
}
window.onload = loadArray();