-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.html
274 lines (228 loc) · 7.97 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Image Converter</title>
<style type="text/css">
h1, h3 {
text-align: center;
}
h4 {
text-align: right;
}
body {
font-family: Arial;
background-color: #6E9E6D;
text-shadow: 2px 2px black;
color: white;
}
a {
color: white;
text-decoration: none;
}
a:hover {
color: yellow;
background-color: black;
}
.shadowy {
}
.image-view {
display: inline-block;
font-size: 1pt;
border: 2px solid lightgrey;
margin: 1px;
}
.image-view:hover {
border: 2px solid red;
}
canvas,
img {
image-rendering: crisp-edges;
image-rendering: -moz-crisp-edges;
image-rendering: -webkit-optimize-contrast;
image-rendering: optimize-contrast;
image-rendering: pixelated;
-ms-interpolation-mode: nearest-neighbor;
}
.code {
overflow: hidden;
display: block;
position: fixed;
top: 0;
left: 0;
width: 1px;
height: 1px;
}
#image-container {
padding: 1px;
}
#ino-download-pane {
background-color: rgba(0, 0, 0, 0.5);
margin: 6px;
padding: 6px;
float: left;
}
</style>
</head>
<body>
<h1>Arduboy Game Image Converter</h1>
<h3>Using Chrome, paste an image into this page. Click the image to copy the resulting code and enjoy!</h3>
<hr />
<div id="image-container">
</div>
<hr />
<div id="ino-download-pane" style="display:none">
Image code copied to the clipboard!<br /><br />
Test it out on your arduboy now:<br />
<a id="ino-download-link" href="about:blank" download="imgtest.ino"></a>
</div>
<h4>
<a href="https://github.com/fuopy/arduboy-image-converter" target="_blank">View on GitHub</a></p>
</h4>
<script type="text/javascript">
function getImageData(event) {
// Get items from the clipboard
var clipboardData = event.clipboardData || event.originalEvent.clipboardData;
var clipboardItems = clipboardData.items;
// Return the image item, if it exists
for(var item in clipboardItems) {
if(clipboardItems[item].type.indexOf("image") === 0) {
return clipboardItems[item].getAsFile();
}
}
}
function addPastedElement(imageData) {
var reader = new FileReader();
reader.onload = function(event) {
event.preventDefault;
// Create image element
var pastedImage = new Image();
pastedImage.src = event.target.result;
// Create canvas for image
var pastedImageCanvas = document.createElement("canvas");
pastedImageCanvas.width = pastedImage.naturalWidth;
pastedImageCanvas.height = pastedImage.naturalHeight;
// Create code container
var pastedImageCode = document.createElement("div");
pastedImageCode.className = "code";
// Create div container
var pastedImageDiv = document.createElement("div");
pastedImageDiv.className = "image-view";
pastedImageDiv.appendChild(pastedImageCanvas);
var imageContainer = document.getElementById("image-container");
imageContainer.appendChild(pastedImageDiv);
imageContainer.appendChild(pastedImageCode);
// Create context for drawing
var pastedImageContext = pastedImageCanvas.getContext("2d");
// Draw the image
pastedImageContext.drawImage(pastedImage, 0, 0);
// Generate the sprite string
var spriteString = "{<br />	";
var inoString = "#include <Arduboy.h>\n\nArduboy arduboy;\n\n" +
"#define WIDTH " + pastedImage.naturalWidth + "\n" +
"#define HEIGHT " + pastedImage.naturalHeight + "\n\n" +
"const unsigned char image[] PROGMEM = {\n\t";
var pageCount = Math.ceil(pastedImage.naturalHeight / 8);
var columnCount = pastedImage.naturalWidth;
var currentByte = 0;
// Read the sprite page-by-page
for(var page = 0; page < pageCount; page++) {
// Read the page column-by-column
for(var column = 0; column < columnCount; column++) {
// Read the column into a byte
var spriteByte = 0;
for(var yPixel = 0; yPixel < 8; yPixel++) {
// If the color of the pixel is not black, count it as white
var pixelColor = pastedImageContext.getImageData(column, page*8 + yPixel, 1, 1).data;
if(pixelColor[0] > 0 || pixelColor[1] > 0 || pixelColor[2] > 0) {
spriteByte |= (1 << yPixel);
}
}
// Print the column in hex notation, add a comma for formatting
var digitStr = spriteByte.toString(16);
if(digitStr.length == 1) {
digitStr = "0" + digitStr;
}
spriteString += "0x" + digitStr + ", ";
inoString += "0x" + digitStr + ", ";
if(currentByte%8 == 7) {
spriteString += "<br />	";
inoString += "\n\t";
}
currentByte++;
}
}
// Terminate the array
spriteString += "<br />};";
inoString += '\n};\n\n' +
'void setup()\n' +
'{\n' +
'\tarduboy.beginNoLogo();\n' +
'\t\n' +
'\tarduboy.clear();\n' +
'\tarduboy.drawBitmap(0, 0, image, WIDTH, HEIGHT, 1);\n' +
'\tarduboy.display();\n' +
'}\n\n' +
'void loop()\n' +
'{\n' +
'\n' +
'}\n\n';
// Create an invisible element containing the string
pastedImageCode.innerHTML = spriteString;
// Resize canvas to show 3x scaled image
pastedImageCanvas.width = pastedImage.naturalWidth * 3;
pastedImageCanvas.height = pastedImage.naturalHeight * 3;
pastedImageContext = pastedImageCanvas.getContext("2d");
pastedImageContext.imageSmoothingEnabled = false;
pastedImageContext.drawImage(pastedImage, 0, 0, pastedImage.naturalWidth * 3, pastedImage.naturalHeight * 3);
pastedImageCanvas.onclick = function() {
// Copy the string to the clipboard
var range = document.createRange();
range.selectNode(pastedImageCode);
var userSelection = window.getSelection();
userSelection.removeAllRanges();
userSelection.addRange(range);
document.execCommand('copy')
userSelection.removeAllRanges();
// Create test .ino download
createSketchDownload(inoString);
}
};
reader.readAsDataURL(imageData);
}
var generatedInoFile = null;
function createInoFile(text) {
var file = generatedInoFile;
// Create a new blob from the text
var data = new Blob([text], {type: 'text/plain'});
// Remove the old file, if it exists
if(file !== null) {
window.URL.revokeObjectURL(file);
}
// Return a URL for the file
file = window.URL.createObjectURL(data);
return file;
}
function createSketchDownload(text) {
// Set the filename for the download link
var downloadLink = document.getElementById("ino-download-link");
var filename = "imgtest-" + new Date().toISOString().replace(/\s/g, '') + ".ino";
downloadLink.download = filename;
downloadLink.innerHTML = filename;
// Create the file to download
downloadLink.href = createInoFile(text);
// Show the download file pane
var downloadPane = document.getElementById("ino-download-pane");
downloadPane.style.display = "inline-block";
}
document.onpaste = function (event) {
// Get the image data
var imageData = getImageData(event);
// If there is some data, add it to the page
if(imageData !== null) {
addPastedElement(imageData);
}
}
</script>
</body>
</html>