๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
IT/Java - Spring Boot

[HTML/CSS/JS] codepen ํŽ˜์ด์ง€์— ๋ฒˆ๊ฐœ ํšจ๊ณผ ๊ตฌํ˜„ํ•˜๊ธฐ

by ITyranno 2023. 10. 30.
728x90
๋ฐ˜์‘ํ˜•

 

 

ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์„ธ๊ณ„๋ฅผ ํƒ๊ตฌํ•ฉ์‹œ๋‹ค.

 

 

 

 

codepen์˜ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŽ˜์ด์ง€์— ๋ฒˆ๊ฐœ ํšจ๊ณผ๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

https://codepen.io/jackrugile/pen/kQwPRO

 

Canvas Lightning WIP

Work in Progress...

codepen.io

 

 

 

 

๊ตฌํ˜„ ํ™”๋ฉด

 

 

 

 

 

 

Codepen์„ ํ™œ์šฉํ•˜์—ฌ ๋ฉ‹์ง„ ๋ฒˆ๊ฐœ ํšจ๊ณผ๋ฅผ ํŽ˜์ด์ง€์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

HTML, CSS ๊ทธ๋ฆฌ๊ณ  JavaScript์˜ ๊ธฐ๋ณธ์ ์ธ ์ง€์‹์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

 

์•„๋ž˜๋Š” ์ฝ”๋“œ ํ™œ์šฉ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

 

 

 

HTML

 

<body>
<canvas id="canvas"></canvas>
</body>

 

 

CSS

 

<style>
canvas {
    display: block;
    position: fixed;
    top: 0;
    left: 0;
    pointer-events: none;
    /* ๋ฐฐ๊ฒฝ์— ๋ฒˆ๊ฐœ๊ฐ€ ์žˆ์–ด๋„ ๋งˆ์šฐ์Šค ์ด๋ฒคํŠธ๊ฐ€ ๋™์ž‘ํ•˜์ง€ ์•Š๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. */
  }
  </style>

 

 

JS

 

