gsap实现一个json配置生成动画场景播放声音和画面引擎可导出webm视频代码

代码语言:html

所属分类:多媒体

代码描述:gsap实现一个json配置生成动画场景播放声音和画面引擎可导出webm视频代码

代码标签: gsap json 配置 生成 动画 场景 播放 声音 画面 引擎 代码 导出 webm 视频

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JSON动画生成系统</title>
<script type="text/javascript" src="https://repo.bfw.wiki/bfwrepo/js/gsap.3.12.2.js"></script>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #0a0a1a;
            color: #ffffff;
            margin: 0;
            padding: 20px;
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        .container {
            width: 100%;
            max-width: 1200px;
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        h1 {
            color: #64B5F6;
            margin-bottom: 10px;
        }

        .description {
            color: #B0BEC5;
            text-align: center;
            margin-bottom: 30px;
            max-width: 800px;
        }

        .canvas-container {
            position: relative;
            margin: 20px 0;
            box-shadow: 0 0 20px rgba(0, 150, 255, 0.3);
            border-radius: 4px;
            overflow: hidden;
            /* Will be sized by JS based on config */
        }

        canvas {
            display: block;
            background-color: #0a0a1a; /* Default, can be overridden by config */
        }

        .controls {
            display: flex;
            gap: 15px;
            margin: 20px 0;
            flex-wrap: wrap;
            justify-content: center;
        }

        button {
            padding: 12px 24px;
            background: linear-gradient(135deg, #2196F3, #673AB7);
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: all 0.3s ease;
            min-width: 150px;
        }

        button:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 150, 255, 0.4);
        }

        button:disabled {
            background: #555;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        .config-editor {
            width: 100%;
            margin: 20px 0;
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        .editor-container {
            width: 100%;
            max-width: 800px; /* Limit editor width for better readability */
            display: flex;
            flex-direction: column;
        }

        .editor-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 10px;
        }

        .editor-title {
            font-size: 18px;
            color: #64B5F6;
        }

        textarea {
            width: 100%;
            height: 300px;
            background-color: #1a1a2a;
            color: #ffffff;
            border: 1px solid #333;
            border-radius: 4px;
            padding: 10px;
            font-family: monospace;
            resize: vertical;
            box-sizing: border-box;
        }

        .video-preview {
            margin-top: 30px;
            width: 100%;
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        video {
            max-width: 100%;
            border-radius: 4px;
            box-shadow: 0 0 20px rgba(0, 150, 255, 0.3);
        }

        .download-link {
            margin-top: 15px;
            padding: 12px 24px;
            background: linear-gradient(135deg, #4CAF50, #009688);
            color: white;
            text-decoration: none;
            border-radius: 4px;
            transition: all 0.3s ease;
        }

        .download-link:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(76, 175, 80, 0.4);
        }

        .status {
            margin-top: 10px;
            min-height: 1.2em; /* Prevent layout shift */
            color: #64B5F6;
        }

        .error {
            color: #FF5252;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>动画生成系统</h1>
        <p class="description">基于JSON配置的动画生成器 - 通过修改配置文件创建自定义动画,并导出为WebM视频</p>

        <div class="canvas-container">
            <canvas id="animationCanvas"></canvas>
        </div>

        <div class="controls">
            <button id="previewBtn">预览动画</button>
            <button id="recordBtn">录制视频</button>
            <button id="stopBtn" disabled>停止</button>
            <button id="resetBtn">重置</button>
        </div>

        <div class="config-editor">
            <div class="editor-container">
                <div class="editor-header">
                    <div class="editor-title">动画配置 (JSON)</div>
                    <button id="applyConfigBtn">应用配置</button>
                </div>
                <textarea id="configEditor"></textarea>
            </div>
        </div>

        <div class="video-preview" id="videoPreviewContainer" style="display: none;">
            <h2>视频预览</h2>
            <video id="recordedVideo" controls></video>
            <a id="downloadLink" class="download-link" download="animation.webm">下载视频</a>
        </div>

        <div id="status" class="status"></div>
    </div>

    <script>
    // Placeholder image URLs (replace with actual, accessible URLs for testing)
    // You can use services like placeholder.com or host your own images/SVGs.
    //const placeholderImageUrl = (width, height, text = "Image") => `//repo.bfw.wiki/random/${width}x${height}/${encodeURIComponent(text)}`;
    
     const placeholderImageUrl = (width, height, text = "Image") => `/asset/test.jpg`;
    const placeholderSvgUrl = (width, height, text = "SVG") => `data:image/svg+xml;charset=utf-8,${encodeURIComponent(`<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}"><rect width="100%" height="100%" fill="#333"/><text x="50%" y="50%" dominant-baseline="middle" text-anchor="middle" font-family="Arial" font-size="20" fill="#fff">${text}</text></svg>`)}`;

    const gsapAnimationConfigExample = {
        canvas: {
            width: 800,
            height: 450,
            backgroundColor: '#111122' // Default canvas background if scene doesn't specify one
        },
        // Preload assets (images or SVGs by URL, or inline SVG strings)
        // Keys are assetIds that can be referenced by elements.
        preloadAssets: {
           // "rocketIcon": "/asset/demo2.svg", // Example external SVG URL
          //  "planetIcon": "/asset/demo3.svg",
            "starIconString": `/asset/demo.svg`,
           // "genericImage": placeholderImageUrl(100,100, "Preloaded Img")
        },
        scenes: [
            {
                id: "intro",
                duration: 3000, // ms
                background: { type: "color", color: "#0a0a1a" },
                narrations: [{ text: "欢迎来到动画生成系统", time: 0.5 }],
                elements: [
                    {
                        type: "text", content: "URL加载与高级功能", position: { x: 400, y: 180 },
                        style: { font: "bold 40px Arial", color: "#E1F5FE", textAlign: "center" },
                        animation: { type: "fadeIn", startAt: 0, duration: 1.5 }
                    },
                    {
                        type: "text", content: "支持图片、SVG、麦克风录制", position: { x: 400, y: 240 },
                        style: { font: "24px Arial", color: "#B3E5FC", textAlign: "center" },
                        animation: { type: "fadeIn", startAt: 1, duration: 1.5 }
                    }
                ]
            },
            {
                id: "image-background-scene",
                duration: 4000,
                background: { type: "image", src: placeholderImageUrl(800, 450, "Background Image") },
                narrations: [{ text: "场景背景可以是图片。", time: 0.5 }],
                elements: [
                    {
                        type: "text", content: "图片背景", position: { x: 400, y: 225 },
                        style: { font: "bold 48px Arial", color: "#000000", textAlign: "center", textBaseline: "middle", strokeStyle: "#FFFFFF", lineWidth: 2}, // Text on image bg
                        animation: { type: "fadeIn", startAt: 0.5, duration: 1.0 }
                    }
                ]
            },
            {
                id: "svg-background-scene",
                duration: 4000,
                // background: { type: "svg", src: "https://www.svgrepo.com/show/503330/pattern.svg" }, // Example external SVG background
                background: { type: "svg", svgContent: `<svg width="800" height="450" xmlns="http://www.w3.org/2000/svg"><defs><pattern id="p1" width="50" height="50" patternUnits="userSpaceOnUse"><circle cx="25" cy="25" r="10" fill="#64B5F6"/></pattern></defs><rect width="100%" height="100%" fill="#1A237E"/><rect width="100%" height="100%" fill="url(#p1)"/></svg>`},
                narrations: [{ text: "或者SVG作.........完整代码请登录后点击上方下载按钮下载查看

网友评论0