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 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
| <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>墨水屏风格图片处理工具</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; } body { background: linear-gradient(135deg, #1a2a6c, #b21f1f, #fdbb2d); color: #fff; min-height: 100vh; padding: 20px; } .container { max-width: 1200px; margin: 0 auto; background: rgba(0, 0, 0, 0.7); border-radius: 15px; padding: 25px; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5); } header { text-align: center; margin-bottom: 30px; padding-bottom: 15px; border-bottom: 1px solid rgba(255, 255, 255, 0.2); } h1 { font-size: 2.5rem; margin-bottom: 10px; background: linear-gradient(to right, #ff9966, #ff5e62); -webkit-background-clip: text; -webkit-text-fill-color: transparent; } .subtitle { font-size: 1.1rem; opacity: 0.8; } .app-container { display: flex; flex-wrap: wrap; gap: 30px; } .control-panel { flex: 1; min-width: 300px; background: rgba(30, 30, 40, 0.8); padding: 25px; border-radius: 12px; box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3); } .preview-panel { flex: 2; min-width: 500px; display: flex; flex-direction: column; gap: 25px; } .section { margin-bottom: 25px; } h2 { font-size: 1.4rem; margin-bottom: 15px; color: #ff9966; display: flex; align-items: center; gap: 10px; } h2 i { font-size: 1.2rem; } .upload-area { border: 2px dashed #ff9966; border-radius: 10px; padding: 30px; text-align: center; cursor: pointer; transition: all 0.3s; background: rgba(255, 153, 102, 0.1); } .upload-area:hover { background: rgba(255, 153, 102, 0.2); } .upload-icon { font-size: 3rem; margin-bottom: 15px; color: #ff9966; } .slider-container { margin: 20px 0; } .slider-label { display: flex; justify-content: space-between; margin-bottom: 8px; } .slider { width: 100%; height: 8px; -webkit-appearance: none; background: rgba(255, 255, 255, 0.1); border-radius: 5px; outline: none; } .slider::-webkit-slider-thumb { -webkit-appearance: none; width: 20px; height: 20px; border-radius: 50%; background: #ff5e62; cursor: pointer; transition: all 0.2s; } .slider::-webkit-slider-thumb:hover { background: #ff9966; transform: scale(1.2); } .ratio-presets { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 15px; } .ratio-btn { padding: 8px 15px; background: rgba(255, 255, 255, 0.1); border: 1px solid rgba(255, 255, 255, 0.3); border-radius: 5px; color: white; cursor: pointer; transition: all 0.2s; } .ratio-btn:hover { background: rgba(255, 153, 102, 0.3); } .ratio-btn.active { background: #ff5e62; border-color: #ff5e62; } .preview-container { background: rgba(30, 30, 40, 0.8); border-radius: 12px; padding: 20px; box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3); flex: 1; display: flex; flex-direction: column; } .preview-title { display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px; } .preview-area { flex: 1; background: rgba(0, 0, 0, 0.3); border-radius: 8px; display: flex; justify-content: center; align-items: center; overflow: hidden; min-height: 300px; } .preview-image { max-width: 100%; max-height: 100%; display: none; } .effects-container { display: flex; gap: 20px; flex-wrap: wrap; } .effect-box { flex: 1; min-width: 200px; background: rgba(30, 30, 40, 0.8); border-radius: 12px; padding: 20px; box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3); text-align: center; } .effect-title { font-size: 1.2rem; margin-bottom: 15px; color: #ff9966; } .effect-preview { width: 100%; height: 200px; background: rgba(0, 0, 0, 0.3); border-radius: 8px; display: flex; justify-content: center; align-items: center; margin-bottom: 15px; overflow: hidden; } .effect-image { max-width: 100%; max-height: 100%; display: none; } .btn { padding: 12px 25px; background: linear-gradient(to right, #ff9966, #ff5e62); border: none; border-radius: 8px; color: white; font-weight: bold; cursor: pointer; transition: all 0.3s; display: inline-flex; align-items: center; justify-content: center; gap: 8px; } .btn:hover { transform: translateY(-3px); box-shadow: 0 5px 15px rgba(255, 94, 98, 0.4); } .btn:active { transform: translateY(0); } .btn-full { width: 100%; margin-top: 10px; } .hidden { display: none; } .placeholder-text { color: rgba(255, 255, 255, 0.5); font-style: italic; } .info-box { background: rgba(255, 153, 102, 0.1); border-left: 4px solid #ff9966; padding: 15px; border-radius: 5px; margin-top: 20px; } @media (max-width: 900px) { .app-container { flex-direction: column; } .control-panel, .preview-panel { min-width: 100%; } } </style> </head> <body> <div class="container"> <header> <h1>墨水屏风格图片处理工具</h1> <p class="subtitle">上传图片,按比例裁切,生成黑白红三色墨水屏效果</p> </header> <div class="app-container"> <div class="control-panel"> <div class="section"> <h2><i>📁</i> 图片上传</h2> <div class="upload-area" id="uploadArea"> <div class="upload-icon">📁</div> <p>点击或拖拽图片到此处上传</p> <p class="placeholder-text">支持 JPG, PNG, GIF 格式</p> </div> <input type="file" id="fileInput" class="hidden" accept="image/*"> </div> <div class="section"> <h2><i>✂️</i> 图片裁切</h2> <div class="slider-container"> <div class="slider-label"> <span>宽度比例:</span> <span id="widthValue">100%</span> </div> <input type="range" min="10" max="100" value="100" class="slider" id="widthSlider"> </div> <div class="slider-container"> <div class="slider-label"> <span>高度比例:</span> <span id="heightValue">100%</span> </div> <input type="range" min="10" max="100" value="100" class="slider" id="heightSlider"> </div> <div class="ratio-presets"> <div class="ratio-btn active" data-ratio="1:1">1:1 (正方形)</div> <div class="ratio-btn" data-ratio="16:9">16:9 (宽屏)</div> <div class="ratio-btn" data-ratio="4:3">4:3 (标准)</div> <div class="ratio-btn" data-ratio="3:2">3:2 (照片)</div> <div class="ratio-btn" data-ratio="9:16">9:16 (手机)</div> </div> </div> <div class="section"> <h2><i>🎨</i> 墨水屏效果</h2> <div class="slider-container"> <div class="slider-label"> <span>红色阈值:</span> <span id="redThresholdValue">50%</span> </div> <input type="range" min="1" max="100" value="50" class="slider" id="redThreshold"> </div> <div class="slider-container"> <div class="slider-label"> <span>黑白对比度:</span> <span id="contrastValue">50%</span> </div> <input type="range" min="1" max="100" value="50" class="slider" id="contrast"> </div> <button class="btn btn-full" id="generateBtn"> <i>🔄</i> 生成墨水屏效果 </button> <button class="btn btn-full" id="downloadBtn" style="margin-top: 15px;"> <i>💾</i> 下载墨水屏图片 </button> </div> <div class="info-box"> <h3>墨水屏效果说明</h3> <p>此工具模拟只能显示黑色、白色和红色的墨水屏设备。</p> <p>通过调整红色阈值和对比度,可以控制哪些区域显示为红色,哪些区域显示为黑白。</p> </div> </div> <div class="preview-panel"> <div class="preview-container"> <div class="preview-title"> <h2><i>👁️</i> 原图预览</h2> <button class="btn" id="resetBtn"> <i>🔄</i> 重置 </button> </div> <div class="preview-area"> <p class="placeholder-text" id="previewPlaceholder">上传的图片将显示在这里</p> <img id="previewImage" class="preview-image" alt="预览图片"> </div> </div> <div class="effects-container"> <div class="effect-box"> <h3 class="effect-title">墨水屏效果预览</h3> <div class="effect-preview"> <p class="placeholder-text">墨水屏效果预览</p> <img id="einkImage" class="effect-image" alt="墨水屏效果"> </div> <p style="font-size: 0.9rem; margin-top: 10px;">模拟三色墨水屏显示效果,仅使用黑、白、红三种颜色</p> </div> <div class="effect-box"> <h3 class="effect-title">效果说明</h3> <div class="effect-preview" style="background: #000; display: flex; flex-direction: column; justify-content: center; align-items: center; color: white;"> <div style="display: flex; flex-direction: column; gap: 10px; text-align: center;"> <div style="background: #000; padding: 5px; border-radius: 3px;">黑色区域</div> <div style="background: #fff; color: #000; padding: 5px; border-radius: 3px;">白色区域</div> <div style="background: #f00; padding: 5px; border-radius: 3px;">红色区域</div> </div> </div> <p style="font-size: 0.9rem; margin-top: 10px;">调整红色阈值和对比度参数可以优化显示效果</p> </div> </div> </div> </div> </div>
<script> const fileInput = document.getElementById('fileInput'); const uploadArea = document.getElementById('uploadArea'); const widthSlider = document.getElementById('widthSlider'); const heightSlider = document.getElementById('heightSlider'); const widthValue = document.getElementById('widthValue'); const heightValue = document.getElementById('heightValue'); const redThreshold = document.getElementById('redThreshold'); const redThresholdValue = document.getElementById('redThresholdValue'); const contrast = document.getElementById('contrast'); const contrastValue = document.getElementById('contrastValue'); const ratioBtns = document.querySelectorAll('.ratio-btn'); const generateBtn = document.getElementById('generateBtn'); const resetBtn = document.getElementById('resetBtn'); const downloadBtn = document.getElementById('downloadBtn'); const previewImage = document.getElementById('previewImage'); const einkImage = document.getElementById('einkImage'); const previewPlaceholder = document.getElementById('previewPlaceholder'); let originalImage = null; let croppedImage = null; let einkCanvas = null; uploadArea.addEventListener('click', () => { fileInput.click(); }); uploadArea.addEventListener('dragover', (e) => { e.preventDefault(); uploadArea.style.background = 'rgba(255, 153, 102, 0.3)'; }); uploadArea.addEventListener('dragleave', () => { uploadArea.style.background = 'rgba(255, 153, 102, 0.1)'; }); uploadArea.addEventListener('drop', (e) => { e.preventDefault(); uploadArea.style.background = 'rgba(255, 153, 102, 0.1)'; if (e.dataTransfer.files.length) { fileInput.files = e.dataTransfer.files; handleImageUpload(e.dataTransfer.files[0]); } }); fileInput.addEventListener('change', (e) => { if (e.target.files.length) { handleImageUpload(e.target.files[0]); } }); function handleImageUpload(file) { if (!file.type.match('image.*')) { alert('请上传图片文件!'); return; } const reader = new FileReader(); reader.onload = (e) => { originalImage = new Image(); originalImage.onload = () => { previewPlaceholder.style.display = 'none'; previewImage.style.display = 'block'; previewImage.src = e.target.result; widthSlider.value = 100; heightSlider.value = 100; widthValue.textContent = '100%'; heightValue.textContent = '100%'; applyCrop(); }; originalImage.src = e.target.result; }; reader.readAsDataURL(file); } widthSlider.addEventListener('input', () => { widthValue.textContent = `${widthSlider.value}%`; applyCrop(); }); heightSlider.addEventListener('input', () => { heightValue.textContent = `${heightSlider.value}%`; applyCrop(); }); redThreshold.addEventListener('input', () => { redThresholdValue.textContent = `${redThreshold.value}%`; generateEinkEffect(); }); contrast.addEventListener('input', () => { contrastValue.textContent = `${contrast.value}%`; generateEinkEffect(); }); function applyCrop() { if (!originalImage) return; const widthRatio = widthSlider.value / 100; const heightRatio = heightSlider.value / 100; const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d'); const newWidth = originalImage.width * widthRatio; const newHeight = originalImage.height * heightRatio; const startX = (originalImage.width - newWidth) / 2; const startY = (originalImage.height - newHeight) / 2; canvas.width = newWidth; canvas.height = newHeight; ctx.drawImage( originalImage, startX, startY, newWidth, newHeight, 0, 0, newWidth, newHeight ); previewImage.src = canvas.toDataURL('image/png'); croppedImage = new Image(); croppedImage.onload = () => { generateEinkEffect(); }; croppedImage.src = canvas.toDataURL('image/png'); } ratioBtns.forEach(btn => { btn.addEventListener('click', () => { ratioBtns.forEach(b => b.classList.remove('active')); btn.classList.add('active'); const ratio = btn.getAttribute('data-ratio').split(':'); const width = parseInt(ratio[0]); const height = parseInt(ratio[1]); if (originalImage) { const imgRatio = originalImage.width / originalImage.height; const targetRatio = width / height; if (imgRatio > targetRatio) { const newHeight = (originalImage.height * targetRatio / imgRatio) / originalImage.height * 100; heightSlider.value = newHeight; widthSlider.value = 100; heightValue.textContent = `${Math.round(newHeight)}%`; widthValue.textContent = '100%'; } else { const newWidth = (originalImage.width * imgRatio / targetRatio) / originalImage.width * 100; widthSlider.value = newWidth; heightSlider.value = 100; widthValue.textContent = `${Math.round(newWidth)}%`; heightValue.textContent = '100%'; } applyCrop(); } }); }); function generateEinkEffect() { if (!croppedImage) return; einkCanvas = document.createElement('canvas'); const ctx = einkCanvas.getContext('2d'); einkCanvas.width = croppedImage.width; einkCanvas.height = croppedImage.height; ctx.drawImage(croppedImage, 0, 0); const imageData = ctx.getImageData(0, 0, einkCanvas.width, einkCanvas.height); const data = imageData.data; const redThresholdValue = redThreshold.value / 100; const contrastValue = contrast.value / 100 * 2; for (let i = 0; i < data.length; i += 4) { const r = data[i]; const g = data[i+1]; const b = data[i+2]; let brightness = (r * 0.299 + g * 0.587 + b * 0.114) / 255; brightness = (brightness - 0.5) * contrastValue + 0.5; brightness = Math.max(0, Math.min(1, brightness)); const redIntensity = r / 255; const greenIntensity = g / 255; const blueIntensity = b / 255; const isRed = redIntensity > greenIntensity * 1.5 && redIntensity > blueIntensity * 1.5 && redIntensity > redThresholdValue; if (isRed) { data[i] = 255; data[i+1] = 0; data[i+2] = 0; } else { const bwValue = brightness > 0.5 ? 255 : 0; data[i] = bwValue; data[i+1] = bwValue; data[i+2] = bwValue; } } ctx.putImageData(imageData, 0, 0); einkImage.style.display = 'block'; einkImage.src = einkCanvas.toDataURL('image/png'); } generateBtn.addEventListener('click', generateEinkEffect); resetBtn.addEventListener('click', () => { if (originalImage) { widthSlider.value = 100; heightSlider.value = 100; widthValue.textContent = '100%'; heightValue.textContent = '100%'; redThreshold.value = 50; redThresholdValue.textContent = '50%'; contrast.value = 50; contrastValue.textContent = '50%'; ratioBtns[0].classList.add('active'); for (let i = 1; i < ratioBtns.length; i++) { ratioBtns[i].classList.remove('active'); } previewImage.src = originalImage.src; croppedImage = originalImage; generateEinkEffect(); } }); downloadBtn.addEventListener('click', () => { if (!einkCanvas) { alert('请先生成墨水屏效果图!'); return; } const link = document.createElement('a'); link.download = '墨水屏效果图.png'; link.href = einkCanvas.toDataURL('image/png'); document.body.appendChild(link); link.click(); document.body.removeChild(link); }); </script> </body> </html>
|