canvas实现鼠标跟随模拟物理绳子甩动交互效果代码
代码语言:html
所属分类:其他
代码描述:canvas实现鼠标跟随模拟物理绳子甩动交互效果代码
代码标签: canvas 鼠标 跟随 模拟 物理 绳子 甩动 交互
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <style> body{ background-color:#1A1B1F; } #container{ max-width:800px; height:480px; background-color:black; margin: 0 auto; -webkit-box-sizing::border-box; border-style: solid; border-color: white; } #container p{ color:white; margin-left:25px; } </style> </head> <body> <div id="container"> <canvas id="canvas" width="800" height="480"></canvas> <p>Verlet Physics Rope (move the mouse!)</p> </div> <script > //A small scaffold specifically to help me design code pen interactions //Math extensions Math.lerp = (first, second, percentage) => { return first + (second - first) * percentage; }; Math.clamp = (value, min, max) => { return value < min ? min : value > max ? max : value; }; class Vector2 { static zero() { return { x: 0, y: 0 }; } static sub(a, b) { return { x: a.x - b.x, y: a.y - b.y }; } static add(a, b) { return { x: a.x + b.x, y: a.y + b.y }; } static mult(a, b) { return { x: a.x * b.x, y: a.y * b.y }; } static scale(v, scaleFactor) { return { x: v.x * scaleFactor, y: v.y * scaleFactor }; } static mag(v) { return Math.sqrt(v.x * v.x + v.y * v.y); } static normalized(v) { const mag = Vector2.mag(v); if (mag === 0) { return Vector2.zero(); } return { x: v.x / mag, y: v.y / mag }; }} class App { co.........完整代码请登录后点击上方下载按钮下载查看
网友评论0