-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
593 lines (513 loc) · 19 KB
/
index.html
File metadata and controls
593 lines (513 loc) · 19 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<title>PDFdiff</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.min.js"></script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: ヒラギノ角ゴ Pro, Noto Sans JP, Arial, sans-serif;
height: 100dvh;
overflow: hidden;
touch-action: none;
}
/* 修正:高さを固定せず、Canvasに合わせて広がるようにする */
.comparison-container {
position: relative;
overflow: hidden;
/* min-heightを削除するか、表示を安定させるために自動に */
border: 1px solid #ccc;
background: #eee;
margin: 0 auto;
/* 中央寄せ */
}
/* 修正:Canvasレイヤーの設定 */
.canvas-layer {
position: absolute;
top: 0;
left: 50%;
transform: translateX(-50%);
/* 中央配置 */
display: block;
}
/* 追加:Canvasが絶対配置なので親要素に高さを確保するためのヘルパー */
#comp-box {
transition: width 0.2s, height 0.2s;
}
canvas {
height: 100%;
}
#slider {
position: absolute;
top: 0;
bottom: 0;
width: 4px;
background: #2563eb;
cursor: ew-resize;
z-index: 10;
}
#slider::after {
content: '↔';
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: #2563eb;
color: white;
padding: 5px;
border-radius: 50%;
font-size: 12px;
}
.diff-overlay {
mix-blend-mode: difference;
}
.loading-spinner {
border: 4px solid rgba(0, 0, 0, 0.1);
width: 36px;
height: 36px;
border-radius: 50%;
border-left-color: #09f;
animation: spin 1s ease infinite;
}
.head1 {
height: 40px;
background: #efb6af;
display: flex;
flex-direction: row;
padding-left: 20px;
gap: 12px;
}
.head1 h1 {
font-size: 16px;
font-weight: bold;
margin: 0;
color: #fff;
display: flex;
align-items: center;
gap: 8px;
}
.head1 p {
margin: 0;
color: #fff;
font-size: 15px;
cursor: pointer;
height: 40px;
display: flex;
align-items: center;
padding: 0 12px;
}
.head1 p:hover {
background: #ba7d75;
}
.head1 h1::after {
content: "|";
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
.hidden {
display: none;
}
.panels {
gap: 20px;
background-color: #f9f9ff;
border-bottom: 3px solid gainsboro;
height: 90px;
padding-left: 20px;
display: flex;
align-items: center;
}
#controlpanel {
display: none;
}
#fileinputpanel {
display: flex;
gap: 20px;
}
.flex {
display: flex;
}
.items-center {
align-items: center;
}
.gap-2 {
gap: 0.5rem;
}
.squareicon {
width: 16px;
height: 16px;
display: inline-block;
border-radius: 10px;
}
.red {
background-color: #ff6666;
}
.blue {
background-color: #6666ff;
}
.purple {
background-color: #cc66ff;
}
.flex-col {
flex-direction: column;
}
.justify-center {
justify-content: center;
}
.fip {
height: 90%;
display: flex;
gap: 3px;
font-size: 14px;
flex-direction: column-reverse;
align-items: center;
border: 1px solid gainsboro;
padding: 5px;
border-radius: 3px;
}
.fip label {
font-size: 10px;
display: flex;
flex-direction: column;
align-items: center;
}
.fip input[type="file"] {
display: none;
}
/* ボタン部分 */
.upload-file-icon-a {
width: 40px;
height: 40px;
border-radius: 5px;
background: #fff;
cursor: pointer;
}
.upload-file-icon-a::before {
content: "↑N";
font-size: 24px;
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
.upload-file-icon-a.new-a::before {
content: "↑O";
}
/* ファイル名表示 */
.file-names {
width: 60px;
font-size: 5px;
text-align: center;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
</style>
</head>
<body>
<div>
<header class="head1">
<h1>PDFdiff - PDF比較ツール</h1>
<p id="file-tab-btn">ファイル</p>
<p id="option-tab-btn">オプション</p>
</header>
<div class="panels" id="panels">
<div id="fileinputpanel">
<div class="fip">
<label>
<div class="upload-file-icon-a old-a"></div>
<p>比較元 (旧)</p>
<input type="file" id="file-old" accept="application/pdf">
<div class="file-names"></div>
</label>
</div>
<div class="fip">
<label>
<div class="upload-file-icon-a new-a"></div>
<p>比較先 (新)</p>
<input type="file" id="file-new" accept="application/pdf">
<div class="file-names"></div>
</label>
</div>
</div>
<!-- Controls -->
<div id="controlpanel">
<div class="flex items-center gap-6">
<label>
<input type="radio" name="mode" value="overlay" checked class="w-4 h-4 text-blue-600">
<span>重ね合わせ (差分)</span>
</label>
<label>
<input type="radio" name="mode" value="slider" class="w-4 h-4 text-blue-600">
<span>スライダー比較</span>
</label>
<label for="fullscreen-cbtn">
<input type="checkbox" id="fullscreen-cbtn">
<span>フルスクリーン表示</span>
</label>
</div>
</div>
</div>
<!-- Main Comparison Area -->
<div id="status-message"
class="text-center py-10 text-gray-400 border-2 border-dashed border-gray-200 rounded-xl">
PDFファイルを2つ選択してください。
</div>
<div class="flex items-center gap-4">
<button id="prev-page"
class="px-4 py-2 bg-gray-200 rounded hover:bg-gray-300 disabled:opacity-50">前へ</button>
<span class="font-medium">ページ: <span id="current-page">1</span> / <span id="total-pages">-</span></span>
<button id="next-page"
class="px-4 py-2 bg-gray-200 rounded hover:bg-gray-300 disabled:opacity-50">次へ</button>
</div>
<div id="viewer-container" class="hidden flex flex-col items-center justify-center">
<div id="loading" class="hidden mb-4 items-center gap-2">
<div class="loading-spinner"></div>
<span>レンダリング中...</span>
</div>
<div class="comparison-container" id="comp-box">
<canvas id="canvas-old" class="canvas-layer"></canvas>
<canvas id="canvas-new" class="canvas-layer"></canvas>
<div id="slider" class="hidden"></div>
</div>
<div class="flex items-center gap-2">
<div class="flex items-center gap-2">
<span class="squareicon red"></span>
<span>削除・変更前</span>
</div>
<div class="flex items-center gap-2">
<span class="squareicon blue"></span>
<span>追加・変更後</span>
</div>
<div class="flex items-center gap-2">
<span class="squareicon purple"></span>
<span>共通部分</span>
</div>
</div>
</div>
</div>
<script>
var pdfjsLib = window['pdfjs-dist/build/pdf'];
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.worker.min.js';
let pdfOld = null;
let pdfNew = null;
let currentPage = 1;
let scale = 1.5;
var fileOldInput = document.getElementById('file-old');
var fileNewInput = document.getElementById('file-new');
var compBox = document.getElementById('comp-box');
var canvasOld = document.getElementById('canvas-old');
var canvasNew = document.getElementById('canvas-new');
var slider = document.getElementById('slider');
var statusMsg = document.getElementById('status-message');
var viewerContainer = document.getElementById('viewer-container');
var controlPanel = document.getElementById('controlpanel');
var loading = document.getElementById('loading');
var panels = document.getElementById('panels');
var fileInputPanel = document.getElementById('fileinputpanel');
var fileTabBtn = document.getElementById('file-tab-btn');
var optionTabBtn = document.getElementById('option-tab-btn');
var fullscreenCbtn = document.getElementById('fullscreen-cbtn');
// Fullscreen Toggle
fullscreenCbtn.onchange = () => {
if (fullscreenCbtn.checked) {
document.documentElement.requestFullscreen();
} else {
document.exitFullscreen();
}
};
// Tab Switching
let currentMode = null;
function togglePanel(mode) {
if (currentMode === mode) {
panels.style.display = 'none';
currentMode = null;
} else {
panels.style.display = 'flex';
fileInputPanel.style.display = (mode === 'file') ? 'flex' : 'none';
controlPanel.style.display = (mode === 'option') ? 'flex' : 'none';
currentMode = mode;
}
}
fileTabBtn.onclick = () => togglePanel('file');
optionTabBtn.onclick = () => togglePanel('option');
// Handle File Uploads
fileOldInput.addEventListener('change', e => loadPDF(e, 'old'));
fileNewInput.addEventListener('change', e => loadPDF(e, 'new'));
async function loadPDF(e, type) {
var file = e.target.files[0];
if (!file) return;
var reader = new FileReader();
reader.onload = async function () {
var typedarray = new Uint8Array(this.result);
try {
var pdf = await pdfjsLib.getDocument(typedarray).promise;
if (type === 'old') pdfOld = pdf;
else pdfNew = pdf;
checkAndRender();
} catch (err) {
console.error(err);
alert("PDFの読み込みに失敗しました。");
}
};
reader.readAsArrayBuffer(file);
}
function checkAndRender() {
if (pdfOld && pdfNew) {
statusMsg.classList.add('hidden');
viewerContainer.classList.remove('hidden');
controlPanel.classList.remove('hidden');
var totalPages = Math.max(pdfOld.numPages, pdfNew.numPages);
document.getElementById('total-pages').textContent = totalPages;
renderPages();
}
}
async function renderPages() {
loading.classList.remove('hidden');
document.getElementById('current-page').textContent = currentPage;
// Render Old
if (currentPage <= pdfOld.numPages) {
await renderToCanvas(pdfOld, currentPage, canvasOld, 'red');
} else {
clearCanvas(canvasOld);
}
// Render New
if (currentPage <= pdfNew.numPages) {
await renderToCanvas(pdfNew, currentPage, canvasNew, 'blue');
} else {
clearCanvas(canvasNew);
}
updateMode();
loading.classList.add('hidden');
}
async function renderToCanvas(pdf, pageNum, canvas, tintColor) {
var page = await pdf.getPage(pageNum);
var viewport = page.getViewport({ scale });
var context = canvas.getContext('2d');
var targetHeight = window.innerHeight - 60;
// 2. PDFの元のサイズ(スケール1.0)を取得
var unscaledViewport = page.getViewport({ scale: 1 });
// 3. 計算した高さに合わせるための倍率(スケール)を算出
var dynamicScale = targetHeight / unscaledViewport.height;
// 4. 計算したスケールでビューポートを生成
var viewport = page.getViewport({ scale: dynamicScale });
canvas.height = viewport.height; // これが targetHeight とほぼ同等になる
canvas.width = viewport.width; // 比率を維持したまま自動計算される
compBox.style.height = canvas.height + "px";
compBox.style.width = canvas.width + "px";
await page.render({ canvasContext: context, viewport: viewport }).promise;
// Apply tint for difference visualization
var imageData = context.getImageData(0, 0, canvas.width, canvas.height);
var data = imageData.data;
for (let i = 0; i < data.length; i += 4) {
// If it's near white, make it transparent so layers can "blend"
var brightness = (data[i] + data[i + 1] + data[i + 2]) / 3;
if (brightness > 240) {
data[i + 3] = 0;
} else {
// Tinting logic:
// Old version becomes reddish
// New version becomes bluish
if (tintColor === 'red') {
data[i] = 255; data[i + 1] = 50; data[i + 2] = 50;
} else {
data[i] = 50; data[i + 1] = 50; data[i + 2] = 255;
}
}
}
context.putImageData(imageData, 0, 0);
}
function clearCanvas(canvas) {
var context = canvas.getContext('2d');
context.clearRect(0, 0, canvas.width, canvas.height);
}
// Navigation
document.getElementById('prev-page').onclick = () => {
if (currentPage > 1) {
currentPage--;
renderPages();
}
};
document.getElementById('next-page').onclick = () => {
var max = Math.max(pdfOld ? pdfOld.numPages : 0, pdfNew ? pdfNew.numPages : 0);
if (currentPage < max) {
currentPage++;
renderPages();
}
};
//keyboard navigation
window.addEventListener('keydown', (e) => {
if (e.key === 'ArrowLeft') {
document.getElementById('prev-page').click();
} else if (e.key === 'ArrowRight') {
document.getElementById('next-page').click();
}
});
// Modes and Interaction
var modeRadios = document.getElementsByName('mode');
modeRadios.forEach(r => r.onchange = updateMode);
function updateMode() {
var mode = document.querySelector('input[name="mode"]:checked').value;
if (mode === 'overlay') {
slider.classList.add('hidden');
canvasOld.style.clipPath = 'none';
canvasNew.style.opacity = '0.7'; // Blend effect
canvasOld.style.opacity = '0.7';
canvasNew.style.mixBlendMode = 'multiply';
} else {
slider.classList.remove('hidden');
canvasNew.style.opacity = '1';
canvasOld.style.opacity = '1';
canvasNew.style.mixBlendMode = 'normal';
initSlider();
}
}
function initSlider() {
var width = compBox.offsetWidth;
let isDragging = false;
var move = (x) => {
var rect = compBox.getBoundingClientRect();
let pos = (x - rect.left) / rect.width * 100;
pos = Math.max(0, Math.min(100, pos));
slider.style.left = pos + '%';
canvasNew.style.clipPath = `inset(0 0 0 ${pos}%)`;
};
slider.style.left = '50%';
canvasNew.style.clipPath = 'inset(0 0 0 50%)';
compBox.onmousedown = () => isDragging = true;
window.onmouseup = () => isDragging = false;
window.onmousemove = (e) => {
if (isDragging) move(e.clientX);
};
// Touch support
compBox.ontouchstart = () => isDragging = true;
window.ontouchend = () => isDragging = false;
window.ontouchmove = (e) => {
if (isDragging) move(e.touches[0].clientX);
};
}
document.querySelectorAll('.fip label').forEach(input => {
const list = input.nextElementSibling;
input.addEventListener('change', () => {
list.innerHTML = '';
[...input.files].forEach(file => {
const div = document.createElement('div');
div.textContent = file.name;
list.prepend(div);
});
});
});
</script>
</body>
</html>