canvas背景效果
2018-06-24 01:44:45来源:未知 阅读 ()
canvas背景效果:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <canvas id="canvas" width="800" height="800"></canvas> </body> <script> let canvas = document.getElementById('canvas'); let ctx = canvas.getContext('2d'); let w = canvas.width = canvas.offsetWidth; let h = canvas.height = canvas.offsetHeight; let circles = []; function rand(min, max) { return parseInt(Math.random() * (max - min + 1) + min); } class Circle { constructor() { this.r = rand(5, 15); let x = rand(0, canvas.width - this.r); let y = rand(0, canvas.height - this.r); this.x = x < this.r ? this.r : x; this.y = y < this.r ? this.r : y; let speed = rand(1, 3); this.speedX = rand(0, 4) > 2 ? speed : -speed; this.speedY = rand(0, 4) > 2 ? speed : -speed; } drawCircle(ctx) { ctx.beginPath(); ctx.arc(this.x, this.y, this.r, 0, 360); ctx.closePath(); ctx.fillStyle = 'rgba(204,204,204,0.3)'; ctx.fill(); } drawLine(ctx, _circle) { let dx = this.x - _circle.x; let dy = this.y - _circle.y; let d = Math.sqrt(dx * dx + dy * dy); if (d < 150) { ctx.beginPath(); ctx.moveTo(this.x, this.y); ctx.lineTo(_circle.x, _circle.y); ctx.closePath(); ctx.strokeStyle = 'rgba(204,204,204,0.3)'; ctx.stroke(); } } move(w, h) { this.x += this.speedX / 10; if (this.x > w || this.x < 0) { this.speedX *= -1; } this.y += this.speedY / 10; if (this.y > h || this.y < 0) { this.speedY *= -1; } } } class curCircle extends Circle { constructor() { super(); } drawCircle(ctx) { ctx.beginPath(); this.r = 5; ctx.arc(this.x, this.y, this.r, 0, 360); ctx.closePath(); ctx.fillStyle = 'rgba(255,77,54,0.6)'; ctx.fill(); } } let circle = new curCircle(); let draw = function () { ctx.clearRect(0, 0, w, h); for (let i = 0; i < circles.length; i++) { circles[i].drawCircle(ctx); for (j = i + 1; j < circles.length; j++) { circles[i].drawLine(ctx, circles[j]); } circles[i].move(w, h); } if (circle.x) { circle.drawCircle(ctx); for (let k = 1; k < circles.length; k++) { circle.drawLine(ctx, circles[k]) } } requestAnimationFrame(draw); }; let init = function (num) { for (let i = 0; i < num; i++) { circles.push(new Circle()); } draw(); }; window.addEventListener('load', init(20)); canvas.addEventListener('mousemove', function (e) { e = e || window.event; circle.x = e.offsetX; circle.y = e.offsetY; }); canvas.addEventListener('mouseout', function (e) { circle.x = null; circle.y = null; }); </script> </html>
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:【css】垂直居中的几种写法
下一篇:菜鸟的 Sass 学习笔记
- CSS导航栏下划线跟随效果 2020-06-11
- HTML+CSS+JS模仿win10亮度调节效果 2020-06-04
- css:背景(背景颜色、图片、平铺、背景固定、背景颜色半透 2020-06-01
- position: sticky实现导航栏下滑吸顶效果 2020-05-30
- 美化博客园样式 2020-05-17
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash