-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathst7565.html
421 lines (347 loc) · 12.8 KB
/
st7565.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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<!DOCTYPE html>
<!--
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<html>
<head>
<title>ST7565 Designer</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="./css/style.css" />
</head>
<body>
<div id="savedItemsContainer">
<div class="button close"></div>
<div id="savedItemsExport">
<span class="button" id="exportSavedItems">Export</span> |
<span class="button" id="importSavedItems">Import</span>
</div>
<div id="savedItems"></div>
</div>
<div id="wrapper">
<div id="canvasField">
<canvas id="canvas" width=50 height=70></canvas>
</div>
<div>
<div id="tools">
<div class="tool">
<span class="button" id="gridToggle">Hide Grid</span> | <span class="button" id="openEncoding">Show Encoding</span>
|
<span class="button" id="saveItem">Save Item</span> | <span class="button" id="openSavedItems">Show Saved Items</span>
</div>
<div class="tool">
Size:<input type="text" name="cols" size="2" maxlength="3" value="5"/>x
<input type="text" name="rows" size="2" maxlength="2" value="7"/>
</div>
<div id="colorBar">Color:<div class="swatch" id="white"></div><div class="swatch selected" id="black"></div></div>
<div class="tool">
<span>Brush Size:</span>
<span><select id="brushSize">
<option value="1" selected>1 px</option>
<option value="2">2 px</option>
<option value="4">4 px</option>
</select>
</span>
</div>
<div class="tool">Page Bit order: <input type="radio" name="bit_order" class="" value="1" checked/>MSB at Top
<input type="radio" name="bit_order" class="" value="0"/>MSB at Bottom
</div>
<div class="tool">
<span>Cell Size:</span>
<span>
<select id="pixelSize">
<option value="5">5 px</option>
<option value="10" selected>10 px</option>
<option value="20">20 px</option>
<option value="30">30 px</option>
<option value="50">50 px</option>
</select>
</span>
</div>
<div id="ditherTools">
<div class="tool">Filter: <select id="filter">
<option value="fs" selected>Floyd Steinberg</option>
<option value="atkinson">Atkinson</option>
<option value="sierra">Sierra Lite</option>
<option value="threshold">Threshold</option>
</select>
</div>
<div class="tool">Scan Pattern: <input type="radio" name="scan_pattern" class="" value="1" checked/>Raster
<input type="radio" name="scan_pattern" class="" value="0"/>Serpentine
</div>
<div class="tool">Scan Direction: <input type="radio" name="scan_direction" class="" value="1" checked/>Horizontal
<input type="radio" name="scan_direction" class="" value="0"/>Vertical
</div>
</div>
<div id="clearBtn">Clear</div>
</div>
</div>
<div class="modal" id="encodingContainer"><div class="button close"></div><div id="hexOutput"></div></div>
<input type="file" id="fileImport"/>
<div class="modal" id="jsonContainer"><div class="button close"></div><div id="jsonOutput"></div></div>
</div>
<script src="./scripts/jquery-1.7.2.min.js" charset="utf-8"></script>
<script src="./scripts/Display.js"></script>
<script src="./scripts/Dither.js"></script>
<script>
(function($){
glcd = new Display('canvas');
dither_img = new Dither(glcd);
dither_img.useFloydSteinbergFilters();
glcd.zeroBuffer();
glcd.drawGrid();
if (localStorage['items']){
glcd.loadSavedItems();
}
// initialize the UI form elements, in case cached values are being used (I'm talking to you, firefox)
$('input[type="text"][name="rows"]').val('7');
$('input[type="text"][name="cols"]').val('5');
$('#pixelSize option[value="10"]').prop('selected','selected');
$('#brushSize option[value="1"]').prop('selected','selected');
// $('input:radio[name=filter][value="fs"]').prop('checked', true);
$('#filter option[value="fs"]').prop('selected','selected');
$('input:radio[name=scan_pattern][value=1]').prop('checked', true);
$('input:radio[name=scan_direction][value=1]').prop('checked', true);
$('input:radio[name=bit_order][value=1]').prop('checked', true);
// show the encoding for the display buffer (initialized to 0 for all pixels)
$('#hexOutput').html('<h3>Encoding:</h3> ' + glcd.encode());
// attach events to the canvas -- mousedown, mousemove, and mouseup
$('#canvas').mousedown(function(evt){
// have to pass the stateBuffer array to previousStates by value, not by reference
glcd.previousStates.push(glcd.stateBuffer.slice());
glcd.editable = true;
glcd.drawPixel(evt);
$('#hexOutput').html('<h3>Encoding:</h3> ' + glcd.encode());
});
$('#canvas').mousemove(function(evt){
if (glcd.editable){
glcd.drawPixel(evt);
$('#hexOutput').html('<h3>Encoding:</h3> ' + glcd.encode());
}
});
$('#canvas').mouseup(function(){
glcd.editable = false;
});
// attach a change event handler to the pixel size select box
$('#pixelSize').change(function(){
glcd.pixelSize = +$('#pixelSize option:selected').val();
glcd.ctx.canvas.height = glcd.rows * glcd.pixelSize;
glcd.ctx.canvas.width = glcd.cols * glcd.pixelSize;
glcd.h = glcd.ctx.canvas.height;
glcd.w = glcd.ctx.canvas.width;
// user may have started a drawing, so redraw the buffer
glcd.drawBuffer();
});
// attach a change event handler to the brush size select box
$('#brushSize').change(function(){
glcd.brushSize = +$('#brushSize option:selected').val();
});
// attach an event handler to the clear button.
// Because it's dynamically created, use "on" to
// handle the bubble up to the hexOutput div
$('#clearBtn').click(function(){
glcd.clear();
$('#hexOutput').html('');
$('#hexOutput').html('<h3>Encoding:</h3> ' + glcd.encode());
});
// attach change event handler to the rows input. Clear and resize, reflect 0 encoding accordingly
$('input[type="text"][name="rows"]').change(function(){
var value = +$(this).val();
if (value > 64){
value = 64;
$(this).val('64');
}
glcd.rows = value;
glcd.numPages = Math.ceil(glcd.rows/8);
glcd.ctx.canvas.height = value * glcd.pixelSize;
glcd.h = glcd.ctx.canvas.height;
glcd.clear();
$('#hexOutput').html('<h3>Encoding:</h3> ' + glcd.encode());
});
// change event handler for cols input. Similar to the rows change event handler
$('input[type="text"][name="cols"]').change(function(){
var value = +$(this).val();
if (value > 128){
value = 128;
$(this).val('128');
}
glcd.cols = value;
glcd.ctx.canvas.width = value * glcd.pixelSize;
glcd.w = glcd.ctx.canvas.width;
glcd.clear();
$('#hexOutput').html('<h3>Encoding:</h3> ' + glcd.encode());
});
// Change event for the bit order radio button. Re-encode accordingly if it changes.
$('input:radio[name="bit_order"]').change(function(){
glcd.encodeAsBitmap = (+$(this).val()) ? false : true;
$('#hexOutput').html('<h3>Encoding:</h3> ' + glcd.encode());
});
// check for shift key and ctrl-z or command-z
$(document).keydown(function(evt){
var ctrlDown = false;
if (evt.shiftKey){
glcd.shiftIsDown = true;
} else if ((evt.ctrlKey || evt.metaKey) && (evt.which === 90)){ //ctrl or command z
if (glcd.previousStates.length){
glcd.stateBuffer = glcd.previousStates.pop();
glcd.drawBuffer();
// if we popped the last item out of previousStates, don't draw the clear button (canvas is blank)
$('#hexOutput').html('<h3>Encoding:</h3> ' + glcd.encode());
}
return false;
}
});
// shift key was lifted
$(document).keyup(function(evt){
if (evt.keyCode === 16){
glcd.shiftIsDown = false;
}
});
// fill the color swatch divs with the appropriate color
$('.swatch').each(function(){
$(this).css('background-color', $(this).attr('id'));
});
// attach click event handler to each color swatch
$('.swatch').click(function(){
$('.swatch').removeClass('selected');
$(this).addClass('selected');
glcd.color = $(this).attr('id');
});
$('#gridToggle').click(function(){
glcd.showGrid ^= 1;
if (glcd.showGrid){
$(this).html('Hide Grid');
} else {
$(this).html('Show Grid');
}
glcd.drawBuffer();
});
// show the encoding for the current buffer
// size the window width based on the number of cols
$('#openEncoding').click(function(){
var el = $('#encodingContainer');
var new_width = ((glcd.cols-5)/128)*600 + 200;
el.css({'width': new_width});
var offset = ($(document).width()/2) - (el.width()/2);
el.css({'left':offset + 'px'});
el.show();
});
// open saved items list
$('#openSavedItems').click(function(){
$('#savedItemsContainer').show();
});
// attach event handler for close elements
$(document).on('click', '.close', function(){
$(this).parent().hide();
});
// delete item from the saved items list
$('#savedItems').on('click', '.deleteBtn', function(){
var key = $(this).parent().parent().attr('id');
$('#savedItems').html('');
glcd.deleteItem(key);
});
// save the current buffer to local storage and refresh the saved items list
$('#saveItem').click(function(){
glcd.saveItem();
$('#savedItems').html('');
glcd.loadSavedItems();
});
// open a saved item for editing
$('#savedItems').on('click', '.editBtn', function(){
var key = $(this).parent().parent().attr('id');
glcd.restoreItem(key);
$('input[type="text"][name="rows"]').val(glcd.rows);
$('input[type="text"][name="cols"]').val(glcd.cols);
$('#hexOutput').html('<h3>Encoding:</h3> ' + glcd.encode());
});
$('#fileImport').change(function(evt){
var evt = evt.originalEvent;
var fileList = evt.target.files;
$('#savedItems').html('');
glcd.loadFromFile(fileList);
});
$('#importSavedItems').click(function(){
if (typeof(FileReader) !== "undefined"){
$('#fileImport').trigger('click');
$('#fileImport').attr('value','');
} else {
var msg = ($.browser.safari) ? "Your version of safari does not support reading from files directly. Recommend you use google chrome or a webkit nightly build from http://webkit.nightly.org in order to gain this capabilty." : "Your browser does not support reading directly from files.";
alert(msg);
}
});
$('#exportSavedItems').click(function(){
$('#jsonOutput').html(glcd.exportSavedItems());
var el = $('#jsonContainer');
el.css({'width': '600px'});
var offset = ($(document).width()/2) - (el.width()/2);
el.css({'left':offset + 'px'});
el.show();
});
$('input:radio[name="scan_direction"]').change(function(){
dither_img.img.vertical_scan = (+$(this).val()) ? false : true;
$(dither_img.img).load();
});
$('input:radio[name="scan_pattern"]').change(function(){
dither_img.img.raster = (+$(this).val()) ? true : false;
$(dither_img.img).load();
});
$('#filter').change(function(){
// $('input:radio[name="filter"]').change(function(){
var technique = $(this).val();
if (technique === "fs"){
dither_img.useFloydSteinbergFilters();
} else if (technique === "atkinson"){
dither_img.useAtkinsonFilters();
} else if (technique === "sierra"){
dither_img.useSierraFilters();
} else {
dither_img.useThresholdFilters();
}
$(dither_img.img).load();
});
$('#canvas').bind('dragover', function(evt){
// prevent the browser from loading the image when it's dropped
// use the "copy" cursor effect on dragover
evt = evt.originalEvent;
evt.stopPropagation();
evt.preventDefault();
evt.dataTransfer.dropEffect = 'copy';
});
$('#canvas').bind('drop', function(evt){
// reference the original event, prevent default action, and bubbling
evt = evt.originalEvent;
evt.stopPropagation();
evt.preventDefault();
// get an array of files dropped on the canvas. Even if multiple files are dropped, only use the first
var files = evt.dataTransfer.files; // FileList object.
// create a file reader object in order to read the file
// use the readAsDataURL method to get the data URL
// set the underlay src to the dataURL using the onload method
// of the file reader object
var reader = new FileReader();
$(reader).load(function(e){
var load;
if ((glcd.rows === 64) && (glcd.cols === 128)){
glcd.clear();
$.when(dither_img.loadImage(e.target.result)).done(function(){
$('#hexOutput').html('<h3>Encoding:</h3> ' + glcd.encode());
$('#ditherTools').show();
});
} else {
alert("Drag and Drop for images only allowed if size set to 128x64");
}
});
reader.readAsDataURL(files[0]);
});
})(jQuery)
</script>
</body>
</html>