js实现浏览器mediaRecorder生成动画并下载webm视频代码

代码语言:html

所属分类:多媒体

代码描述:js实现浏览器mediaRecorder生成动画并下载webm视频代码,js也能生成文字和图片及svg动画视频了,而且直接在浏览器中就能生成。

代码标签: js 浏览器 mediaRecorder 生成 动画 下载 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>AI未来动画</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
            background-color: #0a0a1a;
            font-family: 'Arial', sans-serif;
            color: white;
        }
        
        #canvasContainer {
            position: relative;
            margin: 20px 0;
        }
        
        canvas {
            border: 1px solid #333;
            box-shadow: 0 0 20px rgba(0, 150, 255, 0.3);
        }
        
        .controls {
            margin-top: 20px;
            display: flex;
            gap: 10px;
        }
        
        button {
            padding: 10px 20px;
            background: linear-gradient(135deg, #2196F3, #673AB7);
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: all 0.3s ease;
        }
        
        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;
        }
        
        #videoContainer {
            margin-top: 20px;
            display: none;
        }
        
        video {
            max-width: 100%;
            border-radius: 4px;
            box-shadow: 0 0 20px rgba(0, 150, 255, 0.3);
        }
        
        #downloadLink {
            display: inline-block;
            margin-top: 10px;
            color: #2196F3;
            text-decoration: none;
            padding: 8px 16px;
            border: 1px solid #2196F3;
            border-radius: 4px;
            transition: all 0.3s ease;
        }
        
        #downloadLink:hover {
            background-color: #2196F3;
            color: white;
        }
        
        .status {
            margin-top: 10px;
            color: #4CAF50;
            font-style: italic;
        }
    </style>
</head>
<body>
    <h1>AI未来动画</h1>
    <div id="canvasContainer">
        <canvas id="animationCanvas" width="800" height="450"></canvas>
    </div>
    <div class="controls">
        <button id="startBtn">开始动画预览</button>
        <button id="recordBtn">录制动画</button>
        <button id="stopBtn" disabled>停止录制</button>
    </div>
    <div id="statusText" class="status"></div>
    <div id="videoContainer">
        <h2>录制结果</h2>
        <video id="recordedVideo" controls></video>
        <div>
            <a id="downloadLink" href="#" download="ai-future-animation.webm">下载视频</a>
        </div>
    </div>

    <script>
        // 获取DOM元素
        const canvas = document.getElementById('animationCanvas');
        const ctx = canvas.getContext('2d');
        const startBtn = document.getElementById('startBtn');
        const recordBtn = document.getElementById('recordBtn');
        const stopBtn = document.getElementById('stopBtn');
        const statusText = document.getElementById('statusText');
        const videoContainer = document.getElementById('videoContainer');
        const recordedVideo = document.getElementById('recordedVideo');
        const downloadLink = document.getElementById('downloadLink');
        
        // 动画参数
        const width = canvas.width;
        const height = canvas.height;
        let animationFrameId = null;
        let mediaRecorder = null;
        let recordedChunks = [];
        let startTime = 0;
        let isAnimating = false;
        let isRecording = false;
        
        // SVG图标数据
        const svgIcons = {
            brain: `<svg viewBox="0 0 512 512" width="100" height="100">
 .........完整代码请登录后点击上方下载按钮下载查看

网友评论0