欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js面向?qū)ο髮?shí)現(xiàn)canvas制作彩虹球噴槍效果

 更新時(shí)間:2016年09月24日 10:48:11   作者:剽悍一小兔  
這篇文章主要介紹了js面向?qū)ο髮?shí)現(xiàn)canvas制作彩虹球噴槍效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前段時(shí)間在研究canvas,感覺還挺好玩的,就寫了一個(gè)小demo,效果如下:


第一次嘗試用js面向?qū)ο蟮姆绞絹?lái)寫,經(jīng)驗(yàn)不足,還請(qǐng)大家多多包涵。 

下面開始簡(jiǎn)單介紹代碼: 

canvas畫布:

復(fù)制代碼 代碼如下:
<canvas id='canvas' width='1050' height='500' style='background:#333;overflow: hidden;'></canvas>

彩虹球的隨機(jī)顏色是通過下面兩個(gè)方法來(lái)實(shí)現(xiàn)的,在《js常用方法和一些封裝(2) -- 隨機(jī)數(shù)生成》中曾經(jīng)提到過。

 /**
 * 獲取 0 ~ num的隨機(jī)數(shù)(閉區(qū)間)
 */
function randomNum(num){
 return Math.floor(Math.random()*(num+1));
};

/**
 * 獲取隨機(jī)顏色(支持任意瀏覽器)
 */
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)換成十六進(jì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;
}; 

       

每當(dāng)我鼠標(biāo)點(diǎn)下,其實(shí)是在一個(gè)矩形區(qū)域隨機(jī)產(chǎn)生不同顏色的彩虹球,彩虹球出現(xiàn)的位置也是隨機(jī)的,通過范圍隨機(jī)數(shù)來(lái)實(shí)現(xiàn):

 /*
 * 獲取范圍隨機(jī)數(shù) (閉區(qū)間)
 */
function randomRange(start,end){
 return Math.floor(Math.random()*(end-start+1))+start;
}; 

接下來(lái)是彩虹球的類,用面向?qū)ο髞?lái)做。

 //彩虹球的類
var ColorBall = function(){}

ColorBall.prototype.left = 0; //X軸
ColorBall.prototype.top = 0; //y軸
ColorBall.prototype.r = 10; //半徑 

在本案例中,鼠標(biāo)相當(dāng)于是彩虹球噴槍,我們也用面向?qū)ο蟮乃枷雭?lái)設(shè)計(jì)它:

 //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();
 })
 
} 


它有兩個(gè)方法,一個(gè)是生產(chǎn)彩虹球,另一個(gè)是噴刷彩虹球。 

案例的主要邏輯如下:

 var rainbowBrush = new RainbowBrush(); //初始化彩虹球噴槍
var balls = rainbowBrush.getBalls(1);

//當(dāng)鼠標(biāo)按下
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的隨機(jī)數(shù)(閉區(qū)間)
   */
  function randomNum(num){
   return Math.floor(Math.random()*(num+1));
  };

  /*
   * 獲取范圍隨機(jī)數(shù) (閉區(qū)間)
   */
  function randomRange(start,end){
   return Math.floor(Math.random()*(end-start+1))+start;
  };
  
  
  /**
   * 獲取隨機(jī)顏色(支持任意瀏覽器)
   */
  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)換成十六進(jì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);

  //當(dāng)鼠標(biāo)按下
  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>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 基于JS實(shí)現(xiàn)仿百度百家主頁(yè)的輪播圖效果

    基于JS實(shí)現(xiàn)仿百度百家主頁(yè)的輪播圖效果

    本文給大家分享基于html和js實(shí)現(xiàn)的仿百度百家主頁(yè)的輪播圖效果,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下
    2017-03-03
  • JavaScript+html5 canvas繪制繽紛多彩的三角形效果完整實(shí)例

    JavaScript+html5 canvas繪制繽紛多彩的三角形效果完整實(shí)例

    這篇文章主要介紹了JavaScript+html5 canvas繪制繽紛多彩的三角形效果,以完整實(shí)例形式分析了html5的canvas繪制圖形的相關(guān)技巧,需要的朋友可以參考下
    2016-01-01
  • js攔截alert對(duì)話框另類應(yīng)用

    js攔截alert對(duì)話框另類應(yīng)用

    alert對(duì)話框的使用給用戶帶來(lái)了很多的方便,而有些時(shí)候不希望讓它存在,接下來(lái)介紹如何使用js攔截alert對(duì)話框,感興趣的朋友可以了解下
    2013-01-01
  • js圖片延遲加載的實(shí)現(xiàn)方法及思路

    js圖片延遲加載的實(shí)現(xiàn)方法及思路

    這里延遲加載的意思是,拖動(dòng)滾動(dòng)條時(shí),在圖片出現(xiàn)在瀏覽器顯示區(qū)域后才加載顯示。
    2013-07-07
  • xmlplus組件設(shè)計(jì)系列之按鈕(2)

    xmlplus組件設(shè)計(jì)系列之按鈕(2)

    xmlplus 是一個(gè)JavaScript框架,用于快速開發(fā)前后端項(xiàng)目。這篇文章主要介紹了xmlplus組件設(shè)計(jì)系列之按鈕,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • JS+canvas畫一個(gè)圓錐實(shí)例代碼

    JS+canvas畫一個(gè)圓錐實(shí)例代碼

    本篇文章給大家講解html中用canvas函數(shù)配合JS畫出一個(gè)圓錐形的圖形實(shí)例,有需要的朋友學(xué)習(xí)測(cè)試下吧。
    2017-12-12
  • bootstrap3 兼容IE8瀏覽器!

    bootstrap3 兼容IE8瀏覽器!

    bootstrap3 兼容IE8瀏覽器!IE8瀏覽器并不支持這一優(yōu)秀的Css3特性,Bootstrap在開發(fā)文檔中寫了如何使用進(jìn)行兼容IE8,感興趣的朋友可以參考一下
    2016-05-05
  • 淺談js對(duì)象的創(chuàng)建和對(duì)6種繼承模式的理解和遐想

    淺談js對(duì)象的創(chuàng)建和對(duì)6種繼承模式的理解和遐想

    下面小編就為大家?guī)?lái)一篇淺談js對(duì)象的創(chuàng)建和對(duì)6種繼承模式的理解和遐想。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧
    2016-10-10
  • 原生JS封裝拖動(dòng)驗(yàn)證滑塊的實(shí)現(xiàn)代碼示例

    原生JS封裝拖動(dòng)驗(yàn)證滑塊的實(shí)現(xiàn)代碼示例

    這篇文章主要介紹了原生JS封裝拖動(dòng)驗(yàn)證滑塊的實(shí)現(xiàn)代碼示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • js實(shí)現(xiàn)點(diǎn)贊效果

    js實(shí)現(xiàn)點(diǎn)贊效果

    這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)點(diǎn)贊效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03

最新評(píng)論