-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
jquery.minecraftskin.js
372 lines (329 loc) · 12.7 KB
/
jquery.minecraftskin.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
Serverless 3D Minecraft Skin
Copyright (c) 2014, James Mortemore <[email protected]>
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is
hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Based on minecraft skins in html5 canvas tags by Kent Rasmussen @ earthiverse.ath.cx
*/
(function($) {
var pluginName = 'minecraftSkin'
/* jslint maxlen: 2414 */
, steveImg = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAAAgCAYAAACinX6EAAAGuklEQVRoQ+WYa2wUVRTH/7OP2d1u293i0uVRKMgbi7QBQyKGxwdFJVEbkfggMVGMBj9pNCYQMcaoEEhUxAYVDZpISECRGB81EQmmfLGkCCjlobRCKq3A9rXb7ttz7uzdnZlOtxQIi3CSdnbuvXNnzu/8z7l3RsEQNnNcSZqHxOJxqE6nGM2/2eL9diyfNznvDBvqf1OGukch+4d8OAbADntdLs3pVCoLgM9rZ09DOKqg4hYHzl5IiCNbqFeDdEMAcNpswpk+AuGw2y0B1J/pMQRyybgSgpb+/wOYPMqd5ujLyMs0YCjctnTmrUIBNywATgHpbCKZFFH2ZGrBTQOAnTY7L+vBYAqY7bRjUqXn/58CrAB2nqMeo2OSZB+NKPD7HIYUsKrkN0QNYABkwnG9cTFkYwXks+t+FZg+tkis8yn6rygKbPTncVF0tXSHDUkkyHeHYkM0mUAikYSNCqDTYQdxgT0DghSPvmiC5klTe5rmyVxv00ANto9o7QwPuRTnJXyFnYoeAM+lOhwi2jbywK060B9LZG/B5z5VISR2XOiNEpgkvG5VjEkRQTuBiSW08XoA+fYRp871FxZA1fjSdCxG4bZpDnB0k5TriWSaosznCjY8/yRcThUedyn6wt0UdhtC59qwbvcPCPfFECeJOOyKUEOK4Il0SSlQVTonNeTbR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H6P5t/h69IzbYFxk8x9C9/46NLqwF6BXBxYyv2VEAWQT7nMQMAzDycuyEroaYmB8DKsaamHDAezykg4egVkEkbKwDnu8MI0EsZ23l6J2EwEsJVAaBHmG8V4HGiBkgFSAD66Mv8lzAkAJk2DED2ybE6IG9v35h1TipAAuAjOtsGABDtZAzpkhSgd3jYRdC8CowZY5S/GUBbm3EVkEWQnR4CgHBKJ3EzAKsUuSoA8gEasAzqlyG9UzwJn+sBWFVtY4WAVICUuRUAecllAVi4cKHYCHW1axsgKwVwuy94UPRXVVUZHvEDensUyxQ7w8YKkKaPPi9dVgD04zl1TPWg7vsPxWxWALida4DeGJA+RVa990X+IigByEnMDh49etRwA3N/w0/8uUzbRfJxwuj7TTEExI5x7dQsgKmf046RTG68ZNE98AotcbwCkN25pVMU4Yem0AuUBQDppFwF9IAKBoAf1KwgSUNAeM6P6neO599a05h7PivOQpQAsg3+MaK4yULX1dYyYBkcDoD/ACyhLV1sP+9TAAAAAElFTkSuQmCC'
var methods =
{ init: function(options) {
return this.each(function () {
var $this = $(this)
, data = $this.data(pluginName)
, settings = {}
// If the plugin hasn't been initialized yet
if (!data) {
settings =
{ scale: 6
, hat: true
, draw : 'model'
}
if(options) $.extend(true, settings, options)
}
settings.username = $this.data('minecraft-username')
if ($this.data('minecraft-scale')) settings.scale = $this.data('minecraft-scale')
if ($this.data('minecraft-draw')) settings.draw = $this.data('minecraft-draw')
// Check if valid drawing set
if (settings.draw !== 'head' && settings.draw !== 'model') settings.draw = 'model'
// Request the data
methods.requestData('http://s3.amazonaws.com/MinecraftSkins/' + settings.username + '.png', $this, settings)
})
}
, buildImage: function(imgData, $this, settings) {
// Failed to respond
if(!imgData) return
// Create the canvas
var canvas = document.createElement('canvas')
, scratchCanv = document.createElement('canvas')
, model = canvas.getContext('2d')
, scratch = scratchCanv.getContext('2d')
, skin = new Image()
, heightMultiplier = settings.draw === 'head' ? 17.6 : 44.8
canvas.setAttribute('class', 'model')
// Resize Scratch
scratchCanv.setAttribute('width', 64 * settings.scale)
scratchCanv.setAttribute('height', 32 * settings.scale)
scratchCanv.setAttribute('class', 'scratch')
// Resize Isometric Area (Found by trial and error)
canvas.setAttribute('width', 20 * settings.scale)
canvas.setAttribute('height', heightMultiplier * settings.scale)
$this.append(canvas)
$this.append(scratchCanv)
skin.onload = function () {
scratch.drawImage(skin, 0, 0, 64, 32, 0, 0, 64, 32)
// Scale it
scaleImage(scratch.getImageData(0, 0, 64, 32), scratch, 0, 0, settings.scale)
// Draw the skin
if(settings.draw === 'model') {
methods.drawModel(model, scratchCanv, scratch, settings.hat, settings.scale)
} else {
methods.drawHead(model, scratchCanv, scratch, settings.hat, settings.scale)
}
}
skin.src = imgData
}
, requestData: function(username, $this, settings) {
var query = encodeURIComponent('SELECT * FROM data.uri WHERE url = "' + username + '"')
, yql = 'https://query.yahooapis.com/v1/public/yql?q=' + query + '&format=json&callback=?'
$.getJSON(yql, function (data) {
if (data.query.results.url) {
var imgData = data.query.results.url
// Convert MIME
imgData = imgData.replace('application/octet-stream', 'image/png')
methods.buildImage(imgData, $this, settings)
} else {
methods.buildImage(steveImg, $this, settings)
}
}).fail(function () {
methods.buildImage(steveImg, $this, settings)
})
}
, drawHead: function (model, scratchCanv, scratch, showHat, scale) {
var scaleEight = 8 * scale
// Head - Front
model.setTransform(1, -0.5, 0, 1.2, 0, 0)
model.drawImage
( scratchCanv
, scaleEight
, scaleEight
, scaleEight
, scaleEight
, 10 * scale
, 13/1.2 * scale
, scaleEight
, scaleEight
)
// Head - Right
model.setTransform(1, 0.5, 0, 1.2, 0, 0)
model.drawImage
( scratchCanv
, 0
, scaleEight
, scaleEight
, scaleEight
, 2 * scale
, 3/1.2 * scale
, scaleEight
, scaleEight
)
// Head - Top
model.setTransform(-1, 0.5, 1, 0.5, 0, 0)
model.scale(-1, 1)
model.drawImage
( scratchCanv
, scaleEight
, 0
, scaleEight
, scaleEight
, -3 * scale
, 5 * scale
, scaleEight
, scaleEight
)
if (!showHat) return
// Hat - Front
model.setTransform(1, -0.5, 0, 1.2, 0, 0)
model.drawImage
( scratchCanv
, 40 * scale
, scaleEight
, scaleEight
, scaleEight
, 10 * scale
, 13/1.2 * scale
, scaleEight
, scaleEight
)
// Hat - Right
model.setTransform(1, 0.5, 0, 1.2, 0, 0)
model.drawImage
( scratchCanv
, 32 * scale
, scaleEight
, scaleEight
, scaleEight
, 2 * scale
, 3/1.2 * scale
, scaleEight
, scaleEight
)
// Hat - Top
model.setTransform(-1, 0.5, 1, 0.5, 0, 0)
model.scale(-1, 1)
model.drawImage
( scratchCanv
, 40 * scale
, 0
, scaleEight
, scaleEight
, -3 * scale
, 5 * scale
, scaleEight
, scaleEight
)
}
, drawModel: function(model, scratchCanv, scratch, showHat, scale) {
var scaleEight = 8 * scale
// Left Leg
// Left Leg - Front
model.setTransform(1, -0.5, 0, 1.2, 0, 0)
model.scale(-1, 1)
model.drawImage
( scratchCanv
, 4 * scale
, 20 * scale
, 4 * scale
, 12 * scale
, -16 * scale
, 34.4/1.2 * scale
, 4 * scale
, 12 * scale
)
// Right Leg
// Right Leg - Right
model.setTransform(1, 0.5, 0, 1.2, 0, 0)
model.drawImage
( scratchCanv
, 0 * scale
, 20 * scale
, 4 * scale
, 12 * scale
, 4 * scale
, 26.4/1.2 * scale
, 4 * scale
, 12 * scale
)
// Right Leg - Front
model.setTransform(1, -0.5 ,0, 1.2, 0, 0)
model.drawImage
( scratchCanv
, 4 * scale
, 20 * scale
, 4 * scale
, 12 * scale
, 8 * scale
, 34.4/1.2 * scale
, 4 * scale
, 12 * scale
)
// Arm Left
// Arm Left - Front
model.setTransform(1, -0.5, 0, 1.2, 0, 0)
model.scale(-1, 1)
model.drawImage
( scratchCanv
, 44 * scale
, 20 * scale
, 4 * scale
, 12 * scale
, -20 * scale
, 20/1.2 * scale
, 4 * scale
, 12 * scale
)
// Arm Left - Top
model.setTransform(-1, 0.5, 1, 0.5, 0, 0)
model.drawImage
( scratchCanv
, 44 * scale
, 16 * scale
, 4 * scale
, 4 * scale
, 0
, 16 * scale
, 4 * scale
, 4 * scale
)
// Body
// Body - Front
model.setTransform(1, -0.5, 0, 1.2, 0, 0)
model.drawImage
( scratchCanv
, 20 * scale
, 20 * scale
, 8 * scale
, 12 * scale
, 8 * scale
, 20/1.2 * scale
, scaleEight
, 12 * scale
)
// Arm Right
// Arm Right - Right
model.setTransform(1, 0.5, 0, 1.2, 0, 0)
model.drawImage
( scratchCanv
, 40 * scale
, 20 * scale
, 4 * scale
, 12 * scale
, 0
, 16/1.2 * scale
, 4 * scale
, 12 * scale
)
// Arm Right - Front
model.setTransform(1, -0.5, 0, 1.2, 0, 0)
model.drawImage
( scratchCanv
, 44 * scale
, 20 * scale
, 4 * scale
, 12 * scale
, 4 * scale
, 20/1.2 * scale
, 4 * scale
, 12 * scale
)
// Arm Right - Top
model.setTransform(-1, 0.5, 1, 0.5, 0, 0)
model.scale(-1 ,1)
model.drawImage
( scratchCanv
, 44 * scale
, 16 * scale
, 4 * scale
, 4 * scale
, -16 * scale
, 16 * scale
, 4 * scale
, 4 * scale
)
methods.drawHead(model, scratchCanv, scratch, showHat, scale)
}
}
//Scales using nearest neighbour
function scaleImage(imageData, context, dx, dy, scale) {
var width = imageData.width
, height = imageData.height
context.clearRect(0, 0, width, height) //Clear the spot where it originated from
for (var y = 0; y < height; y++) { // Height original
for (var x = 0; x < width; x++) { // Width original
// Gets original colour, then makes a scaled square of the same colour
var index = (x + y * width) * 4
, fill = imageData.data[index]
fill += ',' + imageData.data[index + 1] + ',' + imageData.data[index + 2] + ',' + imageData.data[index + 3]
context.fillStyle = 'rgba(' + fill + ')'
context.fillRect(dx + x * scale, dy + y * scale, scale, scale)
}
}
}
$.fn[pluginName] = function( method ) {
if (methods[method]) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ))
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments)
} else {
$.error('Method ' + method + ' does not exist in jQuery.' + pluginName)
}
}
})( jQuery )