-
Notifications
You must be signed in to change notification settings - Fork 9
/
canvas-image.js
113 lines (81 loc) · 3.6 KB
/
canvas-image.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
var imageDisplay, displayCanvas, displayContext, displayImage, displayImageData, originalImage;
var worker = new Worker('canvas-image-worker.js');
var fileReader = new FileReader();
function draw () {
displayImage = new Image();
displayImage.src = originalImage;
displayCanvas.width = displayImage.width;
displayCanvas.height = displayImage.height;
displayContext = displayCanvas.getContext('2d');
displayContext.drawImage(displayImage, 0, 0);
displayImageData = displayContext.getImageData(0,0,displayCanvas.width,displayCanvas.height);
var tmpGreyscaleMethod = (document.getElementById('rdo_greyscale_luminance').checked) ? "luminance" : (document.getElementById('rdo_greyscale_average').checked) ? "average" : (document.getElementById('rdo_greyscale_disable').checked) ? "" : "luminance" ;
var tmpDitherMethod = (document.getElementById('rdo_dither_atkinson').checked) ? "atkinson" : (document.getElementById('rdo_dither_threshold').checked) ? "threshold" : "atkinson" ;
var tmpDitherThreshold = document.getElementById('threshold').value;
var tmpReplaceColours = document.getElementById('chk_replace_colours').checked;
var tmpReplaceBlack = {
r: document.getElementById('rep_black_r').value,
g: document.getElementById('rep_black_g').value,
b: document.getElementById('rep_black_b').value,
a: document.getElementById('rep_black_a').value
}
var tmpReplaceWhite = {
r: document.getElementById('rep_white_r').value,
g: document.getElementById('rep_white_g').value,
b: document.getElementById('rep_white_b').value,
a: document.getElementById('rep_white_a').value
}
if (window.console && window.console.time) {
console.log("Starting Web Worker for image (" + displayCanvas.width + "x" + displayCanvas.height + ", Greyscale Method: " + tmpGreyscaleMethod + ", Dither Method: " + tmpDitherMethod + ")");
console.time("Web worker took");
}
worker.postMessage( {
image: {
data: displayImageData,
width: displayCanvas.width,
height: displayCanvas.height
},
processing: {
greyscaleMethod: tmpGreyscaleMethod,
ditherMethod: tmpDitherMethod,
ditherThreshold: tmpDitherThreshold,
replaceColours: tmpReplaceColours,
replaceColourMap: {
black: tmpReplaceBlack,
white: tmpReplaceWhite
}
}
});
}
worker.addEventListener('message', function (e) {
displayContext = displayCanvas.getContext('2d');
if (window.console && window.console.time)
console.timeEnd("Web worker took");
displayContext.putImageData(e.data.image.data, 0, 0);
if (document.getElementById('rdo_format_png').checked == true) {
imageDisplay.src = displayCanvas.toDataURL("image/png");
} else if (document.getElementById('rdo_format_gif').checked == true) {
imageDisplay.src = displayCanvas.toDataURL("image/gif");
}
}, false);
fileReader.onload = function (e) {
originalImage = e.target.result;
document.getElementById('displayImage').src = e.target.result;
}
function handleFileSelect (e) {
var files = e.target.files;
fileReader.readAsDataURL(e.target.files[0]);
}
function setup () {
// Detect Canvas Support
displayCanvas = document.createElement('canvas');
imageDisplay = document.getElementById('displayImage');
if (displayCanvas.getContext) {
document.getElementById('renderbtn').onclick = function() { draw(); };
document.getElementById('fileSelect').addEventListener('change', handleFileSelect, false);
originalImage = document.getElementById('displayImage').src;
} else {
alert("Hi there, you're using an older browser which doesn't support Canvas, so unfortunately I can't show you this demo. Sorry!");
}
}
window.onload = setup;