web建站教程
  1. 首页
  2. vuejs
  3. js
  4. 好玩
  5. AIGC工具
  6. 前端知识
  7. 百度echarts
  8. 更多
    php入门
    nodejs
    mockjs
    reactjs
    mysql
    wordpress
    织梦cms
    帝国cms
    git教程
    IT知识
    模板大全
    休息站
    手机应用

html5+canvas如何做一个彩色六角菱形背景

948 ℃

如何利用html5canvas做一个彩色六角菱形背景效果,下面web建站小编给大家详细介绍一下具体代码!

html代码:

<canvas id="canvas"></canvas>

js代码:

let canvas;
let ctx;
let w, h;
let hexagons;

class Hexagon {
  constructor(x, y, R) {
    this.x = x;
    this.y = y;
    this.R = R;
  }

  draw() {
    let baseHue = Math.random() * 360;
    ctx.save();
    ctx.translate(this.x, this.y);
    let angle = Math.PI * 2 / 3;
    let nrOfRotations = 3;
    let a = Math.PI / 6;
    for (let rot = 0; rot < nrOfRotations; rot++) {
      ctx.rotate(angle);

      setColor(baseHue);
      ctx.beginPath();
      ctx.moveTo(0, 0);
      let x1 = Math.cos(Math.PI / 3 + a) * this.R;
      let y1 = Math.sin(Math.PI / 3 + a) * this.R;
      ctx.lineTo(x1, y1);
      let x2 = Math.cos(a) * this.R;
      let y2 = Math.sin(a) * this.R;
      ctx.lineTo(x2, y2);
      ctx.lineTo(0, 0);
      ctx.fill();
      //ctx.stroke();

      setColor(baseHue);
      ctx.beginPath();
      ctx.moveTo(0, 0);
      ctx.lineTo(x2, y2);
      let x3 = Math.cos(-Math.PI / 3 + a) * this.R;
      let y3 = Math.sin(-Math.PI / 3 + a) * this.R;
      ctx.lineTo(x3, y3);
      ctx.lineTo(0, 0);
      ctx.fill();
      //ctx.stroke();
    }
    ctx.restore();
  }}


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

function setColor(baseHue) {
  let hueOffset = Math.random() * 5;
  let h = baseHue + hueOffset;
  let s = Math.random() * 5 + 40;
  let l = Math.random() * 10 + 60;
  let color = `hsl(${h}, ${s}%, ${l}%)`;
  ctx.strokeStyle = color;
  ctx.fillStyle = color;
}

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

function setupHexagons() {
  hexagons = [];
  let r = Math.random() * 80 + 20;
  let R = r / Math.cos(Math.PI / 6);
  let t = r * 2 / Math.sqrt(3);
  let rows = w / (r * 2) + 1;
  let cols = h / R;
  for (let x = 0; x < rows; x++) {
    for (let y = 0; y < cols; y++) {
      let xOffset = y % 2 === 0 ? r : 0;
      let xPixel = r * x * 2 + xOffset;
      let yPixel = (t / 2 + R) * y;
      let hexagon = new Hexagon(xPixel, yPixel, R);
      hexagons.push(hexagon);
    }
  }
}

function draw() {
  ctx.fillStyle = "grey";
  ctx.fillRect(0, 0, w, h);
  setupHexagons();
  hexagons.forEach(h => {
    h.draw();
  });
}

setup();
draw();
Trae:新一代免费的AI编程工具

一款用于在网页上生成五彩纸屑特效的JavaScript库——Canvas Confetti

利用js做一个炫酷音乐背景效果

svg标签属性有哪些?

js如何判断浏览器是否支持html5?

html5表单input控件有哪些?

标签: canvas, html5

上面是“html5+canvas如何做一个彩色六角菱形背景”的全面内容,想了解更多关于 js 内容,请继续关注web建站教程。

当前网址:https://ipkd.cn/webs_3482.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

当前位置: 网站首页 > js
本文共计1761个字,预计阅读时长12分钟
生活小工具,收录了80多款小工具
上一篇: 介绍几款带有中国风古韵的免费可商用中文字体
下一篇: 推荐9款支持 Vue3 免费开源的前端 UI 组件库
x 打工人ai神器