Spaces:
Running
Running
File size: 13,476 Bytes
b950d11 71bac66 |
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 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PDF Slide Viewer</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.16.105/pdf.min.js"></script>
<style>
body {
margin: 0;
overflow: hidden;
font-family: Arial, sans-serif;
background-color: #000;
color: #fff;
position: relative;
}
canvas {
display: block;
margin: 0 auto;
width: 100vw;
height: 100vh;
object-fit: contain;
cursor: crosshair;
}
.arrow-button {
position: absolute;
top: 50%;
transform: translateY(-50%);
background-color: rgba(0, 0, 0, 0.5);
color: #fff;
border: none;
font-size: 24px;
padding: 10px;
cursor: pointer;
z-index: 10;
}
.arrow-button:disabled {
cursor: not-allowed;
background-color: rgba(100, 100, 100, 0.5);
}
#prev-slide {
left: 10px;
}
#next-slide {
right: 10px;
}
.links-container {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
display: flex;
flex-wrap: wrap;
gap: 10px;
justify-content: center;
}
.link-button {
padding: 10px 15px;
background-color: #1a73e8;
color: #fff;
text-decoration: none;
font-size: 14px;
border-radius: 5px;
border: none;
cursor: pointer;
}
.link-button:hover {
background-color: #1558b8;
}
.toolbar {
position: absolute;
top: 20px;
left: 10px;
z-index: 10;
display: flex;
flex-direction: column;
gap: 10px;
}
.toolbar button {
width: 30px;
height: 30px;
font-size: 16px;
cursor: pointer;
border: none;
border-radius: 50%;
background-color: #444;
color: #fff;
text-align: center;
}
.toolbar button.active {
background-color: #1a73e8;
}
.color-button {
width: 30px;
height: 30px;
border-radius: 50%;
border: 2px solid #fff;
cursor: pointer;
}
.color-button.red {
background-color: red;
}
.color-button.green {
background-color: green;
}
.color-button.blue {
background-color: blue;
}
.color-button.yellow {
background-color: yellow;
}
.slider-container {
position: absolute;
bottom: 20px;
left: 10px;
display: flex;
flex-direction: column;
align-items: center;
z-index: 10;
}
#page-slider {
width: 200px;
}
#page-number-input {
margin-top: 10px;
width: 60px;
text-align: center;
font-size: 16px;
}
.flash-message {
position: absolute;
top: 10px;
left: 50%;
transform: translateX(-50%);
background-color: red;
color: white;
padding: 5px 10px;
border-radius: 5px;
font-size: 14px;
display: none;
z-index: 10;
}
/* Add styles for full-screen button */
#fullscreen-btn {
position: absolute;
top: 20px;
right: 10px;
z-index: 10;
background-color: #444;
color: #fff;
border: none;
font-size: 16px;
padding: 10px;
cursor: pointer;
border-radius: 5px;
}
#fullscreen-btn:hover {
background-color: #1a73e8;
}
</style>
</head>
<body>
<canvas id="pdf-canvas"></canvas>
<button id="prev-slide" class="arrow-button">‹</button>
<button id="next-slide" class="arrow-button">›</button>
<div id="links-container" class="links-container"></div>
<div class="toolbar">
<button id="toggle-draw" class="color-button">D</button>
<button id="clear-draw" class="color-button">E</button>
<button class="color-button red" data-color="red"></button>
<button class="color-button green" data-color="green"></button>
<button class="color-button blue" data-color="blue"></button>
<button class="color-button yellow" data-color="yellow"></button>
</div>
<div class="slider-container">
<input type="range" id="page-slider" min="1" max="1" value="1">
<input type="number" id="page-number-input" min="1" value="1">
</div>
<div id="flash-message" class="flash-message">Invalid Page Number!</div>
<button id="fullscreen-btn">Full Screen</button>
<script>
const pdfUrl = 'mypdf.pdf'; // Replace with the path to your PDF file
const pdfCanvas = document.getElementById('pdf-canvas');
const ctx = pdfCanvas.getContext('2d');
const prevSlideBtn = document.getElementById('prev-slide');
const nextSlideBtn = document.getElementById('next-slide');
const linksContainer = document.getElementById('links-container');
const toggleDrawBtn = document.getElementById('toggle-draw');
const clearDrawBtn = document.getElementById('clear-draw');
const colorButtons = document.querySelectorAll('.color-button');
const pageSlider = document.getElementById('page-slider');
const pageNumberInput = document.getElementById('page-number-input');
const flashMessage = document.getElementById('flash-message');
// Get the toolbar (drawing tools) element for toggling visibility
const toolbar = document.querySelector('.toolbar');
let pdfDoc = null;
let currentPage = 1;
let totalPages = 0;
let isDrawing = false;
let drawMode = false;
let drawColor = 'red'; // Default drawing color
function clearLinks() {
linksContainer.innerHTML = '';
}
async function renderPage(pageNumber) {
const page = await pdfDoc.getPage(pageNumber);
const viewport = page.getViewport({ scale: 2 });
pdfCanvas.width = viewport.width;
pdfCanvas.height = viewport.height;
const renderContext = {
canvasContext: ctx,
viewport: viewport,
enableWebGL: true
};
ctx.clearRect(0, 0, pdfCanvas.width, pdfCanvas.height);
clearLinks();
await page.render(renderContext).promise;
const annotations = await page.getAnnotations();
annotations.forEach(annotation => {
if (annotation.url) {
const linkButton = document.createElement('a');
linkButton.href = annotation.url;
linkButton.target = '_blank';
linkButton.className = 'link-button';
linkButton.textContent = annotation.title || annotation.url;
linksContainer.appendChild(linkButton);
}
});
prevSlideBtn.disabled = currentPage === 1;
nextSlideBtn.disabled = currentPage === totalPages;
}
async function loadPdf() {
pdfDoc = await pdfjsLib.getDocument(pdfUrl).promise;
totalPages = pdfDoc.numPages;
pageSlider.max = totalPages;
renderPage(currentPage);
}
function showFlashMessage() {
flashMessage.style.display = 'block';
setTimeout(() => {
flashMessage.style.display = 'none';
}, 2000);
}
// Keyboard events
document.addEventListener('keydown', (event) => {
switch (event.key) {
case 'ArrowLeft':
if (currentPage > 1) {
currentPage--;
pageSlider.value = currentPage;
pageNumberInput.value = currentPage;
renderPage(currentPage);
}
break;
case 'ArrowRight':
if (currentPage < totalPages) {
currentPage++;
pageSlider.value = currentPage;
pageNumberInput.value = currentPage;
renderPage(currentPage);
}
break;
case 'd':
drawMode = true;
toggleDrawBtn.classList.add('active');
break;
case 's':
drawMode = false;
toggleDrawBtn.classList.remove('active');
break;
case 'e':
ctx.clearRect(0, 0, pdfCanvas.width, pdfCanvas.height);
renderPage(currentPage);
break;
case 'r':
drawColor = 'red';
break;
case 'g':
drawColor = 'green';
break;
case 'b':
drawColor = 'blue';
break;
case 'y':
drawColor = 'yellow';
break;
case 'h':
// Toggle the display of the toolbar (drawing tools)
if (toolbar.style.display === 'none') {
toolbar.style.display = 'flex';
} else {
toolbar.style.display = 'none';
}
break;
}
});
// Arrow button click events to change slides
prevSlideBtn.addEventListener('click', () => {
if (currentPage > 1) {
currentPage--;
pageSlider.value = currentPage;
pageNumberInput.value = currentPage;
renderPage(currentPage);
}
});
nextSlideBtn.addEventListener('click', () => {
if (currentPage < totalPages) {
currentPage++;
pageSlider.value = currentPage;
pageNumberInput.value = currentPage;
renderPage(currentPage);
}
});
// Slider and number input events
pageSlider.addEventListener('input', (event) => {
currentPage = parseInt(event.target.value);
pageNumberInput.value = currentPage;
renderPage(currentPage);
});
pageNumberInput.addEventListener('change', (event) => {
const page = parseInt(event.target.value);
if (page >= 1 && page <= totalPages) {
currentPage = page;
pageSlider.value = currentPage;
renderPage(currentPage);
} else {
showFlashMessage();
}
});
// ---- CHANGES START HERE: Scale mouse coordinates to match canvas's internal dimensions ----
pdfCanvas.addEventListener('mousedown', (e) => {
if (!drawMode) return;
isDrawing = true;
const rect = pdfCanvas.getBoundingClientRect();
const scaleX = pdfCanvas.width / rect.width;
const scaleY = pdfCanvas.height / rect.height;
const x = (e.clientX - rect.left) * scaleX;
const y = (e.clientY - rect.top) * scaleY;
ctx.beginPath();
ctx.moveTo(x, y);
});
pdfCanvas.addEventListener('mousemove', (e) => {
if (!isDrawing || !drawMode) return;
const rect = pdfCanvas.getBoundingClientRect();
const scaleX = pdfCanvas.width / rect.width;
const scaleY = pdfCanvas.height / rect.height;
const x = (e.clientX - rect.left) * scaleX;
const y = (e.clientY - rect.top) * scaleY;
ctx.lineTo(x, y);
ctx.strokeStyle = drawColor;
ctx.lineWidth = 2;
ctx.stroke();
});
pdfCanvas.addEventListener('mouseup', () => {
if (!drawMode) return;
isDrawing = false;
ctx.closePath();
});
// ---- CHANGES END HERE ----
loadPdf().catch(error => {
console.error('Error loading PDF:', error);
alert('Failed to load PDF. Please check the file path.');
});
// Fullscreen functionality
document.getElementById('fullscreen-btn').addEventListener('click', () => {
if (!document.fullscreenElement) {
document.documentElement.requestFullscreen();
} else {
if (document.exitFullscreen) {
document.exitFullscreen();
}
}
});
</script>
</body>
</html>
|