教師標籤設計工具 .dragging { opacity: 0.5; transform: rotate(5deg); } .drop-zone { min-height: 400px; border: 2px dashed #e5e7eb; transition: all 0.3s ease; } .drop-zone.drag-over { border-color: #3b82f6; background-color: #eff6ff; } .element-item { cursor: grab; transition: all 0.2s ease; } .element-item:hover { transform: translateY(-2px); box-shadow: 0 4px 12px rgba(0,0,0,0.15); } .label-element { position: absolute; cursor: move; border: 2px solid transparent; transition: border-color 0.2s ease; } .label-element:hover { border-color: #3b82f6; } .label-element.selected { border-color: #ef4444; box-shadow: 0 0 0 2px rgba(239, 68, 68, 0.2); } .resize-handle { position: absolute; width: 8px; height: 8px; background: #ef4444; border-radius: 50%; right: -4px; bottom: -4px; cursor: se-resize; } .layer-item { transition: all 0.2s ease; border-left: 3px solid transparent; } .layer-item:hover { background-color: #f8fafc; border-left-color: #3b82f6; } .layer-item.selected { background-color: #eff6ff; border-left-color: #2563eb; } .layer-item.hidden { opacity: 0.5; } .layer-item.locked { background-color: #fef3c7; } .layer-controls button { transition: all 0.15s ease; } .layer-controls button:hover { transform: scale(1.1); } .layer-thumbnail { width: 32px; height: 32px; border: 1px solid #e5e7eb; border-radius: 4px; display: flex; align-items: center; justify-content: center; background: white; font-size: 12px; overflow: hidden; }

🏷️ 教師標籤設計工具

拖拉元素到設計區域,創建您的專屬標籤

📦 設計元素

文字

📝 大標題
📄 內容文字
🏷️ 小標籤

圖形

⭕ 圓形
⬜ 方形
⭐ 星形

表情符號

😊
⭐
🎉
📚
✨
🏆
💡
🌟

圖片

📷 上傳圖片

背景設定

100%

📋 範本選擇

選擇範本... 📄 A4 紙張 (21 × 29.7 cm) 📮 標準信封 (10.2 × 15.2 cm) 📷 4R 相片 (10.2 × 15.2 cm) 💳 名片 (8.9 × 5.1 cm) 🏷️ 標籤貼紙 (7.6 × 12.7 cm) 📋 A5 紙張 (14.8 × 21 cm) 🎫 小卡片 (5.4 × 8.6 cm)

🎯 設計區域

像素 (px) 公分 (cm) 英吋 (inch)
px
px
🎨

拖拉元素到這裡開始設計

📚 圖層管理

📄

尚未建立任何圖層

拖拉元素到設計區域開始創建圖層

🎨 屬性設定

選擇一個元素來編輯屬性

let selectedElement = null; let draggedElement = null; let elementCounter = 0; let isDragging = false; let isResizing = false; let startX, startY, startWidth, startHeight; let layers = new Map(); // 儲存圖層資訊 // 拖拉功能 document.querySelectorAll('.element-item').forEach(item => { item.addEventListener('dragstart', (e) => { draggedElement = e.target; e.target.classList.add('dragging'); }); item.addEventListener('dragend', (e) => { e.target.classList.remove('dragging'); }); }); // 設計區域拖放 const designArea = document.getElementById('design-area'); designArea.addEventListener('dragover', (e) => { e.preventDefault(); designArea.classList.add('drag-over'); }); designArea.addEventListener('dragleave', () => { designArea.classList.remove('drag-over'); }); designArea.addEventListener('drop', (e) => { e.preventDefault(); designArea.classList.remove('drag-over'); if (!draggedElement) return; const rect = designArea.getBoundingClientRect(); const x = e.clientX - rect.left; const y = e.clientY - rect.top; createElement(draggedElement, x, y); draggedElement = null; }); // 創建元素 function createElement(sourceElement, x, y, imageData = null) { const type = sourceElement.dataset.type || 'image'; const element = document.createElement('div'); element.className = 'label-element'; element.style.left = x + 'px'; element.style.top = y + 'px'; const elementId = 'element-' + (++elementCounter); element.dataset.id = elementId; // 添加調整大小控制點 const resizeHandle = document.createElement('div'); resizeHandle.className = 'resize-handle'; element.appendChild(resizeHandle); let layerName = ''; let layerIcon = ''; if (type === 'text') { const content = sourceElement.dataset.content; const style = sourceElement.dataset.style; element.innerHTML = `${content}`; element.appendChild(resizeHandle); layerName = content; layerIcon = '📝'; } else if (type === 'emoji') { const content = sourceElement.dataset.content; element.innerHTML = `${content}`; element.appendChild(resizeHandle); layerName = `表情符號 ${content}`; layerIcon = content; } else if (type === 'shape') { const shape = sourceElement.dataset.shape; if (shape === 'circle') { element.innerHTML = '
'; layerName = '圓形'; layerIcon = '⭕'; } else if (shape === 'rectangle') { element.innerHTML = '
'; layerName = '矩形'; layerIcon = '⬜'; } else if (shape === 'star') { element.innerHTML = '
⭐
'; layerName = '星形'; layerIcon = '⭐'; } element.appendChild(resizeHandle); } else if (type === 'image' && imageData) { element.innerHTML = ``; element.appendChild(resizeHandle); layerName = '圖片'; layerIcon = '🖼️'; } // 創建圖層資訊 layers.set(elementId, { element: element, name: layerName, icon: layerIcon, visible: true, locked: false, type: type }); // 添加點擊選擇功能 element.addEventListener('click', (e) => { e.stopPropagation(); const layerInfo = layers.get(elementId); if (!layerInfo.locked) { selectElement(element); } }); // 添加拖拉移動功能 makeDraggable(element); // 添加調整大小功能 makeResizable(element, resizeHandle); designArea.appendChild(element); selectElement(element); updateLayersPanel(); } // 專用圖片創建函數 function createImageElement(sourceElement, x, y, imageData, width, height) { const element = document.createElement('div'); element.className = 'label-element'; element.style.left = x + 'px'; element.style.top = y + 'px'; element.style.width = width + 'px'; element.style.height = height + 'px'; const elementId = 'element-' + (++elementCounter); element.dataset.id = elementId; // 添加調整大小控制點 const resizeHandle = document.createElement('div'); resizeHandle.className = 'resize-handle'; element.innerHTML = ``; element.appendChild(resizeHandle); // 創建圖層資訊 layers.set(elementId, { element: element, name: '圖片', icon: '🖼️', visible: true, locked: false, type: 'image' }); // 添加點擊選擇功能 element.addEventListener('click', (e) => { e.stopPropagation(); const layerInfo = layers.get(elementId); if (!layerInfo.locked) { selectElement(element); } }); // 添加拖拉移動功能 makeDraggable(element); // 添加調整大小功能 makeResizable(element, resizeHandle); designArea.appendChild(element); selectElement(element); updateLayersPanel(); } // 圖片上傳功能 function uploadImage() { document.getElementById('image-upload').click(); } // 處理圖片上傳 document.getElementById('image-upload').addEventListener('change', function(e) { const file = e.target.files[0]; if (file && file.type.startsWith('image/')) { const reader = new FileReader(); reader.onload = function(event) { // 創建臨時圖片來獲取原始尺寸 const tempImg = new Image(); tempImg.onload = function() { // 計算適合的尺寸(最大200px,保持比例) const maxSize = 200; let newWidth = this.naturalWidth; let newHeight = this.naturalHeight; if (newWidth > maxSize || newHeight > maxSize) { const ratio = Math.min(maxSize / newWidth, maxSize / newHeight); newWidth = Math.round(newWidth * ratio); newHeight = Math.round(newHeight * ratio); } // 在設計區域中央創建圖片元素 const centerX = designArea.offsetWidth / 2 - newWidth / 2; const centerY = designArea.offsetHeight / 2 - newHeight / 2; createImageElement({dataset: {type: 'image'}}, centerX, centerY, event.target.result, newWidth, newHeight); }; tempImg.src = event.target.result; }; reader.readAsDataURL(file); } // 清空文件輸入,允許重複上傳同一文件 e.target.value = ''; }); // 背景顏色控制 document.getElementById('canvas-bg-color').addEventListener('input', function() { const color = this.value; const opacity = document.getElementById('canvas-bg-opacity').value / 100; updateCanvasBackground(color, opacity); }); // 背景透明度控制 document.getElementById('canvas-bg-opacity').addEventListener('input', function() { const opacity = this.value / 100; const color = document.getElementById('canvas-bg-color').value; document.getElementById('opacity-value').textContent = this.value; updateCanvasBackground(color, opacity); }); // 更新畫布背景 function updateCanvasBackground(color, opacity) { const rgb = hexToRgb(color); designArea.style.backgroundColor = `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${opacity})`; } // 清除畫布背景 function clearCanvasBackground() { designArea.style.backgroundColor = 'transparent'; document.getElementById('canvas-bg-opacity').value = 0; document.getElementById('opacity-value').textContent = '0'; } // 十六進制轉RGB function hexToRgb(hex) { const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); return result ? { r: parseInt(result[1], 16), g: parseInt(result[2], 16), b: parseInt(result[3], 16) } : null; } // 更新圖層面板 function updateLayersPanel() { const container = document.getElementById('layers-container'); if (layers.size === 0) { container.innerHTML = `
📄

尚未建立任何圖層

拖拉元素到設計區域開始創建圖層

`; return; } // 按 z-index 排序圖層(最上層在最前面) const sortedLayers = Array.from(layers.entries()).sort((a, b) => { const zIndexA = parseInt(a[1].element.style.zIndex) || 0; const zIndexB = parseInt(b[1].element.style.zIndex) || 0; return zIndexB - zIndexA; }); container.innerHTML = sortedLayers.map(([id, layer]) => { const isSelected = selectedElement && selectedElement.dataset.id === id; return `
${layer.icon}
${layer.name}
${layer.type === 'text' ? '文字' : layer.type === 'image' ? '圖片' : layer.type === 'shape' ? '圖形' : layer.type === 'emoji' ? '表情符號' : '元素'}
`; }).join(''); // 綁定圖層點擊事件 container.querySelectorAll('.layer-item').forEach(item => { item.addEventListener('click', (e) => { if (e.target.tagName === 'BUTTON') return; // 忽略按鈕點擊 const layerId = item.dataset.layerId; const layer = layers.get(layerId); if (layer && !layer.locked) { selectElement(layer.element); } }); }); } // 切換圖層可見性 function toggleLayerVisibility(layerId) { const layer = layers.get(layerId); if (!layer) return; layer.visible = !layer.visible; layer.element.style.display = layer.visible ? 'block' : 'none'; // 如果隱藏的是當前選中的元素,取消選擇 if (!layer.visible && selectedElement === layer.element) { selectedElement = null; document.getElementById('properties-panel').innerHTML = '

選擇一個元素來編輯屬性

'; } updateLayersPanel(); } // 切換圖層鎖定狀態 function toggleLayerLock(layerId) { const layer = layers.get(layerId); if (!layer) return; layer.locked = !layer.locked; // 如果鎖定的是當前選中的元素,取消選擇 if (layer.locked && selectedElement === layer.element) { selectedElement = null; document.getElementById('properties-panel').innerHTML = '

選擇一個元素來編輯屬性

'; } updateLayersPanel(); } // 刪除圖層 function deleteLayer(layerId) { const layer = layers.get(layerId); if (!layer) return; if (confirm(`確定要刪除圖層「${layer.name}」嗎?`)) { // 如果刪除的是當前選中的元素,取消選擇 if (selectedElement === layer.element) { selectedElement = null; document.getElementById('properties-panel').innerHTML = '

選擇一個元素來編輯屬性

'; } layer.element.remove(); layers.delete(layerId); updateLayersPanel(); } } // 展開所有圖層 document.getElementById('expand-all-layers').addEventListener('click', () => { layers.forEach(layer => { if (!layer.visible) { layer.visible = true; layer.element.style.display = 'block'; } }); updateLayersPanel(); }); // 收合所有圖層 document.getElementById('collapse-all-layers').addEventListener('click', () => { layers.forEach(layer => { if (layer.visible) { layer.visible = false; layer.element.style.display = 'none'; } }); // 取消選擇 selectedElement = null; document.getElementById('properties-panel').innerHTML = '

選擇一個元素來編輯屬性

'; updateLayersPanel(); }); // 使元素可拖拉 function makeDraggable(element) { let pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0; element.addEventListener('mousedown', (e) => { if (e.target.classList.contains('resize-handle')) return; e.preventDefault(); pos3 = e.clientX; pos4 = e.clientY; isDragging = true; document.addEventListener('mousemove', dragElement); document.addEventListener('mouseup', stopDragging); }); function dragElement(e) { if (!isDragging) return; e.preventDefault(); pos1 = pos3 - e.clientX; pos2 = pos4 - e.clientY; pos3 = e.clientX; pos4 = e.clientY; const newTop = element.offsetTop - pos2; const newLeft = element.offsetLeft - pos1; // 限制在設計區域內 const maxLeft = designArea.offsetWidth - element.offsetWidth; const maxTop = designArea.offsetHeight - element.offsetHeight; element.style.top = Math.max(0, Math.min(newTop, maxTop)) + 'px'; element.style.left = Math.max(0, Math.min(newLeft, maxLeft)) + 'px'; } function stopDragging() { isDragging = false; document.removeEventListener('mousemove', dragElement); document.removeEventListener('mouseup', stopDragging); } } // 使元素可調整大小 function makeResizable(element, handle) { handle.addEventListener('mousedown', (e) => { e.preventDefault(); e.stopPropagation(); isResizing = true; startX = e.clientX; startY = e.clientY; startWidth = parseInt(document.defaultView.getComputedStyle(element).width, 10); startHeight = parseInt(document.defaultView.getComputedStyle(element).height, 10); document.addEventListener('mousemove', doResize); document.addEventListener('mouseup', stopResize); }); function doResize(e) { if (!isResizing) return; const newWidth = startWidth + e.clientX - startX; const newHeight = startHeight + e.clientY - startY; element.style.width = Math.max(20, newWidth) + 'px'; element.style.height = Math.max(20, newHeight) + 'px'; } function stopResize() { isResizing = false; document.removeEventListener('mousemove', doResize); document.removeEventListener('mouseup', stopResize); } } // 選擇元素 function selectElement(element) { // 移除其他選擇 document.querySelectorAll('.label-element').forEach(el => { el.classList.remove('selected'); }); element.classList.add('selected'); selectedElement = element; updatePropertiesPanel(element); updateLayersPanel(); // 更新圖層面板以顯示選中狀態 } // 更新屬性面板 function updatePropertiesPanel(element) { const panel = document.getElementById('properties-panel'); const textElement = element.querySelector('span'); const shapeElement = element.querySelector('div'); const imageElement = element.querySelector('img'); // 獲取當前樣式 const currentColor = textElement ? (textElement.style.color || '#1f2937') : '#3b82f6'; const currentFontFamily = textElement ? (textElement.style.fontFamily || 'Arial') : 'Arial'; const isBold = textElement ? textElement.classList.contains('font-bold') : false; const isItalic = textElement ? textElement.classList.contains('italic') : false; const isUnderline = textElement ? textElement.classList.contains('underline') : false; panel.innerHTML = `

📍 位置

📏 尺寸

${textElement ? `

📝 文字內容

🔤 字體

Arial 微軟正黑體 思源黑體 Times New Roman Georgia Courier New Verdana
極小 (12px) 小 (14px) 中 (16px) 大 (18px) 特大 (20px) 超大 (24px) 巨大 (30px) 極大 (36px)

✨ 文字樣式

🎨 顏色

` : ''} ${imageElement ? `

🖼️ 圖片

100%
無濾鏡 黑白 復古 模糊 增亮 變暗 高對比 高飽和
4px
` : ''} ${shapeElement && !textElement && !imageElement ? `

🔷 圖形

` : ''}

📚 層級

`; // 綁定事件 document.getElementById('pos-x').addEventListener('input', updatePosition); document.getElementById('pos-y').addEventListener('input', updatePosition); document.getElementById('element-width').addEventListener('input', updateSize); document.getElementById('element-height').addEventListener('input', updateSize); if (textElement) { document.getElementById('text-content').addEventListener('input', updateText); document.getElementById('text-color').addEventListener('input', updateTextColor); document.getElementById('text-color-hex').addEventListener('input', updateTextColorHex); document.getElementById('font-size').addEventListener('change', updateFontSize); document.getElementById('font-family').addEventListener('change', updateFontFamily); document.getElementById('bg-color').addEventListener('input', updateBackgroundColor); } if (imageElement) { document.getElementById('image-opacity').addEventListener('input', updateImageOpacity); document.getElementById('image-filter').addEventListener('change', updateImageFilter); document.getElementById('image-border-radius').addEventListener('input', updateImageBorderRadius); document.getElementById('image-border-width').addEventListener('input', updateImageBorderWidth); document.getElementById('image-border-color').addEventListener('input', updateImageBorderColor); } if (shapeElement && !textElement && !imageElement) { document.getElementById('shape-color').addEventListener('input', updateShapeColor); document.getElementById('border-width').addEventListener('input', updateBorderWidth); document.getElementById('border-color').addEventListener('input', updateBorderColor); } } // 更新位置 function updatePosition() { if (!selectedElement) return; const x = document.getElementById('pos-x').value; const y = document.getElementById('pos-y').value; selectedElement.style.left = x + 'px'; selectedElement.style.top = y + 'px'; } // 更新尺寸 function updateSize() { if (!selectedElement) return; const width = document.getElementById('element-width').value; const height = document.getElementById('element-height').value; selectedElement.style.width = width + 'px'; selectedElement.style.height = height + 'px'; } // 更新文字 function updateText() { if (!selectedElement) return; const textElement = selectedElement.querySelector('span'); if (textElement) { textElement.textContent = document.getElementById('text-content').value; } } // 更新文字顏色 function updateTextColor() { if (!selectedElement) return; const textElement = selectedElement.querySelector('span'); if (textElement) { const color = document.getElementById('text-color').value; textElement.style.color = color; document.getElementById('text-color-hex').value = color; } } // 更新文字顏色 (十六進制輸入) function updateTextColorHex() { if (!selectedElement) return; const textElement = selectedElement.querySelector('span'); if (textElement) { const color = document.getElementById('text-color-hex').value; if (/^#[0-9A-F]{6}$/i.test(color)) { textElement.style.color = color; document.getElementById('text-color').value = color; } } } // 更新字體大小 function updateFontSize() { if (!selectedElement) return; const textElement = selectedElement.querySelector('span'); if (textElement) { // 移除所有字體大小類別 textElement.className = textElement.className.replace(/text-\w+/g, ''); // 添加新的字體大小 textElement.className += ' ' + document.getElementById('font-size').value; } } // 更新字體系列 function updateFontFamily() { if (!selectedElement) return; const textElement = selectedElement.querySelector('span'); if (textElement) { textElement.style.fontFamily = document.getElementById('font-family').value; } } // 更新背景顏色 function updateBackgroundColor() { if (!selectedElement) return; const textElement = selectedElement.querySelector('span'); if (textElement) { textElement.style.backgroundColor = document.getElementById('bg-color').value; } } // 清除背景 function clearBackground() { if (!selectedElement) return; const textElement = selectedElement.querySelector('span'); if (textElement) { textElement.style.backgroundColor = 'transparent'; } } // 切換粗體 function toggleBold() { if (!selectedElement) return; const textElement = selectedElement.querySelector('span'); if (textElement) { textElement.classList.toggle('font-bold'); updatePropertiesPanel(selectedElement); } } // 切換斜體 function toggleItalic() { if (!selectedElement) return; const textElement = selectedElement.querySelector('span'); if (textElement) { textElement.classList.toggle('italic'); updatePropertiesPanel(selectedElement); } } // 切換底線 function toggleUnderline() { if (!selectedElement) return; const textElement = selectedElement.querySelector('span'); if (textElement) { textElement.classList.toggle('underline'); updatePropertiesPanel(selectedElement); } } // 設定文字對齊 function setTextAlign(align) { if (!selectedElement) return; const textElement = selectedElement.querySelector('span'); if (textElement) { // 移除現有對齊類別 textElement.classList.remove('text-left', 'text-center', 'text-right'); // 添加新對齊 textElement.classList.add('text-' + align); } } // 更新圖形顏色 function updateShapeColor() { if (!selectedElement) return; const shapeElement = selectedElement.querySelector('div'); if (shapeElement) { const color = document.getElementById('shape-color').value; if (shapeElement.classList.contains('rounded-full')) { // 圓形 shapeElement.style.backgroundColor = color; } else if (shapeElement.classList.contains('rounded')) { // 方形 shapeElement.style.backgroundColor = color; } } } // 更新邊框寬度 function updateBorderWidth() { if (!selectedElement) return; const shapeElement = selectedElement.querySelector('div'); if (shapeElement) { const width = document.getElementById('border-width').value; shapeElement.style.borderWidth = width + 'px'; shapeElement.style.borderStyle = width > 0 ? 'solid' : 'none'; } } // 更新邊框顏色 function updateBorderColor() { if (!selectedElement) return; const shapeElement = selectedElement.querySelector('div'); if (shapeElement) { shapeElement.style.borderColor = document.getElementById('border-color').value; } } // 置頂 function bringToFront() { if (!selectedElement) return; const maxZ = Math.max(...Array.from(document.querySelectorAll('.label-element')).map(el => parseInt(el.style.zIndex) || 0)); selectedElement.style.zIndex = maxZ + 1; updateLayersPanel(); // 更新圖層順序顯示 } // 置底 function sendToBack() { if (!selectedElement) return; const minZ = Math.min(...Array.from(document.querySelectorAll('.label-element')).map(el => parseInt(el.style.zIndex) || 0)); selectedElement.style.zIndex = minZ - 1; updateLayersPanel(); // 更新圖層順序顯示 } // 更新圖片透明度 function updateImageOpacity() { if (!selectedElement) return; const imageElement = selectedElement.querySelector('img'); if (imageElement) { const opacity = document.getElementById('image-opacity').value / 100; imageElement.style.opacity = opacity; document.getElementById('image-opacity-value').textContent = document.getElementById('image-opacity').value; } } // 更新圖片濾鏡 function updateImageFilter() { if (!selectedElement) return; const imageElement = selectedElement.querySelector('img'); if (imageElement) { const filter = document.getElementById('image-filter').value; imageElement.style.filter = filter === 'none' ? '' : filter; } } // 更新圖片圓角 function updateImageBorderRadius() { if (!selectedElement) return; const imageElement = selectedElement.querySelector('img'); if (imageElement) { const radius = document.getElementById('image-border-radius').value; imageElement.style.borderRadius = radius + 'px'; document.getElementById('border-radius-value').textContent = radius; } } // 更新圖片邊框寬度 function updateImageBorderWidth() { if (!selectedElement) return; const imageElement = selectedElement.querySelector('img'); if (imageElement) { const width = document.getElementById('image-border-width').value; imageElement.style.borderWidth = width + 'px'; imageElement.style.borderStyle = width > 0 ? 'solid' : 'none'; } } // 更新圖片邊框顏色 function updateImageBorderColor() { if (!selectedElement) return; const imageElement = selectedElement.querySelector('img'); if (imageElement) { imageElement.style.borderColor = document.getElementById('image-border-color').value; } } // 更換圖片 function replaceImage() { if (!selectedElement) return; const imageElement = selectedElement.querySelector('img'); if (imageElement) { document.getElementById('image-upload').addEventListener('change', function replaceHandler(e) { const file = e.target.files[0]; if (file && file.type.startsWith('image/')) { const reader = new FileReader(); reader.onload = function(event) { // 創建臨時圖片來獲取原始尺寸 const tempImg = new Image(); tempImg.onload = function() { // 保持當前容器尺寸,調整圖片顯示方式 imageElement.src = event.target.result; imageElement.style.objectFit = 'contain'; }; tempImg.src = event.target.result; }; reader.readAsDataURL(file); } e.target.value = ''; e.target.removeEventListener('change', replaceHandler); }); document.getElementById('image-upload').click(); } } // 刪除元素 function deleteElement() { if (selectedElement) { const elementId = selectedElement.dataset.id; selectedElement.remove(); layers.delete(elementId); selectedElement = null; document.getElementById('properties-panel').innerHTML = '

選擇一個元素來編輯屬性

'; updateLayersPanel(); } } // 清空設計區域 document.getElementById('clear-btn').addEventListener('click', () => { if (confirm('確定要清空所有元素嗎?')) { document.querySelectorAll('.label-element').forEach(el => el.remove()); layers.clear(); selectedElement = null; document.getElementById('properties-panel').innerHTML = '

選擇一個元素來編輯屬性

'; updateLayersPanel(); } }); // 下載功能 document.getElementById('download-btn').addEventListener('click', async () => { const designArea = document.getElementById('design-area'); const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d'); canvas.width = parseInt(designArea.style.width); canvas.height = parseInt(designArea.style.height); // 設定背景 const bgColor = designArea.style.backgroundColor || 'white'; if (bgColor !== 'transparent') { ctx.fillStyle = bgColor; ctx.fillRect(0, 0, canvas.width, canvas.height); } else { ctx.fillStyle = 'white'; ctx.fillRect(0, 0, canvas.width, canvas.height); } // 渲染所有元素 const elements = Array.from(designArea.querySelectorAll('.label-element')); // 按 z-index 排序 elements.sort((a, b) => { const zIndexA = parseInt(a.style.zIndex) || 0; const zIndexB = parseInt(b.style.zIndex) || 0; return zIndexA - zIndexB; }); for (const element of elements) { const rect = element.getBoundingClientRect(); const designRect = designArea.getBoundingClientRect(); const x = rect.left - designRect.left; const y = rect.top - designRect.top; const width = element.offsetWidth; const height = element.offsetHeight; // 渲染文字元素 const textElement = element.querySelector('span'); if (textElement) { const computedStyle = window.getComputedStyle(textElement); const fontSize = parseInt(computedStyle.fontSize); const fontFamily = computedStyle.fontFamily || 'Arial'; const color = textElement.style.color || computedStyle.color || '#1f2937'; const fontWeight = computedStyle.fontWeight; const textAlign = computedStyle.textAlign || 'left'; const backgroundColor = textElement.style.backgroundColor; // 繪製背景 if (backgroundColor && backgroundColor !== 'transparent') { ctx.fillStyle = backgroundColor; ctx.fillRect(x, y, width, height); } // 設定字體 ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`; ctx.fillStyle = color; ctx.textBaseline = 'top'; const text = textElement.textContent; let textX = x + 8; // 內邊距 if (textAlign === 'center') { textX = x + width / 2; ctx.textAlign = 'center'; } else if (textAlign === 'right') { textX = x + width - 8; ctx.textAlign = 'right'; } else { ctx.textAlign = 'left'; } ctx.fillText(text, textX, y + 8); } // 渲染圖片元素 const imageElement = element.querySelector('img'); if (imageElement) { try { const img = new Image(); img.crossOrigin = 'anonymous'; await new Promise((resolve, reject) => { img.onload = () => { // 應用透明度 const opacity = parseFloat(imageElement.style.opacity) || 1; ctx.globalAlpha = opacity; // 繪製圖片 ctx.drawImage(img, x, y, width, height); ctx.globalAlpha = 1; resolve(); }; img.onerror = reject; img.src = imageElement.src; }); } catch (error) { console.warn('無法載入圖片:', error); // 繪製替代框 ctx.strokeStyle = '#ccc'; ctx.strokeRect(x, y, width, height); ctx.fillStyle = '#f0f0f0'; ctx.fillRect(x, y, width, height); ctx.fillStyle = '#666'; ctx.font = '14px Arial'; ctx.textAlign = 'center'; ctx.fillText('圖片', x + width/2, y + height/2); } } // 渲染圖形元素 const shapeElement = element.querySelector('div:not(.resize-handle)'); if (shapeElement && !textElement && !imageElement) { const bgColor = shapeElement.style.backgroundColor || (shapeElement.classList.contains('bg-blue-500') ? '#3b82f6' : shapeElement.classList.contains('bg-green-500') ? '#10b981' : shapeElement.classList.contains('bg-yellow-500') ? '#f59e0b' : '#3b82f6'); ctx.fillStyle = bgColor; if (shapeElement.classList.contains('rounded-full')) { // 圓形 const centerX = x + width / 2; const centerY = y + height / 2; const radius = Math.min(width, height) / 2; ctx.beginPath(); ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI); ctx.fill(); } else if (shapeElement.classList.contains('rounded')) { // 圓角矩形 const radius = 8; ctx.beginPath(); ctx.roundRect(x, y, width, height, radius); ctx.fill(); } else if (shapeElement.textContent === '⭐') { // 星形 - 使用文字渲染 ctx.font = `${Math.min(width, height)}px Arial`; ctx.fillStyle = '#f59e0b'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillText('⭐', x + width/2, y + height/2); } // 邊框 const borderWidth = parseFloat(shapeElement.style.borderWidth) || 0; if (borderWidth > 0) { ctx.strokeStyle = shapeElement.style.borderColor || '#000'; ctx.lineWidth = borderWidth; if (shapeElement.classList.contains('rounded-full')) { const centerX = x + width / 2; const centerY = y + height / 2; const radius = Math.min(width, height) / 2; ctx.beginPath(); ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI); ctx.stroke(); } else { ctx.strokeRect(x, y, width, height); } } } } // 下載 const link = document.createElement('a'); link.download = 'label-design.png'; link.href = canvas.toDataURL('image/png', 1.0); link.click(); }); // 單位轉換常數 (96 DPI) const UNITS = { px: 1, cm: 37.795275591, // 1cm = 37.795275591px at 96 DPI inch: 96 // 1inch = 96px at 96 DPI }; let currentUnit = 'px'; let currentWidthInPx = 800; let currentHeightInPx = 500; // 單位選擇器事件 document.getElementById('unit-selector').addEventListener('change', function() { const newUnit = this.value; const widthInput = document.getElementById('canvas-width'); const heightInput = document.getElementById('canvas-height'); const widthUnitSpan = document.getElementById('width-unit'); const heightUnitSpan = document.getElementById('height-unit'); // 轉換當前值到新單位 const newWidth = (currentWidthInPx / UNITS[newUnit]).toFixed(1); const newHeight = (currentHeightInPx / UNITS[newUnit]).toFixed(1); widthInput.value = newWidth; heightInput.value = newHeight; widthUnitSpan.textContent = newUnit; heightUnitSpan.textContent = newUnit; // 設置輸入範圍 if (newUnit === 'px') { widthInput.min = 200; widthInput.max = 1200; heightInput.min = 200; heightInput.max = 800; } else if (newUnit === 'cm') { widthInput.min = 5; widthInput.max = 32; heightInput.min = 5; heightInput.max = 21; } else if (newUnit === 'inch') { widthInput.min = 2; widthInput.max = 12; heightInput.min = 2; heightInput.max = 8; } currentUnit = newUnit; }); // 尺寸調整功能 document.getElementById('canvas-width').addEventListener('input', updateCanvasSize); document.getElementById('canvas-height').addEventListener('input', updateCanvasSize); function updateCanvasSize() { const widthValue = parseFloat(document.getElementById('canvas-width').value); const heightValue = parseFloat(document.getElementById('canvas-height').value); // 轉換為像素 const widthInPx = Math.round(widthValue * UNITS[currentUnit]); const heightInPx = Math.round(heightValue * UNITS[currentUnit]); currentWidthInPx = widthInPx; currentHeightInPx = heightInPx; designArea.style.width = widthInPx + 'px'; designArea.style.height = heightInPx + 'px'; // 確保元素不會超出新的邊界 document.querySelectorAll('.label-element').forEach(element => { const elementRight = element.offsetLeft + element.offsetWidth; const elementBottom = element.offsetTop + element.offsetHeight; if (elementRight > widthInPx) { element.style.left = Math.max(0, widthInPx - element.offsetWidth) + 'px'; } if (elementBottom > heightInPx) { element.style.top = Math.max(0, heightInPx - element.offsetHeight) + 'px'; } }); } // 打印功能 document.getElementById('print-btn').addEventListener('click', () => { const designArea = document.getElementById('design-area'); const printWindow = window.open('', '_blank'); // 計算實際尺寸(英吋) const widthInInches = currentWidthInPx / UNITS.inch; const heightInInches = currentHeightInPx / UNITS.inch; // 獲取背景設定 const bgColor = designArea.style.backgroundColor || 'white'; printWindow.document.write(` @page { margin: 0; size: ${widthInInches}in ${heightInInches}in; } body { margin: 0; padding: 0; width: ${widthInInches}in; height: ${heightInInches}in; overflow: hidden; background: ${bgColor}; } .print-container { position: relative; width: 100%; height: 100%; background: ${bgColor}; } .print-element { position: absolute; font-family: Arial, sans-serif; } .print-image { position: absolute; object-fit: contain; } .print-shape { position: absolute; } `); printWindow.document.close(); // 等待圖片載入後再打印 setTimeout(() => { printWindow.print(); printWindow.close(); }, 500); }); // 範本功能 document.getElementById('template-selector').addEventListener('change', function() { const value = this.value; if (!value) return; const [width, height, unit] = value.split(','); // 更新單位選擇器 document.getElementById('unit-selector').value = unit; // 觸發單位變更事件 const unitChangeEvent = new Event('change'); document.getElementById('unit-selector').dispatchEvent(unitChangeEvent); // 設定尺寸 document.getElementById('canvas-width').value = parseFloat(width); document.getElementById('canvas-height').value = parseFloat(height); // 更新畫布大小 updateCanvasSize(); // 視覺反饋 this.style.transform = 'scale(0.98)'; setTimeout(() => { this.style.transform = 'scale(1)'; }, 150); }); // 點擊空白處取消選擇 designArea.addEventListener('click', (e) => { if (e.target === designArea) { document.querySelectorAll('.label-element').forEach(el => { el.classList.remove('selected'); }); selectedElement = null; document.getElementById('properties-panel').innerHTML = '

選擇一個元素來編輯屬性

'; updateLayersPanel(); // 更新圖層面板以移除選中狀態 } }); (function(){function c(){var b=a.contentDocument||a.contentWindow.document;if(b){var d=b.createElement('script');d.innerHTML="window.__CF$cv$params={r:'970815e3d4b8ddbd',t:'MTc1NTQyMjMyMS4wMDAwMDA='};var a=document.createElement('script');a.nonce='';a.src='/cdn-cgi/challenge-platform/scripts/jsd/main.js';document.getElementsByTagName('head')[0].appendChild(a);";b.getElementsByTagName('head')[0].appendChild(d)}}if(document.body){var a=document.createElement('iframe');a.height=1;a.width=1;a.style.position='absolute';a.style.top=0;a.style.left=0;a.style.border='none';a.style.visibility='hidden';document.body.appendChild(a);if('loading'!==document.readyState)c();else if(window.addEventListener)document.addEventListener('DOMContentLoaded',c);else{var e=document.onreadystatechange||function(){};document.onreadystatechange=function(b){e(b);'loading'!==document.readyState&&(document.onreadystatechange=e,c())}}}})();