国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

node.js|vue.js|jquery|angularjs|React|json|js教程|

服務器之家 - 編程語言 - JavaScript - js教程 - javascript實現(xiàn)點擊產(chǎn)生隨機圖形

javascript實現(xiàn)點擊產(chǎn)生隨機圖形

2022-01-07 16:19半成熟、 js教程

這篇文章主要為大家詳細介紹了javascript實現(xiàn)點擊產(chǎn)生隨機圖形,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

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

效果如下:

javascript實現(xiàn)點擊產(chǎn)生隨機圖形

用javascript來實現(xiàn)

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

第一步

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<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>
?
1
2
3
4
5
6
7
<body>
 <canvas id="canvas" width="600" height="600"></canvas>
 <div id="father">
  <input type="button" id="btn" value="點擊生成">
  <input type="button" id="cle" value="點擊清除">
 </div>
</body>

第二步

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

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

設置圖形的隨機顏色

?
1
2
3
4
5
6
7
8
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;
 }

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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)建點擊清除屏幕的按鈕函數(shù),根據(jù)創(chuàng)建的屏幕大小,在canvas中添加 context.clearRect(0,0,600,600);可實現(xiàn)清除屏幕。

?
1
2
3
4
5
cle.onclick=function(){
  context.beginPath();
  context.clearRect(0,0,600,600);
  context.closePath();
 }

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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/bs775926015/article/details/113050440

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 免费 成 人 黄 色 | 日韩精品1区 | 国产精品69毛片高清亚洲 | 欧美日韩精品一区二区三区蜜桃 | 国产精品伊人影院 | av一区二区三区四区 | av黄色影院 | 日韩在线字幕 | 一区日韩 | 欧美日韩免费 | 国产精品久久久久久久久 | 免费久久精品 | 国产精品色哟哟哟 | 久久精品亚洲精品国产欧美kt∨ | 久久99er6热线精品首页蜜臀 | 欧美另类视频 | 日韩av一区二区在线观看 | 免费一二区| 在线观看一区二区精品 | 日韩在线看片 | 亚洲激情视频 | 午夜精品久久久久久久久久久久久 | 午夜草民福利电影 | 欧美日韩精品一区二区在线观看 | 亚洲一区中文字幕 | 亚洲 欧美 日韩 在线 | 国产成人精品免高潮在线观看 | 久久久青草婷婷精品综合日韩 | 久久久美女| 亚洲欧美v国产一区二区 | 欧美成人免费在线视频 | 亚洲在线视频 | 久草电影在线观看 | 日韩欧美一级片在线观看 | 亚洲专区中文字幕 | 激情综合色综合久久综合 | 成人在线免费观看 | 一级视频免费观看 | av免费在线观看网站 | 中文字幕亚洲综合久久久软件 | 不卡视频一区 |