canvas实现星星下落动画效果代码

代码语言:html

所属分类:动画

代码描述:canvas实现星星下落动画效果代码

代码标签: canvas 星星 下落 动画

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

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

<head>
  <meta charset="UTF-8">

  
  
  
  
<style>
html, body {
  background: #323232;
  margin: 0px;
  padding: 0px;
}

canvas {
  display: block;
}
</style>


  
  
</head>

<body translate="no">
  <canvas id="canvas" width="800" height="600"></canvas>
  
      <script >
let canvas = document.querySelector('#canvas'),
ctx = canvas.getContext('2d'),
w = 800,
h = 600;

const handleResize = () => {
  w = canvas.width = window.innerWidth;
  h = canvas.height = window.innerHeight;
};
window.onresize = () => handleResize();
handleResize();

let starConfigs = [];

const mapToRange = (number, inMin, inMax, outMin, outMax) => {return (number - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;};

for (let i = 0; i < 100; i++) {
  let size = mapToRange(Math.random(), 0, 1, 10, 30);
  let config = {
    fill: `hsl(${mapToRange(Math.random(), 0, 1, 10, 60)}, 60%, 50%)`,
    numPoints: 5,
    origin: { x: Math.random() * w, y: mapToRange(Math.random(), 0, 1, -size, h) },
    radiusInner: size * .6,
    radiusOuter: size,
    rotation: Math.random() * (Math.PI / 2),
    rotationSpeed: mapToRange(Math.random(), 0, 1, 0..........完整代码请登录后点击上方下载按钮下载查看

网友评论0