Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h2>Product demonstration</h2>
<p>Not sure if we can get the job done? View this product demonstration of one of our Kaiju, Gigan, using his laser.</p>

<section id="services-illustration">
  <div id="kaiju-stuff">
    <img src="https://assets.codepen.io/37111/gigan.png" alt="Gigan" id="gigan">
    <canvas id="laser-canvas"></canvas>
  </div>
  
  <div id="cities">
<!-- Images from https://www.worldatlas.com/articles/12-cities-with-the-most-beautiful-skylines-in-asia.html -->
    <img src="https://assets.codepen.io/37111/hong-kong.jpeg" alt="Hong Kong skyline">
    <img src="https://assets.codepen.io/37111/shanghai.jpeg" alt="Shanghai skyline">
    <img src="https://assets.codepen.io/37111/taipei.jpeg" alt="Taipei skyline">
    <img src="https://assets.codepen.io/37111/doha.jpeg" alt="Doha skyline">
    <img src="https://assets.codepen.io/37111/shanghai.jpeg" alt="Shanghai skyline">
    <img src="https://assets.codepen.io/37111/seoul.jpeg" alt="Seoul skyline">
    <img src="https://assets.codepen.io/37111/tokyo.jpeg" alt="Tokyo skyline">
    <img src="https://assets.codepen.io/37111/bangkok.jpeg" alt="Bangkok skyline">
    <img src="https://assets.codepen.io/37111/kuala-lumpur.jpeg" alt="Kuala Lumpur skyline">
    <img src="https://assets.codepen.io/37111/dubai.jpeg" alt="Dubai skyline">
    <img src="https://assets.codepen.io/37111/singapore.jpeg" alt="Singapore skyline">
    <img src="https://assets.codepen.io/37111/chongqing.jpeg" alt="Chongqing skyline">
    <img src="https://assets.codepen.io/37111/guangzhou.jpeg" alt="Guangzhou skyline">
  </div>
  
  <div class="controls">
    <button class="video-game-button">FIRE</button>
  </div>
  
  <div id="audio">
    <!-- Laser audio from https://www.zapsplat.com/music/game-laser-weapon-shoot-1/ -->
    <audio autostart="false" id="laser-sound" src="https://assets.codepen.io/37111/laser.mp3"></audio>
    <!-- Explosion audio from https://www.zapsplat.com/music/retro-8-bit-video-game-explosion-1/ -->
    <audio autostart="false" id="explosion-sound" src="https://assets.codepen.io/37111/explosion.mp3"></audio>
  </div>
</section>

<a href="https://youtu.be/VtmsuVT7BGI" target="_blank" class="component-carousel">
  <span>Watch us make this on</span><img src="https://assets.codepen.io/557388/component-carousel.png" alt="Component Carousel">
</a>

              
            
!

CSS

              
                @font-face {
  font-family: 'Press Start 2p';
}

body {
  margin: 0;
  background-color: white;
  font-family: sans-serif;
}

h2 {
  font-family: "Press Start 2p";
  text-align: center;
  margin-top: 200px;
  font-weight: 400;
}

p {
  font-size: 20px;
  line-height: 130%;
  padding: 0 15px;
  text-align: center;
  max-width: 30em;
  margin: 0 auto 25px;
}

#services-illustration {
  position: relative;
}

#laser-canvas {
  position: absolute;
  width: 100%;
  height: 100%;
}

#kaiju-stuff {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: row-reverse;
  pointer-events: none;
  overflow: hidden;
}
#gigan {
  max-width: 50vw;
  margin-top: 40%;
  margin-right: -10%;
}

#cities {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  
  img {
    position: absolute;
    width: 100%;
    will-change: opacity;
    animation: fadeIn 0.3s;
    
    &:nth-child(n + 2) {
      display: none;
    }
  }
}

.controls {
  text-align: center;
  margin-top: 1em;
  
  // Some button styles from https://codepen.io/reulison/pen/WNNVPZq
  button {
    cursor: pointer;
    font-family: 'Press Start 2p';
    font-size: 2rem;
    line-height: 1.5em;
	  text-align: center;
    display: inline-block;
    padding: 0.5em 1em;
    text-shadow: 1px 1px pink, -1px -1px maroon;
		border-radius: .75em;
	  box-shadow:  0 .2em maroon;
	  color: white;
	  background-color: red;
    background-image: -webkit-linear-gradient(left top, pink 3%, red 22%, maroon 99%);
	  background-image: linear-gradient(left top, pink 3%, red 22%, maroon 99%);
    
    &:active {
      box-shadow: none;
      position: relative;
      top: .2em;
    }
  }
}

@keyframes fadeIn {
  from { opacity: 0 }
  to { opacity: 1 }
}


.component-carousel {
  position: absolute;
  top: 20px;
  right: 20px;
  flex-direction: column;
  display: flex;
  align-items: center;
  justify-content: center;
  color: black;
  text-decoration: none;
  transition: transform 0.2s ease-in-out;
  
  img {
    width: 187px;
    margin-top: 10px;
  }
  
  &:hover {
    text-decoration: underline;

    img {
      transform: translate(2px, 2px) ;
    }
  }
}

              
            
!

JS

              
                const kaijuContainer = document.querySelector("#kaiju-stuff");
const gigan = document.querySelector("#gigan");
const cities = [...document.querySelectorAll("#cities img")];
const laserCanvas = document.querySelector("#laser-canvas");
const laserCtx = laserCanvas.getContext("2d");
const btn = document.querySelector("button");