<script>
var canvasLightning = function (c, cw, ch) {


    this.init = function () {
      this.loop();
    };
    var _this = this;
    this.c = c;
    this.ctx = c.getContext('2d');
    this.cw = cw;
    this.ch = ch;
    this.mx = 0;
    this.my = 0;

    this.lightning = [];
    this.lightTimeCurrent = 0;
    this.lightTimeTotal = 50;

    this.rand = function (rMi, rMa) { return ~~((Math.random() * (rMa - rMi + 1)) + rMi); };
    this.hitTest = function (x1, y1, w1, h1, x2, y2, w2, h2) { return !(x1 + w1 < x2 || x2 + w2 < x1 || y1 + h1 < y2 || y2 + h2 < y1); };

    this.createL = function (x, y, canSpawn) {
      this.lightning.push({
        x: x,
        y: y,
        xRange: this.rand(5, 30),
        yRange: this.rand(5, 25),
        path: [{
          x: x,
          y: y
        }],
        pathLimit: this.rand(10, 35),
        canSpawn: canSpawn,
        hasFired: false
      });
    };

    this.updateL = function () {
      var i = this.lightning.length;
      while (i--) {
        var light = this.lightning[i];


        light.path.push({
          x: light.path[light.path.length - 1].x + (this.rand(0, light.xRange) - (light.xRange / 2)),
          y: light.path[light.path.length - 1].y + (this.rand(0, light.yRange))
        });

        if (light.path.length > light.pathLimit) {
          this.lightning.splice(i, 1)
        }
        light.hasFired = true;
      };
    };

    this.renderL = function () {
      var i = this.lightning.length;
      while (i--) {
        var light = this.lightning[i];

        this.ctx.strokeStyle = 'hsla(0, 100%, 100%, ' + this.rand(10, 100) / 100 + ')';
        this.ctx.lineWidth = 1;
        if (this.rand(0, 30) == 0) {
          this.ctx.lineWidth = 2;
        }
        if (this.rand(0, 60) == 0) {
          this.ctx.lineWidth = 3;
        }
        if (this.rand(0, 90) == 0) {
          this.ctx.lineWidth = 4;
        }
        if (this.rand(0, 120) == 0) {
          this.ctx.lineWidth = 5;
        }
        if (this.rand(0, 150) == 0) {
          this.ctx.lineWidth = 6;
        }

        this.ctx.beginPath();

        var pathCount = light.path.length;
        this.ctx.moveTo(light.x, light.y);
        for (var pc = 0; pc < pathCount; pc++) {

          this.ctx.lineTo(light.path[pc].x, light.path[pc].y);

          if (light.canSpawn) {
            if (this.rand(0, 100) == 0) {
              light.canSpawn = false;
              this.createL(light.path[pc].x, light.path[pc].y, false);
            }
          }
        }

        if (!light.hasFired) {
          this.ctx.fillStyle = 'rgba(255, 255, 255, ' + this.rand(4, 12) / 100 + ')';
          this.ctx.fillRect(0, 0, this.cw, this.ch);
        }

        if (this.rand(0, 30) == 0) {
          this.ctx.fillStyle = 'rgba(255, 255, 255, ' + this.rand(1, 3) / 100 + ')';
          this.ctx.fillRect(0, 0, this.cw, this.ch);
        }

        this.ctx.stroke();
      };
    };


    this.lightningTimer = function () {
      this.lightTimeCurrent++;
      if (this.lightTimeCurrent >= this.lightTimeTotal) {
        var newX = this.rand(100, cw - 100);
        var newY = this.rand(0, ch / 2);
        var createCount = this.rand(1, 3);
        while (createCount--) {
          this.createL(newX, newY, true);
        }
        this.lightTimeCurrent = 0;
        this.lightTimeTotal = this.rand(200, 400);
      }
    }

    this.clearCanvas = function () {
      this.ctx.globalCompositeOperation = 'destination-out';
      this.ctx.fillStyle = 'rgba(0,0,0,' + this.rand(1, 30) / 100 + ')';
      this.ctx.fillRect(0, 0, this.cw, this.ch);
      this.ctx.globalCompositeOperation = 'source-over';
    };

    $(window).on('resize', function () {
      _this.cw = _this.c.width = window.innerWidth;
      _this.ch = _this.c.height = window.innerHeight;
    });

    this.loop = function () {
      var loopIt = function () {
        requestAnimationFrame(loopIt, _this.c);
        _this.clearCanvas();
        _this.updateL();
        _this.lightningTimer();
        _this.renderL();
      };
      loopIt();
    };

  };

  var isCanvasSupported = function () {
    var elem = document.createElement('canvas');
    return !!(elem.getContext && elem.getContext('2d'));
  };

  var setupRAF = function () {
    var lastTime = 0;
    var vendors = ['ms', 'moz', 'webkit', 'o'];
    for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
      window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
      window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'] || window[vendors[x] + 'CancelRequestAnimationFrame'];
    };

    if (!window.requestAnimationFrame) {
      window.requestAnimationFrame = function (callback, element) {
        var currTime = new Date().getTime();
        var timeToCall = Math.max(0, 16 - (currTime - lastTime));
        var id = window.setTimeout(function () { callback(currTime + timeToCall); }, timeToCall);
        lastTime = currTime + timeToCall;
        return id;
      };
    };

    if (!window.cancelAnimationFrame) {
      window.cancelAnimationFrame = function (id) {
        clearTimeout(id);
      };
    };
  };

  $(document).ready(function () {
    if (isCanvasSupported) {
      var c = document.getElementById('canvas');
      var cw = c.width = window.innerWidth;
      var ch = c.height = window.innerHeight;
      var cl = new canvasLightning(c, cw, ch);

      setupRAF();
      cl.init();
    }
  });
  window.addEventListener('load', function () {
    if (isCanvasSupported) {
      var c = document.getElementById('canvas');
      var cw = c.width = window.innerWidth;
      var ch = c.height = window.innerHeight;
      var cl = new canvasLightning(c, cw, ch);

      setupRAF();
      cl.init();
    }
  });
</script>

 

effect.html ์™€ ๊ฐ™์ด ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์„œ ์ฝ”๋“œ๋ฅผ ์ ์ ˆํžˆ ์กฐํ•ฉํ•˜๋ฉด ์œ„ ๊ตฌํ˜„ ํ™”๋ฉด๊ณผ ๊ฐ™์€ ํšจ๊ณผ๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•

loading