-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimg.saver.js
More file actions
210 lines (163 loc) · 5.84 KB
/
Copy pathimg.saver.js
File metadata and controls
210 lines (163 loc) · 5.84 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
(function(NS) {
NS.ImgManager = (function() {
const ALPHA_POS = 4, CANVAS_2D_CONTEXT = '2d', CANVAS_TAG = 'canvas';
var canvas = {},
croppedImages = false,
tempCanvas = {},
source = {},
context = {},
waitQueue = [],
errorQueue = [],
readyQueue = [],
onImageReady = function() {
//[set callback here]
return true;
};
function processImgs(imgAdded) {
var src = '';
if (waitQueue.length > 0) {
src = waitQueue.shift();
source.src = src;
}
if (imgAdded) {
readyQueue.push(imgAdded);
}
}
function onImgError(e) {
errorQueue.push(source.src);
processImgs();
}
function getQueue() {
return {
waiting: waitQueue,
error: errorQueue,
ready: readyQueue
};
}
function absCeil(value) {
return ((value >= 0 ? value : -value)) >> 0;
}
function imgDataPosToPixel(i) {
return absCeil(i / ALPHA_POS);
}
function x(i) {
return imgDataPosToPixel(i) % canvas.width;
}
function distance(x0, y0, x1, y1) {
var x = x1 - x0,
y = y1 - y0;
return Math.sqrt(x * x + y * y);
}
function y(i) {
return absCeil(imgDataPosToPixel(i) / canvas.width) % canvas.height;
}
function index(x, y) {
return ALPHA_POS * (y * canvas.width + x) - 1;
}
function getCroppedCanvas(localContext, threshold) {
var imageData = localContext.getImageData(0, 0, canvas.width, canvas.height),
imgData = imageData.data,
w = 0,
h = 0,
box = {};
//if worker then this must be redirected...
box = getBoundingBox(imgData, threshold);
w = distance(box[0].x, box[0].y, box[1].x, box[0].y);
h = distance(box[0].x, box[0].y, box[0].x, box[1].y);
imageData = localContext.getImageData(box[0].x, box[0].y, w, h);
tempCanvas.width = tempCanvas.width;
tempCanvas.width = w;
tempCanvas.height = h;
tempCanvas.getContext(CANVAS_2D_CONTEXT).putImageData(imageData, 0, 0);
return tempCanvas;
}
function onImgLoaded(loadEvent) {
var cropped, imgData;
//set canvas
canvas.width = source.width;
canvas.height = source.height;
// Draw image into canvas
context.drawImage(source, 0, 0, source.width, source.height);
if(croppedImages){
cropped = getCroppedCanvas(context, 15);
context.clearRect(0, 0, source.width, source.height);
canvas.width = cropped.width;
canvas.height = cropped.height;
context.drawImage(cropped, 0, 0);
}
// Get canvas contents as a data URL
imgData = canvas.toDataURL('image/png');
// Save image into localStorage
try {
sessionStorage.removeItem(source.src);
sessionStorage.setItem(source.src, imgData);
} catch (e) {
console.log('Storage failed: ', e);
} finally {
//console.log('READY IN SESSION STORAGE:',source.src);
onImageReady(source.src);
}
//continue with next image
processImgs(source.src);
}
function getBoundingBox(imgData, threshold) {
const ALPHA_MAX = 255;
var len = imgData.length - 1,
w = 0,
h = 0,
i = ALPHA_POS - 1,
box = {
0: {
x: NaN,
y: NaN
},
1: {
x: NaN,
y: NaN
}
},
evalPos = function(pos) {
var xPos = x(pos),
yPos = y(pos);
if (imgData[pos] > threshold) {
box[0].x = (isNaN(box[0].x) || box[0].x > xPos) ? xPos : box[0].x;
box[0].y = (isNaN(box[0].y) || box[0].y > yPos) ? yPos : box[0].y;
box[1].x = (isNaN(box[1].x) || box[1].x < xPos) ? xPos : box[1].x;
box[1].y = (isNaN(box[1].y) || box[1].y < yPos) ? yPos : box[1].y;
}
};
threshold = (threshold || ALPHA_MAX);
while (len > i) {
evalPos(i);
evalPos(len);
i += ALPHA_POS;
len -= ALPHA_POS;
}
return box;
}
function init(fileList, onImageReadyCallback, cropEmptySpace) {
//sets cropping
croppedImages = cropEmptySpace || croppedImages;
//Creating img and bindings
source = document.createElement('img');
source.addEventListener('load', onImgLoaded); git
source.addEventListener('error', onImgError);
//Creating canvas and context
tempCanvas = document.createElement(CANVAS_TAG);
canvas = document.createElement(CANVAS_TAG);
context = canvas.getContext(CANVAS_2D_CONTEXT);
//set callback
if (typeof onImageReadyCallback === 'function') {
onImageReady = onImageReadyCallback;
}
//set queue
waitQueue = fileList.concat([]);
//start processing
processImgs();
}
return {
init: init,
getQueue: getQueue
};
}());
}(window));