-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskins.html
677 lines (609 loc) · 25.6 KB
/
skins.html
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
<!DOCTYPE html>
<html>
<head>
<title>Minetest Skin Converter</title>
<link rel="stylesheet" type="text/css" href="character.css">
<style type="text/css">
body {
margin: 15px;
font-family: Verdana, Tahoma, Helvetica, Arial, sans-serif;
font-size: 1rem;
font-weight: 400;
line-height: 1.2;
color: #ccc;
text-align: left;
background-color: #1a2351;
max-width: 100vw;
overflow-x: hidden;
overflow: auto;
height: 100%;
}
/* Do not blur zoomed images; https://drafts.csswg.org/css-images-3/#the-image-rendering */
img,
div {
image-rendering: pixelated;
image-rendering: -moz-crisp-edges;
image-rendering: -o-crisp-edges;
image-rendering: -webkit-optimize-contrast;
-ms-interpolation-mode: nearest-neighbor;
}
/* Hide tools and controls */
div#imgTools {
display: none;
}
div#imgControl {
display: none;
}
table,
td,
tr {
margin: 0em;
padding: 0em;
border: 0em;
background-color: inherit;
font: inherit;
text-align: left;
align: left;
}
table {
width: 100%;
}
td {
width: 50%;
}
.tooltip {
position: relative;
display: inline-block;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 140px;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px;
position: absolute;
z-index: 1;
bottom: 150%;
left: 50%;
margin-left: -75px;
opacity: 0;
transition: opacity 0.3s;
}
.tooltip .tooltiptext::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: #555 transparent transparent transparent;
}
.tooltip:hover .tooltiptext {
visibility: visible;
opacity: 1;
}
</style>
</head>
<body id="body">
<h1 style="color:violet;">MC TO MT SKINS #EPIC #NOCLICKBAIT #NOADS #ARKACIA</h1>
<h3 style="color:violet;">#XFDXDFXDF #EASY #QUICK #MINETEST #WHATSLUANTI #MCL #OMGWHATSVOXELIBRETOO</h3>
<!-- use <u>...</u> to mark on-mouse-over help / title tag -->
<div id="loader">
<table>
<tr>
<td>
Load a <span title="64×64 / 256×256 / 512×512 / 1024×1024 PNG file"><u>Minecraft (1:1)</u></span> or
<span title="64×32 / 512×256 / 1024×512 PNG file"><u>Minetest (2:1)</u></span> skin: <input
type="file" id="imgLoader" accept="image/png"
onChange="loadImage(document.getElementById(constImgLoader).files[0])" />
</td>
</tr>
</table>
</div>
<!--<div id="urlImgArea" display="none">
<textarea style="width: 100%;" id="imgUrlBox">
https://www.minecraftskins.com/uploads/skins/2024/11/15/polar-bear-hoodie-22877078.png
</textarea>
<button id="imgUrlBtn" onclick="loadImgFromUrl()">load image URL</button>
</div>-->
<div id="imgTools">
<table>
<tr>
<td>
Zoom to <button type="button" onclick="zoom.to('0.25')">1:4</button>
<button type="button" onclick="zoom.to('0.5')">1:2</button>
<button type="button" onclick="zoom.to('1')">1:1</button>
<button type="button" onclick="zoom.to('2')">2:1</button>
<button type="button" onclick="zoom.to('4')">4:1</button>
<button type="button" onclick="zoom.to('8')">8:1</button>
<button type="button" onclick="zoom.to('16')">16:1</button>
</td>
<td>
Update <button type="button" onclick="renderAs('mcCharacter')"
title="Render as Minecraft Character"><u>Minecraft</u></button>
<button type="button" onclick="renderAs('mcCloth')"
title="Render as Minecraft Character with Clothes"><u>with Clothes</u></button> /
<button type="button" onclick="renderAs('mtCharacter')"
title="Render as Minetest"><u>Minetest</u></button>
<button type="button" onclick="renderAs('mtCloth')"
title="Render as Minetest Character with Clothes"><u>with Hat & Cape</u></button> 3d character.
</td>
</tr>
<tr>
<td>Background Color:<input type="color" value="#ccc"
onchange="setBackgroundColor('dstImg', this.value)"
onclick="setBackgroundColor('dstImg', this.value)" />
<!-- issue: onchange() is not triggered if the bg color was checkerboard before
fix: onclick() but this changes the color value even if the user selects cancel -->
<button type="button" onclick="setBackgroundColor('dstImg', 'checkerboard')">Checkerboard</button>
/ <input type="color" value="#ccc" onchange="setBackgroundColor('srcImg', this.value)"
onclick="setBackgroundColor('srcImg', this.value)" />
<button type="button" onclick="setBackgroundColor('srcImg', 'checkerboard')">Checkerboard</button>
</td>
<td><input type="color" value="#ccc" onchange="setBackgroundColor('characterElementId', this.value)"
onclick="setBackgroundColor('characterElementId', this.value)" />
<button type="button"
onclick="setBackgroundColor('characterElementId', 'checkerboard')">Checkerboard</button>
</td>
</tr>
</table>
</div>
<div id="imgControl">
<table>
<tr>
<td>
<button type="button" onclick="copy('areaJacket.back', 'areaCapeMinetest')"
title="Copy Minecraft 'Cape' to Minetest"><u>Copy</u></button> /
<button type="button" onclick="copy('areaCapeMinetest', 'areaNull')"
title="Replace 'Cape' area with transparency"><u>Erase</u></button> Cape
</td>
<td><!-- empty --></td>
</tr>
<tr>
<td>
Copy: <input id="alphaJacket" type="text" size="1" value="0.5" title="Alpha value"><button
type="button" onclick="copy('areaJacket.all', 'areaBody.all', '0', 'alphaJacket')"
title="Copy Minecraft 'Jacket' over Minetest 'Body' (including cape) with alpha transparency"><u>Jacket</u></button>
/
<button type="button" onclick="copy('areaBody.back', 'areaBody.back')"
title="Restore the back of the Minetest body"><u>Body.Back</u></button> /
<button type="button" onclick="copy('areaBody.all', 'areaBody.all')"
title="Restore the whole Minetest body"><u>Body</u></button>
</td>
<td><!-- empty --></td>
</tr>
<tr>
<td>
Copy Left: <input id="alphaTrouserLeft" type="text" size="1" value="0.5" title="Alpha value"><button
type="button"
onclick="copy('areaTrouserLeft.all', 'areaLegRight.all', '1', 'alphaTrouserLeft' )"
title="Copy and flip 'Left Trouser' over 'Right Leg' with alpha transparency"><u>Trouser</u></button>
/
<button type="button" onclick="copy('areaLegLeft.all', 'areaLegRight.all', '1')"
title="Copy and flip the left leg"><u>Leg</u></button>.
Copy Right: <input id="alphaTrouserRight" type="text" size="1" value="0.5"
title="Alpha value"><button type="button"
onclick="copy('areaTrouserRight.all', 'areaLegRight.all', '0', 'alphaTrouserRight' )"
title="Copy 'Right Trouser' over 'Right Leg' with alpha transparency"><u>Trouser</u></button> /
<button type="button" onclick="copy('areaLegRight.all', 'areaLegRight.all')"
title="Restore the right leg"><u>Leg</u></button>
</td>
<td><!-- empty --></td>
</tr>
<tr>
<td>
Copy Left: <input id="alphaSleeveLeft" type="text" size="1" value="0.5" title="Alpha value"><button
type="button" onclick="copy('areaSleeveLeft.all', 'areaArmRight.all', '1', 'alphaSleeveLeft' )"
title="Copy and flip 'Left Sleeve' over 'Right Arm' with alpha transparency"><u>Sleeve</u></button>
/
<button type="button" onclick="copy('areaArmLeft.all', 'areaArmRight.all', '1')"
title="Copy and flip the left arm"><u>Arm</u></button>
Copy Right: <input id="alphaSleeveRight" type="text" size="1" value="0.5"
title="Alpha value"><button type="button"
onclick="copy('areaSleeveRight.all', 'areaArmRight.all', '0', 'alphaSleeveRight')"
title="Copy 'Right Sleeve' over 'Right Arm' with alpha transparency"><u>Sleeve</u></button>
<button type="button" onclick="copy('areaArmRight.all', 'areaArmRight.all')"
title="Restore the right arm"><u>Arm</u></button>
</td>
<td><!-- empty --></td>
</tr>
</table>
</div>
<div id="images">
<table>
<tr>
<td>
<p style="font-size:15px;">MT skin:</p>
<img id="dstImg" src="" border="1"
alt="When skin loads, right-click & Save Image As ..." /><br />
</td>
<td rowspan="2" valign="top" style="min-width:64px;">
<div id="characterElementId"></div>
</td>
</tr>
<tr>
<td style="display:none;">
<!-- <p style="font-size:15px;">Original skin:</p> -->
<img id="srcImg" src="" border="1" alt="Placeholder for the source skin"
title="Right-Click; Save Image As ..." />
</td>
</tr>
<tr>
<td>
<p style="font-size:15px;">Code:</p>
<button style="background-color: #ccc;" class="tooltip" onclick="copyFunction()">Copy text
<span class="tooltiptext" id="myTooltip">Copy to clipboard</span>
</button>
<textarea readonly style="width:100%;height:200px;background-color: #ccc;" id="txt"></textarea>
</td>
</tr>
</table>
</div>
<!-- loading local js files with UTF-8 characters will fail without charset="utf-8" -->
<script type="text/javascript" charset="utf-8" src="zoom.js"></script>
<script type="text/javascript" charset="utf-8" src="uvareas.js"></script>
<script type="text/javascript" charset="utf-8" src="three.min.js"></script>
<script type="text/javascript" charset="utf-8" src="uvcharacter.js"></script>
<script type="text/javascript" charset="utf-8">//<![CDATA[
// https://godly.github.io/minetest-skin-converter/
// Strings used in HTML as IDs and in code
// Use these vars while coding to avoid typos.
const constImgLoader = "imgLoader";
const constSkinMinecraft = "minecraft";
const constSkinMinetest = "minetest";
const constIdSrcImage = "srcImg";
const constIdDstImage = "dstImg";
const constLoader = "loader";
const constImageTools = "imgTools";
const constImageControls = "imgControl";
const idImages = "images";
const strTransparency = "transparency";
var zoom = new Zooming('images');
let url = 'https://cdn.shopify.com/s/files/1/0234/8017/2591/products/young-man-in-bright-fashion_925x_f7029e2b-80f0-4a40-a87b-834b9a283c39.jpg';
const toDataURL = url => fetch(url)
.then(response => response.blob())
.then(blob => new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.onerror = reject;
reader.readAsDataURL(blob);
}));
function dataURLtoFile(dataurl, filename) {
var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], filename, { type: mime });
}
//scp dollhouse
function loadImgFromUrl() {
//https://www.minecraftskins.com/skin/22877078/polar-bear-hoodie/
//https://namemc.com/profile/Crispy156.1
/*
https://www.minecraftskins.com/uploads/skins/2024/11/15/polar-bear-hoodie-22877078.png
*/
var val = document.getElementById('imgUrlBox').value,
src = val;//'http://webpage.com/images/' + val + '.png',
img = document.createElement('img');
img.src = src;
// document.body.appendChild(img);
// var v = getFileFromUrl(val)
// console.log(v)
var v = toDataURL(val)
.then(dataUrl => {
console.log('Here is Base64 Url', dataUrl);
var fileData = dataURLtoFile(dataUrl, "imageName.jpg");
console.log("Here is JavaScript File Object", fileData);
fileArr.push(fileData);
});
console.log(v);
loadImage(v);
}
async function getFileFromUrl(url, name, defaultType = 'image/png') {
name = name || url.replace(/^.*(\\|\/|\:)/, '');
const response = await fetch(url);
const data = await response.blob();
return new File([data], name, {
type: data.type || defaultType,
});
}
// `await` can only be used in an async body, but showing it here for simplicity.
// const file = await getFileFromUrl('https://example.com/image.jpg', 'example.jpg'); // document.getElementById("imgUrlBox").addEventListener('input', e => { loadImgFromUrl(); });
function copyFunction() {
// Get the text field
var copyText = document.getElementById("txt");
// Select the text field
copyText.select();
copyText.setSelectionRange(0, 99999); // For mobile devices
// Copy the text inside the text field
navigator.clipboard.writeText(copyText.value);
// Alert the copied text
// alert("Copied the text: " + copyText.value);
}
function setBackgroundColor(dstElem, value) {
console.log("INFO\tsetBackgroundColor(" + dstElem + ", " + value + ")");
if (value == "checkerboard") {
document.getElementById(dstElem).style.backgroundImage = "url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAQAAADYv8WvAAAAEklEQVQI12Oc+f8sA9NZBmMGABasA2lR+US6AAAAAElFTkSuQmCC')";
document.getElementById(dstElem).style.backgroundSize = "16px 16px";
} else {
document.getElementById(dstElem).style.backgroundImage = "";
document.getElementById(dstElem).style.backgroundColor = value;
};
};
// uvFaceWidth: Block size in pixels, 1/16 of the image width. 4px for standard skins with 64px width.
var uvFaceWidth = 4;
// srcImg, dstImg: The public source and destination images
var srcImg = document.getElementById(constIdSrcImage);
var dstImg = document.getElementById(constIdDstImage);
// dstCanvas: The internal image buffer for the modifications (copy, alpha, mirror, ...)
var dstCanvas = document.createElement('canvas');
// Main function to load the image after the user selected it
function loadImage(img) {
// reset zoom level before loading. Otherwise "em" scaling causes random issues
zoom.to(1);
// load selected image
var file = img;
srcImg.style.width = "auto";
srcImg.style.height = "auto";
srcImg.addEventListener("load", function() {
imageLoaded();
}, false);
srcImg.src = window.URL.createObjectURL(file);
// function continues in imageLoaded() as soon as the image is loaded
}
function imageLoaded() {
/*
localCharacterCanvas.width = srcImg.width;
localCharacterCanvas.height = srcImg.width; // canvas will be should be square
localCharacterCanvas.getContext("2d").drawImage(srcImg, 0, 0);
minecraftCharacter.render;
*/
// calculate width and height of target image
var srcWidth = srcImg.width;
var srcHeight = srcImg.height; // Skins must be square or ½ square box. Otherwise skin conversion fails
var dstWidth = srcWidth;
var dstHeight = srcWidth / 2; // srcWidth/2 to get ½ square. We don't rely on srcHeight
uvFaceWidth = srcWidth / 16;
uvFaceWidth = srcWidth / constUvWidth;
// assume Minecraft skin
var srcSkinType = constSkinMinecraft;
if (srcWidth == srcHeight * 2) {
// setting skin source to Minetest and the image format to square (widht=height).
srcSkinType = constSkinMinetest;
dstHeight = srcWidth;
}
// Set image size to 'em' to allow zooming
srcImg.style.width = srcWidth / 16 + "em";
srcImg.style.height = srcHeight / 16 + "em";
dstImg.style.width = dstWidth / 16 + "em";
dstImg.style.height = dstHeight / 16 + "em";
if ((srcWidth == srcHeight) || (srcWidth == (srcHeight * 2))) {
// Expected image size
setBackgroundColor(constIdSrcImage, 'checkerboard');
setBackgroundColor(constIdDstImage, 'checkerboard');
// Hide loader and show zoom and background buttons
document.getElementById(constLoader).style.display = "none";
document.getElementById(constImageTools).style.display = "initial";
if (srcWidth == srcHeight) {
// Minecraft image, show copy buttons etc.
document.getElementById(constImageControls).style.display = "initial";
}
} else {
// Expect the unexpected
alert("Please upload a 64×64 / 256×256 / 512×512 / 1024×1024 Minecraft or a 64×32 / 512×256 / 1024×512 Minetest image! (not " + srcWidth + "×" + srcHeight + ")");
return;
};
// Copy upper part of skin (1:1)
dstCanvas.width = dstWidth;
dstCanvas.height = dstHeight;
var context = dstCanvas.getContext('2d');
draw(srcImg, areaMinetest, context, areaMinetest, uvFaceWidth, 0);
// adjust for Minetest / Minecraft
if (srcSkinType == constSkinMinecraft) {
// cleanup unused areas
draw(srcImg, areaHead.unusedL, context, areaNull, uvFaceWidth, 0);
draw(srcImg, areaHead.unusedR, context, areaNull, uvFaceWidth, 0);
draw(srcImg, areaHat.unusedL, context, areaNull, uvFaceWidth, 0);
draw(srcImg, areaHat.unusedR, context, areaNull, uvFaceWidth, 0);
draw(srcImg, areaLegRight.unusedL, context, areaNull, uvFaceWidth, 0);
draw(srcImg, areaLegRight.unusedR, context, areaNull, uvFaceWidth, 0);
draw(srcImg, areaBody.unusedL, context, areaNull, uvFaceWidth, 0);
draw(srcImg, areaBody.unusedR, context, areaNull, uvFaceWidth, 0);
draw(srcImg, areaArmRight.unusedL, context, areaNull, uvFaceWidth, 0);
draw(srcImg, areaArmRight.unusedR, context, areaNull, uvFaceWidth, 0);
draw(srcImg, areaUnusedMinetest, context, areaNull, uvFaceWidth, 0);
// copy Jacket.back(Cape) to areaCapeMinetest
draw(srcImg, areaJacket.back, context, areaCapeMinetest, uvFaceWidth, 0);
} else {
// srcSkinType == constSkinMinetest
// cleanup unused areas in upper part of the image
draw(srcImg, areaHead.unusedL, context, areaNull, uvFaceWidth, 0);
draw(srcImg, areaHead.unusedR, context, areaNull, uvFaceWidth, 0);
draw(srcImg, areaHat.unusedL, context, areaNull, uvFaceWidth, 0);
draw(srcImg, areaHat.unusedR, context, areaNull, uvFaceWidth, 0);
draw(srcImg, areaLegRight.unusedL, context, areaNull, uvFaceWidth, 0);
draw(srcImg, areaLegRight.unusedR, context, areaNull, uvFaceWidth, 0);
draw(srcImg, areaBody.unusedL, context, areaNull, uvFaceWidth, 0);
draw(srcImg, areaBody.unusedR, context, areaNull, uvFaceWidth, 0);
draw(srcImg, areaArmRight.unusedL, context, areaNull, uvFaceWidth, 0);
draw(srcImg, areaArmRight.unusedR, context, areaNull, uvFaceWidth, 0);
draw(srcImg, areaUnusedMinecraft, context, areaNull, uvFaceWidth, 0);
// Lower part of the image is blank, nothing to clear
// copy Jacket.back
draw(srcImg, areaCapeMinetest, context, areaJacket.back, uvFaceWidth, 0);
// copy and mirror leg: top
draw(srcImg, areaLegRight.top, context, areaLegLeft.top, uvFaceWidth, 1);
// copy and mirror leg: bottom
draw(srcImg, areaLegRight.bottom, context, areaLegLeft.bottom, uvFaceWidth, 1);
// copy and mirror leg: right, front, left
draw(srcImg, areaLegRight.lfr, context, areaLegLeft.lfr, uvFaceWidth, 1);
// copy and mirror leg: back
draw(srcImg, areaLegRight.back, context, areaLegLeft.back, uvFaceWidth, 1);
// copy and mirror arm: top
draw(srcImg, areaArmRight.top, context, areaArmLeft.top, uvFaceWidth, 1);
// copy and mirror arm: bottom
draw(srcImg, areaArmRight.bottom, context, areaArmLeft.bottom, uvFaceWidth, 1);
// copy and mirror arm: right, front, left
draw(srcImg, areaArmRight.lfr, context, areaArmLeft.lfr, uvFaceWidth, 1);
// copy and mirror arm: back
draw(srcImg, areaArmRight.back, context, areaArmLeft.back, uvFaceWidth, 1);
}
// copy data from the internal dstCanvas to the public dstImg
var tmpDataUrl = dstCanvas.toDataURL('image/png');
dstImg.src = tmpDataUrl;
var idname = document.getElementById(constImgLoader).files[0].name.replace(new RegExp("\\..*", ""), "");
idname = (idname.replace(/[^a-zA-Z0-9]/g, '_'));
document.getElementById("txt").value = " " + idname + " = \"" + tmpDataUrl.replace("data:image/png;base64,", "") + "\",\n";
renderAs('mcCloth');
zoom.to(4);
}
/*
copy(from, to, flip, alphaId)
from: srcArea
to: dstArea. Set to 'areaNull' to clean the region defined in srcArea
flip values: 0 = normal copy, 1 = flip area
alphaId: element which stores the desired alpha value
*/
function copy(from, to, flip, alphaId) {
var flipValue = 0;
if (flip != null) {
flipValue = flip;
}
var alpha = 1;
if (alphaId) {
alpha = document.getElementById(alphaId).value;
};
console.log("INFO\tcopy from " + from + " to " + to + ". Alpha: " + alpha + " (" + alphaId + "). Flip:" + flip);
var context = dstCanvas.getContext('2d');
areaFrom = eval(from);
areaTo = eval(to);
if (areaTo == areaNull) {
draw(srcImg, areaFrom, context, areaNull, uvFaceWidth, flipValue);
} else if (flip == 1) {
var fromArray = from.split(".");
var toArray = to.split(".");
if ((fromArray[1] == "all") && (toArray[1] == "all")) {
// copy and mirror arm: top; bottom; right, front, left; back
context.globalAlpha = alpha;
draw(srcImg, eval(fromArray[0] + ".top"), context, eval(toArray[0] + ".top"), uvFaceWidth, flipValue);
draw(srcImg, eval(fromArray[0] + ".bottom"), context, eval(toArray[0] + ".bottom"), uvFaceWidth, flipValue);
draw(srcImg, eval(fromArray[0] + ".lfr"), context, eval(toArray[0] + ".lfr"), uvFaceWidth, flipValue);
draw(srcImg, eval(fromArray[0] + ".back"), context, eval(toArray[0] + ".back"), uvFaceWidth, flipValue);
} else {
console.error("ERROR\tcopy 'from' and 'to' must end with '.all'");
}
} else {
context.globalAlpha = alpha;
draw(srcImg, areaFrom, context, areaTo, uvFaceWidth, flipValue);
}
var tmpDataUrl = dstCanvas.toDataURL('image/png');
dstImg.src = tmpDataUrl;
}
/*
draw(srcImg, srcArea, dstContext, dstArea, uvFaceWidth, flip)
srcImg: the source image to read the skin from
srcArea: the area to read
dstContext: the context to write the skin to
dstArea: dstArea. Set to 'areaNull' to clean the region defined in srcArea
uvFaceWidth: uvFaceWidth
flip: 0 = normal copy, 1 = flip area horizontally
*/
function draw(srcImg, srcArea, dstContext, dstArea, uvFaceWidth, flip) {
console.log("INFO\tdraw (" + srcArea.x + "/" + srcArea.y + ")-(" + srcArea.w + "/" + srcArea.h + ") (" + dstArea.x + "/" + dstArea.y + ") " + uvFaceWidth + " " + flip);
var bsClipX = srcArea.x * uvFaceWidth;
var bsClipY = srcArea.y * uvFaceWidth;
var bsWidth = srcArea.w * uvFaceWidth;
var bsHeight = srcArea.h * uvFaceWidth;
var bsDstX = dstArea.x * uvFaceWidth;
var bsDstY = dstArea.y * uvFaceWidth;
if ((dstArea.w == 0) || (dstArea.h == 0)) {
dstContext.clearRect(bsClipX, bsClipY, bsWidth, bsHeight);
} else if (flip == 0) {
dstContext.drawImage(srcImg, bsClipX, bsClipY, bsWidth, bsHeight, bsDstX, bsDstY, bsWidth, bsHeight);
} else {
// use a temporary canvas to flip the element
var canvas = document.createElement('canvas');
canvas.width = bsWidth;
canvas.height = bsHeight;
var context = canvas.getContext('2d');
context.save();
if (flip == 1) {
context.translate(bsWidth, 0);
context.scale(-1, 1);
}
context.drawImage(srcImg, bsClipX, bsClipY, bsWidth, bsHeight, 0, 0, bsWidth, bsHeight);
context.restore();
dstContext.drawImage(canvas, bsDstX, bsDstY, bsWidth, bsHeight);
}
}
// create 3d character (uvcharacter.js)
var minecraftCharacter = new MinecraftCharacter('characterElementId');
var localScene = minecraftCharacter.scene; // The THREE.js scene
var localCharacterCanvas = minecraftCharacter.canvas; // The canvas which contains the UV map of the skin
function renderAs(type) {
console.log("renderAs(" + type + ")");
var showCloth = false;
if ((type == "mcCloth") || (type == "mtCloth")) {
// show cloth
showCloth = true;
}
localScene.getObjectByName(constHat).visible = showCloth;
localScene.getObjectByName(constJacket).visible = showCloth;
localScene.getObjectByName(constSleeveLeft).visible = showCloth;
localScene.getObjectByName(constSleeveRight).visible = showCloth;
localScene.getObjectByName(constTrouserLeft).visible = showCloth;
localScene.getObjectByName(constTrouserRight).visible = showCloth;
localCharacterCanvas.width = dstCanvas.width;
localCharacterCanvas.height = dstCanvas.width; // square
var context = localCharacterCanvas.getContext("2d");
context.clearRect(0, 0, dstCanvas.width, dstCanvas.width);
if ((type == "mcCharacter") || (type == "mcCloth")) {
// show Minecraft skin
if (dstCanvas.width == dstCanvas.height) {
// srcImg is Minetest (½ square), dstCanvas was properly build
context.drawImage(dstCanvas, 0, 0);
} else {
// srcImg is Minecraft (square), use it here
context.drawImage(srcImg, 0, 0);
}
}
if ((type == "mtCharacter") || (type == "mtCloth")) {
if (dstCanvas.width == dstCanvas.height) {
// srcImg is Minetest (½ square), dstCanvas was properly build
context.drawImage(dstCanvas, 0, 0);
} else {
// srcImg is Minecraft (square), dstCanvas (½ square) contains the Minetest image
// copy the Minetest image
context.drawImage(dstCanvas, 0, 0);
// fixme / duplicate code
// copy Jacket.back
draw(dstCanvas, areaCapeMinetest, context, areaJacket.back, uvFaceWidth, 0);
// copy and mirror leg: top
draw(dstCanvas, areaLegRight.top, context, areaLegLeft.top, uvFaceWidth, 1);
// copy and mirror leg: bottom
draw(dstCanvas, areaLegRight.bottom, context, areaLegLeft.bottom, uvFaceWidth, 1);
// copy and mirror leg: right, front, left
draw(dstCanvas, areaLegRight.lfr, context, areaLegLeft.lfr, uvFaceWidth, 1);
// copy and mirror leg: back
draw(dstCanvas, areaLegRight.back, context, areaLegLeft.back, uvFaceWidth, 1);
// copy and mirror arm: top
draw(dstCanvas, areaArmRight.top, context, areaArmLeft.top, uvFaceWidth, 1);
// copy and mirror arm: bottom
draw(dstCanvas, areaArmRight.bottom, context, areaArmLeft.bottom, uvFaceWidth, 1);
// copy and mirror arm: right, front, left
draw(dstCanvas, areaArmRight.lfr, context, areaArmLeft.lfr, uvFaceWidth, 1);
// copy and mirror arm: back
draw(dstCanvas, areaArmRight.back, context, areaArmLeft.back, uvFaceWidth, 1);
}
}
minecraftCharacter.render;
minecraftCharacter.reRender; // update character size manually
}
//]]>
</script>
</body>
</html>