当前位置:首页 > 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
分享给朋友:

相关文章

js 实现继承

js 实现继承

原型链继承 通过让子类的原型对象指向父类的实例来实现继承。子类实例可以访问父类原型上的属性和方法。 function Parent() { this.name = 'parent'; } Par…

js分组实现

js分组实现

分组实现方法 在JavaScript中,可以通过多种方式实现数组或对象的分组操作。以下是几种常见的方法: 使用Array.prototype.reduce() 通过reduce方法可以轻松实现数组分…

js实现延迟

js实现延迟

实现延迟的方法 在JavaScript中,实现延迟操作有多种方式,以下是几种常见的方法: 使用setTimeout函数 setTimeout是JavaScript中最常用的延迟执行方法。它接受一个回…

利用js实现

利用js实现

使用 JavaScript 实现 在 JavaScript 中,可以通过多种方式实现功能,具体取决于需求。以下是几种常见的方法: 方法一:使用原生 JavaScript // 示例代码…

js钟表实现

js钟表实现

实现JavaScript钟表的基本方法 创建一个简单的JavaScript钟表可以通过以下步骤完成,涵盖数字和模拟两种形式。 数字钟表实现 HTML结构只需一个显示时间的容器: <div i…

js 实现跳转

js 实现跳转

使用 window.location.href 进行跳转 通过修改 window.location.href 可以跳转到指定 URL,浏览器会加载新页面: window.location.hre…