当前位置:首页 > JavaScript

js实现摆针

2026-03-15 01:22:45JavaScript

js实现摆针

js实现摆针

以下是使用JavaScript实现摆针动画的几种方法,涵盖基础实现和优化方案:

使用Canvas绘制摆针动画

const canvas = document.getElementById('pendulumCanvas');
const ctx = canvas.getContext('2d');
const width = canvas.width = 400;
const height = canvas.height = 400;
const pivotX = width / 2;
const pivotY = height / 4;
const rodLength = 150;
const bobRadius = 20;
let angle = Math.PI / 4;
let angularVelocity = 0;
const damping = 0.995;
const gravity = 0.5;

function update() {
    const angularAcceleration = -gravity / rodLength * Math.sin(angle);
    angularVelocity += angularAcceleration;
    angularVelocity *= damping;
    angle += angularVelocity;
}

function draw() {
    ctx.clearRect(0, 0, width, height);
    const bobX = pivotX + rodLength * Math.sin(angle);
    const bobY = pivotY + rodLength * Math.cos(angle);

    ctx.beginPath();
    ctx.moveTo(pivotX, pivotY);
    ctx.lineTo(bobX, bobY);
    ctx.strokeStyle = '#333';
    ctx.lineWidth = 3;
    ctx.stroke();

    ctx.beginPath();
    ctx.arc(bobX, bobY, bobRadius, 0, Math.PI * 2);
    ctx.fillStyle = '#c00';
    ctx.fill();
    ctx.stroke();
}

function animate() {
    update();
    draw();
    requestAnimationFrame(animate);
}
animate();

使用CSS动画模拟摆针

<style>
.pendulum {
    position: relative;
    width: 2px;
    height: 200px;
    background: #333;
    margin: 0 auto;
    transform-origin: top center;
    animation: swing 2s ease-in-out infinite alternate;
}
.bob {
    position: absolute;
    bottom: -20px;
    left: -18px;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: #c00;
}
@keyframes swing {
    0% { transform: rotate(30deg); }
    100% { transform: rotate(-30deg); }
}
</style>
<div class="pendulum">
    <div class="bob"></div>
</div>

使用物理引擎实现精确模拟(如matter.js)

const engine = Matter.Engine.create();
const render = Matter.Render.create({
    element: document.body,
    engine: engine,
    options: {
        width: 800,
        height: 600,
        wireframes: false
    }
});

const pendulum = Matter.Composites.stack(400, 100, 1, 1, 0, 0, function() {
    return Matter.Bodies.circle(0, 0, 20, {
        restitution: 0.8,
        friction: 0.005
    });
});

Matter.Composite.add(pendulum, Matter.Constraint.create({
    pointA: { x: 400, y: 100 },
    bodyB: pendulum.bodies[0],
    length: 200,
    stiffness: 0.05
}));

Matter.Composite.add(engine.world, pendulum);
Matter.Engine.run(engine);
Matter.Render.run(render);

优化建议

  • 添加鼠标交互功能允许用户拖动摆针
  • 实现多摆针系统展示混沌效应
  • 添加能量损耗参数使摆动逐渐停止
  • 使用WebGL(如Three.js)实现3D摆针效果

以上方法可根据需求选择,Canvas方案最适合需要精确物理模拟的场景,CSS方案适合简单UI动画,物理引擎方案适合复杂交互系统。

标签: js
分享给朋友:

相关文章

vue.js实现轮播

vue.js实现轮播

vue.js实现轮播的方法 使用第三方库(如Swiper) 安装Swiper库: npm install swiper 在Vue组件中引入Swiper: <template>…

js实现预览

js实现预览

文件上传预览实现 使用JavaScript实现文件上传预览功能,可以通过FileReader对象读取文件内容并显示预览。以下代码支持图片、文本和PDF文件的预览: // HTML部分需要包含inpu…

jquery js

jquery js

jQuery 简介 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。它兼容多种浏览器,提供易于使用的 API,使开发者能够更高…

js怎么实现异步

js怎么实现异步

异步实现方法 JavaScript中实现异步操作主要通过以下几种方式: Promise Promise是ES6引入的异步解决方案,用于处理异步操作的成功或失败状态。 const promise =…

js实现按钮点击

js实现按钮点击

实现按钮点击的JavaScript方法 HTML按钮元素 在HTML中创建按钮元素,为其添加id或class以便JavaScript选择: <button id="myButton">点…

js 实现图片 放大

js 实现图片 放大

使用 CSS transform 实现图片放大 通过 CSS 的 transform: scale() 属性可以实现图片的平滑放大效果。结合 JavaScript 监听鼠标事件控制放大状态: con…