饮料包装生成器edit icon

作者:
邓朝元
Fork(复制)
下载
嵌入
BUG反馈
index.html
md
README.md
现在支持上传本地图片了!
index.html
            
            <!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>
        :root {
            --can-width: 200px;
            --can-height: 420px;
            --bg-color: #eef2f5;
        }

        body {
            font-family: 'Helvetica Neue', Arial, sans-serif;
            background-color: var(--bg-color);
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
            margin: 0;
            overflow: hidden;
        }

        h1 {
            color: #333;
            font-weight: 300;
            margin-bottom: 10px;
        }

        .subtitle {
            color: #666;
            font-size: 0.9rem;
            margin-bottom: 40px;
        }

        .scene {
            perspective: 1200px;
            margin-bottom: 50px;
        }

        .can {
            width: var(--can-width);
            height: var(--can-height);
            position: relative;
            border-radius: 25px 25px 35px 35px;
            background-color: #ccc; 
            box-shadow: 
                inset -15px 0 30px rgba(0,0,0,0.3),
                inset 15px 0 30px rgba(255,255,255,0.4),
                0 30px 60px rgba(0,0,0,0.3);
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            transition: transform 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.27);
            overflow: hidden;
            border: 1px solid rgba(0,0,0,0.1);
            /* 确保背景图正确显示 */
            background-position: center;
            background-repeat: no-repeat;
        }

        .can:hover {
            transform: rotateY(10deg) rotateX(5deg);
        }

        .can::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 50px;
            background: linear-gradient(to bottom, rgba(255,255,255,0.8), rgba(255,255,255,0.1));
            border-radius: 25px 25px 10px 10px;
            z-index: 10;
            pointer-events: none;
        }

        .label-content {
            z-index: 5;
            text-align: center;
            width: 80%;
            padding: 20px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            height: 60%;
        }

        .brand-name {
            font-size: 2.8rem;
            font-weight: 900;
            text-transform: uppercase;
            color: #fff;
            text-shadow: 2px 2px 0px rgba(0,0,0,0.4);
            line-height: 0.9;
            margin-bottom: 15px;
            font-style: italic;
            letter-spacing: -1px;
            transform: skewX(-10deg);
        }

        .flavor-badge {
            background: rgba(255, 255, 255, 0.25);
            backdrop-filter: blur(4px);
            color: #fff;
            padding: 8px 16px;
            border-radius: 50px;
            font-size: 1.1rem;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 1px;
            border: 1px solid rgba(255,255,255,0.4);
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        }

        .type-indicator {
            position: absolute;
            bottom: 40px;
            font-size: 0.8rem;
            color: rgba(255,255,255,0.8);
            text-transform: uppercase;
            letter-spacing: 3px;
            border-top: 2px solid rgba(255,255,255,0.5);
            padding-top: 8px;
            width: 60%;
            text-align: center;
            z-index: 5;
        }

        .nutrition-fake {
            position: absolute;
            right: 15px;
            bottom: 60px;
            width: 40px;
            height: 80px;
            background: rgba(255,255,255,0.2);
            border-radius: 4px;
            z-index: 5;
            display: flex;
            flex-direction: column;
            justify-content: space-around;
            padding: 4px;
            box-sizing: border-box;
        }
        .nutrition-line {
            height: 2px;
            background: rgba(255,255,255,0.6);
            width: 100%;
        }

        .controls {
            display: flex;
            gap: 20px;
            z-index: 20;
        }

        button {
            padding: 15px 35px;
            font-size: 1.1rem;
            background: #2d3436;
            color: white;
            border: none;
            border-radius: 50px;
            cursor: pointer;
            box-shadow: 0 10px 20px rgba(0,0,0,0.2);
            transition: all 0.2s ease;
            font-weight: 600;
        }

        button:hover {
            background: #000;
            transform: translateY(-2px);
            box-shadow: 0 15px 25px rgba(0,0,0,0.3);
        }

        button:active {
            transform: translateY(1px);
        }

        .stats {
            margin-top: 30px;
            font-size: 0.85rem;
            color: #b2bec3;
            text-align: center;
            line-height: 1.5;
        }

        .pop-in {
            animation: popIn 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.27);
        }

        @keyframes popIn {
            0% { transform: scale(0.8); opacity: 0; }
            100% { transform: scale(1); opacity: 1; }
        }
    </style>
