3 function Display(_canvas,_context,_file,_image,_sorce){
5 this.context = _context;
9 this.magnification = 1;
15 var display1 = new Display();//1
16 var display2 = new Display();//2
17 var display3 = new Display();//3
18 var display4 = new Display();//4
21 display1.canvas = document.getElementById('canvas');
22 display2.canvas = document.getElementById('canvas2');
23 display3.canvas = document.getElementById('canvas3');
24 display4.canvas = document.getElementById('canvas4');
25 display1.context = display1.canvas.getContext('2d');
26 display2.context = display2.canvas.getContext('2d');
27 display3.context = display3.canvas.getContext('2d');
28 display4.context = display4.canvas.getContext('2d');
29 display1.canvas.height = CANVAS_SIZE;
30 display1.canvas.width = CANVAS_SIZE;
31 display2.canvas.height = CANVAS_SIZE;
32 display2.canvas.width = CANVAS_SIZE;
33 display3.canvas.height = CANVAS_SIZE;
34 display3.canvas.width = CANVAS_SIZE;
35 display4.canvas.height = CANVAS_SIZE;
36 display4.canvas.width = CANVAS_SIZE;
40 function loadFile(files,number){
44 display1.context.clearRect(0,0,display1.canvas.width,display1.canvas.height);
45 magnification_init(display1);
46 rotation_init(display1);
47 move_params_init(display1);
48 displayImage(display1);
49 }else if(number == 2){
51 display2.context.clearRect(0,0,display2.canvas.width,display2.canvas.height);
52 magnification_init(display2);
53 rotation_init(display2);
54 move_params_init(display2);
55 displayImage(display2);
59 //ファイルを選択でローカルから画像ファイルを読み込んでcanvasに描画する
60 function displayImage(display){
66 var image = new Image();
67 var reader = new FileReader();
69 if(!display.file){//display4に対する描画
70 console.log("no file");
72 display.context.mozImageSmoothingEnabled = false;
73 display.context.webkitImageSmoothingEnabled = false;
74 display.context.msImageSmoothingEnabled = false;
75 display.context.imageSmoothingEnabled = false
78 imageAspect = image.width / image.height;
79 if (imageAspect >= 1) { //画像が横長
81 width = display.canvas.width;
82 height = display.canvas.height / imageAspect;
83 to = (display.canvas.height - height) / 2;
86 width = display.canvas.width * imageAspect;
87 height = display.canvas.height;
88 left = (display.canvas.width - width) / 2;
91 //ファイルアップロード2回目以降のcanvasの初期化
92 display.context.clearRect(0,0,display.canvas.width,display.canvas.height);
93 /****************変換適用***********************************************/
94 display.context.translate(parseInt(display.canvas.width / 2),parseInt(display.canvas.height / 2));//コンテキストに対する効果の始点を中心に移動
95 display.context.scale(display.magnification,display.magnification);//倍率をコンテキストに反映
96 display.context.rotate(display.angle / 180 * Math.PI);//回転をコンテキストに反映
97 display.context.translate(-parseInt(display.canvas.width / 2),-parseInt(display.canvas.height / 2));
98 /**************************************************************************/
100 //画像をアスペクト比を維持して中心に表示(描画時は効果を元の位置に戻す)
101 display.context.drawImage(display3.canvas, 0, 0, display.canvas.width, display.canvas.height, display.movx, display.movy,display.canvas.width, display.canvas.height);
103 /*****************変換調整***********************************************/
104 display.context.translate(parseInt(display.canvas.width / 2),parseInt(display.canvas.height / 2)); //コンテキストに対する効果の始点を中心に移動
105 display.context.scale(1 / display.magnification,1 / display.magnification);//画像の倍率の初期化
106 display.context.rotate(-1 * display.angle / 180 * Math.PI);//角度の初期化
107 display.context.translate(-parseInt(display.canvas.width / 2),-parseInt(display.canvas.height / 2));
108 /****************************************************************************/
110 console.log("no image");
111 reader.onload = function(event){
112 image.onload = function(){
115 display.context.mozImageSmoothingEnabled = false;
116 display.context.webkitImageSmoothingEnabled = false;
117 display.context.msImageSmoothingEnabled = false;
118 display.context.imageSmoothingEnabled = false
120 imageAspect = image.width / image.height;
121 if (imageAspect >= 1) { //画像が横長
123 width = display.canvas.width;
124 height = display.canvas.height / imageAspect;
125 to = (display.canvas.height - height) / 2;
128 width = display.canvas.width * imageAspect;
129 height = display.canvas.height;
130 left = (display.canvas.width - width) / 2;
133 //ファイルアップロード2回目以降のcanvasの初期化
134 display.context.clearRect(0,0,display.canvas.width,display.canvas.height);
136 /****************変換適用***********************************************/
137 display.context.translate(parseInt(display.canvas.width / 2),parseInt(display.canvas.height / 2));//コンテキストに対する効果の始点を中心に移動
138 display.context.scale(display.magnification,display.magnification);//倍率をコンテキストに反映
139 display.context.rotate(display.angle / 180 * Math.PI);//回転をコンテキストに反映
140 display.context.translate(-parseInt(display.canvas.width / 2),-parseInt(display.canvas.height / 2));
141 /**************************************************************************/
143 //画像をアスペクト比を維持して中心に表示(描画時は効果を元の位置に戻す)
144 display.context.drawImage(image, 0, 0, image.width, image.height, left+display.movx, to-display.movy, width, height);
146 /*****************変換調整***********************************************/
147 display.context.translate(parseInt(display.canvas.width / 2),parseInt(display.canvas.height / 2)); //コンテキストに対する効果の始点を中心に移動
148 display.context.scale(1 / display.magnification,1 / display.magnification);//画像の倍率の初期化
149 display.context.rotate(-1 * display.angle / 180 * Math.PI);//角度の初期化
150 display.context.translate(-parseInt(display.canvas.width / 2),-parseInt(display.canvas.height / 2));
151 /****************************************************************************/
153 image.src = event.target.result;
155 if(display.file && display.file.type.match('image.*')){
156 reader.readAsDataURL(display.file);
164 function scaling(number){
166 display1.magnification = parseFloat(form1.magnification.value);
167 displayImage(display1);
168 }else if(number == 2){
169 display2.magnification = parseFloat(form2.magnification.value);
170 displayImage(display2);
171 }else if(number == 4){
172 display4.magnification = parseFloat(form4.magnification.value);
173 displayImage(display4);
178 function rotation(number){
180 display1.angle = parseFloat(form1.angle.value);
181 displayImage(display1);
182 }else if(number == 2){
183 display2.angle = parseFloat(form2.angle.value);
184 displayImage(display2);
189 function moving(number){
191 display1.movx = parseFloat(form1.movex.value);
192 display1.movy = parseFloat(form1.movey.value);
193 displayImage(display1);
194 }else if(number == 2){
195 display2.movx = parseFloat(form2.movex.value);
196 display2.movy = parseFloat(form2.movey.value);
197 displayImage(display2);
198 }else if(number == 4){
199 display4.movx = parseFloat(form4.movex.value);
200 display4.movy = parseFloat(form4.movey.value);
201 displayImage(display4);
206 function rotation_init(number,display){
208 form1.angle.value = 0;
209 display.angle = parseFloat(form1.angle.value);
210 }else if(number == 2){
211 form2.angle.value = 0;
212 display.angle = parseFloat(form2.angle.value);
217 function magnification_init(number,display){
219 form1.magnification.value = 1.00;
220 display.magnification = parseFloat(form1.magnification.value);
221 }else if(number == 2){
222 form2.magnification.value = 1.00;
223 display.magnification = parseFloat(form2.magnification.value);
224 }else if(number == 4){
225 form4.magnification.value = 1.00;
226 display.magnification = parseFloat(form4.magnification.value);
231 function move_params_init(number,display){
233 form1.movex.value = 0;
234 form1.movey.value = 0;
235 display.movx = parseFloat(form1.movex.value);
236 display.movy = parseFloat(form1.movey.value);
237 }else if(number == 2){
238 form2.movex.value = 0;
239 form2.movey.value = 0;
240 display.movx = parseFloat(form2.movex.value);
241 display.movy = parseFloat(form2.movey.value);
242 }else if(number == 4){
243 form4.movex.value = 0;
244 form4.movey.value = 0;
245 display.movx = parseFloat(form4.movex.value);
246 display.movy = parseFloat(form4.movey.value);
250 function reseting(number){
252 move_params_init(number,display1);
253 rotation_init(number,display1);
254 magnification_init(number,display1);
255 displayImage(display1);
256 }else if(number == 2){
257 move_params_init(number,display2);
258 rotation_init(number,display2);
259 magnification_init(number,display2);
260 displayImage(display2);
261 }else if(number == 4){
262 move_params_init(number,display4);
263 magnification_init(number,display4);
264 displayImage(display4);
270 /******* WITs *********************************************************** vem ********************
271 ******************* WITs ****************************************** vem ******** vem *************
272 vem ************************ WITs **************************** vem ******************* vem *******
273 ****** vem ***************************** WITs ********* vem ****************************** vem ***
274 *************** vem **************************** vem WITs ************************************ vem
275 ************************ vem ************* vem ***************** WITs ****************************
276 ********************************* vem ***************************************** WITs ************/
280 var image = display1.context.getImageData(0,0,display1.canvas.width,display1.canvas.height);
281 var image2 = display2.context.getImageData(0,0,display2.canvas.width,display2.canvas.height);
282 var image3 = display3.context.createImageData(display3.canvas.width,display3.canvas.height);
284 var image_rgba = image.data;
285 var image2_rgba = image2.data;
286 var image3_rgba = image3.data;
288 image_rgba = color_change(image_rgba, 1);
289 image2_rgba = color_change(image2_rgba, 2);
290 image3_rgba = make_new_Image(image_rgba, image2_rgba, image3_rgba);
292 display3.context.putImageData(image3, 0, 0);
293 displayImage(display4);
297 function color_change(image_rgba, mode){
301 var before_color,after_color;
304 //canvas選択 1-display1のcanvas ,2-display2のcanvas
306 before_color = parseInt(color_form.before_color.value);
307 after_color = parseInt(color_form.after_color.value);
309 before_color = parseInt(color_form.before_color2.value);
310 after_color = parseInt(color_form.after_color2.value);
312 diff_color = after_color - before_color;
314 /**************RGB->HSV*******************************/
315 for (var i = 0; i < image_rgba.length ; i++){
316 R = image_rgba[i * 4] / 255;//RGBAの値は0-255で与えられている
317 G = image_rgba[i * 4 + 1] / 255;
318 B = image_rgba[i * 4 + 2] / 255;
320 if(R == G && G == B){
322 }else if(R >= G && R >= B){
330 }else if(G >= R && G >= B){
338 }else if(B >= R && B >= G){
349 H = 60 * (G - B)/(max - min);
352 H = (60 * (B - R) / (max - min)) + 120;
355 H = (60 * (R - G) / (max - min)) + 240;
359 /*****************color change*********************/
361 if (before_color != 1 && after_color != 1){
362 if(H >= (before_color-1) * 60 -30 && H < (before_color * 60 -30)){
363 H = H + 60 * diff_color;
365 }else if(before_color == 1 && after_color != 1){
366 if(H >= 0 && H < 30){
367 H = H + 60 * diff_color;
368 }else if(H >= 330 && H <= 360){
369 H = H - 60 * (6 - diff_color);
371 }else if(before_color != 1 && after_color == 1){
372 if(H >= (before_color - 1) * 60 -30 && H < (before_color - 1) * 60 ){
373 H = H + 60 * (6 + diff_color);
374 }else if(H >= (before_color - 1) * 60 && H < before_color * 60 -30){
375 H = H + 60 * diff_color;
381 /***********************HSV->RGB************************/
382 if(H >= 0 && H < 60){
384 G = (H / 60) * (max - min) + min;
386 }else if(H >= 60 && H < 120){
387 R = -((H-120) / 60) * (max - min) + min;
390 }else if(H >= 120 && H < 180){
393 B = ((H-120) / 60) * (max - min) + min;
394 }else if(H >= 180 && H < 240){
396 G = -((H-240) / 60) * (max - min) + min;
398 }else if(H >= 240 && H < 300){
399 R = ((H-240) / 60) * (max - min) + min;
402 }else if(H >= 300 && H <= 360){
405 B = (-(H-360) / 60) * (max - min) + min;
410 image_rgba[i * 4] = R * 255;
411 image_rgba[i * 4 + 1] = G * 255;
412 image_rgba[i * 4 + 2] = B * 255;
419 /*************create new image data*************/
421 image_rgba[count * 4] //R
422 image_rgba[count * 4 + 1] //G
423 image_rgba[count * 4 + 2] //B
424 image_rgba[count * 4 + 3] //A
426 image2_rgba[count * 4] //R
427 image2_rgba[count * 4 + 1] //G
428 image2_rgba[count * 4 + 2] //B
429 image2_rgba[count * 4 + 3] //A
431 function make_new_Image(image_rgba, image2_rgba, image3_rgba){
433 var alpha = parseFloat(color_form.alpha.value);
434 var beta = parseFloat(color_form.beta.value);
436 for(var count = 0; count < image3_rgba.length; count++){
437 image3_rgba[count * 4] = (image_rgba[count * 4] * alpha) + (image2_rgba[count * 4] * beta);
438 image3_rgba[count * 4 + 1] = (image_rgba[count * 4 + 1] * alpha) + (image2_rgba[count * 4 + 1] * beta);
439 image3_rgba[count * 4 + 2] = (image_rgba[count * 4 + 2] * alpha) + (image2_rgba[count * 4 + 2] * beta);
440 image3_rgba[count * 4 + 3] = (image_rgba[count * 4 + 3] * alpha) + (image2_rgba[count * 4 + 3] * beta);
441 //image3_rgba[count * 4 + 3] = 255;
446 function set_alpha(){
447 var alpha = parseFloat(color_form.alpha.value);
448 var beta = parseFloat(color_form.beta.value); //beta = 1 - alpha
449 var comp = 1.00 - alpha
450 color.beta.value = comp.toFixed(2);
454 var alpha = parseFloat(color_form.alpha.value);
455 var beta = parseFloat(color_form.beta.value);
456 var comp = 1.00 - beta;
457 color.alpha.value = comp.toFixed(2);