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
|
var jaws = (function(jaws) {
/**
* @class Manage a parallax scroller with different layers. "Field Summary" contains options for the Parallax()-constructor.
* @constructor
*
* @property scale number, scale factor for all layers (2 will double everything and so on)
* @property repeat_x true|false, repeat all parallax layers horizontally
* @property repeat_y true|false, repeat all parallax layers vertically
* @property camera_x number, x-position of "camera". add to camera_x and layers will scroll left. defaults to 0
* @property camera_y number, y-position of "camera". defaults to 0
*
* @example
* parallax = new jaws.Parallax({repeat_x: true})
* parallax.addLayer({image: "parallax_1.png", damping: 100})
* parallax.addLayer({image: "parallax_2.png", damping: 6})
* parallax.camera_x += 1 // scroll layers horizontally
* parallax.draw()
*
*/
jaws.Parallax = function Parallax(options) {
if( !(this instanceof arguments.callee) ) return new arguments.callee( options );
jaws.parseOptions(this, options, this.default_options)
}
jaws.Parallax.prototype.default_options = {
width: function() { return jaws.width },
height: function() { return jaws.height },
scale: 1,
repeat_x: null,
repeat_y: null,
camera_x: 0,
camera_y: 0,
layers: []
}
/** Draw all layers in parallax scroller */
jaws.Parallax.prototype.draw = function(options) {
var layer, numx, numy, initx;
for(var i=0; i < this.layers.length; i++) {
layer = this.layers[i]
if(this.repeat_x) {
initx = -((this.camera_x / layer.damping) % layer.width);
}
else {
initx = -(this.camera_x / layer.damping)
}
if (this.repeat_y) {
layer.y = -((this.camera_y / layer.damping) % layer.height);
}
else {
layer.y = -(this.camera_y / layer.damping);
}
layer.x = initx;
while (layer.y < this.height) {
while (layer.x < this.width) {
if (layer.x + layer.width >= 0 && layer.y + layer.height >= 0) { //Make sure it's on screen
layer.draw(); //Draw only if actually on screen, for performance reasons
}
layer.x = layer.x + layer.width;
if (!this.repeat_x) {
break;
}
}
layer.y = layer.y + layer.height;
layer.x = initx;
if (!this.repeat_y) {
break;
}
}
}
}
/** Add a new layer to the parallax scroller */
jaws.Parallax.prototype.addLayer = function(options) {
var layer = new jaws.ParallaxLayer(options)
layer.scaleAll(this.scale)
this.layers.push(layer)
}
/** Debugstring for Parallax() */
jaws.Parallax.prototype.toString = function() { return "[Parallax " + this.x + ", " + this.y + ". " + this.layers.length + " layers]" }
/**
* @class A single layer that's contained by Parallax()
*
* @property damping number, higher the number, the slower it will scroll with regards to other layers, defaults to 0
* @constructor
* @extends jaws.Sprite
*/
jaws.ParallaxLayer = function ParallaxLayer(options) {
if( !(this instanceof arguments.callee) ) return new arguments.callee( options );
this.damping = options.damping || 0
jaws.Sprite.call(this, options)
}
jaws.ParallaxLayer.prototype = jaws.Sprite.prototype
/** Debugstring for ParallaxLayer() */
// This overwrites Sprites toString, find another sollution.
// jaws.ParallaxLayer.prototype.toString = function() { return "[ParallaxLayer " + this.x + ", " + this.y + "]" }
return jaws;
})(jaws || {});
|