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
|
Swoosh = (function () {
/**
* Methods
*/
// Canvas
var canvas = function(name) { // Prototype
if (name) {
this.setCanvas(name);
}
};
canvas.prototype = {
setCanvas : function (name) {
Swoosh.id = document.getElementById(name);
Swoosh.context = Swoosh.id.getContext("2d");
},
};
// Images
var image = function () {
// prototype
this.sprites = [];
};
image.prototype = {
/**
* Sprites will contain an array of images and its properties
* @type Array
*/
sprites : [],
imageInstance : [],
imagesLoaded : false,
addSprite : function (name, url, x, y) {
this.imagesLoaded = false;
image = new Image();
image.src = url;
image.onload = function() {
this.imagesLoaded = true;
};
var properties = {url: url, image: image, x: x, y: y};
this.imageInstance[name] = properties;
this.sprites = this.imageInstance;
return this.sprites[String(name)];
} ,
/**
* Adds a sheet with multiple images that can be devided.
* @param Array division : an array of 4 axis to devide each images and name them ?
*/
addSpriteSheet : function (name, url, x, y, division) {
},
getSprites : function () {
return this.sprites;
},
getSprite : function (name) {
return this.sprites[String(name)];
},
/**
* Showing image functions
*/
// Shows all images
show : function (x, y) {
for (var key in data = this.sprites) {
var sprite = data[key];
Swoosh.context.drawImage(sprite.image, sprite.x, sprite.y);
}
},
// shows image and associated images only
showPart: function (name, x, y) {
x = x || 640;
y = y || 480;
var sprite = this.getSprite(String(name));
Swoosh.context.drawImage(sprite.image, sprite.x, sprite.y);
},
/**
* Animation functions
*/
// Moves all the images
moveTo : function (x, y) {
this.refresh();
for (var key in data = this.sprites) {
this.movePartTo(key, x, y);
}
},
// Moves the sprite and associated sprites only
movePartTo : function (name, x, y) {
Swoosh.context.drawImage(sprite.image, sprite.x, sprite.y);
},
}
return {
canvas : canvas,
image : image
};
})(); |
Partager