-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy patheditor.html
More file actions
301 lines (255 loc) · 7.17 KB
/
editor.html
File metadata and controls
301 lines (255 loc) · 7.17 KB
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Image Draw v2</title>
<style type="text/css">
div {
margin: auto;
text-align: center;
width: 100%
}
div#bin {
width: 1000px;
margin-top: 10px;
}
input#wid, input#hei, input#widGroup {
width: 4em;
}
#cvsled {
margin: auto;
}
</style>
<script type="text/javascript">
// Configuration options
var sizeOfLED = 24; // Size to draw LED in pixels, including gutter
var sizeOfGutter = 1; // Size of gutter between LEDs
var colorOff = "#000";
var colorOn = "#F00";
var colorGutter = "#666";
// Variables
var canvasNode; // Main canvas node
var cx; // 2d canvas context
var map = new Array();
var width;
var height;
var widthGroup;
var renderCircle = false;
var pixelsInAByte = 4;
var reversePixelOrder = true;
var iconEncode = true;
function initialize(){
updateCanvasSize();
updateUseCircle();
$('changecanvas').addEventListener("click", updateCanvasSize, false);
$('loadimage').addEventListener("click", loadImage, false);
}
function canvasClick(oEvent) {
var pos_x = oEvent.offsetX?(oEvent.offsetX):oEvent.pageX-canvasNode.offsetLeft;
var pos_y = oEvent.offsetY?(oEvent.offsetY):oEvent.pageY-canvasNode.offsetTop;
var cellX = Math.floor(pos_x/sizeOfLED);
var cellY = Math.floor(pos_y/sizeOfLED);
map[cellX][cellY] = !map[cellX][cellY];
canvasRender();
}
function textUpdate() {
if (iconEncode)
{
val = "";
val += String.fromCharCode('A'.charCodeAt(0)+width*1);
val += String.fromCharCode('A'.charCodeAt(0)+height*1);
for(var i=0; i<width; ++i) {
for(var j=0; j<height; j+=pixelsInAByte) {
var b=0;
if (reversePixelOrder) {
for(var k=pixelsInAByte - 1; k>=0; --k)
{
b *= 2;
if (map[i][j+k])
b += 1;
}
} else {
for(var k=0; k < pixelsInAByte; ++k)
{
b *= 2;
if (map[i][j+k])
b += 1;
}
}
val += String.fromCharCode('A'.charCodeAt(0)+b);
}
}
}
else
{
val = "{";
for(var i=0; i<width; ++i) {
for(var j=0; j<height; j+=pixelsInAByte) {
val += "0b";
if (reversePixelOrder) {
for(var k=pixelsInAByte - 1; k>=0; --k)
val += (map[i][j+k]?"1":"0");
} else {
for(var k=0; k < pixelsInAByte; ++k)
val += (map[i][j+k]?"1":"0");
}
val += ", ";
}
if ((i+1) % (widthGroup) == 0)
{
val = val.substr(0, val.length - 2); // remove trailing ", "
val += "},\n{";
}
}
val = val.substr(0, val.length - 3);
}
$('binary_out').value = val;
}
function loadImage() {
// Load the image from the textbox contents
var src = $('binary_out').value.replace(/[\{\}\s]/g,"").split(",");
var eleCountHeight = Math.ceil(height/pixelsInAByte);
var i = 0; // Current array element.
for(var x = 0; x < width; ++x)
for(var y = 0; y < height; y += pixelsInAByte) {
var currData = trim(src[i++]).replace("0b", "").split("");
if (reversePixelOrder)
currData.reverse(); // After reversing, [0] is the element with the lowest y-value.
for(var k = 0; (k < pixelsInAByte) && (y + k < height); ++k)
map[x][y+k] = (currData[k] == "0"?false:true);
}
canvasRender();
}
// The following function is from http://blog.stevenlevithan.com/archives/faster-trim-javascript
function trim(str) {
return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
function updateCanvasSize() {
width = $('wid').value;
widthGroup = $('widGroup').value;
height = $('hei').value;
if(isNaN(width) || isNaN(height)) {
alert("Incorrect width/height");
return false;
}
map = new Array();
for(var i=0; i<width; ++i) {
map[i] = new Array();
for(var j=0; j<height; ++j)
map[i][j]=0;
}
if(canvasNode)
$('main').removeChild(canvasNode);
canvasNode = document.createElement("canvas");
canvasNode.width = sizeOfLED*width;
canvasNode.height = sizeOfLED*height;
cx = canvasNode.getContext("2d");
canvasNode.id = "cvsled";
$('main').appendChild(canvasNode);
$('cvsled').addEventListener("click", canvasClick, false);
canvasRender();
return true;
}
function canvasRender() {
cx.fillStyle = colorOff;
cx.fillRect(0, 0, canvasNode.width, canvasNode.height);
// Draw pixels
cx.fillStyle = colorOn;
for(var i=0; i<width; ++i)
for(var j=0; j<height; ++j)
if(map[i][j])
if(renderCircle) {
cx.beginPath();
cx.arc((i+0.5)*sizeOfLED, (j+0.5)*sizeOfLED, 0.45*sizeOfLED, 0, Math.PI * 2, false);
cx.closePath();
cx.fill();
} else
cx.fillRect(i*sizeOfLED, j*sizeOfLED, sizeOfLED-1, sizeOfLED-1);
cx.strokeStyle = colorGutter;
cx.lineWidth = sizeOfGutter;
cx.beginPath();
// Draw vert grid
for(var i=0; i<=canvasNode.width; i+=sizeOfLED) {
cx.moveTo(i,0);
cx.lineTo(i,canvasNode.height);
}
// Draw horiz grid
for(var i=0; i<=canvasNode.height; i+=sizeOfLED) {
cx.moveTo(0, i);
cx.lineTo(canvasNode.width, i);
}
cx.stroke();
cx.strokeStyle = colorGutter;
cx.lineWidth = sizeOfGutter*4;
cx.beginPath();
// Draw vert groups
for(var i=0; i<=canvasNode.width; i+=sizeOfLED) {
if (((i/sizeOfLED) % widthGroup) == 0)
{
cx.moveTo(i,0);
cx.lineTo(i,canvasNode.height);
}
}
cx.stroke();
textUpdate();
}
function $(e){
return document.getElementById(e);
}
function updateUseCircle() {
if($("chkUseCircle").checked)
renderCircle = true;
else
renderCircle = false;
canvasRender();
}
function updatePxPerByte() {
var v = $("pxInByte").value * 1;
if(v > 0 && v <= 8){
pixelsInAByte = v;
canvasRender();
}
}
function updateReversePixel() {
reversePixelOrder = $("chkRevPx").checked;
canvasRender();
}
function updateIconEncode() {
iconEncode = $("chkIconEncode").checked;
canvasRender();
}
window.addEventListener("load", initialize, false);
function settings(ver) {
if (ver == 1) {
pixelsInAByte = 4;
reversePixelOrder = true;
} else {
pixelsInAByte = 8;
reversePixelOrder = false;
}
$("chkIconEncode").checked = iconEncode;
$("chkRevPx").checked = reversePixelOrder;
$("pxInByte").value = pixelsInAByte;
canvasRender();
}
</script>
</head>
<body>
<div id="main"></div>
<div id="bin">
Width: <input type="number" id="wid" min="1" value="32" onclick="updateCanvasSize()" />
Group Width: <input type="number" id="widGroup" min="1" value="8" onclick="updateCanvasSize()" />
Height: <input type="number" id="hei" min="1" value="8" onclick="updateCanvasSize()" />
<input type="button" value="Clear" id="changecanvas" />
<input type="button" value="Load" id="loadimage" />
<label><input type="checkbox" id="chkUseCircle" onclick="updateUseCircle()" />Use Circle</label><br />
Pixels per Byte: <input type="number" id="pxInByte" min="1" max="8" value="4" onchange="updatePxPerByte()" />
<label><input type="checkbox" checked="checked" id="chkRevPx" onclick="updateReversePixel()" />Reverse Pixel Order</label>
<button onclick="settings(1)">Version 1.0</button>
<button onclick="settings(2)">Version 2.0</button><br />
<label><input type="checkbox" xchecked="checked" id="chkIconEncode" onclick="updateIconEncode()" />Icon Encode</label>
<br />
<textarea id="binary_out" style="width: 100%; height: 200px;" onkeyup="loadImage()"></textarea>
</div>
</body>
</html>