点击查看html编辑器说明文档

单摆edit icon

|
|
Fork(复制)
|
|
作者:
柳峰

👉 新版编辑器已上线,点击进行体验吧!

BUG反馈
嵌入
设置
下载
HTML
格式化
支持Emmet,输入 p 后按 Tab键试试吧!
<head> ...
展开
</head>
<body>
            
            <!DOCTYPE html>
<html>
<head>
    <title>神奇的单摆实验室</title>
    <style>
        body {
            background: linear-gradient(180deg, #83a4d4, #b6fbff);
            font-family: 'Comic Sans MS', cursive;
            display: flex;
            padding: 20px;
        }

        #controls {
            background: #fff3d6;
            padding: 20px;
            border-radius: 15px;
            box-shadow: 0 0 20px rgba(0,0,0,0.2);
            margin-right: 20px;
            min-width: 250px;
        }

        canvas {
            background: #fff;
            border-radius: 15px;
            box-shadow: 0 0 20px rgba(0,0,0,0.2);
            border: 5px solid #ff9a9e;
        }

        .slider-container {
            margin: 15px 0;
        }

        label {
            color: #ff6b6b;
            font-size: 18px;
        }

        input[type="range"] {
            width: 100%;
            margin: 10px 0;
            -webkit-appearance: none;
            height: 15px;
            border-radius: 10px;
            background: #ffe66d;
        }

        input[type="range"]::-webkit-slider-thumb {
            -webkit-appearance: none;
            width: 25px;
            height: 25px;
            background: #ff6b6b;
            border-radius: 50%;
            cursor: pointer;
        }

        #values {
            background: #4ecdc4;
            padding: 15px;
            border-radius: 10px;
            color: white;
            font-size: 18px;
            margin-top: 20px;
        }

        h1 {
            color: #ff6b6b;
            text-align: center;
            text-shadow: 2px 2px white;
        }
    </style>
</head>
<body>
    <div id="controls">
        <h1>神奇摆摆实验室</h1>
        <div class="slider-container">
            <label>摆长调节器:</label>
            <input type="range" id="length" min="50" max="300" value="150" step="10">
        </div>
        <div id="values">
            <div>当前摆长:<span id="currentLength">150</span> cm</div>
            <div>摆动周期:<span id="period">2.45</span> 秒</div>
            <div>✨公式小贴士:T = 2π√(L/g)</div>
        </div>
    </div>
    <canvas id="pendulumCanvas" width="600" height="500"></canvas>

    <script>
        const canvas = document.getElementById('pendulumCanvas');
        const ctx = canvas.getContext('2d');
        let length = 150;
        let angle = Math.PI/4;
        let velocity = 0;
        const gravity = 9.8;
        let lastTime = 0;

        function drawPendulum() {
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            
            // 绘制支架
            ctx.fillStyle = '#ff9a9e';
            ctx.fillRect(290, 50, 20, 40);
            
            // 绘制摆绳
            const pivotX = 300;
            const pivotY = 100;
            const bobX = pivotX + length * Math.sin(angle);
            const bobY = pivotY + length * Math.cos(angle);
            
            ctx.beginPath();
            ctx.moveTo(pivotX, pivotY);
            ctx.lineTo(bobX, bobY);
            ctx.strokeStyle = '#4a5568';
            ctx.lineWidth = 3;
            ctx.stroke();
            
            // 绘制摆锤
            ctx.beginPath();
            ctx.arc(bobX, bobY, 20, 0, Math.PI*2);
            ctx.fillStyle = '#ff6b6b';
            ctx.fill();
            ctx.strokeStyle = '#ff4757';
            ctx.lineWidth = 2;
            ctx.stroke();
        }

        function update(timestamp) {
            if (!lastTime) lastTime = timestamp;
            const deltaTime = (timestamp - lastTime) / 1000;
            lastTime = timestamp;
            
            const acceleration = -gravity / (length/100) * Math.sin(angle);
            velocity += acceleration * deltaTime;
            angle += velocity * deltaTime;
            
            drawPendulum();
            requestAnimationFrame(update);
        }

        document.getElementById('length').addEventListener('input', function(e) {
            length = parseInt(e.target.value);
            document.getElementById('currentLength').textContent = length;
            const period = (2 * Math.PI * Math.sqrt((length/100)/9.8)).toFixed(2);
            document.getElementById('period').textContent = period;
        });

        // 添加云朵装饰
        function drawCloud(x, y) {
            ctx.fillStyle = 'white';
            ctx.beginPath();
            ctx.arc(x, y, 20, 0, Math.PI*2);
            ctx.arc(x+15, y-10, 15, 0, Math.PI*2);
            ctx.arc(x+15, y+10, 15, 0, Math.PI*2);
            ctx.arc(x+30, y, 20, 0, Math.PI*2);
            ctx.fill();
        }

        // 初始绘制
        drawPendulum();
        requestAnimationFrame(update);
    </script>
</body>
</html>
        
编辑器加载中
</body>
CSS
格式化
            
            /* 示例代码 */
body {
  text-align: center;
}
i {
  color: #777;
}

        
编辑器加载中
JS
格式化
            
            // 示例代码
console.log(["Hello 笔.COOL 控制台"])
        
编辑器加载中
预览
控制台
清空