soiz1 commited on
Commit
d01a8a4
·
verified ·
1 Parent(s): 869e87b

Create script.js

Browse files
Files changed (1) hide show
  1. script.js +179 -0
script.js ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ document.addEventListener('DOMContentLoaded', function() {
2
+ // 要素の取得
3
+ const imageUpload = document.getElementById('image-upload');
4
+ const canvas = document.getElementById('image-canvas');
5
+ const ctx = canvas.getContext('2d');
6
+
7
+ // スライダー要素の取得
8
+ const brightnessSlider = document.getElementById('brightness');
9
+ const contrastSlider = document.getElementById('contrast');
10
+ const saturationSlider = document.getElementById('saturation');
11
+ const shadowsSlider = document.getElementById('shadows');
12
+ const highlightsSlider = document.getElementById('highlights');
13
+ const hueSlider = document.getElementById('hue');
14
+ const hueSaturationSlider = document.getElementById('hue-saturation');
15
+ const lightnessSlider = document.getElementById('lightness');
16
+ const redCurveSlider = document.getElementById('red-curve');
17
+ const greenCurveSlider = document.getElementById('green-curve');
18
+ const blueCurveSlider = document.getElementById('blue-curve');
19
+
20
+ // 値表示要素の取得
21
+ const brightnessValue = document.getElementById('brightness-value');
22
+ const contrastValue = document.getElementById('contrast-value');
23
+ const saturationValue = document.getElementById('saturation-value');
24
+ const shadowsValue = document.getElementById('shadows-value');
25
+ const highlightsValue = document.getElementById('highlights-value');
26
+ const hueValue = document.getElementById('hue-value');
27
+ const hueSaturationValue = document.getElementById('hue-saturation-value');
28
+ const lightnessValue = document.getElementById('lightness-value');
29
+ const redCurveValue = document.getElementById('red-curve-value');
30
+ const greenCurveValue = document.getElementById('green-curve-value');
31
+ const blueCurveValue = document.getElementById('blue-curve-value');
32
+
33
+ // ボタン要素の取得
34
+ const resetBtn = document.getElementById('reset-btn');
35
+ const downloadBtn = document.getElementById('download-btn');
36
+
37
+ let camanInstance = null;
38
+ let originalImageData = null;
39
+
40
+ // 画像アップロードの処理
41
+ imageUpload.addEventListener('change', function(e) {
42
+ if (e.target.files && e.target.files[0]) {
43
+ const reader = new FileReader();
44
+
45
+ reader.onload = function(event) {
46
+ const img = new Image();
47
+ img.onload = function() {
48
+ // キャンバスのサイズを画像に合わせて調整
49
+ const maxWidth = 800;
50
+ const maxHeight = 600;
51
+ let width = img.width;
52
+ let height = img.height;
53
+
54
+ if (width > maxWidth) {
55
+ height = (maxWidth / width) * height;
56
+ width = maxWidth;
57
+ }
58
+
59
+ if (height > maxHeight) {
60
+ width = (maxHeight / height) * width;
61
+ height = maxHeight;
62
+ }
63
+
64
+ canvas.width = width;
65
+ canvas.height = height;
66
+
67
+ // 画像をキャンバスに描画
68
+ ctx.drawImage(img, 0, 0, width, height);
69
+
70
+ // CamanJSのインスタンスを作成
71
+ Caman(canvas, function() {
72
+ camanInstance = this;
73
+ originalImageData = this.toBase64();
74
+
75
+ // スライダーをリセット
76
+ resetSliders();
77
+ });
78
+ };
79
+
80
+ img.src = event.target.result;
81
+ };
82
+
83
+ reader.readAsDataURL(e.target.files[0]);
84
+ }
85
+ });
86
+
87
+ // スライダー変更時の処理
88
+ function setupSlider(slider, valueElement, filterName, paramName) {
89
+ slider.addEventListener('input', function() {
90
+ if (!camanInstance) return;
91
+
92
+ valueElement.textContent = this.value;
93
+ applyFilters();
94
+ });
95
+ }
96
+
97
+ // 各スライダーにイベントリスナーを設定
98
+ setupSlider(brightnessSlider, brightnessValue, 'brightness', 'brightness');
99
+ setupSlider(contrastSlider, contrastValue, 'contrast', 'contrast');
100
+ setupSlider(saturationSlider, saturationValue, 'saturation', 'saturation');
101
+ setupSlider(shadowsSlider, shadowsValue, 'exposure', 'shadows');
102
+ setupSlider(highlightsSlider, highlightsValue, 'exposure', 'highlights');
103
+ setupSlider(hueSlider, hueValue, 'hue', 'hue');
104
+ setupSlider(hueSaturationSlider, hueSaturationValue, 'saturation', 'hueSaturation');
105
+ setupSlider(lightnessSlider, lightnessValue, 'vibrance', 'lightness');
106
+ setupSlider(redCurveSlider, redCurveValue, 'channels', 'red');
107
+ setupSlider(greenCurveSlider, greenCurveValue, 'channels', 'green');
108
+ setupSlider(blueCurveSlider, blueCurveValue, 'channels', 'blue');
109
+
110
+ // フィルターを適用する関数
111
+ function applyFilters() {
112
+ if (!camanInstance) return;
113
+
114
+ camanInstance.revert(false);
115
+
116
+ // 各フィルターを適用
117
+ camanInstance.brightness(parseInt(brightnessSlider.value));
118
+ camanInstance.contrast(parseInt(contrastSlider.value));
119
+ camanInstance.saturation(parseInt(saturationSlider.value));
120
+ camanInstance.exposure({
121
+ shadows: parseInt(shadowsSlider.value),
122
+ highlights: parseInt(highlightsSlider.value)
123
+ });
124
+ camanInstance.hue(parseInt(hueSlider.value));
125
+ camanInstance.vibrance(parseInt(lightnessSlider.value));
126
+ camanInstance.channels({
127
+ red: parseInt(redCurveSlider.value) / 100,
128
+ green: parseInt(greenCurveSlider.value) / 100,
129
+ blue: parseInt(blueCurveSlider.value) / 100
130
+ });
131
+
132
+ camanInstance.render();
133
+ }
134
+
135
+ // スライダーをリセットする関数
136
+ function resetSliders() {
137
+ brightnessSlider.value = 0;
138
+ contrastSlider.value = 0;
139
+ saturationSlider.value = 0;
140
+ shadowsSlider.value = 0;
141
+ highlightsSlider.value = 0;
142
+ hueSlider.value = 0;
143
+ hueSaturationSlider.value = 0;
144
+ lightnessSlider.value = 0;
145
+ redCurveSlider.value = 0;
146
+ greenCurveSlider.value = 0;
147
+ blueCurveSlider.value = 0;
148
+
149
+ brightnessValue.textContent = '0';
150
+ contrastValue.textContent = '0';
151
+ saturationValue.textContent = '0';
152
+ shadowsValue.textContent = '0';
153
+ highlightsValue.textContent = '0';
154
+ hueValue.textContent = '0';
155
+ hueSaturationValue.textContent = '0';
156
+ lightnessValue.textContent = '0';
157
+ redCurveValue.textContent = '0';
158
+ greenCurveValue.textContent = '0';
159
+ blueCurveValue.textContent = '0';
160
+
161
+ if (camanInstance) {
162
+ camanInstance.revert(false);
163
+ camanInstance.render();
164
+ }
165
+ }
166
+
167
+ // リセットボタンの処理
168
+ resetBtn.addEventListener('click', resetSliders);
169
+
170
+ // ダウンロードボタンの処理
171
+ downloadBtn.addEventListener('click', function() {
172
+ if (!camanInstance) return;
173
+
174
+ const link = document.createElement('a');
175
+ link.download = 'edited-image.png';
176
+ link.href = canvas.toDataURL('image/png');
177
+ link.click();
178
+ });
179
+ });