SenY commited on
Commit
3c13b11
·
1 Parent(s): 5e75f24
Files changed (4) hide show
  1. AspectCalc.js +149 -0
  2. index.html +238 -18
  3. index.js +288 -0
  4. style.css +0 -28
AspectCalc.js ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * AspectCalc.js - アスペクト比計算ライブラリ
3
+ * 縦解像度と横解像度から3種類のアスペクト比を計算します
4
+ *
5
+ * MIT License
6
+ *
7
+ * Copyright (c) 2025 SenY
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ * of this software and associated documentation files (the "Software"), to deal
11
+ * in the Software without restriction, including without limitation the rights
12
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ * copies of the Software, and to permit persons to whom the Software is
14
+ * furnished to do so, subject to the following conditions:
15
+ *
16
+ * The above copyright notice and this permission notice shall be included in all
17
+ * copies or substantial portions of the Software.
18
+ *
19
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ * SOFTWARE.
26
+ */
27
+
28
+ class AspectCalc {
29
+ /**
30
+ * 最大公約数を計算する関数
31
+ * @param {number} a - 数値1
32
+ * @param {number} b - 数値2
33
+ * @returns {number} 最大公約数
34
+ */
35
+ static gcd(a, b) {
36
+ while (b !== 0) {
37
+ let temp = b;
38
+ b = a % b;
39
+ a = temp;
40
+ }
41
+ return a;
42
+ }
43
+
44
+ /**
45
+ * 2つの数値の最大公約数で割って、最も簡単な整数比を求める
46
+ * @param {number} width - 幅
47
+ * @param {number} height - 高さ
48
+ * @returns {Object} {x: number, y: number} の形式
49
+ */
50
+ static getIntegerRatio(width, height) {
51
+ const divisor = this.gcd(width, height);
52
+ return {
53
+ x: width / divisor,
54
+ y: height / divisor
55
+ };
56
+ }
57
+
58
+ /**
59
+ * 縦横共に20以下の整数比で表せる最も近似のアスペクト比を求める
60
+ * @param {number} width - 幅
61
+ * @param {number} height - 高さ
62
+ * @returns {Object} {x: number, y: number} の形式
63
+ */
64
+ static getApproximateRatio(width, height) {
65
+ const targetRatio = width / height;
66
+ let bestRatio = { x: 1, y: 1 };
67
+ let minError = Math.abs(targetRatio - 1);
68
+
69
+ // 1から20までの全ての組み合わせを試す
70
+ for (let x = 1; x <= 20; x++) {
71
+ for (let y = 1; y <= 20; y++) {
72
+ const ratio = x / y;
73
+ const error = Math.abs(targetRatio - ratio);
74
+ if (error < minError) {
75
+ minError = error;
76
+ bestRatio = { x, y };
77
+ }
78
+ }
79
+ }
80
+
81
+ return bestRatio;
82
+ }
83
+
84
+ /**
85
+ * 一般的なディスプレイアスペクト比の中で最も近似のものを求める
86
+ * @param {number} width - 幅
87
+ * @param {number} height - 高さ
88
+ * @returns {Object} {x: number, y: number} の形式
89
+ */
90
+ static getStandardRatio(width, height) {
91
+ const targetRatio = width / height;
92
+
93
+ // 一般的なアスペクト比のリスト(正規化された値)
94
+ const standardRatios = [
95
+ { x: 1, y: 1 }, // 1:1
96
+ { x: 16, y: 9 }, // 16:9
97
+ { x: 4, y: 3 }, // 4:3
98
+ { x: 8, y: 5 }, // 8:5
99
+ { x: 3, y: 2 }, // 3:2
100
+ { x: 5, y: 4 }, // 5:4
101
+ { x: 9, y: 16 }, // 9:16 (16:9の逆)
102
+ { x: 3, y: 4 }, // 3:4 (4:3の逆)
103
+ { x: 5, y: 8 }, // 5:8 (8:5の逆)
104
+ { x: 2, y: 3 }, // 2:3 (3:2の逆)
105
+ { x: 4, y: 5 } // 4:5 (5:4の逆)
106
+ ];
107
+
108
+ let bestRatio = standardRatios[0];
109
+ let minError = Math.abs(targetRatio - (bestRatio.x / bestRatio.y));
110
+
111
+ for (const ratio of standardRatios) {
112
+ const error = Math.abs(targetRatio - (ratio.x / ratio.y));
113
+ if (error < minError) {
114
+ minError = error;
115
+ bestRatio = ratio;
116
+ }
117
+ }
118
+
119
+ return bestRatio;
120
+ }
121
+
122
+ /**
123
+ * メイン関数:3種類のアスペクト比を計算して返す
124
+ * @param {number} width - 幅
125
+ * @param {number} height - 高さ
126
+ * @returns {Object} 3種類のアスペクト比を含むオブジェクト
127
+ */
128
+ static calculate(width, height) {
129
+ if (width <= 0 || height <= 0) {
130
+ throw new Error('幅と高さは正の数である必要があります');
131
+ }
132
+
133
+ return {
134
+ integer: this.getIntegerRatio(width, height),
135
+ approximate: this.getApproximateRatio(width, height),
136
+ standard: this.getStandardRatio(width, height)
137
+ };
138
+ }
139
+ }
140
+
141
+ // モジュールエ��スポート対応
142
+ if (typeof module !== 'undefined' && module.exports) {
143
+ module.exports = AspectCalc;
144
+ }
145
+
146
+ // グローバル変数としても利用可能にする
147
+ if (typeof window !== 'undefined') {
148
+ window.AspectCalc = AspectCalc;
149
+ }
index.html CHANGED
@@ -1,19 +1,239 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  </html>
 
