canvas牛顿摆动画效果代码

代码语言:html

所属分类:动画

代码描述:canvas牛顿摆动画效果代码

代码标签: canvas 牛顿摆 动画

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en" >

<head>

 
<meta charset="UTF-8">


 
 
<style>
body
{
 
width: 100vw;
 
height: 100vh;
 
margin: 0;
 
overflow: hidden;
}

.container {
 
width: 100%;
 
height: 100%;
 
display: flex;
 
justify-content: center;
 
align-items: center;
 
background: #0d0d0d;
}

.canvas {
 
width: 600px;
 
height: 600px;
 
background: antiquewhite;
 
border: 10px solid dimgrey;
}
</style>


</head>

<body  >
 
<div class="container">
                       
<canvas class="canvas"></canvas>
</div>

 
     
<script  >
const $canvas = document.querySelector('.canvas');
const context = $canvas.getContext('2d');
const pixelRatio = window.devicePixelRatio || 1;
const canvasStyleWidth = $canvas.clientWidth;
const canvasStyleHeight = $canvas.clientHeight;
$canvas.width = canvasStyleWidth * pixelRatio;
$canvas.height = canvasStyleHeight * pixelRatio;
context.scale(pixelRatio, pixelRatio);
const middleX = canvasStyleWidth / 2;

const platFormYPos = 450;
const platformWidth = 500;
const platformFrontHeight = 50;
const backWidth = 300;
const backFrameBottomY = 410;
const backFrameWidth = 270;
const backFrameHeight = 200;
const frontFrameBottomY = 440;
const frontFrameWidth = 360;
const frontFrameHeight = 270;
const numBalls = 5;
const ballRadius = backFrameWidth / numBalls / 2;
const ballMiddleY = 380;
const frontFrameSingleSegment = frontFrameWidth / (numBalls + 1);
const backFrameSingleSegment = backFrameWidth / (numBalls + 1);
let rotationA = 0;
let backRotationA = 0;
let rotationB = 0;
let backRotationB = 0;
let swingTurn = 1;
let rotationACounter = 0;
let rotationBCounter = 0;


function drawPlatform() {
  // front edge
  context.save();
  context.translate(middleX, platFormYPos);
  let x = -(platformWidth / 2);
  let y = 0;
  context.fillStyle = '#000';
  context.fillRect(x, y, platformWidth, platformFrontHeight);
  const stripHeight = 20;
  y = (platformFrontHeight - stripHeight) / 2;
  context.fillStyle = '#626262';
  context.fillRect(x, y, p.........完整代码请登录后点击上方下载按钮下载查看

网友评论0