-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
179 lines (162 loc) · 6.31 KB
/
main.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
const imageLoader = document.getElementById('fileinput');
imageLoader.addEventListener('change', importImg, false)
const badgecanvas = document.getElementById("badgeCanvas");
const badgecanvasctx = badgecanvas.getContext("2d");
const namefield = document.getElementById("inputtext");
const inputarea = document.getElementById("inputarea");
var uploadedPhoto = new Image();
var userfile;
var uploadedPhoto;
function importImg(e) {
var reader = new FileReader();
userfile = e.target.files[0];
if (userfile) {
var imagename = document.getElementById("imagename");
reader.onprogress = e=> {imagename.innerText = `loading (${Math.round((e.loaded/e.total)*100)}%)..`};
reader.onload = e=> {
imagename.innerText = "applying photo..";
uploadedPhoto.onload = function() {
processBadge(curbadge[0], curbadge[1])
selectBadge(curbadge[1])
imagename.innerText = `(${userfile.name.length >= 15 ? `${userfile.name.substr(0, 8)}..${userfile.name.substr(userfile.name.length - 4)}` : userfile.name})`;
}
uploadedPhoto.src = e.target.result;
}
reader.readAsDataURL(e.target.files[0]);
}
}
// reads badgeslist and creates a div with the content
var badgesdiv = document.getElementById("badges");
function genBadges() {
for (var b in badgeslist) {
badgesdiv.innerHTML +=
`<div class="badge" badgeid="${b}" id="badge${b}">\
<img class="badgePreview" src="images/${badgeslist[b].file}"/>\n\t\
<img class="badgePreview" src="" style="filter: unset; display: none;"/>\n\
<div class="badgeChecker" onclick="selectBadge(${b})">\
<i class="far fa-check-circle" style="position: relative; top: 74px;"></i>\
</div\
</div>`;
}
}
genBadges()
var currentURL = window.location.href;
window.onload = ()=>{
namefield.value = localStorage.getItem("namefield");
selectBadge(currentURL.includes("#badge") ? currentURL.substr(currentURL.indexOf("#badge")+6, 1) : 0)
}
var previd = -1;
function selectBadge(id) {
var badge = document.getElementsByClassName("badge");
window.location.href = `${currentURL.substr(0, currentURL.indexOf("#"))}#badge${id}`;
// previous preview
if (previd != -1) {
var oldpreview = badge[previd].getElementsByTagName("img");
oldpreview[0].style.display = "unset";
oldpreview[1].style.display = "none";
badge[previd].style.filter = "grayscale(1)";
badge[previd].style.transform = "scale(0.9)";
badge[previd].style.borderStyle = "unset";
badge[previd].getElementsByClassName("badgeChecker")[0].firstElementChild.className = "far fa-check-circle";
}
previd = id;
// current preview
var curpreview = badge[id].getElementsByTagName("img");
badge[id].style.filter = "unset";
badge[id].style.transform = "scale(1)";
badge[id].style.borderStyle = "inset";
badge[id].style.borderColor = "red";
badge[id].getElementsByClassName("badgeChecker")[0].firstElementChild.className = "fas fa-sync";
processBadge(curpreview[0], id)
curpreview[0].style.display = "none";
curpreview[1].src = badgecanvas.toDataURL("image/jpeg", 0.3)
curpreview[1].style.display = "unset";
}
function writeText(x, y, sizetype, len, spacing, color, align) {
localStorage.setItem("namefield", namefield.value);
if (namefield.value.length >= len) {
var firstname = namefield.value.substr(0, 20).lastIndexOf(" ");
splitname.push(namefield.value.substr(0, firstname))
splitname.push(namefield.value.substr(firstname + 1))
} else {splitname[0] = namefield.value}
badgecanvasctx.textAlign = align;
badgecanvasctx.fillStyle = color;
badgecanvasctx.font = sizetype;
y = splitname.length!=1 ? y-=20 : y;
//if (splitname.length != 1) {y-= 20}
for (var l = 0; l < splitname.length; l++) {
badgecanvasctx.fillText(splitname[l], x, y + l*(40+spacing))
}
}
function drawPhoto(x, y, ratiow, ratioh) {
if (uploadedPhoto) {
var uphotoratio = calcARatio(uploadedPhoto.width, uploadedPhoto.height, ratiow, ratioh);
// force center and receive parameters as additions
var x = (badgewidth/2-uphotoratio.width/2)+x;
var y = (badgeheight/2-uphotoratio.height/2)+y;
badgecanvasctx.drawImage(uploadedPhoto, x, y, uphotoratio.width, uphotoratio.height)
}
}
function addOverlay(src, func) {
badgecanvasctx.filter = 'blur(20px)';
badgecanvasctx.drawImage(uploadedPhoto, 0, 0, badgecanvas.width, badgecanvas.height)
badgecanvasctx.filter = 'none';
func()
badgecanvasctx.drawImage(src, 0, 0, badgecanvas.width, badgecanvas.height)
}
function setOrientation(src, orient) {
if (orient == 1) {
badgecanvas.width = badgeheight;
badgecanvas.height = badgewidth;
badgecanvasctx.save()
badgecanvasctx.translate(badgecanvas.width/2, badgecanvas.height/2)
badgecanvasctx.rotate(90*Math.PI/180)
badgecanvasctx.drawImage(src, -badgewidth/2, -badgeheight/2, badgewidth, badgeheight)
badgecanvasctx.restore()
} else {
badgecanvas.width = badgewidth;
badgecanvas.height = badgeheight;
badgecanvasctx.drawImage(src, 0, 0, badgecanvas.width, badgecanvas.height)
}
}
function calcARatio(srcWidth, srcHeight, maxWidth, maxHeight) {
var ratio = Math.min(maxWidth/srcWidth, maxHeight/srcHeight);
return {width: srcWidth*ratio, height: srcHeight*ratio};
}
function processBadge(src, id) {
curbadge = [src, id];
splitname = [];
badgewidth = src.width * 5;
badgeheight = src.height * 5;
badgeslist[id].prop(src)
inputarea.style.filter = "unset";
inputarea.style.pointerEvents = "unset";
}
var download = function() {
var link = document.createElement("a");
link.download = `Badge-${namefield.value.replace(/\s/g, "_")}.png`;
link.href = badgecanvas.toDataURL();
link.click()
}
/*
// Experimental Twitter OAuth
function dataURItoBlob(dataURI) {
var arr = dataURI.split(','), mime = arr[0].match(/:(.*?);/)[1];
return new Blob([atob(arr[1])], {type:mime});
}
OAuth.initialize("I5csvxxzDZtLuxVE_f9nMLHLvxc");
function tweetBadge() {
var image = dataURItoBlob(badgeCanvas.toDataURL());
OAuth.popup("twitter").then(function(result) {
var data = new FormData();
data.append('status', "I'm following the #Perseverance rover's journey to Mars, this is my custom badge made with #MarsBadgeMaker (https://salatielsauer.github.io/Mars-Badge-Maker)! #CountdownToMars")
data.append('media[]', image, `Mars-Badge-Maker_${namefield.value.replace(/\s/g, "_")}.png`)
result.post('/1.1/statuses/upload.json', {
data: data,
cache: false,
processData: false,
contentType: false
}).fail(e=>{console.log("error:", e)});
}).fail(e=>{console.log("error:", e)});
}
*/