const volume = 0.3;
const laserSound = document.querySelector("#laser-sound");
laserSound.volume = volume;
const explosionSound = document.querySelector("#explosion-sound");
explosionSound.volume = volume;

function playAudio(audioElem) {
  audioElem.currentTime = 0;
  audioElem.play();
}

// Random number within given range
function range(start, end) {
  return Math.random() * (end - start) + start;
}

class Laser {
  constructor(config) {
    // this.startX = config.startX;
    // this.startY = config.startY;
    // this.endX = config.endX;
    // this.endY = config.endY;
    
    this.animDur = 200;
    
    if(config.target) {
      this.target(config.target);
    }
    
    this.resize();
    window.addEventListener("resize", () => this.resize());
  }
  
  target(elem) {
    this.targetElem = elem;
    this.endX = elem.offsetWidth * 0.5;
    this.endY = elem.offsetHeight * 0.5;
  }
  
  resize() {
    laserCanvas.width = this.width = laserCanvas.scrollWidth;
    laserCanvas.height = this.height = laserCanvas.scrollHeight;
    this.laserBaseSize = gigan.offsetWidth / 40;
    
    this.startX = kaijuContainer.offsetWidth * (0.735 + 0.1);
    this.startY = kaijuContainer.offsetHeight * (0.205 + 0.4);
    
    this.endX = this.targetElem ? this.targetElem.offsetWidth * 0.5 : 0;
    this.endY = this.targetElem ? this.targetElem.offsetHeight * 0.5 : 0;
  }
  
  clear() {
    laserCtx.clearRect(0, 0, this.width, this.height);
  }
  
  drawLine(startX, startY, endX, endY, width, stroke) {
    laserCtx.beginPath();
		laserCtx.lineCap = "round";
		laserCtx.lineWidth = width;
		laserCtx.strokeStyle = stroke;
		laserCtx.moveTo(startX, startY);
		laserCtx.lineTo(endX, endY);
		laserCtx.stroke();
		laserCtx.closePath();
  }
  
  drawLaser(startX, startY, endX, endY) {
    this.clear();
    this.drawLine(startX, startY, endX, endY, this.laserBaseSize * 2, `rgba(255, 0, 0, ${range(0, 0.5)})`);
    this.drawLine(startX, startY, endX, endY, this.laserBaseSize, `rgba(255, 200, 200, ${range(0.5, 1)})`);
    this.drawLine(startX, startY, endX, endY, this.laserBaseSize * 3, `rgba(255, 80, 80, ${range(0.2, 0.5)})`);
  }
  
  fireLaser() {
    this.startTime = Date.now();
    this.endTime = this.startTime + this.animDur;
    this.isAnimating = true;
    
    playAudio(laserSound);
  }
  
  draw() {
    const timeDiff = Date.now() - this.startTime;
    let percentComplete = timeDiff / this.animDur;
    
    if(percentComplete < 1) {
      // Draw laser expanding from Gigan to city
      const currX = (this.endX - this.startX) * percentComplete + this.startX;
      const currY = (this.endY - this.startY) * percentComplete + this.startY;
      
      this.drawLaser(this.startX, this.startY, currX, currY);
    } else if(percentComplete < 2) {
      // Draw laser retracting from city to Gigan
      percentComplete -= 1; // Get value between 0 and 1
      const currX = (this.endX - this.startX) * percentComplete + this.startX;
      const currY = (this.endY - this.startY) * percentComplete + this.startY;
      
      this.drawLaser(currX, currY, this.endX, this.endY);
    } else {
      this.isAnimating = false;
      this.clear();
      
      this.blowUpTarget();
    }
  }
  
  blowUpTarget() {
    if(this.targetElem) {
      const target = this.targetElem;
      const disObj = disintegrate.getDisObj(target);

      // Create our particles
      disintegrate.createSimultaneousParticles(disObj);

      // Hide the original card
      target.style.display = "none";
      
      // Play the audio
      playAudio(explosionSound);
      
      const context = this;

      // Remove the whole thing after the animation completes
      function removeTarget() {
        // Get index of target and next target
        const index = cities.indexOf(target);
        const nextIndex = index === cities.length - 1 ? 0 : index + 1;
        
        // Switch out images
        context.targetElem = cities[nextIndex];
        context.targetElem.style.display = "inline";
        
        disObj.elem.removeEventListener("disComplete", removeTarget);
      }
      disObj.elem.addEventListener("disComplete", removeTarget);
    }
  }
}

function update() {
  if(laser.isAnimating) {
    laser.draw();
  }
  
  requestAnimationFrame(update);
}

function init()  {
  const disParams = [];
  cities.forEach(city => {
    const myDisData = {
      elem: city,
      data: {
        disType: "simultaneous",
        disParticleType: "ExplodingParticle",
        disReductionFactor: Math.pow(innerWidth, 2) / 4000
      }
    };
    
    disParams.push(myDisData);
  });
  
  disintegrate.init(disParams);
  
  window.addEventListener("particlesReady", () => {
    laser = new Laser({ target: cities[0] });
    update();
    
    btn.addEventListener("click", () => {
      if(!laser.isAnimating) {
        laser.fireLaser();
      }
    });
    btn.click();
  });
}

if(gigan.complete) {
  init();
} else {
  gigan.addEventListener("load", init);
}

              
            
!
999px

Console