canvas蒲公英背景绘制效果

代码语言:html

所属分类:背景

代码描述:canvas蒲公英背景绘制效果,通过js及canvas实现

代码标签: 绘制 效果

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

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">

<style>
body, html {
  margin: 0;
}

canvas {
  display: block;
}
</style>

</head>
<body translate="no">
<canvas id="canvas"></canvas>

<script >
/*
  Johan Karlsson, 2020
  https://twitter.com/DonKarlssonSan
  MIT License, see Details View
*/

class Circle {
  constructor(x, y) {
    this.x = x;
    this.y = y;
    this.r = 3;
  }
  
  draw() {
    let nrOfPoints = Math.round(this.r * 0.2 + 3);
    let deltaAngle = Math.PI * 2 / nrOfPoints;
    for(let i = 0; i < nrOfPoints; i += 1) {
      let angle = i * deltaAngle;
      let r = Math.random() * this.r * 0.32 + this.r * 0.65;
      let x = Math.cos(angle) * r;
      let y = Math.sin(angle) * r;
      let x2 = this.x + x;
      let y2 = this.y + y;
      ctx.beginPath();
      ctx.moveTo(this.x, this.y);
      ctx.lineTo(x2, y2);
      ctx.stroke();
      ctx.beginPath();
      ctx.arc(x2, y2, 4, 0, Math.PI * 2);
      ctx.fill();
    }
  }
}

let canvas;
let ctx;
let w, h;
let circles;

function setup() {
  canvas = document.querySelector("#canvas");
  ctx = canvas.getContext("2d");
  reset();
  window.addEventListener("resize", () => {
    reset();
    draw();
  });
  canvas.addEventListener("click", draw);
}

function reset() {
  w = canvas.width = window.innerWidth;
  h = canvas.height = window.innerHeight;
}

function clear() {
  ctx.fillStyle = "#224";
  ctx.fillRect(0, 0, w, h);
  ctx.fi.........完整代码请登录后点击上方下载按钮下载查看

网友评论0