前段時間在研究canvas详炬,感覺還挺好玩的嫡良,就寫了一個小demo袜炕,效果如下:
canvas.gif
第一次嘗試用js面向?qū)ο蟮姆绞絹韺懕久眨?jīng)驗不足,還請大家多多包涵偎窘。
下面開始簡單介紹代碼:
canvas畫布:
<canvas id='canvas' width='1050' height='500' style='background:#333;overflow: hidden;'></canvas>
彩虹球的隨機顏色是通過下面兩個方法來實現(xiàn)的乌助,在《js常用方法和一些封裝(2) -- 隨機數(shù)生成》中曾經(jīng)提到過溜在。
/**
* 獲取 0 ~ num的隨機數(shù)(閉區(qū)間)
*/
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的數(shù)字轉(zhuǎn)換成十六進制
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;
};
每當我鼠標點下,其實是在一個矩形區(qū)域隨機產(chǎn)生不同顏色的彩虹球他托,彩虹球出現(xiàn)的位置也是隨機的掖肋,通過范圍隨機數(shù)來實現(xiàn):
/*
* 獲取范圍隨機數(shù) (閉區(qū)間)
*/
function randomRange(start,end){
return Math.floor(Math.random()*(end-start+1))+start;
};
接下來是彩虹球的類,用面向?qū)ο髞碜觥?/p>
//彩虹球的類
var ColorBall = function(){}
ColorBall.prototype.left = 0; //X軸
ColorBall.prototype.top = 0; //y軸
ColorBall.prototype.r = 10; //半徑
在本案例中赏参,鼠標相當于是彩虹球噴槍志笼,我們也用面向?qū)ο蟮乃枷雭碓O(shè)計它:
//RainbowBrush 彩虹球噴槍
RainbowBrush = function(){}
//生產(chǎn)小球數(shù)組的方法
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();
})
}
它有兩個方法,一個是生產(chǎn)彩虹球把篓,另一個是噴刷彩虹球纫溃。
案例的主要邏輯如下:
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的隨機數(shù)(閉區(qū)間)
*/
function randomNum(num){
return Math.floor(Math.random()*(num+1));
};
/*
* 獲取范圍隨機數(shù) (閉區(qū)間)
*/
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的數(shù)字轉(zhuǎn)換成十六進制
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(){}
//生產(chǎn)小球數(shù)組的方法
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>