帝王谷资源网 Design By www.wdxyy.com
本文实例为大家分享了用canvas实现跟随鼠标事件的具体代码,供大家参考,具体内容如下
//鼠标移动 展现光片
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> body { margin: 0; overflow: hidden; } #canvas { background: #000; } </style> </head> <body> <canvas id="canvas"></canvas> <script> var canvas = document.getElementById('canvas'); var context = canvas.getContext('2d'); var circleList = []; canvas.width = window.innerWidth; canvas.height = window.innerHeight; canvas.addEventListener('mousemove', function (e) { // 将对象push到数组中,画出来的彩色小点可以看作每一个对象中记录着信息 然后存在数组中 circleList.push(new Circle(e.clientX, e.clientY)); }) //取x到y之间随机数:Math.round(Math.random()*(y-x)+x) 包括y function random(min, max) { return Math.round(Math.random() * (max - min) + min); } function Circle(x, y) { this.x = x; this.y = y; this.vx = (Math.random() - 0.5) * 3; //随机出来一个正数,或者负数。乘3是为了让速度变得大一点 this.vy = (Math.random() - 0.5) * 3; this.color = 'rgb(' + random(0, 255) + ',' + random(0, 255) + ',' + random(0, 255) + ')'; this.a = 1; // 初始透明度 this.draw(); } Circle.prototype = { draw() { context.beginPath(); context.fillStyle = this.color; context.globalCompositeOperation = 'lighter'; context.globalAlpha = this.a; //全局透明度 context.arc(this.x, this.y, 30, 0, Math.PI * 2, false); context.fill(); this.update(); }, update() { // 根据速度更新每一个小圆的位置 this.x += this.vx; this.y += this.vy; this.a *= 0.98; } } function render() { //把原来的内容区域清除掉 context.clearRect(0, 0, canvas.width, canvas.height); circleList.forEach(function (ele, i) { ele.draw(); if (ele.a < 0.05) { circleList.splice(i, 1); } }); requestAnimationFrame(render); //动画,会根据浏览器的刷新频率更新动画 } render(); </script> </body> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
帝王谷资源网 Design By www.wdxyy.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
帝王谷资源网 Design By www.wdxyy.com
暂无评论...
更新日志
2024年10月25日
2024年10月25日
- 群星2013-青春缤纷辑压箱宝大公开3CD2[新加坡限量版][WAV整轨]
- 林育群.2013-BalladShow(日本版)【环球】【WAV+CUE】
- 陈加洛.1992-痛到感觉不到【宝丽金】【WAV+CUE】
- 群星.2023-宿命之敌电视剧原声带【韶愔音乐】【FLAC分轨】
- 東京事変-大発見[FLAC+CUE]
- 椎名林檎-三文ゴシップ[FLAC+CUE]
- 2024年08月04日
- 裘德《裘德「最后的水族馆」演唱会LIVE》[320K/MP3][228.89MB]
- 裘德《裘德「最后的水族馆」演唱会LIVE》[24bit 48kHz][FLAC/分轨][2.08G]
- 基因三重奏《如果你什么都不说 音乐会现场录音》[320K/MP3][145.37MB]
- 孟庭苇.1996-月亮说话(2020环球24KGOLD限量版)【上华】【WAV+CUE】
- 群星.1997-新艺宝优质音响系列·国语精选监听版【新艺宝】【WAV+CUE】
- 阿桑.2005-寂寞在唱歌(星外星引进版)【华研国际】【WAV+CUE】
- 基因三重奏《如果你什么都不说 音乐会现场录音》[FLAC/分轨][287.43MB]
- 蔡题谦《我爱你,却依然要看你走》[320K/MP3][88.65MB]