前段时间在研究canvas,感觉还挺好玩的,就写了一个小demo,效果如下:
第一次尝试用js面向对象的方式来写,经验不足,还请大家多多包涵。
下面开始简单介绍代码:
canvas画布:
复制代码 代码如下:<canvas id='canvas' width='1050' height='500' style='background:#333;overflow: hidden;'></canvas>
彩虹球的随机颜色是通过下面两个方法来实现的,在《js常用方法和一些封装(2) -- 随机数生成》中曾经提到过。
/** * 获取 0 ~ num的随机数(闭区间) */ function randomNum(num){ return Math.floor(Math.random()*(num+1)); }; /** * 获取随机颜色(支持任意浏览器) */ function randomColor16(){ //0-255 var r = randomNum(255).toString(16); var g = randomNum(255).toString(16); var b = randomNum(255).toString(16); //255的数字转换成十六进制 if(r.length<2)r = "0"+r; if(g.length<2)g = "0"+g; if(b.length<2)b = "0"+b; return "#"+r+g+b; };
每当我鼠标点下,其实是在一个矩形区域随机产生不同颜色的彩虹球,彩虹球出现的位置也是随机的,通过范围随机数来实现:
/* * 获取范围随机数 (闭区间) */ function randomRange(start,end){ return Math.floor(Math.random()*(end-start+1))+start; };
接下来是彩虹球的类,用面向对象来做。
//彩虹球的类 var ColorBall = function(){} ColorBall.prototype.left = 0; //X轴 ColorBall.prototype.top = 0; //y轴 ColorBall.prototype.r = 10; //半径
在本案例中,鼠标相当于是彩虹球喷枪,我们也用面向对象的思想来设计它:
//RainbowBrush 彩虹球喷枪 RainbowBrush = function(){} //生产小球数组的方法 RainbowBrush.prototype.getBalls = function(num){ var colorBalls = []; for(var i = 0; i < num; i++){ var ball = new ColorBall(); colorBalls.push(ball); } return colorBalls; } //喷刷彩虹球 RainbowBrush.prototype.brush = function(context,balls,x,y){ balls.forEach(function(ballIem){ ballIem.x = randomRange(x - 6,x + 6); ballIem.y = randomRange(y - 6,y + 6); ballIem.r = 5; context.beginPath(); context.fillStyle=randomColor16(); context.arc(ballIem.x,ballIem.y,ballIem.r,0,Math.PI*2); context.fill(); }) }
它有两个方法,一个是生产彩虹球,另一个是喷刷彩虹球。
案例的主要逻辑如下:
var rainbowBrush = new RainbowBrush(); //初始化彩虹球喷枪 var balls = rainbowBrush.getBalls(1); //当鼠标按下 canvasDom.onmousedown = function(){ var flag = true; canvasDom.onmousemove = function(e){ var x = e.clientX; var y = e.clientY; if(flag) rainbowBrush.brush(context,balls,x,y); } canvasDom.onmouseup = function(){ flag = false; } }
案例全部代码:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>彩虹球喷枪</title> </head> <body> <canvas id='canvas' width='1050' height='500' style='background:#333;overflow: hidden;'></canvas> </body> <script type="text/javascript"> /** * 获取 0 ~ num的随机数(闭区间) */ function randomNum(num){ return Math.floor(Math.random()*(num+1)); }; /* * 获取范围随机数 (闭区间) */ function randomRange(start,end){ return Math.floor(Math.random()*(end-start+1))+start; }; /** * 获取随机颜色(支持任意浏览器) */ function randomColor16(){ //0-255 var r = randomNum(255).toString(16); var g = randomNum(255).toString(16); var b = randomNum(255).toString(16); //255的数字转换成十六进制 if(r.length<2)r = "0"+r; if(g.length<2)g = "0"+g; if(b.length<2)b = "0"+b; return "#"+r+g+b; }; var canvasDom = document.getElementById('canvas'); var context = canvasDom.getContext('2d'); var maxWidth = 1050; var maxHeight = 500; //彩虹球的类 var ColorBall = function(){} ColorBall.prototype.left = 0; //X轴 ColorBall.prototype.top = 0; //y轴 ColorBall.prototype.r = 10; //半径 //RainbowBrush 彩虹球喷枪 RainbowBrush = function(){} //生产小球数组的方法 RainbowBrush.prototype.getBalls = function(num){ var colorBalls = []; for(var i = 0; i < num; i++){ var ball = new ColorBall(); colorBalls.push(ball); } return colorBalls; } //喷刷彩虹球 RainbowBrush.prototype.brush = function(context,balls,x,y){ balls.forEach(function(ballIem){ ballIem.x = randomRange(x - 6,x + 6); ballIem.y = randomRange(y - 6,y + 6); ballIem.r = 5; context.beginPath(); context.fillStyle=randomColor16(); context.arc(ballIem.x,ballIem.y,ballIem.r,0,Math.PI*2); context.fill(); }) } var rainbowBrush = new RainbowBrush(); //初始化彩虹球喷枪 var balls = rainbowBrush.getBalls(1); //当鼠标按下 canvasDom.onmousedown = function(){ var flag = true; canvasDom.onmousemove = function(e){ var x = e.clientX; var y = e.clientY; if(flag) rainbowBrush.brush(context,balls,x,y); } canvasDom.onmouseup = function(){ flag = false; } } </script> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“js面向对象实现canvas制作彩虹球喷枪效果”评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新动态
2024年05月09日
2024年05月09日
- 欢乐钓鱼大师银翼的奇术师怎么钓 欢乐钓鱼大师银翼的奇术师钓鱼攻略
- 陈琳.1994-你的柔情我永远不懂[WAV]
- 邓瑞霞2006-广东风云2IN1[南方]2CD[WAV+CUE]
- 黄征-爱情诺曼底-2003-APE整轨
- 妥协只是暂时的!《对马岛之魂》PC版的多人模式仍需绑定PSN
- 开启抢先体验后 《盗贼之海》成PS美国第二畅销游戏
- 《恶意不息》老板再用中文询问中国玩家:对汉化是否满意
- 2个女生.2000-坚固友情精选辑【EMI百代】【WAV+CUE】
- 潘越云李丽芬吴楚楚.1981-三人展(滚石民歌巨星系列)【滚石】【WAV+CUE】.
- 元若蓝.2006-爱无限大【滚石】【WAV+CUE】
- 刘德华《经典传说·粤语精选集》[WAV/分轨][610MB]
- 庄心妍《2020年新专辑歌曲 念》[WAV/分轨][400MB]
- 群星《音色精美绝伦 粤语经典 2CD》[WAV+CUE][1.4GB]
- 欢乐钓鱼大师不充钱怎么玩 欢乐钓鱼大师不充钱搞鱼竿玩法介绍
- 欢乐钓鱼大师隐藏奖励有什么 欢乐钓鱼大师隐藏奖励介绍