simplex-noise实现多彩方块背景效果

代码语言:html

所属分类:背景

代码描述:simplex-noise实现多彩方块背景效果

代码标签: 方块 背景 效果

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

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

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

canvas {
  display: block;
  cursor: pointer;
}
</style>

</head>
<body translate="no">
<canvas id="canvas"></canvas>
<script type="text/javascript" src="http://repo.bfw.wiki/bfwrepo/js/simplex-noise.min.js"></script>
<script >
/*
  Johan Karlsson, 2020
  https://twitter.com/DonKarlssonSan
  MIT License, see Details View
*/
let canvas;
let ctx;
let w, h;
let simplex;

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 draw() {
  simplex = new SimplexNoise();
  ctx.fillStyle = "white";
  ctx.fillRect(0, 0, w, h);
  drawSquares();
}
function drawSquares() {
  let size = Math.random() * 80 + 20;
  let baseHueOffset = Math.random() * 360;
  for(let x = 0; x < w + size; x+= size) {
    for(let y = 0; y < h + size; y+= size) {
      drawSquare(x, y, size, baseHueOffset);
    }
  }
}

function drawSquare(x, y, size, baseHueOffset) {
  ctx.save();
  let randomHueOffset = Math.random() * 10;
  let hue = (x + y) / 8 + 140 + randomHueOffset + baseHueOffs.........完整代码请登录后点击上方下载按钮下载查看

网友评论0