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
|
<meta charset="UTF-8">
<link rel="stylesheet" href="css/sally.css" type="text/css" charset="utf-8" />
<style>
#controls { width: 220px; float: left; padding: 10px; }
label { min-width: 70px; display: inline-block; }
#text_style { width: 100px; }
label.cbox { min-width: 50px; }
</style>
<body class="transparent">
<div id="controls">
<label></label> <span id="width_span"></span>x<span id="height_span"></span>
<br>
<label for="width_el">width</label><input type="range" min="1" max="120" value="40" id="width_el">
<br>
<label for="ratio_el">ratio</label><input type="range" min="0.0" max="8" value="2" step="0.005" id="ratio_el"><br>
<label for="hue_el">hue</label><input type="range" min="-1" max="1" value="0" step="0.005" id="hue_el"><br>
<label for="sat_el">sat</label><input type="range" min="-1" max="1" value="0" step="0.005" id="sat_el"><br>
<label for="lum_el">lum</label><input type="range" min="-1" max="1" value="0" step="0.005" id="lum_el"><br>
<label for="quant_el">quantize</label><input type="range" min="1" max="255" value="1" step="1" id="quant_el"><br>
<label for="palette_el" style="padding-top: 5px;">palette</label>
<select id="palette_el">
<option default value="colors">all colors</label>
<option value="hues">hues only</label>
<option value="grays">grayscale</label>
<option value="reds">reds</label>
<option value="yellows">yellows</label>
<option value="blues">blues</label>
</select>
<br>
<label class="cbox"></label><input type="checkbox" id="invert_el"> <label for="invert_el" style="padding-top: 7px;">invert</label>
<br>
<label class="cbox"></label><input type="checkbox" checked id="nn_el"> <label for="nn_el" style="padding-top: 7px;">nearest neighbor</label>
<br>
<br>
<input type="text" id="text_style">
<button id="save_el">SAVE</button>
<button id="timer_el">TIMER</button>
</div>
<div id="image_style"></div>
</body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://asdf.us/dither/js/color.js"></script>
<script src="http://asdf.us/dither/js/util.js"></script>
<script src="js/photo.js"></script>
<script>
var width = parseInt( width_span.innerHTML = width_el.value )
var ratio = parseFloat( ratio_el.value )
var nn = $(nn_el).prop('checked')
var invert = $(invert_el).prop('checked')
var width_timeout
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
function getStream(cb){
var constraints = {
video: true,
audio: false
}
navigator.getUserMedia(constraints, success, error);
function success (stream) {
var video = document.createElement("video")
video.src = window.URL.createObjectURL(stream)
video.play()
cb(video)
}
function error(error){
console.log('navigator.getUserMedia error: ', error);
}
}
var camera
getStream(function(video){
camera = video
animate()
})
function animate () {
requestAnimationFrame(animate)
if (! camera.videoWidth) return
Photo.fromCanvas(camera, toCanvas, { width: width, ratio: ratio, neighbor: nn })
}
Photo.set_recolor_fn(function(rgb){
var hsl = rgb2hsl(rgb)
hsl[0] = mod(hsl[0] + hue, 1.0)
hsl[1] = clamp(hsl[1] + sat, 0.0, 1.0)
hsl[2] = clamp(hsl[2] + lum, 0.0, 1.0)
rgb = hsl2rgb.apply(this, hsl)
if (quant > 1) {
rgb[0] = quantize(rgb[0], quant)
rgb[1] = quantize(rgb[1], quant)
rgb[2] = quantize(rgb[2], quant)
}
if (invert) {
rgb[0] = 255 - rgb[0]
rgb[1] = 255 - rgb[1]
rgb[2] = 255 - rgb[2]
}
return rgb
})
// Photo.denoise = 2
var hue = 0, sat = 0, lum = 0, quant = 1
listen(hue_el, window, "hue")
listen(sat_el, window, "sat")
listen(lum_el, window, "lum")
listen(quant_el, window, "quant")
save_el.addEventListener('click', save)
function save (){
Photo.fromCanvas(camera, saveText, { width: width, ratio: ratio, neighbor: nn })
function saveText(rows) {
text_style.value = Photo.ascii(rows)
if (window.self !== window.top) {
window.parent.postMessage(text_style.value, "*");
}
}
}
nn_el.addEventListener('change', function(){
nn = $(nn_el).prop('checked')
})
invert_el.addEventListener('change', function(){
invert = $(invert_el).prop('checked')
})
palette_el.addEventListener('change', function(){
var palette = $(palette_el).val()
Photo.set_colors( Photo[palette] )
})
ratio_el.addEventListener("input", function(){
ratio = parseFloat( ratio_el.value )
if (ratio < 0.03) ratio = 0
width_span.innerHTML = width
height_span.innerHTML = "..."
})
width_el.addEventListener("input", function(){
width = parseInt( width_el.value )
width_span.innerHTML = width
height_span.innerHTML = "..."
})
function listen (el, obj, val) {
el.addEventListener("input", function(){
obj[val] = parseFloat( el.value )
})
}
var canvas = document.createElement("canvas"), ctx = canvas.getContext('2d')
function toCanvas(rows){
var wpx = 6, hpx = 12
var rgb_colors = Photo.colors.map(function(c){ return "rgb(" + c + ")" })
canvas.width = rows[0].length * wpx
canvas.height = rows.length * hpx
rows.forEach(function(row, j){
row.forEach(function(lex, i){
ctx.fillStyle = rgb_colors[lex]
ctx.fillRect(i*wpx,j*hpx,wpx,hpx)
})
})
height_span.innerHTML = rows.length
image_style.innerHTML = ""
image_style.appendChild(canvas)
}
timer_el.addEventListener("click", function(){
var secs = 5;
(function step (){
if (secs > 0) {
timer_el.innerHTML = secs--
setTimeout(step, 1000)
}
else {
timer_el.innerHTML = "TIMER"
save()
}
})()
})
</script>
|