canvas实现彩色迷宫动画代码
代码语言:html
所属分类:动画
代码描述:canvas实现彩色迷宫动画代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> </head> <body > <script> "use strict"; // Paul Slaymaker, paul25882@gmail.com const body = document.getElementsByTagName("body").item(0); body.style.background = "#000"; const TP = 2 * Math.PI; const CSIZE = 360; const ctx = (() => { let d = document.createElement("div"); d.style.textAlign = "center"; body.append(d); let c = document.createElement("canvas"); c.width = c.height = 2 * CSIZE; d.append(c); return c.getContext("2d"); })(); ctx.setTransform(1, 0, 0, 1, CSIZE, CSIZE); ctx.lineCap = "round"; onresize = () => { let D = Math.min(window.innerWidth, window.innerHeight) - 40; ctx.canvas.style.width = ctx.canvas.style.height = D + "px"; }; const getRandomInt = (min, max, low) => { if (low) return Math.floor(Math.random() * Math.random() * (max - min)) + min;else return Math.floor(Math.random() * (max - min)) + min; }; var colors = []; var hues = []; var setColors = () => { colors = []; let colorCount = 3; let h = getRandomInt(180, 270); for (let i = 0; i < colorCount; i++) { let hd = Math.round(180 / colorCount) * i + getRandomInt(-10, 10); let hue = (h + hd) % 360; hues.splice(getRandomInt(0, hues.length + 1), 0, hue); } for (let i = 0; i < colorCount; i++) colors[i] = "hsl(" + hues[i] + ",98%,40%)"; }; const df = [ point => {return pts[point.i + 1][point.j];}, point => {return pts[point.i][point.j + 1];}, point => {return pts[point.i - 1][point.j];}, point => {return pts[point.i][point.j - 1];}]; const iptX = [[2, 3, 1, 0], [3, 2, 0, 1]]; var getFalsePoint = () => { let pt = pts[getRandomInt(1, CT - 1, true)][getRandomInt(1, CT - 1, true)]; while (pt.d) { pt = pts[getRandomInt(1, CT - 1, true)][getRandomInt(1, CT - 1, true)]; } return pt; }; function SPath(idx) { let initPoint = getFalsePoint(); initPoint.d = true; initPoint.ppt = pts[0][0]; this.pa = [initPoint]; this.path; this.term; this.ipt = iptX[0]; this.grow = sec => { for (let c = 0; c < this.pa.length; c++) { let ci = this.pa.length - c - 1; let pt = this.pa[ci]; if (sec && pt == this.growPoint) continue; for (let ic = 0; ic < 4; ic++) { let dfunc = df[this.ipt[ic]]; let cpt = dfunc(pt); if (cpt.d) continue; if (cpt == this.shrinkPoint) continue; cpt.d = true; pt.cpa.push(cpt); cpt.ppt = pt; this.pa.push(cpt); if (sec) this.growPoint2 = cpt;else this.growPoint = cpt; return true; } } return false; }; this.setShrink = () => { this.shrinkPoint = false; if (this.pa.length < 3) { return; } let pt = this.pa[0]; if (pt.cpa.length < 2) { this.shrinkPoint = pt; return; } for (let i = 1; i < this.pa.length - 1; i++) {// -4 not needed? // always shrink from oldest end, if possible let pt = this.pa[i]; if (pt == this.growPoint) continue; if (pt.cpa.length == 0) { this.shrinkPoint = pt; return; } } this.shrinkPoint = false; }; this.shrink2 = () => { if (!this.shrinkPoint) return; let pt = this.shrinkPoint; if (this.pa[0] == pt) { if (!pt.ppt.d) pt.ppt = false; } else { if (pt.ppt.cpa.length) { let idx = pt.ppt.cpa.indexOf(pt); pt.ppt.cpa.splice(idx, 1); } } pt.cpa = []; let idx2 = this.pa.indexOf(pt); this.pa.splice(idx2, 1); pt.d = false; }; this.setPath = .........完整代码请登录后点击上方下载按钮下载查看
网友评论0