// script.js (() => { 'use strict'; // ----------------------------- // Accordion panel (shared) // ----------------------------- function setupAccordionPanel(options) { const { panelId, expandBtnId, collapseBtnId, reorderBtnId, storagePrefix } = options; const accPanel = document.getElementById(panelId); if (!accPanel) return; const expandBtn = document.getElementById(expandBtnId); const collapseBtn = document.getElementById(collapseBtnId); const reorderBtn = document.getElementById(reorderBtnId); const ACC_ORDER_KEY = `${storagePrefix}:accOrder:v1`; const ACC_OPEN_KEY = `${storagePrefix}:accOpen:v1`; const SCROLL_KEY = `${storagePrefix}:controlsScroll:v1`; const accSections = () => Array.from(accPanel.querySelectorAll('details[data-acc-id]')); function saveAccOpen() { const map = {}; accSections().forEach(d => (map[d.dataset.accId] = d.open ? 1 : 0)); try { localStorage.setItem(ACC_OPEN_KEY, JSON.stringify(map)); } catch {} } function restoreAccOpen() { try { const map = JSON.parse(localStorage.getItem(ACC_OPEN_KEY) || '{}'); accSections().forEach(d => { if (map[d.dataset.accId] === 1) d.open = true; if (map[d.dataset.accId] === 0) d.open = false; }); } catch {} } function saveAccOrder() { const order = accSections().map(d => d.dataset.accId); try { localStorage.setItem(ACC_ORDER_KEY, JSON.stringify(order)); } catch {} } function restoreAccOrder() { try { const order = JSON.parse(localStorage.getItem(ACC_ORDER_KEY) || '[]'); if (!Array.isArray(order) || order.length === 0) return; const map = new Map(accSections().map(d => [d.dataset.accId, d])); order.forEach(id => { const el = map.get(id); if (el) accPanel.appendChild(el); }); } catch {} } // --- drag/reorder within the panel let drag = { el: null, ph: null }; accPanel.addEventListener('click', e => { if (e.target.closest('.drag-handle')) e.preventDefault(); }); accPanel.addEventListener('pointerdown', e => { if (e.target.closest('.drag-handle')) e.stopPropagation(); }); accPanel.addEventListener('dragstart', e => { const handle = e.target.closest('.drag-handle'); if (!handle) return; drag.el = handle.closest('details[data-acc-id]'); drag.ph = document.createElement('div'); drag.ph.className = 'rounded-lg border border-dashed border-gray-300 bg-white/30'; drag.ph.style.height = drag.el.offsetHeight + 'px'; drag.el.classList.add('opacity-50'); drag.el.after(drag.ph); e.dataTransfer.effectAllowed = 'move'; }); accPanel.addEventListener('dragover', e => { if (!drag.el) return; e.preventDefault(); const y = e.clientY; let closest = null, dist = Infinity; for (const it of accSections().filter(x => x !== drag.el)) { const r = it.getBoundingClientRect(); const m = r.top + r.height / 2; const d = Math.abs(y - m); if (d < dist) { dist = d; closest = it; } } if (!closest) return; const r = closest.getBoundingClientRect(); if (y < r.top + r.height / 2) accPanel.insertBefore(drag.ph, closest); else accPanel.insertBefore(drag.ph, closest.nextSibling); }); function cleanupDrag() { if (!drag.el) return; drag.el.classList.remove('opacity-50'); if (drag.ph && drag.ph.parentNode) { accPanel.insertBefore(drag.el, drag.ph); drag.ph.remove(); } drag.el = drag.ph = null; saveAccOrder(); } accPanel.addEventListener('drop', e => { if (drag.el) { e.preventDefault(); cleanupDrag(); }}); accPanel.addEventListener('dragend', () => cleanupDrag()); accPanel.addEventListener('toggle', e => { if (e.target.matches('details[data-acc-id]')) saveAccOpen(); }, true); accPanel.addEventListener('scroll', () => { try { localStorage.setItem(SCROLL_KEY, String(accPanel.scrollTop)); } catch {} }); function restorePanelScroll() { accPanel.scrollTop = Number(localStorage.getItem(SCROLL_KEY)) || 0; } // Toolbar expandBtn?.addEventListener('click', () => { accSections().forEach(d => (d.open = true)); saveAccOpen(); }); collapseBtn?.addEventListener('click', () => { accSections().forEach(d => (d.open = false)); saveAccOpen(); }); reorderBtn?.addEventListener('click', () => { const isReordering = accPanel.classList.toggle('reorder-on'); reorderBtn.setAttribute('aria-pressed', String(isReordering)); }); // Init restoreAccOrder(); restoreAccOpen(); restorePanelScroll(); } // expose for classic.js window.setupAccordionPanel = setupAccordionPanel; // ----------------------------- // Organic app logic // ----------------------------- document.addEventListener('DOMContentLoaded', () => { // ====== GLOBAL SCALE ====== const PX_PER_INCH = 4; const SIZE_PRESETS = [24, 18, 11, 9, 5]; // ====== Shine ellipse tuning ====== const SHINE_OFFSET = 0.30, SHINE_RX = 0.40, SHINE_RY = 0.24, SHINE_ROT = -25, SHINE_ALPHA = 0.7; // ROT is now in degrees let view = { s: 1, tx: 0, ty: 0 }; const FIT_PADDING_PX = 15; // ====== Texture defaults ====== const TEXTURE_ZOOM_DEFAULT = 1.8; const TEXTURE_FOCUS_DEFAULT = { x: 0.5, y: 0.5 }; const SWATCH_TEXTURE_ZOOM = 2.5; const clamp = (v, min, max) => Math.max(min, Math.min(max, v)); const clamp01 = v => clamp(v, 0, 1); const QUERY_KEY = 'd'; // ====== Flatten palette & maps ====== const FLAT_COLORS = []; const NAME_BY_HEX = new Map(); const HEX_TO_FIRST_IDX = new Map(); const allowedSet = new Set(); (function buildFlat() { if (!Array.isArray(window.PALETTE)) return; window.PALETTE.forEach(group => { (group.colors || []).forEach(c => { if (!c?.hex) return; const item = { ...c, family: group.family }; item.imageZoom = Number.isFinite(c.imageZoom) ? Math.max(1, c.imageZoom) : TEXTURE_ZOOM_DEFAULT; item.imageFocus = { x: clamp01(c.imageFocusX ?? c.imageFocus?.x ?? TEXTURE_FOCUS_DEFAULT.x), y: clamp01(c.imageFocusY ?? c.imageFocus?.y ?? TEXTURE_FOCUS_DEFAULT.y) }; item._idx = FLAT_COLORS.length; FLAT_COLORS.push(item); const key = (c.hex || '').toLowerCase(); if (!NAME_BY_HEX.has(key)) NAME_BY_HEX.set(key, c.name); if (!HEX_TO_FIRST_IDX.has(key)) HEX_TO_FIRST_IDX.set(key, item._idx); allowedSet.add(key); }); }); })(); // ====== Image cache ====== const IMG_CACHE = new Map(); function getImage(path) { if (!path) return null; let img = IMG_CACHE.get(path); if (!img) { img = new Image(); img.decoding = 'async'; img.loading = 'eager'; img.src = path; img.onload = () => draw(); IMG_CACHE.set(path, img); } return img; } // ====== DOM ====== const canvas = document.getElementById('balloon-canvas'); const ctx = canvas?.getContext('2d'); // tool buttons const toolDrawBtn = document.getElementById('tool-draw'); const toolEraseBtn = document.getElementById('tool-erase'); const toolSelectBtn = document.getElementById('tool-select'); // panels/controls const eraserControls = document.getElementById('eraser-controls'); const selectControls = document.getElementById('select-controls'); const eraserSizeInput = document.getElementById('eraser-size'); const eraserSizeLabel = document.getElementById('eraser-size-label'); const deleteSelectedBtn = document.getElementById('delete-selected'); const duplicateSelectedBtn = document.getElementById('duplicate-selected'); const sizePresetGroup = document.getElementById('size-preset-group'); const toggleShineBtn = document.getElementById('toggle-shine-btn'); const paletteBox = document.getElementById('color-palette'); const usedPaletteBox = document.getElementById('used-palette'); const sortUsedToggle = document.getElementById('sort-used-toggle'); // replace colors panel const replaceFromSel = document.getElementById('replace-from'); const replaceToSel = document.getElementById('replace-to'); const replaceBtn = document.getElementById('replace-btn'); const replaceMsg = document.getElementById('replace-msg'); // IO const clearCanvasBtn = document.getElementById('clear-canvas-btn'); const saveJsonBtn = document.getElementById('save-json-btn'); const loadJsonInput = document.getElementById('load-json-input'); // delegate export buttons (shared IDs across tabs) document.body.addEventListener('click', e => { if (e.target.id === 'export-png-btn') exportPng(); else if (e.target.id === 'export-svg-btn') exportSvg(); }); const generateLinkBtn = document.getElementById('generate-link-btn'); const shareLinkOutput = document.getElementById('share-link-output'); const copyMessage = document.getElementById('copy-message'); // messages const messageModal = document.getElementById('message-modal'); const modalText = document.getElementById('modal-text'); const modalCloseBtn = document.getElementById('modal-close-btn'); // layout const controlsPanel = document.getElementById('controls-panel'); const canvasPanel = document.getElementById('canvas-panel'); const expandBtn = document.getElementById('expand-workspace-btn'); const fullscreenBtn = document.getElementById('fullscreen-btn'); if (!canvas || !ctx) return; // nothing to do if organic UI isn't on page // ====== State ====== let balloons = []; let selectedColorIdx = 0; let currentDiameterInches = 11; let currentRadius = inchesToRadiusPx(currentDiameterInches); let isShineEnabled = true; // will be initialized from localStorage let dpr = 1; let mode = 'draw'; let eraserRadius = parseInt(eraserSizeInput?.value || '40', 10); let mouseInside = false; let mousePos = { x: 0, y: 0 }; let selectedBalloonId = null; let usedSortDesc = true; // ====== Helpers ====== const normalizeHex = h => (h || '').toLowerCase(); function inchesToRadiusPx(diam) { return (diam * PX_PER_INCH) / 2; } function radiusToSizeIndex(r) { let best = 0, bestDiff = Infinity; for (let i = 0; i < SIZE_PRESETS.length; i++) { const diff = Math.abs(inchesToRadiusPx(SIZE_PRESETS[i]) - r); if (diff < bestDiff) { best = i; bestDiff = diff; } } return best; } function showModal(msg) { if (!messageModal) return; modalText.textContent = msg; messageModal.classList.remove('hidden'); } function hideModal() { if (!messageModal) return; messageModal.classList.add('hidden'); } function showCopyMessage() { if (!copyMessage) return; copyMessage.classList.add('show'); setTimeout(() => copyMessage.classList.remove('show'), 2000); } function getMousePos(e) { const r = canvas.getBoundingClientRect(); return { x: (e.clientX - r.left) / view.s - view.tx, y: (e.clientY - r.top) / view.s - view.ty }; } // ====== Global shine sync (shared with Classic) window.syncAppShine = function(isEnabled) { isShineEnabled = isEnabled; // mirror both UIs const organicBtn = document.getElementById('toggle-shine-btn'); const classicCb = document.getElementById('classic-shine-enabled'); if (organicBtn) organicBtn.textContent = isEnabled ? 'Turn Off Shine' : 'Turn On Shine'; if (classicCb) classicCb.checked = isEnabled; try { localStorage.setItem('app:shineEnabled:v1', JSON.stringify(isEnabled)); } catch {} // push into Classic engine if available if (window.ClassicDesigner?.api?.setShineEnabled) { window.ClassicDesigner.api.setShineEnabled(isEnabled); } // redraw both tabs (cheap + robust) try { draw?.(); } catch {} try { window.ClassicDesigner?.redraw?.(); } catch {} }; function setMode(next) { mode = next; toolDrawBtn?.setAttribute('aria-pressed', String(mode === 'draw')); toolEraseBtn?.setAttribute('aria-pressed', String(mode === 'erase')); toolSelectBtn?.setAttribute('aria-pressed', String(mode === 'select')); eraserControls?.classList.toggle('hidden', mode !== 'erase'); selectControls?.classList.toggle('hidden', mode !== 'select'); canvas.style.cursor = (mode === 'erase') ? 'none' : (mode === 'select' ? 'pointer' : 'crosshair'); draw(); persist(); } function updateSelectButtons() { const has = !!selectedBalloonId; if (deleteSelectedBtn) deleteSelectedBtn.disabled = !has; if (duplicateSelectedBtn) duplicateSelectedBtn.disabled = !has; } // ====== Pointer Events ====== let pointerDown = false; canvas.addEventListener('pointerdown', e => { e.preventDefault(); canvas.setPointerCapture?.(e.pointerId); mouseInside = true; mousePos = getMousePos(e); if (e.altKey) { pickColorAt(mousePos.x, mousePos.y); return; } if (mode === 'erase') { pointerDown = true; eraseAt(mousePos.x, mousePos.y); return; } if (mode === 'select') { selectAt(mousePos.x, mousePos.y); return; } // draw mode: add addBalloon(mousePos.x, mousePos.y); }, { passive: false }); canvas.addEventListener('pointermove', e => { mousePos = getMousePos(e); if (mode === 'erase') { if (pointerDown) eraseAt(mousePos.x, mousePos.y); else draw(); } }, { passive: true }); canvas.addEventListener('pointerup', e => { pointerDown = false; canvas.releasePointerCapture?.(e.pointerId); }, { passive: true }); canvas.addEventListener('pointerleave', () => { mouseInside = false; if (mode === 'erase') draw(); }, { passive: true }); // ====== Canvas & Drawing ====== function resizeCanvas() { const rect = canvas.getBoundingClientRect(); dpr = Math.max(1, window.devicePixelRatio || 1); canvas.width = Math.round(rect.width * dpr); canvas.height = Math.round(rect.height * dpr); ctx.setTransform(dpr, 0, 0, dpr, 0, 0); fitView(); draw(); } function clearCanvasArea() { ctx.clearRect(0, 0, canvas.width / dpr, canvas.height / dpr); } function draw() { clearCanvasArea(); ctx.save(); ctx.scale(view.s, view.s); ctx.translate(view.tx, view.ty); balloons.forEach(b => { if (b.image) { const img = getImage(b.image); if (img && img.complete && img.naturalWidth > 0) { const meta = FLAT_COLORS[b.colorIdx] || {}; const zoom = Math.max(1, meta.imageZoom ?? TEXTURE_ZOOM_DEFAULT); const fx = clamp01(meta.imageFocus?.x ?? TEXTURE_FOCUS_DEFAULT.x); const fy = clamp01(meta.imageFocus?.y ?? TEXTURE_FOCUS_DEFAULT.y); const srcW = img.naturalWidth / zoom; const srcH = img.naturalHeight / zoom; const srcX = clamp(fx * img.naturalWidth - srcW/2, 0, img.naturalWidth - srcW); const srcY = clamp(fy * img.naturalHeight - srcH/2, 0, img.naturalHeight - srcH); ctx.save(); ctx.beginPath(); ctx.arc(b.x, b.y, b.radius, 0, Math.PI * 2); ctx.clip(); ctx.drawImage(img, srcX, srcY, srcW, srcH, b.x - b.radius, b.y - b.radius, b.radius * 2, b.radius * 2); ctx.restore(); } else { // fallback solid ctx.beginPath(); ctx.arc(b.x, b.y, b.radius, 0, Math.PI * 2); ctx.fillStyle = b.color; ctx.shadowColor = 'rgba(0,0,0,0.2)'; ctx.shadowBlur = 10; ctx.fill(); ctx.shadowBlur = 0; } } else { // solid fill ctx.beginPath(); ctx.arc(b.x, b.y, b.radius, 0, Math.PI * 2); ctx.fillStyle = b.color; ctx.shadowColor = 'rgba(0,0,0,0.2)'; ctx.shadowBlur = 10; ctx.fill(); ctx.shadowBlur = 0; } if (isShineEnabled) { const sx = b.x - b.radius * SHINE_OFFSET; const sy = b.y - b.radius * SHINE_OFFSET; const rx = b.radius * SHINE_RX; const ry = b.radius * SHINE_RY; const rotRad = SHINE_ROT * Math.PI / 180; ctx.save(); ctx.shadowColor = 'rgba(0,0,0,0.1)'; ctx.shadowBlur = 3; // SHINE_BLUR ctx.beginPath(); if (ctx.ellipse) { ctx.ellipse(sx, sy, rx, ry, rotRad, 0, Math.PI * 2); } else { ctx.translate(sx, sy); ctx.rotate(rotRad); ctx.scale(rx / ry, 1); ctx.arc(0, 0, ry, 0, Math.PI * 2); } ctx.fillStyle = `rgba(255,255,255,${SHINE_ALPHA})`; ctx.fill(); ctx.restore(); } }); // selection ring if (selectedBalloonId) { const b = balloons.find(bb => bb.id === selectedBalloonId); if (b) { ctx.save(); ctx.beginPath(); ctx.arc(b.x, b.y, b.radius + 3, 0, Math.PI * 2); ctx.setLineDash([6, 4]); ctx.lineWidth = 2 / view.s; ctx.strokeStyle = '#2563eb'; ctx.stroke(); ctx.setLineDash([]); ctx.restore(); } } // eraser preview if (mode === 'erase' && mouseInside) { ctx.save(); ctx.beginPath(); ctx.arc(mousePos.x, mousePos.y, eraserRadius, 0, Math.PI * 2); ctx.lineWidth = 1.5 / view.s; ctx.strokeStyle = 'rgba(31,41,55,0.8)'; ctx.setLineDash([4 / view.s, 4 / view.s]); ctx.stroke(); ctx.restore(); } ctx.restore(); } // --- Workspace expansion + Fullscreen --- let expanded = false; function setExpanded(on) { expanded = on; controlsPanel?.classList.toggle('hidden', expanded); if (canvasPanel) { canvasPanel.classList.toggle('lg:w-full', expanded); canvasPanel.classList.toggle('lg:w-2/3', !expanded); } if (expanded) { canvas.classList.remove('aspect-video'); canvas.style.height = '85vh'; } else { canvas.classList.add('aspect-video'); canvas.style.height = ''; } resizeCanvas(); if (expandBtn) expandBtn.textContent = expanded ? 'Exit expanded view' : 'Expand workspace'; persist(); } function isFullscreen() { return !!(document.fullscreenElement || document.webkitFullscreenElement); } async function toggleFullscreenPage() { try { if (!isFullscreen()) { await document.documentElement.requestFullscreen(); } else { await document.exitFullscreen(); } } catch { // if blocked, just use expanded setExpanded(true); } } const onFsChange = () => { if (fullscreenBtn) fullscreenBtn.textContent = isFullscreen() ? 'Exit Fullscreen' : 'Fullscreen'; resizeCanvas(); }; expandBtn?.addEventListener('click', () => setExpanded(!expanded)); fullscreenBtn?.addEventListener('click', toggleFullscreenPage); document.addEventListener('fullscreenchange', onFsChange); document.addEventListener('webkitfullscreenchange', onFsChange); new ResizeObserver(() => resizeCanvas()).observe(canvas.parentElement); canvas.style.touchAction = 'none'; // ====== State Persistence ====== const APP_STATE_KEY = 'obd:state:v3'; function saveAppState() { // Note: isShineEnabled is managed globally. const state = { balloons, selectedColorIdx, currentDiameterInches, eraserRadius, mode, view, usedSortDesc, expanded }; try { localStorage.setItem(APP_STATE_KEY, JSON.stringify(state)); } catch {} } const persist = (() => { let t; return () => { clearTimeout(t); t = setTimeout(saveAppState, 120); }; })(); function loadAppState() { try { const s = JSON.parse(localStorage.getItem(APP_STATE_KEY) || '{}'); if (Array.isArray(s.balloons)) balloons = s.balloons; if (typeof s.selectedColorIdx === 'number') selectedColorIdx = s.selectedColorIdx; if (typeof s.currentDiameterInches === 'number') { currentDiameterInches = s.currentDiameterInches; currentRadius = inchesToRadiusPx(currentDiameterInches); } if (typeof s.eraserRadius === 'number') { eraserRadius = s.eraserRadius; if (eraserSizeInput) eraserSizeInput.value = eraserRadius; if (eraserSizeLabel) eraserSizeLabel.textContent = eraserRadius; } if (typeof s.mode === 'string') mode = s.mode; if (s.view && typeof s.view.s === 'number') view = s.view; if (typeof s.usedSortDesc === 'boolean') { usedSortDesc = s.usedSortDesc; if (sortUsedToggle) sortUsedToggle.textContent = usedSortDesc ? 'Sort: Most → Least' : 'Sort: Least → Most'; } if (typeof s.expanded === 'boolean') setExpanded(s.expanded); } catch {} } loadAppState(); // ====== UI Rendering (Palettes) ====== function renderAllowedPalette() { if (!paletteBox) return; paletteBox.innerHTML = ''; (window.PALETTE || []).forEach(group => { const title = document.createElement('div'); title.className = 'family-title'; title.textContent = group.family; paletteBox.appendChild(title); const row = document.createElement('div'); row.className = 'swatch-row'; (group.colors || []).forEach(c => { const idx = FLAT_COLORS.find(fc => fc.name === c.name && fc.hex === c.hex && fc.family === group.family)?._idx ?? HEX_TO_FIRST_IDX.get(normalizeHex(c.hex)); const sw = document.createElement('div'); sw.className = 'swatch'; if (c.image) { const meta = FLAT_COLORS[idx] || {}; sw.style.backgroundImage = `url("${c.image}")`; sw.style.backgroundSize = `${100 * SWATCH_TEXTURE_ZOOM}%`; sw.style.backgroundPosition = `${(meta.imageFocus?.x ?? 0.5) * 100}% ${(meta.imageFocus?.y ?? 0.5) * 100}%`; } else { sw.style.backgroundColor = c.hex; } if (idx === selectedColorIdx) sw.classList.add('active'); sw.title = c.name; sw.addEventListener('click', () => { selectedColorIdx = idx ?? 0; renderAllowedPalette(); persist(); }); row.appendChild(sw); }); paletteBox.appendChild(row); }); } function getUsedColors() { const map = new Map(); balloons.forEach(b => { const key = normalizeHex(b.color); if (!allowedSet.has(key)) return; if (!map.has(key)) { const meta = FLAT_COLORS[HEX_TO_FIRST_IDX.get(key)] || {}; map.set(key, { hex: key, count: 0, image: meta.image, name: meta.name }); } map.get(key).count++; }); const arr = [...map.values()]; arr.sort((a, b) => (usedSortDesc ? (b.count - a.count) : (a.count - b.count))); return arr; } function renderUsedPalette() { if (!usedPaletteBox) return; usedPaletteBox.innerHTML = ''; const used = getUsedColors(); if (used.length === 0) { usedPaletteBox.innerHTML = '