-
Notifications
You must be signed in to change notification settings - Fork 63
/
main.htm
396 lines (350 loc) · 12.1 KB
/
main.htm
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
<!doctype html><meta charset=utf-8>
<title>Plotterfun</title>
<style>
*{font-family:sans-serif}
h2{padding:10px;margin:0}
input~input[type=text] {width:40px; border:1px solid #999;vertical-align:3px}
input[type=checkbox]{vertical-align:-5%;margin:0px 5px}
input[type=number]{width:50px}
label:hover{color:#4a6071}
#sidebar {width:300px;background:#dfedf7}
video{max-width:100%;transform:scaleX(-1)}
canvas{max-width:100%; max-height:320px}
svg{position:fixed; left:320px; top:10px}
form{padding:5px; line-height:2em}
#msgbox{position:fixed; left:320px; bottom:20px; padding:6px}
#msgbox:not(:empty) {background:rgba(255,255,255,0.8)}
#webcam{display:none}
button{cursor:pointer; border:0;padding:5px 10px;margin:0 20px}
#tabbar {background:#c3d7e5;}
#tabbar button{margin:0}
button{background:#c3d7e5;outline:none;font-weight:bold;color:#2f404d}
button:hover{background:#5d90bc !important;color:#fff}
#tabbar button.active{background:#99afbf}
#imgselect,#webcam {text-align:justify;text-align-last:justify}
#algoSelect{background:#d2e4f0;margin:5px 0;border:solid #5d90bc;border-width:2px 0 2px 0}
a:hover{color:red}
canvas{ /* checkerboard pattern */
background-color: #f8f8f8;
background-image:
linear-gradient(45deg, #e0e0e0 25%, transparent 25%, transparent 75%, #e0e0e0 75%, #e0e0e0),
linear-gradient(45deg, #e0e0e0 25%, transparent 25%, transparent 75%, #e0e0e0 75%, #e0e0e0);
background-size: 40px 40px;
background-position: 0 0, 20px 20px;
}
</style>
<link rel=stylesheet href=range.css>
<div id=sidebar>
<h2>Plotterfun</h2>
<div id=tabbar>
<button id=tab1 onclick='tabImage()'>Image</button><button id=tab2 onclick='tabWebcam()'>Webcam</button>
</div>
<div id=imgselect>
<form>
Canvas
W:<input id=pcw type=number min=10 value=800 oninput='changeSize()'>
H:<input id=pch type=number min=10 value=600 oninput='changeSize()'>
</form>
<canvas></canvas><br>
<button onclick='selectImage()'>Select image</button>
<button onclick='useImage()'>Use image</button>
</div>
<div id=webcam>
<video autoplay></video>
<button onclick='video.paused?video.play():video.pause()'>Pause</button>
<button onclick='snapshot()'>Use image</button>
</div>
<form id=algoSelect>
Algorithm:
<select id=algorithm onchange='loadWorker(this.value)'>
<option value=squiggle.js>Squiggle</option>
<option value=squiggleLeftRight.js>Squiggle Left/Right</option>
<option value=spiral.js>Spiral</option>
<option value=polyspiral.js>Polygon Spiral</option>
<option value=sawtooth.js>Sawtooth</option>
<option value=stipple.js>Stipples</option>
<option value=delaunay.js>Delaunay</option>
<option value=linedraw.js>Linedraw</option>
<option value=mosaic.js>Mosaic</option>
<option value=subline.js>Subline</option>
<option value=springs.js>Springs</option>
<option value=waves.js>Waves</option>
<option value=needles.js>Needles</option>
<option value=implode.js>Implode</option>
<option value=halftone.js title="Algorithm by HomineLudens">Halftone</option>
<option value=boxes.js title="Algorithm by MarkJB">Boxes</option>
<option value=dots.js title="Algorithm by Tim Koop">Dots</option>
<option value=jaggy.js title="Algorithm by Tim Koop">Jaggy</option>
<option value=longwave.js>Longwave</option>
<option value=linescan.js title="Algorithm by J-Waal">Linescan</option>
<option value=woven.js title="Algorithm by J-Waal">Woven</option>
<option value=peano.js title="Algorithm by J-Waal">Peano</option>
</select>
<img id=buffering src=loading.gif style='vertical-align:middle; display:inline-block; visibility:hidden;'>
</form>
<form id=algoParams></form>
<button style='font-size:large; margin:10px' onclick=download()>Download SVG</button>
<div style='padding:10px 15px'>
plotterfun by mitxela<br>
<a href=https://mitxela.com/projects/plotting>more info</a> •
<a href=https://github.com/mitxela/plotterfun>source code</a>
</div>
</div>
<svg></svg>
<div id=msgbox></div>
<script src=helpers.js></script>
<script>
preview=document.querySelector("canvas")
prectx=preview.getContext("2d")
svg=document.querySelector('svg');
video=document.querySelector('video');
canvas=document.createElement("canvas")
ctx=canvas.getContext("2d")
config = {};
img = null;
scale = 1;
offset = [0,0]
// TODO
// Allow editing path display style
// "Animate" button
// plot time estimate
function checkScroll(){
svg.style.position= (window.innerWidth < canvas.width+320 || window.innerHeight < canvas.height+10) ? "absolute" : "fixed";
}
window.onresize=checkScroll;
function changeSize(){
cw = parseInt(pcw.value)
ch = parseInt(pch.value)
preview.width=cw
preview.height=ch
draw()
checkScroll()
}
changeSize()
function tabImage(){
if (video.srcObject && video.srcObject.getTracks().length) video.srcObject.getTracks()[0].stop()
webcam.style.display='none'
imgselect.style.display='block'
tab1.className='active'
tab2.className=''
}
tabImage()
function selectImage(){
let d= document.createElement('input')
d.type='file'
d.onchange=function(e){
img = new Image();
img.onload=function(){
let w=img.width, h=img.height;
cw=parseInt(pcw.defaultValue) // reset size - worth it?
if (w>cw || h>ch) { ch=Math.round(cw*h/w) }
else if (w>10&&h>10){ch=h;cw=w}
preview.height = pch.value = ch
preview.width = pcw.value = cw
scale = Math.min(ch/h, cw/w)
offset = [ cw/2, ch/2 ]
draw()
}
img.src = URL.createObjectURL(e.target.files[0]);
}
d.click()
}
function draw(){
if (!img) return
prectx.clearRect(0,0,cw,ch)
prectx.drawImage(img, offset[0]-scale*img.width*0.5,offset[1]-scale*img.height*0.5, scale*img.width, scale*img.height)
}
preview.onmousedown=function(e){
let dx=e.clientX, dy=e.clientY
let csc = cw / preview.getBoundingClientRect().width
document.onmousemove=function(e){
let x=e.clientX-dx, y=e.clientY-dy
offset[0] += x*csc
offset[1] += y*csc
dx=e.clientX
dy=e.clientY
draw()
return false
}
document.onmouseup=function(){
document.onmousemove=null
document.onmouseup=null
}
return false
}
preview.addEventListener("wheel", function(e){
e.preventDefault();
e.stopPropagation();
let os = scale
scale *= e.deltaY>0 ?1.1:0.9;
offset[0] = ((offset[0] -cw/2)/os)*scale +cw/2
offset[1] = ((offset[1] -ch/2)/os)*scale +ch/2
draw()
},{passive:false});
function tabWebcam(){
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
alert("could not open webcam");
} else
navigator.mediaDevices.getUserMedia({video: {width:800}}).then(function(v) {
video.srcObject = v;
webcam.style.display='block'
imgselect.style.display='none'
tab2.className='active'
tab1.className=''
}).catch((e)=>alert("error opening webcam"));
}
var imageset = false;
function snapshot(){
imageset=true;
// config width height should come from sliders maybe?
[canvas.width, canvas.height] = [video.videoWidth, video.videoHeight];
ctx.drawImage(video, 0, 0)
checkScroll()
process()
}
function useImage(){
if (!img) return
imageset=true;
[canvas.width, canvas.height] = [cw, ch];
ctx.fillStyle='#fff'
ctx.fillRect(0,0,cw,ch)
ctx.drawImage(preview, 0,0)
checkScroll()
process()
}
function sendToWorker(msg){
if (!window.onmessage) myWorker.postMessage(msg)
else setTimeout(()=>window.onmessage({data:msg}),10)
}
function process(){
if (!imageset) return;
[config.width, config.height] = [canvas.width, canvas.height]
resetSVG()
loadWorker(window.algo)
sendToWorker([ window.config, ctx.getImageData(0,0,config.width,config.height) ]);
}
function ctrlChange(control){
if (!imageset) return;
if (!control.noRestart)
process()
else
sendToWorker([ window.config ]);
}
function addController(control, form){
if (!control.type) control.type = "range"
let s = document.createElement("input")
Object.assign(s, control)
let l = document.createElement('label')
let p = document.createElement('div')
// Don't reset values when reloading controls with same name
if (control.type !='checkbox'){
if (config[control.label]) control.value = config[control.label];
else config[control.label] = control.value;
}
if (control.type=='select') {
s = document.createElement('select')
s.oninput=e=>{config[control.label] = s.value; ctrlChange(control)}
for (let i in control.options) {
let selected = control.options[i]==config[control.label]
s.append(new Option( control.options[i], control.options[i], selected, selected ))
}
l.append(control.label+': ')
l.append(s)
p.append(l)
form.append(p)
return
} else if (control.type=='checkbox') {
if (config[control.label]!==undefined) s.checked = config[control.label]
s.oninput=e=>{config[control.label] = s.checked; ctrlChange(control)}
config[control.label] = s.checked
l.append(s)
} else {
let n = document.createElement('input')
n.type="text" // type=number does not allow fractions
n.pattern="\-?[0-9]+\.?[0-9]*"
s.oninput=e=>{config[control.label] = Number( n.value = s.value ); ctrlChange(control)}
n.oninput=e=>{config[control.label] = Number( s.value = n.value ); ctrlChange(control)}
n.onkeydown=function(e){
if (e.keyCode!=38 && e.keyCode!=40) return;
n.value = parseFloat(n.value) + (e.keyCode==38?1:-1) * (control.step || 1)
// avoid floating point errors like 1.0000001 when step is fractional
if (control.step && control.step != Math.round(control.step))
n.value = parseFloat( (n.value*1).toFixed( control.step.toString().split('.')[1].length ) )
n.oninput()
};
s.value = n.value = config[control.label] //.assign already set value, but value has to be set after min/max are set
p.append(s);
p.append(n)
}
l.append(control.label)
l.append(p)
form.append(l)
}
function appendScript(src) {
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', src);
document.head.append(script);
}
function loadWorker(src){
buffering.style.visibility='visible'
if (window.myWorker) myWorker.terminate()
if (window.location.protocol == 'file:') {
// Workers not working - fall back to embedding the script
window.myWorker={terminate:()=>{}}
window.importScripts=function(...args){
for (let a of args) appendScript(a)
}
window.postMessage=function(msg){
myWorker.onmessage({data:msg})
}
appendScript(src)
} else window.myWorker = new Worker(src);
msgbox.innerHTML = "";
myWorker.onmessage = function(msg) {
let [type, data] = msg.data
// setup, declare parameters
if (type == 'sliders') {
buffering.style.visibility='hidden'
if (src==window.algo) return;
window.algo=src
algoParams.innerHTML="";
data.forEach(c=>addController( c, algoParams ))
process()
// message, e.g. progress bar
} else if (type == 'msg') {
msgbox.innerHTML = data;
} else if (type == 'dbg'){
window.data = data
console.log(data)
// vector data result
}else if (type == 'svg-path'){
mainpath.setAttributeNS(null, "d", data)
}
}
}
const svgNS = "http://www.w3.org/2000/svg"
function resetSVG(){
// erase existing contents
let c; while (c = svg.firstChild) svg.removeChild(c)
svg.setAttribute("width",config.width)
svg.setAttribute("height",config.height)
svg.setAttribute("viewBox", `0 0 ${config.width} ${config.height}`)
svg.style.background=config.Inverted?"black":"white";
window.mainpath=document.createElementNS(svgNS, "path");
mainpath.setAttributeNS(null, "style", "stroke-width: 2px; fill: none; stroke: " + (config.Inverted?"white":"black"))
svg.appendChild(mainpath)
}
function download(){
const svgString = '<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">'
+ (new XMLSerializer()).serializeToString(svg);
const blob = new Blob([svgString], {type: 'image/svg+xml;charset=utf-8'});
const downloadLink = document.createElement("a");
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = window.algo.replace('.js','_') + Date.now() + ".svg";
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
algorithm.onchange()
</script>