最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

javascript實(shí)現(xiàn)點(diǎn)擊產(chǎn)生隨機(jī)圖形

 更新時(shí)間:2021年01月25日 11:58:34   作者:半成熟、  
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)點(diǎn)擊產(chǎn)生隨機(jī)圖形,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了javascript實(shí)現(xiàn)點(diǎn)擊產(chǎn)生隨機(jī)圖形的具體代碼,供大家參考,具體內(nèi)容如下

點(diǎn)擊產(chǎn)生隨機(jī)圖形

效果如下:

用javascript來實(shí)現(xiàn)

主要用canvas和隨機(jī)函數(shù)完成各種圖形

第一步

在HTML和CSS中創(chuàng)建出現(xiàn)圖形的矩形和兩個(gè)按鈕。第一個(gè)按鈕用來產(chǎn)生圖形,第二個(gè)按鈕用來清除產(chǎn)生的所有圖形。

<style>
  *{
   margin: 0;
   padding: 0;
  }
  #canvas{
   border: solid 1px red;
   display: block;
   margin: 0 auto;
  }
  #father{
   width: 200px;
   margin:0 auto;
   
  }
  #btn{
   margin-right: 40px;
   cursor: pointer;
  }
  #cle{
   cursor: pointer;
  }
</style>
<body>
 <canvas id="canvas" width="600" height="600"></canvas>
 <div id="father">
  <input type="button" id="btn" value="點(diǎn)擊生成">
  <input type="button" id="cle" value="點(diǎn)擊清除">
 </div>
</body>

第二步

在javascript中分別創(chuàng)建用來隨機(jī)顏色的函數(shù),點(diǎn)擊隨機(jī)產(chǎn)生圖形的函數(shù),點(diǎn)擊清除屏幕的函數(shù)。

var canvas=document.getElementById("canvas");
 var context=canvas.getContext("2d");
 var btn=document.getElementById("btn");
 var cle=document.getElementById("cle");

設(shè)置圖形的隨機(jī)顏色

function color(){
  var r=Math.floor(Math.random()*255);
  var g=Math.floor(Math.random()*255);
  var b=Math.floor(Math.random()*255);
  var a=Math.random();
  var bg="rgba("+r+","+g+","+b+","+a+")";
  return bg;
 }

設(shè)置點(diǎn)擊按鈕隨機(jī)產(chǎn)生圖形的函數(shù),第一種實(shí)心和空心矩形,第二種實(shí)心和空心圓,第三種直線,它們的位置和大小分別寫隨機(jī)函數(shù),再分別加上canvas代碼,用來畫圖形,如context.beginPath()-context closePath()。

btn.onclick=function(){
  var random=Math.floor(Math.random()*3+1);
  if(random==1){
   var rectr=Math.floor(Math.random()*2);
   var rectx=Math.floor(Math.random()*600);
   var recty=Math.floor(Math.random()*600);
   var rectwidth=Math.floor(Math.random()*200+200);
   var rectheight=Math.floor(Math.random()*200+200);
   if(rectr== 0){
    context.beginPath();
    context.strokeStyle=color();
    context.strokeRect(rectx,recty,rectwidth,rectheight)
    context.closePath();
   }
   else {
    context.beginPath();
    context.fillStyle=color();
    context.fillRect(rectx,recty,rectwidth,rectheight);
    context.closePath();
   }
  }
  else if(random == 2){
   var arcr=Math.floor(Math.random()*2);
   var arcx=Math.floor(Math.random()*600);
   var arcy=Math.floor(Math.random()*600);
   var arcr=Math.floor(Math.random()*300);
   if(arcr==0){
    context.beginPath();
    context.strokeStyle=color();
    context.arc(arcx,arcy,arcr,0,2*Math.PI,false);
    context.stroke();
    context.closePath();
   }
  
   else{
    context.beginPath();
    context.fillStyle=color();
    context.arc(arcx,arcy,arcr,0,2*Math.PI,false);
    context.fill();
    context.closePath();
   }
  }
  else if(random==3){
   var movex=Math.floor(Math.random()*600);
   var movey=Math.floor(Math.random()*600);
   var linex=Math.floor(Math.random()*600);
   var liney=Math.floor(Math.random()*600);
   var linew=Math.floor(Math.random()*20);
   context.beginPath();
   context.strokeStyle=color();
   context.moveTo(movex,movey);
   context.lineTo(linex,liney);
   context.lineWidth=linew;
   context.stroke();
   context.closePath();
  }
}

第三步

最后創(chuàng)建點(diǎn)擊清除屏幕的按鈕函數(shù),根據(jù)創(chuàng)建的屏幕大小,在canvas中添加 context.clearRect(0,0,600,600);可實(shí)現(xiàn)清除屏幕。

cle.onclick=function(){
  context.beginPath();
  context.clearRect(0,0,600,600);
  context.closePath();
 }

點(diǎn)擊產(chǎn)生隨機(jī)圖形的效果完成了!

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

相關(guān)文章

最新評(píng)論

得荣县| 武汉市| 双城市| 洛阳市| 巴彦淖尔市| 汉川市| 吴堡县| 齐河县| 马公市| 临沭县| 雷山县| 阳城县| 惠安县| 平乐县| 油尖旺区| 海晏县| 波密县| 印江| 安阳县| 平江县| 侯马市| 友谊县| 普兰店市| 新蔡县| 商河县| 高唐县| 长兴县| 泸水县| 林州市| 河北区| 铜川市| 峨眉山市| 隆德县| 莫力| 福贡县| 股票| 买车| 苗栗县| 广灵县| 子长县| 公主岭市|