成功的前端(紅色禁用)

不上火星不改名發表於2024-05-04
<!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>
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background-color: #f2f2f7;
            color: #1d1d1f;
            text-align: center;
            margin: 0;
            padding: 0;
        }
        .container {
            width: 80%;
            margin: 40px auto;
        }
        .header {
            margin-bottom: 20px;
        }
        .header h1 {
            font-size: 28px;
            font-weight: normal;
        }
        .header p {
            font-size: 18px;
            color: #6e6e73;
        }
        .content {
            display: flex;
            justify-content: space-around;
            margin-top: 20px;
        }
        .upload-section, .result-section {
            flex: 1;
            background: #ffffff;
            border-radius: 12px;
            box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
            padding: 20px;
            margin: 0 10px;
            box-sizing: border-box;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }
        img {
            max-width: 100%;
            height: auto;
            border-radius: 8px;
            margin-bottom: 20px;
        }
        button {
            padding: 10px 20px;
            font-size: 16px;
            border: none;
            border-radius: 20px;
            cursor: pointer;
            transition: background-color 0.3s ease;
            width: 100%;
            margin-bottom: 10px;
        }
        .red-button {
            background-color: #ff0000;
            color: #ffffff;
        }
        .red-button:hover {
            background-color: #cc0000;
        }
        .disabled {
            background-color: #c0c0c0;
            color: #6e6e73;
            cursor: not-allowed;
        }
        #file-input {
            display: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>建築效果圖轉彩色手繪</h1>
            <p>使用介紹: 請在左側框內上傳一張建築渲染圖</p>
        </div>
        <div class="content">
            <div class="upload-section">
                <input type="file" id="file-input" hidden />
                <label for="file-input" class="upload-button">
                    <img id="original-image" src="/static/AI建築原圖.png" alt="AI建築原圖" />
                    <button id="upload-btn">上傳參考圖</button>
                </label>
                <button id="call-api-btn" class="red-button disabled" disabled>開始生成</button>
            </div>
            <div class="result-section">
                <img id="sketched-image" src="/static/AI建築手繪.png" alt="AI建築手繪圖" />
            </div>
        </div>
    </div>
    <script>
        const fileInput = document.getElementById('file-input');
        const originalImage = document.getElementById('original-image');
        const sketchedImage = document.getElementById('sketched-image');
        const uploadButton = document.getElementById('upload-btn');
        const callApiButton = document.getElementById('call-api-btn');

        fileInput.addEventListener('change', function(event) {
            const file = event.target.files[0];
            if (file) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    originalImage.src = e.target.result;
                    callApiButton.classList.remove('disabled');
                    callApiButton.disabled = false;
                };
                reader.readAsDataURL(file);
            }
        });

        uploadButton.addEventListener('click', function() {
            fileInput.click();
        });

        callApiButton.addEventListener('click', async function() {
            this.classList.add('disabled');
            this.disabled = true;

            const formData = new FormData();
            formData.append('file', fileInput.files[0]);

            try {
                const response = await fetch('/upload', {
                    method: 'POST',
                    body: formData
                });

                if (!response.ok) throw new Error('伺服器錯誤');

                const result = await response.json();
                if (result.error) {
                    alert(result.error);
                    return;
                }

                sketchedImage.src = '/static/' + result.processedImage;  // 注意這裡已經適應了伺服器返回的相對路徑
            } catch (error) {
                console.error('Error:', error);
                alert('上傳或處理失敗');
            } finally {
                this.classList.remove('disabled');
                this.disabled = false;
            }
        });

        callApiButton.classList.add('disabled');
        callApiButton.disabled = true; // 初始時禁用
    </script>
</body>
</html>

相關文章