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

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

服務(wù)器之家 - 編程語言 - JavaScript - js教程 - html+css+js實現(xiàn)canvas跟隨鼠標的小圓特效源碼

html+css+js實現(xiàn)canvas跟隨鼠標的小圓特效源碼

2022-02-17 19:37北極光之夜。 js教程

這篇文章主要介紹了html+css+js實現(xiàn)canvas跟隨鼠標的小圓特效源碼,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

效果(源碼在最后):

html+css+js實現(xiàn)canvas跟隨鼠標的小圓特效源碼

實現(xiàn):

1.定義標簽:

?
1
2
3
4
<h1>北極光之夜</h1>
<canvas id="draw" style=" position: fixed; display: block;"
           當(dāng)前瀏覽器不支持Canvas,請更換瀏覽器后再試
</canvas>

2. 文字的基本樣式:

?
1
2
3
4
5
6
7
8
9
h1{
   position: absolute;
   top: 50%;
   left: 50%;
   transform: translate(-50%,-50%);
   font-size: 5em;
   font-family: 'fangsong';
   color: rgb(38, 205, 247);
  }

top: 50%;
left: 50%;
transform: translate(-50%,-50%); 居中對齊
3. js部分,詳細看注釋 :

?
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<script>
  /* 首先獲取canvas畫布 */
  var canvas = document.querySelector("#draw");
  var yuan = canvas.getContext("2d"); 
  /* 綁定窗口大小發(fā)生改變事件,讓canvas隨時鋪滿瀏覽器可視區(qū) */
   window.onresize=resizeCanvas;
  function resizeCanvas(){
   canvas.width=window.innerWidth;
   canvas.height=window.innerHeight;
  }
  resizeCanvas();
 
  /* 定義數(shù)組,存下面觸發(fā)移動事件時產(chǎn)生的小圓 */
  var arr = [];
  
  /* 繪制小圓形的方法,x與y是初始位置,r是圓半徑 */
  function circle (x,y,r){
   this.x=x;
   this.y=y;
   this.r=r;
   /* 得一個隨機顏色 */
   this.color = `rgb(${255*Math.random()},${255*Math.random()},${255*Math.random()})`
   /* 圓的移動方向,random函數(shù)為隨機返回一個0.0到1.0的數(shù),x可得隨機正負數(shù),y為隨機正數(shù) */
   this.xZou = parseInt(Math.random()*10-5);
   this.yZou = parseInt(Math.random()*10); 
   /* 向arr數(shù)組末尾添加這個元素 */
   arr.push(this);
  }
 
  /* 更新圓形的方法 */
   circle.prototype.updated = function() {
    /* x和y增加,呈現(xiàn)圓形一直走 */
   this.x = this.x + this.xZou ;
   this.y = this.y + this.yZou ;
   /* 半徑慢慢減少 */
   this.r = this.r - 0.1 ;
   /* 當(dāng)半徑小于1清除這個圓 */
   if(this.r<0){
    this.remove();
   }
   }
   /* 刪除小圓的函數(shù) */
   circle.prototype.remove = function (){
    /* 遍歷數(shù)組,找到和調(diào)用這個函數(shù)相同的圓后用splice函數(shù)刪除 */
   for(let i=0;i<arr.length;i++){
     if(this==arr[i])
     {
      arr.splice(i,1);
     }
   }
  }
   /* 渲染小圓 */
   circle.prototype.render = function(){
 
   yuan.beginPath();
   yuan.arc(this.x,this.y,this.r,0,2*3.14,false);
   yuan.fillStyle = this.color;
   yuan.fill();
   }
   /* 給畫布綁定鼠標經(jīng)過事件 */
   canvas.addEventListener('mousemove',function(e){
    /* 傳入x,y,r。offsetX距離左側(cè)距離,.., */
   new circle(e.offsetX,e.offsetY,Math.random()*15);
   })
 
    /* 定時器渲染小圓,開始動畫 ,30毫秒一次 */
   setInterval(function(){
     /* 清屏 */
    yuan.clearRect(0,0,canvas.width,canvas.height);
    /* 循環(huán)數(shù)組,給每個小圓更新和渲染 */
    for(let i=0;i<arr.length;i++){
     /* 更新 */
     arr[i].updated();
     /* 如果瀏覽器支持,則渲染 */
     if(arr[i].render()){
      arr[i].render();
     }
     
    }
 
   },30)
 
 </script>

