JS 캔버스 동적 매트릭스 배경 효과 구현
효과 도
 
 전체 코드
<!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" style="background:#111"></canvas>
  <script>
   window.onload = function(){
    //      
    var canvas = document.getElementById("mom");
    //        
    //getContext()                  。
    var context =canvas.getContext("2d");
    //             
    var W = window.innerWidth;
    var H = window.innerHeight;
    //  canvas      
    canvas.width = W;
    canvas.height = H;
    //         
    var fontSize = 16;
    //   
    var colunms = Math.floor(W /fontSize);
    //       y   
    var drops = [];
    //                 
    //              y     
    for(var i=0;i<colunms;i++){
      drops.push(0);
    }
    //     
    var str ="JavaScript function(){}";
    //4:fillText(str,x,y);       y     
    //     
    function draw(){
      context.fillStyle = "rgba(0,0,0,0.05)";
      //fillRect()     “   ”   。          。
      context.fillRect(0,0,W,H);
      //       
      context.font = "700 "+fontSize+"px     ";
      //       
      context.fillStyle ="#00cc33";//  rgb,hsl,    ,      
      //     
      for(var i=0;i<colunms;i++){
        var index = Math.floor(Math.random() * str.length);//           Math.floor(Math.random()*str.length)               
        var x = i*fontSize;
        var y = drops[i] *fontSize;//  y              
        context.fillText(str[index],x,y);
        // //       ,            
        if(y >= canvas.height && Math.random()>0.99){
          drops[i] = 0;
        }
        drops[i]++;//           ,     y    
      }
    };
    //    
    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>이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[2022.04.19] 자바스크립트 this - 생성자 함수와 이벤트리스너에서의 this18일에 this에 대해 공부하면서 적었던 일반적인 함수나 객체에서의 this가 아닌 오늘은 이벤트리스너와 생성자 함수 안에서의 this를 살펴보기로 했다. new 키워드를 붙여 함수를 생성자로 사용할 때 this는...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.