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 EM = location.href.endsWith("em"); const TP = 2 * Math.PI; const CSIZE = 400; 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.translate(CSIZE, CSIZE); ctx.globalCompositeOperation = "destination-over"; 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; }; function Color() { const CBASE = 160; const CT = 255 - CBASE; this.getRGB = () => { let red = Math.round(CBASE + CT * (this.fr * Math.cos(this.RK2 + t / this.RK1) + (1 - this.fr) * Math.cos(t / this.RK3))); let grn = Math.round(CBASE + CT * (this.fg * Math.cos(this.GK2 + t / this.GK1) + (1 - this.fg) * Math.cos(t / this.GK3))); let blu = Math.round(CBASE + CT * (this.fb * Math.cos(this.BK2 + t / this.BK1) + (1 - this.fb) * Math.cos(t / this.BK3))); return "rgb(" + red + "," + grn + "," + blu + ")"; }; this.randomizeF = () => { this.RK3 = 1 + 6 * Math.random(); this.GK3 = 1 + 6 * Math.random(); this.BK3 = 1 + 6 * Math.random(); this.fr = 1 - Math.pow(0.9 * Math.random(), 5); this.fg = 1 - Math.pow(0.9 * Math.random(), 5); this.fb = 1 - Math.pow(0.9 * Math.random(), 5); }; this.randomize = () => { this.RK1 = 160 + 160 * Math.random(); this.GK1 = 160 + 160 * Math.random(); this.BK1 = 160 + 160 * Math.random(); this.RK2 = TP * Math.random(); this.GK2 = TP * Math.random(); this.BK2 = TP * Math.random(); this.randomizeF(); }; this.randomize(); } var colors = [new Color(), new Color()]; const KT = 12000; var Circle = function (k1) { this.randomize = () => { this.maxr = 20 + 60 * Math.random(); this.ka = (80 + 160 * Math.random()) * [-1, 1][getRandomInt(0, 2)]; this.alpha = TP / 8 - TP / 4 * Math.random(); this.er = 1 + 3 * Math.pow(Math.random(), 4); this.erk = (20 + 100 * Math.random()) * [-1, 1][getRandomInt(0, 2)]; }; this.randomize(); this.getPath = () => { let p = new Path2D(); let rr = Math.max(0, this.r); p.ellipse(this.x, this.y, rr, rr / this.er, t / this.erk, 0, TP); return p; }; this.extend = () => { if (this.cc) { .........完整代码请登录后点击上方下载按钮下载查看
网友评论0