canvas鏈接
splice()方法鏈接
random()方法鏈接
push()方法鏈接
resize事件鏈接

完整源碼:

?
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<!DOCTYPE html>
<html lang="zh-CN">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Document</title>
 <style>
  *{
   margin: 0;
   padding: 0;
   box-sizing: border-box;
  }
  
  body{
   background-color: rgb(72, 75, 122);
  }
  
  h1{
   position: absolute;
   top: 50%;
   left: 50%;
   transform: translate(-50%,-50%);
   font-size: 5em;
   font-family: 'fangsong';
   color: rgb(38, 205, 247);
  }
  
 </style>
</head>
<body>
 
  <h1>北極光之夜</h1>
 
 <canvas id="draw" style=" position: fixed; display: block;"
            當(dāng)前瀏覽器不支持Canvas,請更換瀏覽器后再試
 </canvas>
 
 <script>
  /* 首先獲取canvas畫布 */
  var canvas = document.querySelector("#draw");
  var yuan = canvas.getContext("2d"); 
  /* 綁定窗口大小發(fā)生改變事件,讓canvas隨時鋪滿瀏覽器可視區(qū) */
   window.onresize=resizeCanvas;
  function resizeCanvas(){
   canvas.width=window.innerWidth;
   canvas.height=window.innerHeight;
  }
  resizeCanvas();
 
  /* 定義數(shù)組,存下面觸發(fā)移動事件時產(chǎn)生的小圓 */
  var arr = [];
  
  /* 繪制小圓形的方法,x與y是初始位置,r是圓半徑 */
  function circle (x,y,r){
   this.x=x;
   this.y=y;
   this.r=r;
   /* 得一個隨機顏色 */
   this.color = `rgb(${255*Math.random()},${255*Math.random()},${255*Math.random()})`
   /* 圓的移動方向,random函數(shù)為隨機返回一個0.0到1.0的數(shù),x可得隨機正負數(shù),y為隨機正數(shù) */
   this.xZou = parseInt(Math.random()*10-5);
   this.yZou = parseInt(Math.random()*10); 
   /* 向arr數(shù)組末尾添加這個元素 */
   arr.push(this);
  }
 
  /* 更新圓形的方法 */
   circle.prototype.updated = function() {
    /* x和y增加,呈現(xiàn)圓形一直走 */
   this.x = this.x + this.xZou ;
   this.y = this.y + this.yZou ;
   /* 半徑慢慢減少 */
   this.r = this.r - 0.1 ;
   /* 當(dāng)半徑小于1清除這個圓 */
   if(this.r<0){
    this.remove();
   }
   }
   /* 刪除小圓的函數(shù) */
   circle.prototype.remove = function (){
    /* 遍歷數(shù)組,找到和調(diào)用這個函數(shù)相同的圓后用splice函數(shù)刪除 */
   for(let i=0;i<arr.length;i++){
     if(this==arr[i])
     {
      arr.splice(i,1);
     }
   }
  }
   /* 渲染小圓 */
   circle.prototype.render = function(){
 
   yuan.beginPath();
   yuan.arc(this.x,this.y,this.r,0,2*3.14,false);
   yuan.fillStyle = this.color;
   yuan.fill();
   }
   /* 給畫布綁定鼠標經(jīng)過事件 */
   canvas.addEventListener('mousemove',function(e){
    /* 傳入x,y,r。offsetX距離左側(cè)距離,.., */
   new circle(e.offsetX,e.offsetY,Math.random()*15);
   })
 
    /* 定時器渲染小圓,開始動畫 ,30毫秒一次 */
   setInterval(function(){
     /* 清屏 */
    yuan.clearRect(0,0,canvas.width,canvas.height);
    /* 循環(huán)數(shù)組,給每個小圓更新和渲染 */
    for(let i=0;i<arr.length;i++){
     /* 更新 */
     arr[i].updated();
     /* 如果瀏覽器支持,則渲染 */
     if(arr[i].render()){
      arr[i].render();
     }
     
    }
 
   },30)
 
 </script>
</body>
</html>

其它:

