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 = 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); 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 start() { if (stopped) { requestAnimationFrame(animate); stopped = false; } else { stopped = true; } } ctx.canvas.addEventListener("click", start, false); var rotation = 0; var stopped = true; var t = 0; var duration = 1600; function animate(ts) { if (stopped) return; t++; rotation += 0.02 / C; for (let i = 0; i < 2; i++) { F += 0.0024 / C; F2 += 0.0031 / C; lines[i].setLine(); if (i % 2) lines[i].rotate(rotation);else lines[i].rotate(-rotation); } K = 120 + 100 * Math.sin(t / 60); K2 = 120 + 100 * Math.sin(t / 70); color.set(); color2.set(); color3.set(); draw(); requestAnimationFrame(animate); } ctx.lineWidth = 2; var F = TP * Math.random(); var F2 = TP * Math.random(); var R = 380; var K = 120; var K2 = 100; var Line = function (idx) { this.a = idx * TP / C + (C / 4 - 1) * TP / C / 2; this.dp1 = new DOMPoint(); this.dp2 = new DOMPoint(); this.rotate = z => { let dm2 = new DOMMatrix([Math.cos(z), Math.sin(z), -Math.sin(z), Math.cos(z), this.mx, this.my]); let dm3 = dm2.multiply(this.dm1);.........完整代码请登录后点击上方下载按钮下载查看
网友评论0