-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhexagon.html
167 lines (157 loc) · 6 KB
/
hexagon.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Free hexagonal avatars</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta property="og:title" content="hexagonal avatar generator" />
<meta property="og:description" content="just right-click save it! it's free!" />
<meta property="og:image" content="og-image.png" />
<style>
/* ubuntu-mono-regular - latin */
@font-face {
font-family: 'Ubuntu Mono';
font-style: normal;
font-weight: 400;
src: local(''),
url('./fonts/ubuntu-mono-v14-latin-regular.woff2') format('woff2'), /* Chrome 26+, Opera 23+, Firefox 39+ */
url('./fonts/ubuntu-mono-v14-latin-regular.woff') format('woff'); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
}
body {
background-color: #9d71fc;
font-family: sans-serif;
}
h1 {
font-family: 'Ubuntu Mono', monospace;
}
h2 {
font-family: 'Ubuntu Mono', monospace;
}
#avatar {
max-width: 95%;
}
.box {
display: block;
margin-left: auto;
margin-right: auto;
text-align: center;
padding: 10px;
}
.fit-content {
display: inline-block;
padding: 5px;
}
.rainbow-text {
font-family: 'Comic Sans Ms', 'Comic Sans', 'Comic Code', 'Chalkboard SE', sans-serif;
background-image: linear-gradient(to left, violet, indigo, blue, green, yellow, orange, red);
-webkit-background-clip: text;
background-clip: text;
color: transparent;
-webkit-text-fill-color: transparent;
}
</style>
</head>
<body>
<div class="box">
<h1>hexagonal avatar generator</h1>
<h2>just right-click save it! it's free!</h2>
<p>
<canvas id="avatar" width="400" height="400"></canvas>
</p>
<p>
<select id="type">
<option value="pentagon">pentagon</option>
<option value="hexagon" selected>hexagon</option>
<option value="octagon">octagon</option>
<option value="star">star</option>
<option value="heart">heart</option>
</select>
<input type="file" id="rawfile">
</p>
<p>(Just click on the image to download, since phones don't have right click. Use Twitter web to upload and don't resize it to make sure it stays transparent.)</p>
<p><div class="fit-content"><h2 class="rainbow-text">cryptobros get dunked on</h2></div></p>
</div>
</body>
<script type="text/javascript">
// https://gist.github.com/felixzapata/3684117
var canvas = document.getElementById('avatar');
var context = canvas.getContext("2d");
var download = document.getElementById('download');
var type = document.getElementById('type');
var fileinput = document.getElementById('rawfile'); // input file
var img = new Image();
var masks = new Array();
Object.values(type.options).forEach(opt => {
typename = opt.value;
console.log(typename);
masks[typename] = new Image();
masks[typename].src = typename+'.png';
});
canvas.onclick = () => {
download_image = canvas.toDataURL("image/png", 1.0).replace("image/png", "image/octet-stream");
var dl_link = document.createElement('a');
dl_link.download = type.value.substring(0,3)+"-avatar.png";
dl_link.href = download_image;
dl_link.click();
}
var renderImage = () => {
context.clearRect(0, 0, canvas.width, canvas.height);
context.drawImage(masks[type.value], 0, 0, 400, 400);
context.globalCompositeOperation = 'source-out';
var scale = ScaleImage(img.width, img.height, 400, 400, false);
context.drawImage(img, scale.targetleft, scale.targettop, scale.width, scale.height);
}
fileinput.onchange = function(evt) {
var files = evt.target.files; // FileList object
var file = files[0];
if(file.type.match('image.*')) {
var reader = new FileReader();
reader.onload = function(evt){
if( evt.target.readyState == FileReader.DONE) {
img.src = evt.target.result;
img.onload = renderImage
}
}
// Read in the image file as a data URL.
reader.readAsDataURL(file);
} else {
alert("not an image");
}
};
type.onchange = renderImage;
// https://stackoverflow.com/a/14087607 CC BY-SA 3.0
function ScaleImage(srcwidth, srcheight, targetwidth, targetheight, fLetterBox) {
var result = { width: 0, height: 0, fScaleToTargetWidth: true };
if ((srcwidth <= 0) || (srcheight <= 0) || (targetwidth <= 0) || (targetheight <= 0)) {
return result;
}
// scale to the target width
var scaleX1 = targetwidth;
var scaleY1 = (srcheight * targetwidth) / srcwidth;
// scale to the target height
var scaleX2 = (srcwidth * targetheight) / srcheight;
var scaleY2 = targetheight;
// now figure out which one we should use
var fScaleOnWidth = (scaleX2 > targetwidth);
if (fScaleOnWidth) {
fScaleOnWidth = fLetterBox;
}
else {
fScaleOnWidth = !fLetterBox;
}
if (fScaleOnWidth) {
result.width = Math.floor(scaleX1);
result.height = Math.floor(scaleY1);
result.fScaleToTargetWidth = true;
}
else {
result.width = Math.floor(scaleX2);
result.height = Math.floor(scaleY2);
result.fScaleToTargetWidth = false;
}
result.targetleft = Math.floor((targetwidth - result.width) / 2);
result.targettop = Math.floor((targetheight - result.height) / 2);
return result;
}
</script>
</html>