-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
163 lines (138 loc) · 6.58 KB
/
app.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
// Create head canvas
const CANVAS = new HeadCanvas({
textureURL: 'img/grian.png',
width: 350,
height: 350,
distance: 2.5,
backgroundColor: 0xeeeeee
});
CANVAS.setParent(document.getElementById('putCanvasHere'));
CANVAS.init(() => {
// == Elements ==========================
const $textureMethod = document.getElementById('textureMethod');
const $uploadSkinTab = document.getElementById('uploadSkinTab');
const $uuidTab = document.getElementById('uuidTab');
const $uploadSkin = document.getElementById('uploadSkin');
const $uuid = document.getElementById('uuid');
const $uuidSet = document.getElementById('uuidSet');
const $getUUIDOfPlayer = document.getElementById('getUUIDOfPlayer');
const $enablePrimaryLayer = document.getElementById('enablePrimaryLayer');
const $enableSecundaryLayer = document.getElementById('enableSecundaryLayer');
const $enableAnimation = document.getElementById('enableAnimation');
const $backgroundColor = document.getElementById('backgroundColor');
// == URL Params ==========================
const urlParams = new URLSearchParams(window.location.search);
let uuidParam = urlParams.get('uuid');
let isPrimaryEnabledParam = urlParams.get('isPrimaryEnabled');
let isSecundaryEnabledParam = urlParams.get('isSecundaryEnabled');
let isAnimationEnabledParam = urlParams.get('isAnimationEnabled');
let backgroundColorParam = urlParams.get('backgroundColor');
if (uuidParam) {
$uuid.value = uuidParam;
$textureMethod.value = 'uuid';
uuidInput();
}
// I know the following code looks ugly...
if (typeof isPrimaryEnabledParam == 'string' && isPrimaryEnabledParam.toLowerCase() == 'true') $enablePrimaryLayer.checked = true;
if (typeof isPrimaryEnabledParam == 'string' && isPrimaryEnabledParam.toLowerCase() == 'false') $enablePrimaryLayer.checked = false;
if (typeof isSecundaryEnabledParam == 'string' && isSecundaryEnabledParam.toLowerCase() == 'true') $enableSecundaryLayer.checked = true;
if (typeof isSecundaryEnabledParam == 'string' && isSecundaryEnabledParam.toLowerCase() == 'false') $enableSecundaryLayer.checked = false;
if (typeof isAnimationEnabledParam == 'string' && isAnimationEnabledParam.toLowerCase() == 'true') $enableAnimation.checked = true;
if (typeof isAnimationEnabledParam == 'string' && isAnimationEnabledParam.toLowerCase() == 'false') $enableAnimation.checked = false;
$backgroundColor.value = /^\#[\da-f]{6}$/i.test(backgroundColorParam) ? backgroundColorParam : '#eeeeee';
function setURLParam(param, val) {
urlParams.set(param, val);
let newUrl = window.location.protocol + "//" + window.location.host + window.location.pathname + '?' + urlParams.toString();
window.history.pushState({path: newUrl}, '', newUrl);
}
// == Animation function ==========================
let isAnimationEnabled = $enableAnimation.checked;
function animate() {
requestAnimationFrame( animate );
if (isAnimationEnabled && CANVAS.cubeP) {
CANVAS.cubeP.rotation.y += .005;
CANVAS.cubeS.rotation.y += .005;
CANVAS.controls.update();
}
CANVAS.render();
}
animate();
// == Settings ==========================
// Primary layer
function primaryLayerEnabledChanged() {
CANVAS.setPrimaryVisibility($enablePrimaryLayer.checked);
setURLParam('isPrimaryEnabled', $enablePrimaryLayer.checked.toString());
}
primaryLayerEnabledChanged();
$enablePrimaryLayer.addEventListener('change', primaryLayerEnabledChanged);
// Secundary layer
function secundaryLayerEnabledChanged() {
CANVAS.setSecundaryVisibility($enableSecundaryLayer.checked);
setURLParam('isSecundaryEnabled', $enableSecundaryLayer.checked.toString());
}
secundaryLayerEnabledChanged();
$enableSecundaryLayer.addEventListener('change', secundaryLayerEnabledChanged);
// Animation
function animationEnabledChanged() {
isAnimationEnabled = $enableAnimation.checked;
setURLParam('isAnimationEnabled', $enableAnimation.checked.toString());
}
animationEnabledChanged();
$enableAnimation.addEventListener('change', animationEnabledChanged);
// Background color
function backgroundColorChanged() {
CANVAS.setBackgroundColor(parseInt(`0x${$backgroundColor.value.substr(1)}`));
setURLParam('backgroundColor', $backgroundColor.value);
}
$backgroundColor.addEventListener('change', backgroundColorChanged);
backgroundColorChanged();
// == Inputs ==========================
function textureMethodChanged() {
$uploadSkinTab.classList.add('hidden');
$uuidTab.classList.add('hidden');
let textureMethod = $textureMethod.value;
if (textureMethod == 'file') {
$uploadSkinTab.classList.remove('hidden');
if ($uploadSkin.files[0]) uploadedSkin();
} else {
$uuidTab.classList.remove('hidden');
}
}
$textureMethod.addEventListener('change', textureMethodChanged);
textureMethodChanged();
// File upload
async function uploadedSkin() {
let file = $uploadSkin.files[0];
let base64URL = await getBase64FromFile(file);
CANVAS.setTextureFromURL(base64URL);
}
$uploadSkin.addEventListener('change', uploadedSkin);
async function getBase64FromFile(file) {
return new Promise( resolve => {
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
resolve(reader.result);
};
reader.onerror = function (error) {
console.error('Could not load file: ', error);
resolve(null);
};
});
}
// UUID input
function uuidInput() {
let uuid = $uuid.value.replaceAll('-', '');
CANVAS.setTextureFromURL('https://crafatar.com/skins/' + uuid);
}
$uuidSet.addEventListener('click', uuidInput);
$getUUIDOfPlayer.addEventListener('click', () => {
let playerName = window.prompt('Type the name of a player...');
if (playerName == '' || playerName == null) return;
let apiWindow = window.open(`https://api.mojang.com/users/profiles/minecraft/${playerName}`);
let uuid = window.prompt('Get the UUID from the page (the long string of characters next to \'id\') and paste it in this dialog box');
if (uuid == '' || uuid == null) return;
if (apiWindow) apiWindow.close();
CANVAS.setTextureFromURL('https://crafatar.com/skins/' + uuid);
});
});