</head>
<body>

    <h1>随机饮料生成器</h1>
    <div class="subtitle">每次点击生成独一无二的包装</div>

    <div class="scene">
        <div class="can" id="drinkCan">
            <div class="label-content">
                <div class="brand-name" id="brandText">BRAND</div>
                <div class="flavor-badge" id="flavorText">FLAVOR</div>
            </div>
            <div class="type-indicator" id="typeText">TYPE</div>
            <div class="nutrition-fake">
                <div class="nutrition-line"></div>
                <div class="nutrition-line"></div>
                <div class="nutrition-line"></div>
                <div class="nutrition-line"></div>
                <div class="nutrition-line"></div>
            </div>
        </div>
    </div>

    <div class="controls">
        <!-- 移除了 onclick,改用 ID 监听 -->
        <button id="generateBtn">🎲 生成新饮料</button>
    </div>

    <div class="stats" id="statsText">
        准备就绪
    </div>

    <script>
        // 数据源
        const brandPrefixes = [
            "Ultra", "Hyper", "Mega", "Super", "Neon", "Cyber", "Retro", "Pure", "Zen", "Wild",
            "Crazy", "Happy", "Lucky", "Golden", "Silver", "Iron", "Titan", "Solar", "Lunar", "Star",
            "Cosmo", "Astro", "Geo", "Hydro", "Aero", "Terra", "Flora", "Fauna", "Aqua", "Ignis",
            "Ventus", "Nova", "Quantum", "Pixel", "Vapor", "Liquid", "Atomic", "Molecular", "Robo", "Techno"
        ];
        const brandSuffixes = [
            "Fizz", "Pop", "Soda", "Cola", "Drink", "Brew", "Juice", "Tea", "Water", "Energy",
            "Boost", "Flow", "Wave", "Spark", "Bolt", "Flash", "Glow", "Shine", "Zest", "Kick",
            "Rush", "Vibe", "Mode", "X", "Zero", "One", "Max", "Pro", "Plus", "Go"
        ];

        const flavorBase = [
            "Lemon", "Lime", "Orange", "Grapefruit", "Berry", "Strawberry", "Blueberry", "Raspberry",
            "Mango", "Peach", "Apple", "Pear", "Cherry", "Watermelon", "Melon", "Coconut", "Pineapple",
            "Passion Fruit", "Kiwi", "Banana", "Grape", "Plum", "Apricot", "Fig", "Pomegranate",
            "Cola", "Root Beer", "Ginger", "Mint", "Vanilla"
        ];
        const flavorMod = [
            "Spicy", "Sweet", "Sour", "Bitter", "Fresh", "Icy", "Frozen", "Chilled", "Warm", "Hot",
            "Zesty", "Tangy", "Creamy", "Smooth", "Rich", "Light", "Dark", "Golden", "Silver", "Midnight"
        ];

        const drinkTypes = [
            "Sparkling Water", "Still Water", "Energy Drink", "Sports Drink", "Iced Tea",
            "Green Tea", "Black Tea", "Fruit Juice", "Vegetable Juice", "Smoothie",
            "Protein Shake", "Coffee Soda", "Kombucha", "Kefir", "Yogurt Drink",
            "Alcohol-Free Beer", "Malt Beverage", "Tonic Water", "Ginger Ale", "Root Beer",
            "Cream Soda", "Fruit Punch", "Lemonade", "Limeade", "Orangeade",
            "Coconut Water", "Almond Milk", "Oat Milk", "Soy Milk", "Rice Milk"
        ];

        // 工具函数
        function getRandomItem(arr) {
            return arr[Math.floor(Math.random() * arr.length)];
        }

        function getRandomColor() {
            const h = Math.floor(Math.random() * 360);
            const s = Math.floor(Math.random() * 40) + 60; 
            const l = Math.floor(Math.random() * 40) + 30; 
            return `hsl(${h}, ${s}%, ${l}%)`;
        }

        function getRandomLightColor() {
            const h = Math.floor(Math.random() * 360);
            return `hsl(${h}, 70%, 85%)`;
        }

        function generatePattern() {
            const color1 = getRandomColor();
            const color2 = getRandomColor();
            const color3 = getRandomLightColor();
            const angle = Math.floor(Math.random() * 360);
            const size = Math.floor(Math.random() * 50) + 10;
            
            const patterns = [
                `solid ${color1}`,
                `linear-gradient(${angle}deg, ${color1}, ${color2})`,
                `radial-gradient(circle at 30% 30%, ${color1}, ${color2})`,
                `conic-gradient(from ${angle}deg, ${color1}, ${color2}, ${color1})`,
                `repeating-linear-gradient(${angle}deg, ${color1}, ${color1} 10px, ${color2} 10px, ${color2} 20px)`,
                `repeating-linear-gradient(45deg, ${color1}, ${color1} 15px, ${color2} 15px, ${color2} 30px)`,
                `radial-gradient(${color2} 20%, transparent 20%), radial-gradient(${color2} 20%, transparent 20%)`,
                `linear-gradient(${color1} 1px, transparent 1px), linear-gradient(90deg, ${color1} 1px, transparent 1px)`,
                `repeating-linear-gradient(${angle}deg, ${color1}, ${color1} 5px, ${color2} 5px, ${color2} 10px)`,
                `radial-gradient(${color2} 1px, transparent 1px)`,
                `linear-gradient(45deg, ${color1} 25%, transparent 25%, transparent 75%, ${color1} 75%, ${color1}), linear-gradient(45deg, ${color1} 25%, transparent 25%, transparent 75%, ${color1} 75%, ${color1})`,
                `radial-gradient(circle at 50% 0%, ${color3}, ${color1} 60%)`
            ];

            let selectedPattern = getRandomItem(patterns);
            
            if (selectedPattern.includes("radial-gradient") && selectedPattern.includes("transparent")) {
                return {
                    image: selectedPattern,
                    size: `${size}px ${size}px`,
                    color: color1
                };
            }
            
            return {
                image: selectedPattern,
                size: 'cover',
                color: color1
            };
        }

        // 主生成函数
        function generateDrink() {
            try {
                const can = document.getElementById('drinkCan');
                const brandText = document.getElementById('brandText');
                const flavorText = document.getElementById('flavorText');
                const typeText = document.getElementById('typeText');
                const statsText = document.getElementById('statsText');
                const btn = document.getElementById('generateBtn');

                // 按钮点击反馈
                btn.innerText = "生成中...";
                setTimeout(() => btn.innerText = "🎲 生成新饮料", 200);

                // 1. 生成文本
                const brand = getRandomItem(brandPrefixes) + " " + getRandomItem(brandSuffixes);
                const flavor = getRandomItem(flavorMod) + " " + getRandomItem(flavorBase);
                const type = getRandomItem(drinkTypes);

                // 2. 生成视觉
                const visual = generatePattern();

                // 3. 应用更新
                can.classList.remove('pop-in');
                void can.offsetWidth; // 触发重绘
                can.classList.add('pop-in');

                can.style.backgroundImage = visual.image;
                if(visual.size !== 'cover') {
                    can.style.backgroundSize = visual.size;
                } else {
                    can.style.backgroundSize = 'cover';
                }
                
                brandText.innerText = brand;
                flavorText.innerText = flavor;
                typeText.innerText = type;

                const totalCombinations = (brandPrefixes.length * brandSuffixes.length) * 
                                          (flavorBase.length * flavorMod.length) * 
                                          drinkTypes.length * 12;
                
                statsText.innerHTML = `
                    品牌组合:${brandPrefixes.length * brandSuffixes.length}+ <br>
                    口味组合:${flavorBase.length * flavorMod.length}+ <br>
                    视觉图案:无限 (CSS 程序化生成) <br>
                    <strong>理论总组合数:${totalCombinations.toLocaleString()}+</strong>
                `;
            } catch (error) {
                console.error(error);
                alert("生成出错啦!请按 F12 打开控制台查看错误信息,或者刷新页面重试。");
            }
        }

        // 确保页面加载完成后绑定事件
        document.addEventListener('DOMContentLoaded', function() {
            const btn = document.getElementById('generateBtn');
            if(btn) {
                btn.addEventListener('click', generateDrink);
            }
            // 初始化运行一次
            generateDrink();
        });

    </script>
</body>
</html>
        
编辑器加载中
预览
控制台