File size: 11,254 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 |
"""
Package comic as self-contained HTML file with all editing features
"""
import os
import base64
import json
from pathlib import Path
def create_portable_comic(pages_json_path="output/pages.json", output_path="output/comic_portable.html"):
"""
Create a single HTML file that contains everything:
- All images embedded as base64
- All editing functionality
- No external dependencies
"""
# Read pages data
with open(pages_json_path, 'r') as f:
pages_data = json.load(f)
# Convert all images to base64
embedded_images = {}
frames_dir = "frames/final"
for page in pages_data:
for panel in page.get('panels', []):
img_name = panel.get('image', '')
if img_name and img_name not in embedded_images:
img_path = os.path.join(frames_dir, img_name)
if os.path.exists(img_path):
with open(img_path, 'rb') as img_file:
img_data = base64.b64encode(img_file.read()).decode('utf-8')
embedded_images[img_name] = f"data:image/png;base64,{img_data}"
# Create self-contained HTML
html_content = f'''<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Portable Comic Editor</title>
<style>
body {{ margin: 0; padding: 20px; background: #f0f0f0; font-family: Arial, sans-serif; }}
.header {{ text-align: center; margin-bottom: 20px; }}
.header h1 {{ color: #333; }}
.save-notice {{ background: #4CAF50; color: white; padding: 10px; border-radius: 5px; margin: 10px 0; }}
.comic-container {{ max-width: 1200px; margin: 0 auto; }}
.comic-page {{ background: white; padding: 20px; box-shadow: 0 0 10px rgba(0,0,0,0.1); margin-bottom: 30px; }}
.comic-grid {{ display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; gap: 10px; height: 600px; }}
.panel {{ position: relative; border: 2px solid #333; overflow: hidden; }}
.panel img {{ width: 100%; height: 100%; object-fit: contain; background: #000; }}
.speech-bubble {{
position: absolute;
background: white;
border: 3px solid #333;
border-radius: 15px;
padding: 12px;
max-width: 200px;
font-size: 14px;
font-weight: bold;
box-shadow: 3px 3px 8px rgba(0,0,0,0.4);
z-index: 10;
text-align: center;
color: #333;
cursor: move;
transition: transform 0.2s, box-shadow 0.2s;
}}
.speech-bubble:hover {{
transform: scale(1.02);
box-shadow: 3px 3px 12px rgba(0,0,0,0.6);
}}
.speech-bubble.editing {{ cursor: text; }}
.speech-bubble textarea {{
width: 100%;
height: 100%;
border: none;
background: transparent;
font: inherit;
text-align: center;
resize: none;
outline: 2px solid #4CAF50;
padding: 5px;
}}
.edit-controls {{
position: fixed;
bottom: 20px;
right: 20px;
background: rgba(0,0,0,0.85);
color: white;
padding: 15px 20px;
border-radius: 10px;
font-size: 14px;
z-index: 1000;
box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}}
.edit-controls h4 {{ margin: 0 0 10px 0; color: #4CAF50; }}
.edit-controls p {{ margin: 5px 0; opacity: 0.9; }}
.edit-controls button {{
margin-top: 10px;
padding: 8px 15px;
border: none;
border-radius: 5px;
cursor: pointer;
font-weight: bold;
width: 100%;
}}
.save-html-btn {{ background: #4CAF50; color: white; }}
.save-html-btn:hover {{ background: #45a049; }}
</style>
</head>
<body>
<div class="header">
<h1>🎨 Portable Comic Editor</h1>
<div class="save-notice">
💡 This is a self-contained file! Save this HTML to keep your comic and edits.
<br>You can open and edit it anytime in any browser.
</div>
</div>
<div class="comic-container">
<div id="comic-pages"></div>
</div>
<div class="edit-controls">
<h4>✏️ Interactive Editor</h4>
<p>• <strong>Drag</strong> bubbles to move</p>
<p>• <strong>Double-click</strong> to edit text</p>
<p>• <strong>Save</strong> this HTML file to keep edits!</p>
<button class="save-html-btn" onclick="saveThisFile()">💾 Download Updated HTML</button>
</div>
<script>
// Embedded pages data
const pagesData = {json.dumps(pages_data)};
// Embedded images
const embeddedImages = {json.dumps(embedded_images)};
// Render comic
function renderComic() {{
const container = document.getElementById('comic-pages');
container.innerHTML = '';
pagesData.forEach((page, pageIndex) => {{
const pageDiv = document.createElement('div');
pageDiv.className = 'comic-page';
const gridDiv = document.createElement('div');
gridDiv.className = 'comic-grid';
page.panels.forEach((panel, panelIndex) => {{
const panelDiv = document.createElement('div');
panelDiv.className = 'panel';
const img = document.createElement('img');
img.src = embeddedImages[panel.image] || panel.image;
panelDiv.appendChild(img);
gridDiv.appendChild(panelDiv);
}});
pageDiv.appendChild(gridDiv);
// Add bubbles
page.bubbles.forEach((bubble, bubbleIndex) => {{
const bubbleDiv = document.createElement('div');
bubbleDiv.className = 'speech-bubble';
bubbleDiv.style.left = bubble.bubble_offset_x + 'px';
bubbleDiv.style.top = bubble.bubble_offset_y + 'px';
bubbleDiv.innerText = bubble.dialog;
const targetPanel = gridDiv.children[Math.floor(bubbleIndex / 2)];
if (targetPanel) {{
targetPanel.appendChild(bubbleDiv);
}}
}});
container.appendChild(pageDiv);
}});
// Initialize editor
setTimeout(initializeEditor, 100);
}}
// Editor functionality (same as before)
let currentEditBubble = null;
let draggedBubble = null;
let offset = {{x: 0, y: 0}};
function initializeEditor() {{
document.querySelectorAll('.speech-bubble').forEach(bubble => {{
bubble.addEventListener('dblclick', (e) => {{
e.stopPropagation();
editBubbleText(bubble);
}});
bubble.addEventListener('mousedown', startDrag);
}});
document.addEventListener('mousemove', drag);
document.addEventListener('mouseup', stopDrag);
}}
function editBubbleText(bubble) {{
if (currentEditBubble) return;
currentEditBubble = bubble;
bubble.classList.add('editing');
const text = bubble.innerText;
const textarea = document.createElement('textarea');
textarea.value = text;
bubble.innerHTML = '';
bubble.appendChild(textarea);
textarea.focus();
textarea.select();
textarea.addEventListener('keydown', (e) => {{
if (e.key === 'Enter' && !e.shiftKey) {{
e.preventDefault();
saveBubbleText(bubble, textarea.value);
}}
if (e.key === 'Escape') {{
saveBubbleText(bubble, text);
}}
}});
textarea.addEventListener('blur', () => {{
setTimeout(() => {{
if (currentEditBubble === bubble) {{
saveBubbleText(bubble, textarea.value);
}}
}}, 100);
}});
}}
function saveBubbleText(bubble, text) {{
bubble.innerText = text;
bubble.classList.remove('editing');
currentEditBubble = null;
}}
function startDrag(e) {{
if (e.target.tagName === 'TEXTAREA') return;
const bubble = e.target.closest('.speech-bubble');
if (!bubble || currentEditBubble) return;
draggedBubble = bubble;
const rect = bubble.getBoundingClientRect();
offset.x = e.clientX - rect.left;
offset.y = e.clientY - rect.top;
bubble.style.opacity = '0.9';
bubble.style.zIndex = '100';
e.preventDefault();
}}
function drag(e) {{
if (!draggedBubble) return;
const parent = draggedBubble.parentElement;
const parentRect = parent.getBoundingClientRect();
let x = e.clientX - parentRect.left - offset.x;
let y = e.clientY - parentRect.top - offset.y;
x = Math.max(0, Math.min(x, parentRect.width - draggedBubble.offsetWidth));
y = Math.max(0, Math.min(y, parentRect.height - draggedBubble.offsetHeight));
draggedBubble.style.left = x + 'px';
draggedBubble.style.top = y + 'px';
}}
function stopDrag() {{
if (draggedBubble) {{
draggedBubble.style.opacity = '';
draggedBubble.style.zIndex = '';
draggedBubble = null;
}}
}}
function saveThisFile() {{
// Get current state
const currentHTML = document.documentElement.outerHTML;
// Create blob and download
const blob = new Blob([currentHTML], {{type: 'text/html'}});
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'comic_editable_' + new Date().getTime() + '.html';
a.click();
URL.revokeObjectURL(url);
}}
// Initialize on load
renderComic();
</script>
</body>
</html>'''
# Write the file
with open(output_path, 'w', encoding='utf-8') as f:
f.write(html_content)
return output_path |