html canvas绘制网格,canvas如何绘制n个相切的蜂窝网格

Title

window.onload = function () {

const canvas = document.getElementsByTagName('canvas')[0]

const ctx = canvas.getContext("2d");

const w = document.documentElement.clientWidth;

const h = document.documentElement.clientHeight;

canvas.width = w;

canvas.height = h;

let mols = []

function init() {

let mol = new generate_mol(1)

mols.push(mol);

}

function draw(){

canvas.width = canvas.width;

for(var i=0;i

var m = mols[i];

m.draw();

}

}

function generate_mol(mol) {

this.x = w*0.3;

this.y = h*0.3;

// this.vx = Math.random()*-2;

// this.vy = Math.random()*2;

// this.vr = 0.1;

this.r = Math.random()*Math.PI;

this.draw = function() {

// ctx.save();

// ctx.translate(this.x + 20, this.y + 80);

// ctx.rotate(Math.PI * 0.17);

// ctx.translate(-this.x + 20, -this.y - 80);

// ctx.beginPath();

// ctx.moveTo(this.x, this.y + 30);

// ctx.lineTo(this.x - 26, this.y + 45);

// ctx.lineTo(this.x - 26, this.y + 75);

// ctx.lineTo(this.x, this.y + 90);

// ctx.lineTo(this.x + 26, this.y + 75);

// ctx.lineTo(this.x + 26, this.y + 45);

// ctx.lineTo(this.x, this.y + 30);

// ctx.moveTo(this.x - 20, this.y + 47);

// ctx.strokeStyle = "rgba(0,0,0,0.2)";

// ctx.lineWidth = 3;

// ctx.stroke();

// ctx.closePath();

// ctx.restore()

ctx.fillStyle = "rgba(0,0,0,0.5)";

ctx.font = "15px Arial";

ctx.fillText("A", this.x+40 , this.y+80);

ctx.save();

ctx.translate(this.x + 20, this.y + 30);

ctx.rotate(Math.PI * 0.17);

ctx.translate(-this.x + 20, -this.y - 80);

ctx.beginPath();

ctx.moveTo(this.x, this.y + 30);

ctx.lineTo(this.x - 26, this.y + 45);

ctx.lineTo(this.x - 26, this.y + 75);

ctx.lineTo(this.x, this.y + 90);

ctx.lineTo(this.x + 26, this.y + 75);

ctx.lineTo(this.x + 26, this.y + 45);

ctx.lineTo(this.x, this.y + 30);

ctx.moveTo(this.x - 20, this.y + 47);

ctx.strokeStyle = "rgba(0,0,0,0.2)";

ctx.lineWidth = 3;

ctx.stroke();

ctx.fillStyle = "rgba(0,0,0,0.5)";

ctx.font = "15px Arial";

ctx.fillText("B", this.x , this.y+70);

ctx.closePath();

ctx.restore()

ctx.save();

ctx.translate(this.x + 68, this.y + 56);

ctx.rotate(Math.PI * 0.17);

ctx.translate(-this.x + 20, -this.y - 80);

ctx.beginPath();

ctx.moveTo(this.x, this.y + 30);

ctx.lineTo(this.x - 26, this.y + 45);

ctx.lineTo(this.x - 26, this.y + 75);

ctx.lineTo(this.x, this.y + 90);

ctx.lineTo(this.x + 26, this.y + 75);

ctx.lineTo(this.x + 26, this.y + 45);

ctx.lineTo(this.x, this.y + 30);

ctx.moveTo(this.x - 20, this.y + 47);

ctx.strokeStyle = "rgba(0,0,0,0.2)";

ctx.lineWidth = 3;

ctx.stroke();

ctx.closePath();

ctx.restore()

ctx.save();

ctx.translate(this.x + 66, this.y + 110);

ctx.rotate(Math.PI * 0.17);

ctx.translate(-this.x + 20, -this.y - 80);

ctx.beginPath();

ctx.moveTo(this.x, this.y + 30);

ctx.lineTo(this.x - 26, this.y + 45);

ctx.lineTo(this.x - 26, this.y + 75);

ctx.lineTo(this.x, this.y + 90);

ctx.lineTo(this.x + 26, this.y + 75);

ctx.lineTo(this.x + 26, this.y + 45);

ctx.lineTo(this.x, this.y + 30);

ctx.moveTo(this.x - 20, this.y + 47);

ctx.strokeStyle = "rgba(0,0,0,0.2)";

ctx.lineWidth = 3;

ctx.stroke();

ctx.closePath();

ctx.restore()

ctx.save();

ctx.translate(this.x + 20, this.y + 135);

ctx.rotate(Math.PI * 0.17);

ctx.translate(-this.x + 20, -this.y - 80);

ctx.beginPath();

ctx.moveTo(this.x, this.y + 30);

ctx.lineTo(this.x - 26, this.y + 45);

ctx.lineTo(this.x - 26, this.y + 75);

ctx.lineTo(this.x, this.y + 90);

ctx.lineTo(this.x + 26, this.y + 75);

ctx.lineTo(this.x + 26, this.y + 45);

ctx.lineTo(this.x, this.y + 30);

ctx.moveTo(this.x - 20, this.y + 47);

ctx.strokeStyle = "rgba(0,0,0,0.2)";

ctx.lineWidth = 3;

ctx.stroke();

ctx.closePath();

ctx.restore()

//

ctx.save();

ctx.translate(this.x -27,this.y + 108);

ctx.rotate(Math.PI * 0.17);

ctx.translate(-this.x + 20, -this.y - 80);

ctx.beginPath();

ctx.moveTo(this.x, this.y + 30);

ctx.lineTo(this.x - 26, this.y + 45);

ctx.lineTo(this.x - 26, this.y + 75);

ctx.lineTo(this.x, this.y + 90);

ctx.lineTo(this.x + 26, this.y + 75);

ctx.lineTo(this.x + 26, this.y + 45);

ctx.lineTo(this.x, this.y + 30);

ctx.moveTo(this.x - 20, this.y + 47);

ctx.strokeStyle = "rgba(0,0,0,0.2)";

ctx.lineWidth = 3;

ctx.stroke();

ctx.closePath();

ctx.restore()

ctx.save();

ctx.translate(this.x -26, this.y + 56);

ctx.rotate(Math.PI * 0.17);

ctx.translate(-this.x + 20, -this.y - 80);

ctx.beginPath();

ctx.moveTo(this.x, this.y + 30);

ctx.lineTo(this.x - 26, this.y + 45);

ctx.lineTo(this.x - 26, this.y + 75);

ctx.lineTo(this.x, this.y + 90);

ctx.lineTo(this.x + 26, this.y + 75);

ctx.lineTo(this.x + 26, this.y + 45);

ctx.lineTo(this.x, this.y + 30);

ctx.moveTo(this.x - 20, this.y + 47);

ctx.strokeStyle = "rgba(0,0,0,0.2)";

ctx.lineWidth = 3;

ctx.stroke();

ctx.closePath();

ctx.restore()

}

}

init()

draw()

}

没画好 画偏了

应该还要加算法 来生成指定个数的

预览地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值