溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

利用JavaScript畫(huà)布實(shí)現(xiàn)代碼下墜效果

發(fā)布時(shí)間:2020-11-09 15:08:36 來(lái)源:億速云 閱讀:223 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

利用JavaScript畫(huà)布實(shí)現(xiàn)代碼下墜效果?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。

效果圖

利用JavaScript畫(huà)布實(shí)現(xiàn)代碼下墜效果

完整代碼

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    *{
      padding:0;
      margin:0;
    }
    body{
      overflow: hidden;
    }
  </style>
</head>
<body>
    <canvas id="mom" ></canvas>
  <script>
   window.onload = function(){
    //獲取畫(huà)布對(duì)象
    var canvas = document.getElementById("mom");
    //獲取畫(huà)布的上下文
    //getContext() 方法返回一個(gè)用于在畫(huà)布上繪圖的環(huán)境。
    var context =canvas.getContext("2d");
    //獲取瀏覽器屏幕的寬度和高度
    var W = window.innerWidth;
    var H = window.innerHeight;
    //設(shè)置canvas的寬度和高度
    canvas.width = W;
    canvas.height = H;
    //每個(gè)文字的字體大小
    var fontSize = 16;
    //計(jì)算列
    var colunms = Math.floor(W /fontSize);
    //記錄每列文字的y軸坐標(biāo)
    var drops = [];
    //給每一個(gè)文字初始化一個(gè)起始點(diǎn)的位置
    //計(jì)算每一個(gè)文字所謂坐標(biāo) 存儲(chǔ)y軸的坐標(biāo) 
    for(var i=0;i<colunms;i++){
      drops.push(0);
    }
    //運(yùn)動(dòng)的文字
    var str ="JavaScript function(){}";
    //4:fillText(str,x,y);原理就是去更改y的坐標(biāo)位置
    //繪畫(huà)的函數(shù)
    function draw(){
      context.fillStyle = "rgba(0,0,0,0.05)";
      //fillRect() 方法繪制“已填色”的矩形。默認(rèn)的填充顏色是黑色。
      context.fillRect(0,0,W,H);
      //給字體設(shè)置樣式
      context.font = "700 "+fontSize+"px 微軟雅黑";
      //給字體添加顏色
      context.fillStyle ="#00cc33";//可以rgb,hsl, 標(biāo)準(zhǔn)色,十六進(jìn)制顏色
      //寫(xiě)入畫(huà)布中
      for(var i=0;i<colunms;i++){
        var index = Math.floor(Math.random() * str.length);//設(shè)置文字出發(fā)時(shí)間隨機(jī) Math.floor(Math.random()*str.length)讓數(shù)組里面的文字索引隨機(jī)出現(xiàn) 
        var x = i*fontSize;
        var y = drops[i] *fontSize;//也讓y軸方向也向下掉一個(gè)文字的距離
        context.fillText(str[index],x,y);
        // //如果要改變時(shí)間,肯定就是改變每次他的起點(diǎn)
        if(y >= canvas.height && Math.random()>0.99){
          drops[i] = 0;
        }
        drops[i]++;//讓數(shù)組里面的值每次加一,用于上面的y軸下掉 
      }
    };
    //隨機(jī)顏色
    function randColor(){
      var r = Math.floor(Math.random() * 256);
      var g = Math.floor(Math.random() * 256);
      var b = Math.floor(Math.random() * 256);
      return "rgb("+r+","+g+","+b+")";
    }
    draw();
    setInterval(draw,20);
  };
  </script>
</body>
</html>

看完上述內(nèi)容,你們掌握利用JavaScript畫(huà)布實(shí)現(xiàn)代碼下墜效果的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI