-
Notifications
You must be signed in to change notification settings - Fork 0
/
spritify.js
287 lines (218 loc) · 6.86 KB
/
spritify.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
* Spritesheet generating JavaScript library
*
* Version 2.1 - August 2013
*
* Author: Sam Stonehouse
* Date Created: Dec 2012
*
*/
if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
// closest thing possible to the ECMAScript 5 internal IsCallable function
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function () {},
fBound = function () {
return fToBind.apply(this instanceof fNOP && oThis
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
var Spritify = (function(GrowingPacker, JSZip, undefined) {
"use strict";
var defaults = {
spacing: 0,
doRenames: true,
debug: false,
renames: [
["-hover", ":hover"]
]
};
var ImagePacker = function() {
this.images = [];
this.png = undefined;
this.loadedImages = 0;
this.cssString = "";
this.referenceString = "";
};
ImagePacker.prototype.loadFiles = function(files, opts, callback) {
if (arguments.length === 0) {
throw new Exception("No files provided");
}
if (arguments.length === 1) {
opts = {};
callback = function() {};
}
if (typeof(opts) === 'function') {
callback = opts;
opts = {};
}
this.numImages = files.length;
this.onComplete = callback || function() {};
//Set options as merge of defaults and any supplied options
this.options = opts ? mergeOptions(defaults, opts) : defaults;
if (this.options.debug) { console.log("Loading images"); }
//Load each image as an ImageFile object and append it to the images array
for (var i = 0; i < this.numImages; i++) {
var imgFile = new ImageFile();
imgFile.load(files[i], this.options, this._loadedImage.bind(this));
this.images[i] = imgFile;
}
};
ImagePacker.prototype._loadedImage = function() {
if (this.options.debug) { console.log("Loading images"); }
this.loadedImages++;
//If all images are loaded move onto next step
if (this.loadedImages === this.numImages) {
this._pack();
}
};
ImagePacker.prototype._pack = function() {
if (this.options.debug) { console.log("Packing images"); }
//Sort images so larges ones get packed first
this.images.sort(function(a, b) {
return (b.w > b.h ? b.w : b.h) - (a.w > a.h ? a.w : a.h);
});
//Run bin packer
this.imgPacker = new GrowingPacker();
this.imgPacker.fit(this.images);
this._drawImages();
};
ImagePacker.prototype._drawImages = function() {
if (this.options.debug) { console.log("Drawing images"); }
//Create canvas
this.canvas = document.createElement("canvas");
var context = this.canvas.getContext('2d');
//Set canvas dimensions for drawing
this.canvas.height = this.imgPacker.root.h;
this.canvas.width = this.imgPacker.root.w;
for (var i = 0; i < this.images.length; i++) {
context.drawImage(this.images[i].img, this.images[i].fit.x, this.images[i].fit.y );
}
this._complete();
};
ImagePacker.prototype._complete = function() {
this.png = this.canvas.toDataURL("image/png");
this.cssString = "";
this.referenceString = "";
this.onComplete();
};
ImagePacker.prototype.getPng = function() {
return this.png;
};
ImagePacker.prototype.getCSS = function() {
if (this.cssString !== "") {
return this.cssString;
}
var newCSSString = "/* Spritesheet generated by http://spritifycss.co.uk */\n\n";
newCSSString += "[class^=\"sprite-\"], [class*=\" sprite-\"] {\n";
newCSSString += " background-image: url('spritesheet.png');\n";
newCSSString += " display: inline-block;\n";
newCSSString += "}\n\n";
for (var i = 0; i < this.images.length; i++) {
newCSSString += images[i].getCSS();
}
this.cssString = newCSSString;
return this.cssString;
};
ImagePacker.prototype.getReference = function() {
if (this.referenceString !== "") {
return this.referenceString;
}
var newReferenceString = "";
for (var i = 0; i < this.images.length; i++) {
newReferenceString += images[i].getReference();
}
this.referenceString = newReferenceString;
return this.referenceString;
};
ImagePacker.prototype.getZip = function() {
//Create ZIP
var zip = new JSZip();
zip.file("sprites.css", this.getCSS());
zip.file("spritesheet.png", this.getPng().split(",")[1], {base64: true});
return zip.generate();
};
var ImageFile = function() {
this.img = document.createElement("img");
this.w = 0;
this.h = 0;
console.log(this);
};
ImageFile.prototype.load = function(file, options, callback) {
this.file = file;
this.options = options;
var imgFile = this;
var reader = new FileReader();
console.log(this);
reader.onloadend = function() {
imgFile.img.src = reader.result;
imgFile.img.onload = function() {
imgFile.w = imgFile.img.width + options.spacing;
imgFile.h = imgFile.img.height + options.spacing;
callback();
};
};
reader.readAsDataURL(file);
};
ImageFile.prototype.getCSS = function() {
var name = this.getFormattedName();
var returnString = name + " {\n";
returnString += "	background-position: -" + (this.fit.x) + "px -" + (this.fit.y) + "px;\n";
returnString += "	height: " + this.img.height + "px;\n";
returnString += "	width: " + this.img.width + "px;\n";
returnString += "}\n\n";
return returnString;
};
ImageFile.prototype.getReference = function() {
var returnString = this.file.name + "\n";
returnString += "	x: " + (this.fit.x) + "\n";
returnString += "	y: " + (this.fit.y) + "\n";
returnString += "	h: " + this.image.height + "\n";
returnString += "	w: " + this.image.width + "\n\n";
return returnString;
};
ImageFile.prototype.getFormattedName = function() {
//Remove the file extension
var res = this.file.name.split(".");
if (res.length > 1) {
res.pop();
}
//Replace whitespace with '-' and make everything lowercase
res = '.sprite-' + res.toLowerCase()
.replace(/^\s+|\s+$/g, '')
.replace(/[\s.-]+/g, '-');
if (this.options.doRenames) {
for (var i = 0; i < this.options.renames.length; i++) {
res = res.replace(this.options.renames[i][0], this.options.renames[i][1]);
}
}
return res;
};
//Merge two options objects, defaults and then add the options provided
var mergeOptions = function(defaults, settings){
var results = {};
var attrname;
for (attrname in defaults) {
if (defaults.hasOwnProperty(attrname)) {
results[attrname] = defaults[attrname];
}
}
for (attrname in settings) {
if (settings.hasOwnProperty(attrname)) {
results[attrname] = settings[attrname];
}
}
return results;
};
return ImagePacker;
})(GrowingPacker, JSZip);