/
fokus.js
196 lines (167 loc) · 4.89 KB
/
fokus.js
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
// DOM ELEMENTS
var $input = document.getElementById('input')
var $canvas = document.getElementById('canvas')
var $run = document.getElementById('run')
var $canvasRef = document.getElementById('canvas-ref')
var $level = document.getElementById('level')
var $blur = document.getElementById('blur')
var $file = document.getElementById('file')
var $download = document.getElementById('download')
var $a = document.getElementById('a')
// Canvas Contexts
var ctx = $canvas.getContext('2d')
var ctxRef = $canvasRef.getContext('2d')
// Objects
var worker = new Worker('fokusworker.js')
var reader = new FileReader()
var image = new Image()
// File
var filename
var imageFile
var edited = false
// File Input Event - start of the process !
$input.addEventListener('change', function () {
reader.readAsDataURL(this.files[0])
filename = this.files[0].name.split('.')[0]
updateRunBtn()
reader.onload = function () {
// Create new Image in memory for canvas to use
image.src = reader.result
image.onload = function () {
$canvas.width = image.width
$canvas.height = image.height
ctx.drawImage(image, 0, 0)
$canvasRef.width = image.width
$canvasRef.height = image.height
ctxRef.drawImage(image, 0, 0)
updateRunBtn()
startApp()
}
}
}, false)
function startApp () {
var ready = true
var startFlag = false
var startX, startY, rectWidth, rectHeight
// Event handlers
$canvas.addEventListener('click', function (e) {
drawGuide(e.clientX, e.clientY)
startFlag = !startFlag
}, false)
$run.addEventListener('click', function (e) {
if (startX && startY && rectWidth && rectHeight) {
// set to false so that click on canvas won't trigger drawGuide
ready = false
processImage($level.value, $blur.checked)
}
}, false)
$level.oninput = function () {
updateRunBtn(true)
}
$blur.onchange = function () {
updateRunBtn(true)
}
$download.addEventListener('click', function (e) {
// Create Download Link
$canvas.toBlob(function (blob) {
// Check if image file was made previously & remove
if (imageFile !== null) {
window.URL.revokeObjectURL(imageFile)
}
imageFile = window.URL.createObjectURL(blob)
$a.download = filename + '.' + $file.value.split('/')[1]
$a.href = imageFile
$a.click()
}, $file.value)
})
function processImage (level, blur) {
// Entire image to be processed
var wholeimage = ctxRef.getImageData(0, 0, $canvas.width, $canvas.height)
// Part if the Image to be "focused"
var parcial = ctxRef.getImageData(startX, startY, rectWidth, rectHeight)
var data = {img: wholeimage, level: level, blur: blur}
worker.postMessage(data, [data.img.data.buffer])
worker.onmessage = function (message) {
var newimg = message.data
ctx.putImageData(new ImageData(newimg.data, newimg.width, newimg.height), 0, 0)
ctx.putImageData(parcial, startX, startY)
// set the flag back on
edited = true
ready = true
updateRunBtn()
}
}
function drawGuide (xpos, ypos) {
// Only draw guide rectangle when Web Worker is not running
if (ready) {
var rect = $canvas.getBoundingClientRect()
var x = xpos - rect.left
var y = ypos - rect.top
// 1st click
if (!startFlag) {
startX = x
startY = y
edited = false
clearRect(ctx)
ctx.drawImage(image, 0, 0)
appendCircle(ctx, x, y)
updateRunBtn()
return
}
// 2nd click
rectWidth = x - startX
rectHeight = y - startY
if (x < startX) {
rectWidth = startX - x
startX = x
}
if (y < startY) {
rectHeight = startY - y
startY = y
}
appendCircle(ctx, x, y)
appendRect(ctx, startX, startY, rectWidth, rectHeight)
return
}
return
}
}
// Canvas Manipulation Functions
function clearRect (ctx) {
ctx.clearRect(0, 0, $canvas.width, $canvas.height)
}
function appendCircle (ctx, x, y) {
ctx.beginPath()
ctx.arc(x, y, 3, 0, 2 * Math.PI, false)
ctx.fillStyle = '#888'
ctx.fill()
}
function appendRect (ctx, x, y, w, h) {
ctx.strokeStyle = '#888'
ctx.lineWidth = 2
ctx.strokeRect(x, y, w, h)
}
// UTILITIS
function updateRunBtn (rebuild) {
if (rebuild && edited) {
$run.textContent = 'Rebuild Image'
$run.style.backgroundColor = '#00549B'
return
}
$run.textContent = 'Process Image'
$run.style.backgroundColor = '#00245B'
}
// toBlob Polyfill
if (!HTMLCanvasElement.prototype.toBlob) {
Object.defineProperty(HTMLCanvasElement.prototype, 'toBlob', {
value: function (callback, type, quality) {
var binStr = atob(this.toDataURL(type, quality).split(',')[1])
var len = binStr.length
var arr = new Uint8Array(len)
for (var i = 0; i < len; i++) {
arr[i] = binStr.charCodeAt(i)
}
callback(new Blob( [arr], {type: type || 'image/png'}))
}
})
}