1
+ <!DOCTYPE html>
2
+ <html lang="ja">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>AspectCalc サンプル</title>
7
+
8
+ <!-- Bootstrap 5 CSS -->
9
+ <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
10
+
11
+ <!-- Font Awesome -->
12
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
13
+
14
+ <style>
15
+ .drop-zone {
16
+ border: 2px dashed #dee2e6;
17
+ border-radius: 10px;
18
+ padding: 2rem;
19
+ text-align: center;
20
+ transition: all 0.3s ease;
21
+ cursor: pointer;
22
+ }
23
+
24
+ .drop-zone:hover {
25
+ border-color: #0d6efd;
26
+ background-color: #f8f9fa;
27
+ }
28
+
29
+ .drop-zone.dragover {
30
+ border-color: #0d6efd;
31
+ background-color: #e7f3ff;
32
+ }
33
+
34
+ .image-preview {
35
+ max-width: 100%;
36
+ max-height: 300px;
37
+ border-radius: 8px;
38
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
39
+ }
40
+
41
+ .aspect-ratio-card {
42
+ transition: transform 0.2s ease;
43
+ }
44
+
45
+ .aspect-ratio-card:hover {
46
+ transform: translateY(-2px);
47
+ }
48
+
49
+ .ratio-display {
50
+ font-size: 1.5rem;
51
+ font-weight: bold;
52
+ color: #0d6efd;
53
+ }
54
+
55
+ .copy-btn {
56
+ opacity: 0;
57
+ transition: opacity 0.2s ease;
58
+ }
59
+
60
+ .aspect-ratio-card:hover .copy-btn {
61
+ opacity: 1;
62
+ }
63
+ </style>
64
+ </head>
65
+ <body class="bg-light">
66
+ <div class="container py-5">
67
+ <div class="row justify-content-center">
68
+ <div class="col-lg-10">
69
+ <!-- ヘッダー -->
70
+ <div class="text-center mb-5">
71
+ <h1 class="display-4 fw-bold text-primary">
72
+ <i class="fas fa-calculator me-3"></i>AspectCalc
73
+ </h1>
74
+ <p class="lead text-muted">画像の解像度からアスペクト比を計算するツール</p>
75
+ </div>
76
+
77
+ <!-- メインカード -->
78
+ <div class="card shadow-lg border-0">
79
+ <div class="card-body p-4">
80
+ <!-- 入力セクション -->
81
+ <div class="row">
82
+ <div class="col-md-6">
83
+ <h4 class="mb-3">
84
+ <i class="fas fa-upload me-2"></i>画像の入力
85
+ </h4>
86
+
87
+ <!-- ドラッグ&ドロップエリア -->
88
+ <div class="drop-zone mb-3" id="dropZone">
89
+ <i class="fas fa-cloud-upload-alt fa-3x text-muted mb-3"></i>
90
+ <p class="mb-2">画像をドラッグ&ドロップ</p>
91
+ <p class="text-muted small">または</p>
92
+ <button class="btn btn-outline-primary" id="fileSelectBtn">
93
+ <i class="fas fa-folder-open me-2"></i>ファイルを選択
94
+ </button>
95
+ <input type="file" id="fileInput" accept="image/*" class="d-none">
96
+ </div>
97
+
98
+ <!-- クリップボード貼り付け -->
99
+ <div class="mb-3">
100
+ <button class="btn btn-outline-secondary w-100" id="pasteBtn">
101
+ <i class="fas fa-paste me-2"></i>Ctrl+V でクリップボードから貼り付け
102
+ </button>
103
+ </div>
104
+
105
+ <!-- 手動入力 -->
106
+ <div class="card bg-light">
107
+ <div class="card-body">
108
+ <h6 class="card-title">
109
+ <i class="fas fa-keyboard me-2"></i>手動入力
110
+ </h6>
111
+ <div class="row g-2">
112
+ <div class="col-6">
113
+ <label for="widthInput" class="form-label small">幅 (px)</label>
114
+ <input type="number" class="form-control" id="widthInput" placeholder="1920" min="1">
115
+ </div>
116
+ <div class="col-6">
117
+ <label for="heightInput" class="form-label small">高さ (px)</label>
118
+ <input type="number" class="form-control" id="heightInput" placeholder="1080" min="1">
119
+ </div>
120
+ </div>
121
+ <button class="btn btn-primary btn-sm mt-2 w-100" id="calculateBtn">
122
+ <i class="fas fa-calculator me-2"></i>計算
123
+ </button>
124
+ </div>
125
+ </div>
126
+ </div>
127
+
128
+ <!-- プレビューエリア -->
129
+ <div class="col-md-6">
130
+ <h4 class="mb-3">
131
+ <i class="fas fa-eye me-2"></i>プレビュー
132
+ </h4>
133
+ <div id="previewArea" class="text-center">
134
+ <div class="text-muted">
135
+ <i class="fas fa-image fa-3x mb-3"></i>
136
+ <p>画像を選択してください</p>
137
+ </div>
138
+ </div>
139
+
140
+ <!-- 解像度情報 -->
141
+ <div id="resolutionInfo" class="mt-3 d-none">
142
+ <div class="alert alert-info">
143
+ <h6 class="alert-heading">
144
+ <i class="fas fa-info-circle me-2"></i>解像度情報
145
+ </h6>
146
+ <p class="mb-0">
147
+ 幅: <span id="actualWidth" class="fw-bold">-</span>px<br>
148
+ 高さ: <span id="actualHeight" class="fw-bold">-</span>px
149
+ </p>
150
+ </div>
151
+ </div>
152
+ </div>
153
+ </div>
154
+ </div>
155
+ </div>
156
+
157
+ <!-- 結果表示エリア -->
158
+ <div id="resultsArea" class="mt-4 d-none">
159
+ <h4 class="mb-3">
160
+ <i class="fas fa-chart-line me-2"></i>アスペクト比の結果
161
+ </h4>
162
+ <div class="row g-3">
163
+ <!-- 整数比 -->
164
+ <div class="col-md-4">
165
+ <div class="card aspect-ratio-card h-100">
166
+ <div class="card-body text-center">
167
+ <h5 class="card-title">
168
+ <i class="fas fa-hashtag me-2"></i>整数比
169
+ </h5>
170
+ <div class="ratio-display" id="integerRatio">-</div>
171
+ <p class="text-muted small mt-2">最大公約数で割った最も簡単な比</p>
172
+ <button class="btn btn-outline-primary btn-sm copy-btn" data-ratio="integer">
173
+ <i class="fas fa-copy me-1"></i>コピー
174
+ </button>
175
+ </div>
176
+ </div>
177
+ </div>
178
+
179
+ <!-- 近似比 -->
180
+ <div class="col-md-4">
181
+ <div class="card aspect-ratio-card h-100">
182
+ <div class="card-body text-center">
183
+ <h5 class="card-title">
184
+ <i class="fas fa-search me-2"></i>近似比
185
+ </h5>
186
+ <div class="ratio-display" id="approximateRatio">-</div>
187
+ <p class="text-muted small mt-2">20以下の整数で最も近似の比</p>
188
+ <button class="btn btn-outline-primary btn-sm copy-btn" data-ratio="approximate">
189
+ <i class="fas fa-copy me-1"></i>コピー
190
+ </button>
191
+ </div>
192
+ </div>
193
+ </div>
194
+
195
+ <!-- 標準比 -->
196
+ <div class="col-md-4">
197
+ <div class="card aspect-ratio-card h-100">
198
+ <div class="card-body text-center">
199
+ <h5 class="card-title">
200
+ <i class="fas fa-tv me-2"></i>標準比
201
+ </h5>
202
+ <div class="ratio-display" id="standardRatio">-</div>
203
+ <p class="text-muted small mt-2">一般的なディスプレイ比</p>
204
+ <button class="btn btn-outline-primary btn-sm copy-btn" data-ratio="standard">
205
+ <i class="fas fa-copy me-1"></i>コピー
206
+ </button>
207
+ </div>
208
+ </div>
209
+ </div>
210
+ </div>
211
+ </div>
212
+ </div>
213
+ </div>
214
+ </div>
215
+
216
+ <!-- Toast通知 -->
217
+ <div class="toast-container position-fixed bottom-0 end-0 p-3">
218
+ <div id="toast" class="toast" role="alert">
219
+ <div class="toast-header">
220
+ <i class="fas fa-check-circle text-success me-2"></i>
221
+ <strong class="me-auto">成功</strong>
222
+ <button type="button" class="btn-close" data-bs-dismiss="toast" title="閉じる"></button>
223
+ </div>
224
+ <div class="toast-body" id="toastMessage">
225
+ コピーしました!
226
+ </div>
227
+ </div>
228
+ </div>
229
+
230
+ <!-- Bootstrap 5 JS -->
231
+ <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
232
+
233
+ <!-- AspectCalc.js -->
234
+ <script src="AspectCalc.js"></script>
235
+
236
+ <!-- メインスクリプト -->
237
+ <script src="index.js"></script>
238
+ </body>
239
  </html>
