-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.js
218 lines (197 loc) · 6.97 KB
/
upload.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
function readURL(input) {
if (input.files && input.files[0]) {
var maxWidth = screen.width;
var maxHeight = screen.height;
var reader = new FileReader();
var c = document.getElementById("origCanvas");
var ctx = c.getContext("2d");
reader.onload = function (e) {
var img = document.createElement("IMG");
img.src = e.target.result;
var imgWidth = img.width;
var imgHeight = img.height;
//adjusting images too large
while ( imgWidth > screen.width - 100 || imgHeight > screen.height - 200 ){
imgWidth = parseInt(imgWidth*9/10);
imgHeight = parseInt(imgHeight*9/10);
// console.log(imgWidth + " " + maxWidth );
// console.log(imgHeight + " " + maxHeight );
}
img.width = "" + imgWidth;
img.height = "" + imgHeight;
c.width = "" + imgWidth;
c.height = "" + imgHeight;
//original picture on left or top
ctx.drawImage(img, 0, 0, imgWidth, imgHeight);
//copying image on to second canvas
var c2 = document.getElementById("newCanvas");
c2.width = "" + imgWidth;
c2.height = "" + imgHeight;
var ctx2 = c2.getContext("2d");
ctx2.drawImage(img, 0, 0, imgWidth, imgHeight);
//canvas and options appear
$("#origCanvas").css("display", "inline");
$("#selections").css("display", "inline");
//horizontal/portrait orientation
if ( isLandscape() ){
console.log("hello");
$("#newCanvas").css("display", "block");
$("#opt2").text("Bottom");
$("#opt3").text("Top");
}
else{
$("#newCanvas").css("display", "inline");
$("#opt2").text("Left");
$("#opt3").text("Right");
}
};
reader.readAsDataURL(input.files[0]);
}
}
//color selector
document.addEventListener("DOMContentLoaded", init, false);
function init()
{
var canvas = document.getElementById("newCanvas");
canvas.addEventListener("mousedown", getPosition, false);
}
function getPosition(event)
{
var x = new Number();
var y = new Number();
var canvas = document.getElementById("newCanvas");
if (event.x != undefined && event.y != undefined)
{
x = event.x;
y = event.y;
}
else // Firefox
{
x = event.clientX + document.body.scrollLeft +
document.documentElement.scrollLeft;
y = event.clientY + document.body.scrollTop +
document.documentElement.scrollTop;
}
x -= canvas.offsetLeft;
y -= canvas.offsetTop;
console.log("x: " + x + " y: " + y);
showColor(x, y);
}
function showColor(xCoor, yCoor){
var x = xCoor;
var y = yCoor;
c = document.getElementById("origCanvas");
ctx = c.getContext("2d");
var imgData = ctx.getImageData(x,y,1,1);
var red = imgData.data[0];
var green = imgData.data[1];
var blue = imgData.data[2];
var hex = rgbToHex( red, green, blue );
c2 = document.getElementById("colorSelector");
ctx2 = c2.getContext("2d");
ctx2.fillStyle="#" + hex;
ctx2.fillRect(0, 0, c2.width, c2.height);
}
function resetCanvas(){ //re-copying original image on to new canvas
var c = document.getElementById("origCanvas");
var ctx = c.getContext("2d");
var c2 = document.getElementById("newCanvas");
var ctx2 = c2.getContext("2d");
ctx2.putImageData(ctx.getImageData(0,0,c.width,c.height), 0, 0);
}
function createImage(numBars) {
resetCanvas(); //erasing previous bars
var c = document.getElementById("newCanvas");
var ctx = c.getContext("2d");
var spacing = 4;
var rows = parseInt(numBars);
var widthBar = 75;
var heightBar = parseInt(c.height/30);
var topX, topY = 0;
if ( isLandscape() ) {
topX = parseInt(c.width/(rows + spacing));
topY = parseInt(c.height/2) - parseInt(widthBar/2);
}
else {
topX = parseInt(c.width/2) - parseInt(widthBar/2);
topY = parseInt(c.height/(rows + spacing));
}
var colors = [];
//color selection
for ( var x = 2; x < rows + 2; x++ ){
if ( isLandscape() ){
// var imgData = ctx.getImageData(topX*x + parseInt(widthBar/2), topY, 1, 1);
var randx = Math.floor((Math.random() * c.width) + 1);
var randy = Math.floor((Math.random() * c.height) + 1);
var imgData = ctx.getImageData( randx, randy, 1, 1)
}
else{
var randx = Math.floor((Math.random() * c.width) + 1);
var randy = Math.floor((Math.random() * c.height) + 1);
var imgData = ctx.getImageData( randx, randy, 1, 1)
// var imgData = ctx.getImageData(topX, topY*x + parseInt(widthBar/2), 1, 1);
}
var red = imgData.data[0];
var green = imgData.data[1];
var blue = imgData.data[2];
var hex = rgbToHex( red, green, blue );
colors[x - 2] = hex;
}
colors.sort(); //dark to light order
var decrement = 0;
//alignment depengind on left, right, middle
for ( var k = 1; k < 4; k++ ){
if (document.getElementById("align" + k).checked){
if ( isLandscape() ){
if (k == 2 ){ //Left
topY = c.height - widthBar;
decrement = parseInt(c.width/(rows + spacing));
}
if (k == 3){ //Right
topY = 1;
decrement = parseInt(c.width/(rows + spacing));
}
}
else{
if (k == 2 ){ //Left
topX = 1;
decrement = parseInt(c.height/(rows + spacing));
}
if (k == 3){ //Right
topX = c.width - widthBar;
decrement = parseInt(c.height/(rows + spacing));
}
}
}
}
//drawing bars
if ( isLandscape() ){ //landscape
for ( var j = 2; j < (rows + 2); j++ ){
ctx.fillStyle = "#" + colors[j-2];
ctx.fillRect( topX*j - decrement, topY, heightBar, widthBar );
}
}
else { //portrait
for ( var j = 2; j < (rows + 2); j++ ){
ctx.fillStyle = "#" + colors[j-2];
ctx.fillRect( topX, topY*j - decrement, widthBar, heightBar );
}
}
}
$('#test').keypress(function(e) {
if (e.which == 13) {
var bars = document.getElementById("test").value;
createImage(bars);
}
});
function isLandscape(){
c = document.getElementById("origCanvas");
return (c.width > c.height);
}
function componentToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
function rgbToHex(r, g, b) {
return componentToHex(r) + componentToHex(g) + componentToHex(b);
}