/
lfmanip.js
196 lines (162 loc) · 3.97 KB
/
lfmanip.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Library of utility functions for LFManip http://github.com/matti-kariluoma/LFManip
//
// Matti Kariluoma Jan 2013 <matti@kariluo.ma>
var lfmanip = {
filenames: new Array(),
dx: new Array(),
dy: new Array(),
render: function(){},
canvas: $('canvas'), // defaults to all canvases.
wheel: function(){},
click: function(){},
init: function(config)
{
lfmanip.filenames = config.filenames;
lfmanip.dx = config.dx;
lfmanip.dy = config.dy;
lfmanip.render = config.render;
lfmanip.canvas = config.canvas; // set before calling resizeCanvas()
lfmanip.wheel = config.wheel;
lfmanip.click = config.click;
// create empty slots
lfmanip.images.length = lfmanip.filenames.length;
// check that dx.length == dy.length == filenames.length
if (lfmanip.filenames.length != lfmanip.dx.length || lfmanip.dx.length != lfmanip.dy.length)
{
for (i=0; i<lfmanip.filenames.length; i++)
{
if (!lfmanip.dx[i])
{
lfmanip.dx.push(0.0);
}
if (!lfmanip.dy[i])
{
lfmanip.dy.push(0.0);
}
}
}
// set listener to mousewheel event
lfmanip.canvas.mousewheel(lfmanip.wheel);
// set listener to click event
lfmanip.canvas.click(lfmanip.click);
// resize the canvas now
lfmanip.resizeCanvas();
// set listener to resize as browser resizes
$(
function()
{
$(window).resize(
function()
{
lfmanip.resizeCanvas();
}
);
}
);
},
resizeCanvas: function()
{
var width = lfmanip.canvas[0].offsetWidth;
var height = lfmanip.canvas[0].offsetHeight;
lfmanip.canvas.attr('width', width);
lfmanip.canvas.attr('height', height);
},
started: false,
loaded: 0,
start: function()
{
var unloadedImages = new Array();
for (i=0; i<lfmanip.filenames.length; i++)
{
var image = new Image();
// wait for the images to load before continuing
image.onload = lfmanip.handleImageLoad(image, i);
image.src = lfmanip.filenames[i];
image.dx = lfmanip.dx[i];
image.dy = lfmanip.dy[i];
unloadedImages.push(image);
}
// handleImageLoad() will eventually call loop()
},
loop: function()
{
// cross-browser selector from http://stackoverflow.com/questions/5864073/game-loop-requestanimframe-javascript-canvas
var requestAnimFrame = (
// define a selector function, then immediately call it
function()
{
return window.requestAnimationFrame
|| window.webkitRequestAnimationFrame
|| window.mozRequestAnimationFrame
|| window.oRequestAnimationFrame
|| window.msRequestAnimationFrame
|| function(callback, element)
{
window.setTimeout(callback, 1000/60);
};
}
)();
var canUpdate = function()
{
if (lfmanip.started)
{
lfmanip.render();
requestAnimFrame(canUpdate);
}
}
// begin the loop
canUpdate();
},
stop: function()
{
lfmanip.started = false;
lfmanip.loaded = 0;
lfmanip.images = new Array();
},
images: new Array(),
handleImageLoad: function(image, index)
{
// bind the img and index variables, then return a function
return function()
{
lfmanip.registerImage(image, index);
lfmanip.loaded++;
if (lfmanip.loaded == lfmanip.images.length && !lfmanip.started)
{
lfmanip.started = true;
lfmanip.loop();
}
};
},
registerImage: function(img, index)
{
lfmanip.images[index] = img
console.log('lfmanip: registered #'+index+" "+img.src);
},
getImage: function(index)
{
var img = null;
if (lfmanip.images.length >= index)
{
img = lfmanip.images[index];
}
else if (lfmanip.images.length > 0)
{
img = lfmanip.images[0];
}
if (!img) // still crashes if the image isn't loaded yet...
{
img = new Image();
}
return img;
},
numImages: function()
{
var num = 0
if (lfmanip.images && lfmanip.images.length)
{
num = lfmanip.images.length
}
return num;
},
};