index.js ADDED
@@ -0,0 +1,288 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * AspectCalc サンプルページのメインスクリプト
3
+ */
4
+
5
+ class AspectCalcApp {
6
+ constructor() {
7
+ this.initializeElements();
8
+ this.bindEvents();
9
+ this.setupClipboardPaste();
10
+ }
11
+
12
+ /**
13
+ * DOM要素の初期化
14
+ */
15
+ initializeElements() {
16
+ this.dropZone = document.getElementById('dropZone');
17
+ this.fileInput = document.getElementById('fileInput');
18
+ this.fileSelectBtn = document.getElementById('fileSelectBtn');
19
+ this.pasteBtn = document.getElementById('pasteBtn');
20
+ this.widthInput = document.getElementById('widthInput');
21
+ this.heightInput = document.getElementById('heightInput');
22
+ this.calculateBtn = document.getElementById('calculateBtn');
23
+ this.previewArea = document.getElementById('previewArea');
24
+ this.resolutionInfo = document.getElementById('resolutionInfo');
25
+ this.actualWidth = document.getElementById('actualWidth');
26
+ this.actualHeight = document.getElementById('actualHeight');
27
+ this.resultsArea = document.getElementById('resultsArea');
28
+ this.integerRatio = document.getElementById('integerRatio');
29
+ this.approximateRatio = document.getElementById('approximateRatio');
30
+ this.standardRatio = document.getElementById('standardRatio');
31
+ this.toast = document.getElementById('toast');
32
+ this.toastMessage = document.getElementById('toastMessage');
33
+ }
34
+
35
+ /**
36
+ * イベントリスナーの設定
37
+ */
38
+ bindEvents() {
39
+ // ファイル選択
40
+ this.fileSelectBtn.addEventListener('click', () => {
41
+ this.fileInput.click();
42
+ });
43
+
44
+ this.fileInput.addEventListener('change', (e) => {
45
+ this.handleFileSelect(e.target.files[0]);
46
+ });
47
+
48
+ // ドラッグ&ドロップ
49
+ this.dropZone.addEventListener('dragover', (e) => {
50
+ e.preventDefault();
51
+ this.dropZone.classList.add('dragover');
52
+ });
53
+
54
+ this.dropZone.addEventListener('dragleave', () => {
55
+ this.dropZone.classList.remove('dragover');
56
+ });
57
+
58
+ this.dropZone.addEventListener('drop', (e) => {
59
+ e.preventDefault();
60
+ this.dropZone.classList.remove('dragover');
61
+ const files = e.dataTransfer.files;
62
+ if (files.length > 0) {
63
+ this.handleFileSelect(files[0]);
64
+ }
65
+ });
66
+
67
+ // 手動計算
68
+ this.calculateBtn.addEventListener('click', () => {
69
+ this.handleManualInput();
70
+ });
71
+
72
+ // Enterキーでの計算
73
+ this.widthInput.addEventListener('keypress', (e) => {
74
+ if (e.key === 'Enter') {
75
+ this.handleManualInput();
76
+ }
77
+ });
78
+
79
+ this.heightInput.addEventListener('keypress', (e) => {
80
+ if (e.key === 'Enter') {
81
+ this.handleManualInput();
82
+ }
83
+ });
84
+
85
+ // コピーボタン
86
+ document.querySelectorAll('.copy-btn').forEach(btn => {
87
+ btn.addEventListener('click', (e) => {
88
+ const ratioType = e.target.getAttribute('data-ratio');
89
+ this.copyRatio(ratioType);
90
+ });
91
+ });
92
+ }
93
+
94
+ /**
95
+ * クリップボード貼り付け機能の設定
96
+ */
97
+ setupClipboardPaste() {
98
+ // キーボードイベント(Ctrl+V)
99
+ document.addEventListener('keydown', (e) => {
100
+ if (e.ctrlKey && e.key === 'v') {
101
+ e.preventDefault();
102
+ this.handleClipboardPaste();
103
+ }
104
+ });
105
+
106
+ // 貼り付けボタン
107
+ this.pasteBtn.addEventListener('click', () => {
108
+ this.handleClipboardPaste();
109
+ });
110
+ }
111
+
112
+ /**
113
+ * ファイル選択の処理
114
+ */
115
+ handleFileSelect(file) {
116
+ if (!file || !file.type.startsWith('image/')) {
117
+ this.showToast('有効な画像ファイルを選択してください', 'error');
118
+ return;
119
+ }
120
+
121
+ const reader = new FileReader();
122
+ reader.onload = (e) => {
123
+ this.processImage(e.target.result, file.name);
124
+ };
125
+ reader.readAsDataURL(file);
126
+ }
127
+
128
+ /**
129
+ * クリップボード貼り付けの処理
130
+ */
131
+ async handleClipboardPaste() {
132
+ try {
133
+ const clipboardItems = await navigator.clipboard.read();
134
+
135
+ for (const clipboardItem of clipboardItems) {
136
+ for (const type of clipboardItem.types) {
137
+ if (type.startsWith('image/')) {
138
+ const blob = await clipboardItem.getType(type);
139
+ const reader = new FileReader();
140
+ reader.onload = (e) => {
141
+ this.processImage(e.target.result, 'クリップボード画像');
142
+ };
143
+ reader.readAsDataURL(blob);
144
+ return;
145
+ }
146
+ }
147
+ }
148
+
149
+ this.showToast('クリップボードに画像が見つかりません', 'error');
150
+ } catch (error) {
151
+ console.error('クリップボード読み取りエラー:', error);
152
+ this.showToast('クリップボードの読み取りに失敗しました', 'error');
153
+ }
154
+ }
155
+
156
+ /**
157
+ * 手動入力の処理
158
+ */
159
+ handleManualInput() {
160
+ const width = parseInt(this.widthInput.value);
161
+ const height = parseInt(this.heightInput.value);
162
+
163
+ if (!width || !height || width <= 0 || height <= 0) {
164
+ this.showToast('有効な数値を入力してください', 'error');
165
+ return;
166
+ }
167
+
168
+ this.calculateAspectRatios(width, height);
169
+ this.showResults();
170
+ }
171
+
172
+ /**
173
+ * 画像の処理
174
+ */
175
+ processImage(imageSrc, fileName) {
176
+ const img = new Image();
177
+ img.onload = () => {
178
+ // プレビュー表示
179
+ this.showImagePreview(imageSrc, fileName);
180
+
181
+ // 解像度情報表示
182
+ this.showResolutionInfo(img.width, img.height);
183
+
184
+ // アスペクト比計算
185
+ this.calculateAspectRatios(img.width, img.height);
186
+
187
+ // 結果表示
188
+ this.showResults();
189
+ };
190
+ img.onerror = () => {
191
+ this.showToast('画像の読み込みに失敗しました', 'error');
192
+ };
193
+ img.src = imageSrc;
194
+ }
195
+
196
+ /**
197
+ * 画像プレビューの表示
198
+ */
199
+ showImagePreview(imageSrc, fileName) {
200
+ this.previewArea.innerHTML = `
201
+ <div class="mb-2">
202
+ <img src="${imageSrc}" alt="プレビュー" class="image-preview">
203
+ </div>
204
+ <small class="text-muted">${fileName}</small>
205
+ `;
206
+ }
207
+
208
+ /**
209
+ * 解像度情報の表示
210
+ */
211
+ showResolutionInfo(width, height) {
212
+ this.actualWidth.textContent = width.toLocaleString();
213
+ this.actualHeight.textContent = height.toLocaleString();
214
+ this.resolutionInfo.classList.remove('d-none');
215
+ }
216
+
217
+ /**
218
+ * アスペクト比の計算
219
+ */
220
+ calculateAspectRatios(width, height) {
221
+ try {
222
+ const results = AspectCalc.calculate(width, height);
223
+
224
+ // 結果の保存(コピー機能用)
225
+ this.currentResults = results;
226
+
227
+ // 表示更新
228
+ this.integerRatio.textContent = `${results.integer.x}:${results.integer.y}`;
229
+ this.approximateRatio.textContent = `${results.approximate.x}:${results.approximate.y}`;
230
+ this.standardRatio.textContent = `${results.standard.x}:${results.standard.y}`;
231
+
232
+ } catch (error) {
233
+ console.error('アスペクト比計算エラー:', error);
234
+ this.showToast('アスペクト比の計算に失敗しました', 'error');
235
+ }
236
+ }
237
+
238
+ /**
239
+ * 結果エリアの表示
240
+ */
241
+ showResults() {
242
+ this.resultsArea.classList.remove('d-none');
243
+ this.resultsArea.scrollIntoView({ behavior: 'smooth' });
244
+ }
245
+
246
+ /**
247
+ * アスペクト比のコピー
248
+ */
249
+ copyRatio(ratioType) {
250
+ if (!this.currentResults) {
251
+ this.showToast('コピーするデータがありません', 'error');
252
+ return;
253
+ }
254
+
255
+ const ratio = this.currentResults[ratioType];
256
+ const text = `${ratio.x}:${ratio.y}`;
257
+
258
+ navigator.clipboard.writeText(text).then(() => {
259
+ this.showToast(`「${text}」をコピーしました!`);
260
+ }).catch(() => {
261
+ this.showToast('コピーに失敗しました', 'error');
262
+ });
263
+ }
264
+
265
+ /**
266
+ * トースト通知の表示
267
+ */
268
+ showToast(message, type = 'success') {
269
+ this.toastMessage.textContent = message;
270
+
271
+ // アイコンの変更
272
+ const icon = this.toast.querySelector('i');
273
+ if (type === 'error') {
274
+ icon.className = 'fas fa-exclamation-circle text-danger me-2';
275
+ } else {
276
+ icon.className = 'fas fa-check-circle text-success me-2';
277
+ }
278
+
279
+ // トーストの表示
280
+ const toast = new bootstrap.Toast(this.toast);
281
+ toast.show();
282
+ }
283
+ }
284
+
285
+ // アプリケーションの初期化
286
+ document.addEventListener('DOMContentLoaded', () => {
287
+ new AspectCalcApp();
288
+ });
style.css DELETED
@@ -1,28 +0,0 @@
1
- body {
2
- padding: 2rem;
3
- font-family: -apple-system, BlinkMacSystemFont, "Arial", sans-serif;
4
- }
5
-
6
- h1 {
7
- font-size: 16px;
8
- margin-top: 0;
9
- }
10
-
11
- p {
12
- color: rgb(107, 114, 128);
13
- font-size: 15px;
14
- margin-bottom: 10px;
15
- margin-top: 5px;
16
- }
17
-
18
- .card {
19
- max-width: 620px;
20
- margin: 0 auto;
21
- padding: 16px;
22
- border: 1px solid lightgray;
23
- border-radius: 16px;
24
- }
25
-
26
- .card p:last-child {
27
- margin-bottom: 0;
28
- }