今日三省吾身:安逸的生活沒意思,充滿挑戰(zhàn),取得勝利,才是生命真諦。

到此這篇關(guān)于html+css+js實現(xiàn)canvas跟隨鼠標的小圓特效源碼的文章就介紹到這了,更多相關(guān)canvas跟隨鼠標的小圓內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://blog.csdn.net/luo1831251387/article/details/114714705

延伸 · 閱讀

精彩推薦
  • js教程原生JavaScript實現(xiàn)進度條

    原生JavaScript實現(xiàn)進度條

    這篇文章主要為大家詳細介紹了原生JavaScript實現(xiàn)進度條,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    weixin_441349725612022-01-21
  • js教程JavaScript中arguments的使用方法詳解

    JavaScript中arguments的使用方法詳解

    這篇文章主要給大家介紹了關(guān)于JavaScript中arguments的使用方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的...

    等待的L先生3732021-12-15
  • js教程js實現(xiàn)碰撞檢測

    js實現(xiàn)碰撞檢測

    這篇文章主要為大家詳細介紹了js實現(xiàn)碰撞檢測,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    搬磚大法8192022-01-11
  • js教程JavaScript 實現(xiàn)繼承的幾種方式

    JavaScript 實現(xiàn)繼承的幾種方式

    這篇文章主要介紹了JavaScript 實現(xiàn)繼承的幾種方式,幫助大家更好的理解和使用JavaScript,感興趣的朋友可以了解下...

    him89822022-01-21
  • js教程選擇 JavaScript 測試框架的標準

    選擇 JavaScript 測試框架的標準

    由于 JavaScript 被廣泛認為是“web語言”,因此該語言的測試自動化框架是最豐富和最受歡迎的也就不足為奇了。通過考慮不同框架的屬性,你將更加清楚哪...

    粵嵌教育6962022-01-07
  • js教程7道關(guān)于JS this的面試題,你能答對幾個

    7道關(guān)于JS this的面試題,你能答對幾個

    這篇文章主要給大家介紹了7道關(guān)于JS this的面試題,來看看你能答對幾個,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)...

    前端小混混3512022-02-12
  • js教程JS中箭頭函數(shù)與this的寫法和理解

    JS中箭頭函數(shù)與this的寫法和理解

    這篇文章主要給大家介紹了關(guān)于JS中箭頭函數(shù)與this的寫法和理解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需...

    limingru10442021-12-31
  • js教程js面向?qū)ο蠓庋b級聯(lián)下拉菜單列表的實現(xiàn)步驟

    js面向?qū)ο蠓庋b級聯(lián)下拉菜單列表的實現(xiàn)步驟

    這篇文章主要介紹了js面向?qū)ο蠓庋b級聯(lián)下拉菜單列表的實現(xiàn)步驟,幫助大家更好的理解和使用JavaScript,感興趣的朋友可以了解下...

    蔣偉平8682022-01-19
主站蜘蛛池模板: 久久精品久久久久久久久久16 | av一区二区三区免费观看 | 亚洲精品视 | 一本大道久久精品 | 亚洲欧美日韩成人 | 亚洲精品欧美 | 一区免费看 | 午夜视频网站 | 久久久久久久久久久久免费 | 亚洲欧美另类久久久精品2019 | 99久久久国产精品 | 国产精品久久久久久久久久东京 | 国产性猛交xxxx免费看久久 | 国产乱码久久久久久一区二区 | 亚洲视频在线一区 | 激情亚洲 | 黑人中文字幕一区二区三区 | 91久久精品国产91久久 | 日本视频二区 | 在线观看国产视频 | 亚洲精品成人在线 | 国产成人久久精品一区二区三区 | 一级毛片在线播放 | 欧美日韩免费 | 久久久精品一区二区 | 亚洲一区二区免费视频 | 免费成人在线电影 | 日韩在线观看中文字幕 | 久操成人 | 精品成人一区二区 | 欧美激情国产日韩精品一区18 | 亚洲精品视频观看 | 国产色在线 | 亚洲电影在线播放 | 午夜国产视频 | 亚色成人 | 国产精品99久久久久久久vr | 亚洲午夜激情 | 福利在线看 | 亚洲男人皇宫 | 亚洲一区二区中文字幕 |