html5 canvas制作一个跟着鼠标移动消失的一堆炫彩小球
canvas
h5
html5
综合利用前面所学,实现一个绚丽的小球动画,这个实例用到的知识点
<head> <meta charset='utf-8' /> <title>canvas炫彩小球</title> <style> #canvas { border: 1px dashed #aaa; } </style> <script> function Ball(x, y, r, color) { this.x = x || 0; this.y = y || 0; this.radius = r || 20; this.color = color || '#09f'; } Ball.prototype = { constructor: Ball, stroke: function (cxt) { cxt.strokeStyle = this.color; cxt.beginPath(); cxt.arc(this.x, this.y, this.radius, 0, 2 * Math.PI); cxt.closePath(); cxt.stroke(); }, fill: function (cxt) { cxt.fillStyle = this.color; cxt.beginPath(); cxt.arc(this.x, this.y, this.radius, 0, 2 * Math.PI); cxt.closePath(); cxt.fill(); }, update : function( balls ){ this.x += this.vx; this.y += this.vy; this.radius--; if ( this.radius < 0 ) { for( var i = 0; i < balls.length; i++ ){ if( balls[i] == this ) { balls.splice( i, 1 ); } } return false; } return true; } } </script> <script> window.onload = function () { var oCanvas = document.querySelector("#canvas"), oGc = oCanvas.getContext('2d'), width = oCanvas.width, height = oCanvas.height, balls = [], n = 50; function getRandColor() { return '#' + (function (color) { return (color += '0123456789abcdef'[Math.floor(Math.random() * 16)]) && (color.length == 6) ? color : arguments.callee(color); })(''); } oCanvas.addEventListener( 'mousemove', function( ev ){ var oEvent = ev || event; var ball = new Ball( oEvent.clientX, oEvent.clientY, 30, getRandColor()); ball.vx = (Math.random() * 2 - 1) * 5; ball.vy = (Math.random() * 2 - 1) * 5; balls.push( ball ); }, false ); ( function move(){ oGc.clearRect( 0, 0, width, height ); for( var i = 0; i < balls.length; i++ ){ balls[i].update( balls ) && balls[i].fill( oGc ); } requestAnimationFrame( move ); } )(); } </script> </head> <body> <canvas id="canvas" width="1200" height="600"></canvas> </body>
登录后复制
以上是html5 canvas制作一个跟着鼠标移动消失的一堆炫彩小球 的详细内容。更多信息请关注PHP中文网其他相关文章!
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章
如何修复KB5055523无法在Windows 11中安装?
3 周前
By DDD
如何修复KB5055518无法在Windows 10中安装?
3 周前
By DDD
<🎜>:死铁路 - 如何驯服狼
4 周前
By DDD
R.E.P.O.的每个敌人和怪物的力量水平
4 周前
By 尊渡假赌尊渡假赌尊渡假赌
<🎜>:种植花园 - 完整的突变指南
2 周前
By DDD

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)