File size: 25,082 Bytes
83e35a7 |
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 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 |
/**
* Interactive Comic Editor
* Allows dragging speech bubbles and editing text
*/
class ComicEditor {
constructor(containerId) {
this.container = document.getElementById(containerId);
this.bubbles = [];
this.selectedBubble = null;
this.isDragging = false;
this.dragOffset = { x: 0, y: 0 };
this.isEditing = false;
this.init();
}
init() {
// Add editor styles
this.addStyles();
// Load comic data
this.loadComicData();
// Setup event listeners
this.setupEventListeners();
// Add toolbar
this.createToolbar();
}
addStyles() {
const style = document.createElement('style');
style.textContent = `
.comic-editor-container {
position: relative;
user-select: none;
background: #f0f0f0;
padding: 20px;
border-radius: 10px;
}
.comic-page {
position: relative;
background: white;
margin: 20px auto;
box-shadow: 0 4px 20px rgba(0,0,0,0.1);
width: 800px; /* exact width */
height: 1080px; /* exact height */
}
.comic-panel {
position: absolute;
border: 2px solid #333;
overflow: hidden;
background: white;
}
.comic-panel img {
width: 100%;
height: 100%;
object-fit: contain;
background: #000;
}
.speech-bubble {
position: absolute;
background: white;
border: 3px solid #333;
border-radius: 20px;
padding: 15px;
cursor: move;
min-width: 100px;
min-height: 50px;
box-shadow: 2px 2px 5px rgba(0,0,0,0.1);
transition: transform 0.1s;
z-index: 10;
}
.speech-bubble:hover {
transform: scale(1.02);
box-shadow: 4px 4px 10px rgba(0,0,0,0.2);
}
.speech-bubble.selected {
border-color: #007bff;
box-shadow: 0 0 0 3px rgba(0,123,255,0.3);
z-index: 100;
}
.speech-bubble.dragging {
opacity: 0.8;
z-index: 1000;
}
.bubble-text {
font-family: 'Comic Sans MS', cursive;
font-size: 14px;
font-weight: bold;
text-align: center;
line-height: 1.4;
color: #000;
word-wrap: break-word;
cursor: text;
}
.bubble-text.editing {
background: rgba(255,255,255,0.9);
border: 1px dashed #007bff;
padding: 5px;
outline: none;
}
.bubble-tail {
position: absolute;
bottom: -15px;
left: 20px;
width: 0;
height: 0;
border-left: 15px solid transparent;
border-right: 5px solid transparent;
border-top: 20px solid #333;
transform: rotate(-20deg);
}
.bubble-tail::after {
content: '';
position: absolute;
bottom: 3px;
left: -12px;
width: 0;
height: 0;
border-left: 12px solid transparent;
border-right: 4px solid transparent;
border-top: 16px solid white;
}
.editor-toolbar {
position: fixed;
top: 20px;
right: 20px;
background: white;
border: 2px solid #333;
border-radius: 10px;
padding: 15px;
box-shadow: 0 4px 20px rgba(0,0,0,0.1);
z-index: 1000;
}
.toolbar-btn {
display: block;
width: 100%;
padding: 10px 15px;
margin: 5px 0;
background: #007bff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-weight: bold;
transition: background 0.2s;
}
.toolbar-btn:hover {
background: #0056b3;
}
.toolbar-btn.danger {
background: #dc3545;
}
.toolbar-btn.danger:hover {
background: #c82333;
}
.toolbar-btn.success {
background: #28a745;
}
.toolbar-btn.success:hover {
background: #218838;
}
.toolbar-btn.download {
background: #ff66b3; /* pink */
color: white;
}
.toolbar-btn.download:hover {
background: #ff4da6;
}
.resize-handle {
position: absolute;
width: 10px;
height: 10px;
background: #007bff;
border: 1px solid white;
border-radius: 50%;
cursor: nwse-resize;
}
.resize-handle.se {
bottom: -5px;
right: -5px;
}
.coordinates {
position: absolute;
bottom: -25px;
left: 0;
font-size: 10px;
color: #666;
background: white;
padding: 2px 5px;
border-radius: 3px;
display: none;
}
.selected .coordinates {
display: block;
}
.edit-hint {
position: fixed;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
background: #333;
color: white;
padding: 10px 20px;
border-radius: 20px;
font-size: 14px;
z-index: 1000;
opacity: 0;
transition: opacity 0.3s;
}
.edit-hint.show {
opacity: 1;
}
`;
document.head.appendChild(style);
}
loadComicData() {
// Load existing comic data or create new
const savedData = localStorage.getItem('comicEditorData');
if (savedData) {
const data = JSON.parse(savedData);
this.renderComic(data);
} else {
// Load from server or create default
this.loadFromServer();
}
}
loadFromServer() {
// Load from server
fetch('/load_comic')
.then(response => response.json())
.then(data => {
if (data.error) {
console.error('Error loading comic:', data.error);
this.createDefaultComic();
} else {
this.renderComic(data);
}
})
.catch(error => {
console.error('Failed to load comic:', error);
this.createDefaultComic();
});
}
createDefaultComic() {
// Create a default comic if loading fails
const sampleData = {
pages: [{
width: 800,
height: 600,
panels: [
{
x: 10, y: 10, width: 380, height: 280,
image: '/frames/frame000.png'
},
{
x: 410, y: 10, width: 380, height: 280,
image: '/frames/frame001.png'
}
],
bubbles: [
{
id: 'bubble1',
x: 50, y: 50, width: 150, height: 60,
text: 'Add your text here!',
panelIndex: 0
}
]
}]
};
this.renderComic(sampleData);
}
renderComic(data) {
this.container.innerHTML = '';
this.container.className = 'comic-editor-container';
data.pages.forEach((page, pageIndex) => {
const pageDiv = document.createElement('div');
pageDiv.className = 'comic-page';
pageDiv.style.width = page.width + 'px';
pageDiv.style.height = page.height + 'px';
pageDiv.dataset.pageIndex = pageIndex;
// Render panels
page.panels.forEach((panel, panelIndex) => {
const panelDiv = document.createElement('div');
panelDiv.className = 'comic-panel';
panelDiv.style.left = panel.x + 'px';
panelDiv.style.top = panel.y + 'px';
panelDiv.style.width = panel.width + 'px';
panelDiv.style.height = panel.height + 'px';
panelDiv.dataset.panelIndex = panelIndex;
const img = document.createElement('img');
img.src = panel.image;
panelDiv.appendChild(img);
pageDiv.appendChild(panelDiv);
});
// Render bubbles
page.bubbles.forEach(bubble => {
this.createBubble(bubble, pageDiv);
});
this.container.appendChild(pageDiv);
});
}
createBubble(bubbleData, pageDiv) {
const bubble = document.createElement('div');
bubble.className = 'speech-bubble';
bubble.id = bubbleData.id || 'bubble_' + Date.now();
bubble.style.left = bubbleData.x + 'px';
bubble.style.top = bubbleData.y + 'px';
bubble.style.width = bubbleData.width + 'px';
bubble.style.height = bubbleData.height + 'px';
// Add text
const text = document.createElement('div');
text.className = 'bubble-text';
text.textContent = bubbleData.text || 'Click to edit';
text.contentEditable = false;
bubble.appendChild(text);
// Add tail
const tail = document.createElement('div');
tail.className = 'bubble-tail';
bubble.appendChild(tail);
// Add resize handle
const resizeHandle = document.createElement('div');
resizeHandle.className = 'resize-handle se';
bubble.appendChild(resizeHandle);
// Add coordinates display
const coords = document.createElement('div');
coords.className = 'coordinates';
bubble.appendChild(coords);
// Store data
bubble.dataset.bubbleData = JSON.stringify(bubbleData);
pageDiv.appendChild(bubble);
this.bubbles.push(bubble);
// Setup bubble events
this.setupBubbleEvents(bubble);
}
setupEventListeners() {
// Document-wide mouse events
document.addEventListener('mousemove', (e) => this.handleMouseMove(e));
document.addEventListener('mouseup', (e) => this.handleMouseUp(e));
// Keyboard shortcuts
document.addEventListener('keydown', (e) => {
if (e.key === 'Delete' && this.selectedBubble && !this.isEditing) {
this.deleteBubble(this.selectedBubble);
}
if (e.key === 'Escape') {
this.deselectBubble();
}
});
// Click outside to deselect
this.container.addEventListener('click', (e) => {
if (e.target === this.container || e.target.classList.contains('comic-page')) {
this.deselectBubble();
}
});
}
setupBubbleEvents(bubble) {
const text = bubble.querySelector('.bubble-text');
const resizeHandle = bubble.querySelector('.resize-handle');
// Drag start
bubble.addEventListener('mousedown', (e) => {
if (e.target === text && this.isEditing) return;
if (e.target === resizeHandle) return;
this.startDragging(bubble, e);
});
// Click to select
bubble.addEventListener('click', (e) => {
e.stopPropagation();
this.selectBubble(bubble);
});
// Double-click to edit text
text.addEventListener('dblclick', (e) => {
e.stopPropagation();
this.startEditingText(bubble, text);
});
// Handle text editing
text.addEventListener('blur', () => {
if (this.isEditing) {
this.stopEditingText(bubble, text);
}
});
text.addEventListener('keydown', (e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
text.blur();
}
});
// Resize handle
resizeHandle.addEventListener('mousedown', (e) => {
e.stopPropagation();
this.startResizing(bubble, e);
});
}
startDragging(bubble, e) {
this.isDragging = true;
this.selectedBubble = bubble;
bubble.classList.add('dragging');
const rect = bubble.getBoundingClientRect();
const containerRect = this.container.getBoundingClientRect();
this.dragOffset = {
x: e.clientX - rect.left,
y: e.clientY - rect.top
};
this.selectBubble(bubble);
}
handleMouseMove(e) {
if (!this.isDragging || !this.selectedBubble) return;
const containerRect = this.container.getBoundingClientRect();
const pageRect = this.selectedBubble.parentElement.getBoundingClientRect();
let newX = e.clientX - pageRect.left - this.dragOffset.x;
let newY = e.clientY - pageRect.top - this.dragOffset.y;
// Constrain to page bounds
const maxX = pageRect.width - this.selectedBubble.offsetWidth;
const maxY = pageRect.height - this.selectedBubble.offsetHeight;
newX = Math.max(0, Math.min(newX, maxX));
newY = Math.max(0, Math.min(newY, maxY));
this.selectedBubble.style.left = newX + 'px';
this.selectedBubble.style.top = newY + 'px';
this.updateCoordinates(this.selectedBubble);
}
handleMouseUp(e) {
if (this.isDragging && this.selectedBubble) {
this.selectedBubble.classList.remove('dragging');
this.isDragging = false;
this.saveBubblePosition(this.selectedBubble);
}
}
selectBubble(bubble) {
// Deselect previous
this.deselectBubble();
// Select new
this.selectedBubble = bubble;
bubble.classList.add('selected');
this.updateCoordinates(bubble);
this.showHint('Double-click to edit text • Drag to move • Delete key to remove');
}
deselectBubble() {
if (this.selectedBubble) {
this.selectedBubble.classList.remove('selected');
this.selectedBubble = null;
}
this.hideHint();
}
startEditingText(bubble, textElement) {
this.isEditing = true;
textElement.contentEditable = true;
textElement.classList.add('editing');
textElement.focus();
// Select all text
const range = document.createRange();
range.selectNodeContents(textElement);
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
this.showHint('Press Enter to save • Shift+Enter for new line');
}
stopEditingText(bubble, textElement) {
this.isEditing = false;
textElement.contentEditable = false;
textElement.classList.remove('editing');
// Save the text
this.saveBubbleText(bubble, textElement.textContent);
this.hideHint();
}
deleteBubble(bubble) {
if (confirm('Delete this speech bubble?')) {
bubble.remove();
const index = this.bubbles.indexOf(bubble);
if (index > -1) {
this.bubbles.splice(index, 1);
}
this.selectedBubble = null;
this.saveComicData();
}
}
updateCoordinates(bubble) {
const coords = bubble.querySelector('.coordinates');
coords.textContent = `x: ${parseInt(bubble.style.left)}, y: ${parseInt(bubble.style.top)}`;
}
createToolbar() {
const toolbar = document.createElement('div');
toolbar.className = 'editor-toolbar';
// Add bubble button
const addBtn = document.createElement('button');
addBtn.className = 'toolbar-btn';
addBtn.textContent = '➕ Add Bubble';
addBtn.onclick = () => this.addNewBubble();
toolbar.appendChild(addBtn);
// Save button
const saveBtn = document.createElement('button');
saveBtn.className = 'toolbar-btn success';
saveBtn.textContent = '💾 Save Comic';
saveBtn.onclick = () => this.saveComic();
toolbar.appendChild(saveBtn);
// Export button
const exportBtn = document.createElement('button');
exportBtn.className = 'toolbar-btn download';
exportBtn.textContent = '⬇️ Download';
exportBtn.onclick = () => this.downloadPages();
toolbar.appendChild(exportBtn);
// Reset button
const resetBtn = document.createElement('button');
resetBtn.className = 'toolbar-btn danger';
resetBtn.textContent = '🔄 Reset';
resetBtn.onclick = () => this.resetComic();
toolbar.appendChild(resetBtn);
document.body.appendChild(toolbar);
}
addNewBubble() {
const page = this.container.querySelector('.comic-page');
if (!page) return;
const newBubble = {
id: 'bubble_' + Date.now(),
x: 100,
y: 100,
width: 150,
height: 60,
text: 'New bubble!'
};
this.createBubble(newBubble, page);
this.saveComicData();
}
saveBubblePosition(bubble) {
this.saveComicData();
}
saveBubbleText(bubble, text) {
const data = JSON.parse(bubble.dataset.bubbleData || '{}');
data.text = text;
bubble.dataset.bubbleData = JSON.stringify(data);
this.saveComicData();
}
saveComicData() {
const data = {
pages: []
};
this.container.querySelectorAll('.comic-page').forEach(page => {
const pageData = {
width: parseInt(page.style.width),
height: parseInt(page.style.height),
panels: [],
bubbles: []
};
// Save panel data
page.querySelectorAll('.comic-panel').forEach(panel => {
pageData.panels.push({
x: parseInt(panel.style.left),
y: parseInt(panel.style.top),
width: parseInt(panel.style.width),
height: parseInt(panel.style.height),
image: panel.querySelector('img').src
});
});
// Save bubble data
page.querySelectorAll('.speech-bubble').forEach(bubble => {
pageData.bubbles.push({
id: bubble.id,
x: parseInt(bubble.style.left),
y: parseInt(bubble.style.top),
width: parseInt(bubble.style.width),
height: parseInt(bubble.style.height),
text: bubble.querySelector('.bubble-text').textContent
});
});
data.pages.push(pageData);
});
localStorage.setItem('comicEditorData', JSON.stringify(data));
this.showHint('Comic saved!');
}
saveComic() {
this.saveComicData();
// Send to server
fetch('/save_comic', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(this.getComicData())
})
.then(response => response.json())
.then(data => {
this.showHint('Comic saved to server!');
})
.catch(error => {
console.error('Error:', error);
this.showHint('Error saving to server!');
});
}
exportComic() {
const data = this.getComicData();
const json = JSON.stringify(data, null, 2);
// Create download
const blob = new Blob([json], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'comic_data.json';
a.click();
URL.revokeObjectURL(url);
this.showHint('Comic exported!');
}
resetComic() {
if (confirm('Reset all changes? This cannot be undone!')) {
localStorage.removeItem('comicEditorData');
this.loadFromServer();
this.showHint('Comic reset!');
}
}
getComicData() {
return JSON.parse(localStorage.getItem('comicEditorData') || '{}');
}
showHint(message) {
let hint = document.querySelector('.edit-hint');
if (!hint) {
hint = document.createElement('div');
hint.className = 'edit-hint';
document.body.appendChild(hint);
}
hint.textContent = message;
hint.classList.add('show');
clearTimeout(this.hintTimeout);
this.hintTimeout = setTimeout(() => {
hint.classList.remove('show');
}, 3000);
}
hideHint() {
const hint = document.querySelector('.edit-hint');
if (hint) {
hint.classList.remove('show');
}
}
startResizing(bubble, e) {
e.preventDefault();
const startX = e.clientX;
const startY = e.clientY;
const startWidth = parseInt(bubble.style.width);
const startHeight = parseInt(bubble.style.height);
const handleResize = (e) => {
const newWidth = startWidth + (e.clientX - startX);
const newHeight = startHeight + (e.clientY - startY);
bubble.style.width = Math.max(100, newWidth) + 'px';
bubble.style.height = Math.max(50, newHeight) + 'px';
this.updateCoordinates(bubble);
};
const stopResize = () => {
document.removeEventListener('mousemove', handleResize);
document.removeEventListener('mouseup', stopResize);
this.saveComicData();
};
document.addEventListener('mousemove', handleResize);
document.addEventListener('mouseup', stopResize);
}
/** Download each page as PNG using html2canvas */
downloadPages() {
const pages = this.container.querySelectorAll('.comic-page');
if (!pages.length) return;
pages.forEach((page, idx) => {
html2canvas(page, {width: 800, height: 1080, scale: 2, useCORS: true, allowTaint: true}).then(canvas => {
canvas.toBlob(blob => {
const a = document.createElement('a');
a.download = `comic_page_${idx+1}.png`;
a.href = URL.createObjectURL(blob);
a.click();
URL.revokeObjectURL(a.href);
}, 'image/png');
});
});
}
}
// Initialize when page loads
document.addEventListener('DOMContentLoaded', () => {
if (document.getElementById('comic-editor')) {
window.comicEditor = new ComicEditor('comic-editor');
}
}); |