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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                 <div class="container">
    <weather-card></weather-card>
  </div>

<div class="created">
  <span>Created by</span>
  <a href="https://manz.dev/"><h2>Manz.dev</h2></a>
  <p>on <a href="https://twitch.tv/ManzDev">Twitch</a> / <a href="https://youtube.com/c/ManzDev">Youtube</a></p>
</div>
              
            
!

CSS

              
                @use postcss-nested;

html {
  height: 100vh;
  background: linear-gradient(#224, #445) fixed;
}

body {
  margin: 0;
}

.container {
  display: flex;
  justify-content: center;
  margin-top: 2em;
}

.created {
  background: 
    url(https://assets.codepen.io/154065/internal/avatars/users/default.png),
    linear-gradient(to bottom, #884ced, #ec1cce);
  background-size: 75px 75px, cover;
  background-repeat: no-repeat;
  position: absolute;
  top: 0;
  right: 0;
  width: 250px;
  height: 75px;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-left: 2em;
  
  & span,
  & h2,
  & p,
  & a {
    font-family: Montserrat;
    margin: 0;
  }
  
  & a,
  & p,
  & span {
    color: #fff;    
  }
  
  & h2 {
    font-weight: 700;
    transform: translate(0, -4px);    
  }
  
  & a {
    text-decoration-color: rgba(255,255,255,0.4);
  }
  
  & a:hover {
    color: #e6e82a;
  }
}
              
            
!

JS

              
                class WeatherCard extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({ mode: "open" });
    this.currentManz = 0;
  }

  static get styles() {
    return /* css */`
      :host {
        --width: 450px;
        --height: 650px;
      }

      .container {
        width: var(--width);
        height: var(--height);
        background: #fff;
        box-shadow: 0 0 25px 5px #0008;
        position: relative;
      }

      .zone {
        position: absolute;
        bottom: 0;
        overflow: hidden;
        display: grid;
        grid-template-columns: 0.5fr 1.5fr;
        align-items: flex-end;
        padding: 20px;
        padding-bottom: 0;
      }

      .zone .text {
        position: relative;
      }

      .zone h2 {
        font-family: "Bebas Neue";
        font-size: 32px;
        margin: 0;
        border-bottom: 2px solid red;
        margin-bottom: 15px;
      }

      .zone p {
        font-family: EnterCommand;
        font-size: 30px;
        margin: 0;
        margin-bottom: 0.75em;
        line-height: 70%;
        letter-spacing: -0.5px;
        text-shadow: 0 0 2px #0004;
      }

      .zone img {
        max-width: 160px;
        transform: translateY(15px) rotate(-5deg);
        filter: drop-shadow(0 0 5px #0008);
      }
    `;
  }

  connectedCallback() {
    this.render();
  }

  render() {
    this.shadowRoot.innerHTML = /* html */`
    <style>${WeatherCard.styles}</style>
    <div class="container">
      <weather-city class="day"></weather-city>
      <div class="zone">
        <img src="https://manzdev.github.io/twitch-weather-city/manzdev.png" alt="Manz.dev">
        <div class="text">
          <h2>Manz.dev</h2>
          <p>En cuanto deje de llover, cierro stream. Seguro que no se alarga.</p>
        </div>
      </div>
    </div>`;
  }
}

customElements.define("weather-card", WeatherCard);

const BUILDINGS_NUMBER = 6;
const TIME_TO_CHANGE_STAGE = 10000;
const CLOUDS_NUMBER = 10;

class WeatherCity extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({ mode: "open" });
  }

  static get styles() {
    return /* css */`
      :host {
      }

      .container {
        width: 100%;
        height: 75%;

        display: flex;
        flex-direction: column;
        justify-content: flex-end;

        transition: background 1.5s;
        position: relative;
        overflow: hidden;
      }

      :host(.night) .container {
        background: #0f0c20;
      }

      :host(.day) .container {
        background: #1588b6;
      }

      :host(.night) {
        --window-turnoff-color: #000;
        --window-color: #d3a50f;
        --shine-color: gold;
      }

      :host(.day) {
        --window-turnoff-color: #1c1d1dff;
        --window-color: #d3a50f22;
        --shine-color: transparent;
      }

      .sun,
      .moon {
        width: var(--size);
        height: var(--size);
        border-radius: 50%;
        position: absolute;
        top: 30px;
        transition: transform 4s ease-in-out;
        z-index: 2;
      }

      .sun {
        --size: 80px;

        background: #e0a911;
        box-shadow: 0 0 25px 12px #e7af16;
        left: 30px;
      }

      .moon {
        --size: 50px;

        background: #fff;
        box-shadow: 0 0 10px #fff;
        right: 30px;
        background-image:
          radial-gradient(circle 25px at 25% 25%, #eee 0 25%, transparent 28%),
          radial-gradient(circle 30px at 75% 75%, #eee 0 30%, transparent 32%),
          radial-gradient(circle 15px at 45% 75%, #eaeaea 0 30%, transparent 32%);
      }

      :host(.night) .moon,
      :host(.day) .sun {
        transform: translate(0, 0);
      }

      :host(.day) .moon,
      :host(.night) .sun {
        transform: translate(0, 500px);
      }

      .clouds {
        width: 100%;
        height: 250px;
        position: absolute;
        top: 0;
        z-index: 5;
      }

      .buildings {
        width: 100%;
        height: 75%;

        display: flex;
        align-items: flex-end;

        position: relative;
        z-index: 10;
      }
    `;
  }

  connectedCallback() {
    this.render();
    setInterval(() => this.changeStage(), TIME_TO_CHANGE_STAGE);
  }

  generateBuildings() {
    return "<building-city></building-city>".repeat(BUILDINGS_NUMBER);
  }

  generateClouds() {
    return "<cloud-city></cloud-city>".repeat(CLOUDS_NUMBER);
  }

  changeStage() {
    this.classList.toggle("night");
    this.classList.toggle("day");
  }

  render() {
    this.shadowRoot.innerHTML = /* html */`
    <style>${WeatherCity.styles}</style>
    <div class="container">
      <rain-city></rain-city>
      <div class="sun"></div>
      <div class="moon"></div>
      <div class="clouds">
        ${this.generateClouds()}
      </div>
      <div class="buildings">
        ${this.generateBuildings()}
      </div>
    </div>`;
  }
}

customElements.define("weather-city", WeatherCity);

const WINDOWS_NUMBER = 40;

const COLORS = [
  "#333333",
  "#3a3a3a",
  "#444444",
  "#4a4a4a"
];

class BuildingCity extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({ mode: "open" });
  }

  static get styles() {
    return /* css */`
      :host {
        --building-width: 100%;

        display: grid;
        grid-template-columns: repeat(2, 15px);
        grid-template-rows: repeat(auto-fill, 15px);
        justify-content: center;
        padding-top: 20px;
        gap: 15px;

        width: var(--building-width);
        height: var(--building-height);
        background: var(--building-color, #444);

        box-shadow: 0 0 5px #0006 inset;
      }
    `;
  }

  init() {
    const width = Math.floor(Math.random() * 25) + 50;
    const height = Math.floor(Math.random() * 50) + 50;
    const colorIndex = Math.floor(Math.random() * COLORS.length);
    this.style.setProperty("--building-width", `${width}%`);
    this.style.setProperty("--building-height", `${height}%`);
    this.style.setProperty("--building-color", `${COLORS[colorIndex]}`);
  }

  get height() {
    return parseInt(this.style.getPropertyValue("height"));
  }

  connectedCallback() {
    this.init();
    this.render();
  }

  generateWindows() {
    let windows = "";
    for (let i = 0; i < WINDOWS_NUMBER; i++) {
      windows += /* html */`
      <building-window></building-window>`;
    }
    return windows;
  }

  render() {
    this.shadowRoot.innerHTML = /* html */`
    <style>${BuildingCity.styles}</style>
    ${this.generateWindows()}
    `;
  }
}

customElements.define("building-city", BuildingCity);

class BuildingWindow extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({ mode: "open" });
  }

  static get styles() {
    return /* css */`
      :host {
        background: var(--window-turnoff-color);
        transition:
          background 0.5s,
          box-shadow 1s;
      }

      :host(.on) {
        background: var(--window-color);
        box-shadow: 0 0 10px var(--shine-color);
      }
    `;
  }

  connectedCallback() {
    this.render();

    this.setEvent();
  }

  turnOn() {
    this.classList.add("on");
  }

  turnOff() {
    this.classList.remove("off");
  }

  toggle() {
    this.classList.toggle("on");
    this.setEvent();
  }

  setEvent() {
    const ocurrs = Math.floor(Math.random() * 35);
    if (ocurrs !== 0) return;

    const time = 2000 + Math.floor(Math.random() * 10000);
    setTimeout(() => this.toggle(), time);
  }

  render() {
    this.shadowRoot.innerHTML = /* html */`
    <style>${BuildingWindow.styles}</style>
    `;
  }
}

customElements.define("building-window", BuildingWindow);


const RAIN_DROPS_NUMBER = 1000;
const ANGLE = -65 * (Math.PI / 180);

const rain = new Howl({
  src: ["https://manzdev.github.io/twitch-weather-city/sounds/rain-sound.mp3"],
  loop: true
});

class RainCity extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({ mode: "open" });
    this.rainEnabled = true;
  }

  static get styles() {
    return /* css */`
      :host {
        display: inline-block;
        width: 100%;
        height: 100%;
        position: absolute;
        z-index: 20;
      }

      canvas {
        width: 100%;
        height: 100%;
        transform: scaleX(-1);
        filter: blur(0.25px);
      }
    `;
  }

  connectedCallback() {
    this.render();
    this.canvas = this.shadowRoot.querySelector("canvas");
    this.init();
  }

  init() {
    this.ctx = this.canvas.getContext("2d");
    this.canvas.width = parseInt(window.getComputedStyle(this).width) * 2;
    this.canvas.height = parseInt(window.getComputedStyle(this).height) * 2;
    rain.play();

    this.drops = [];
    for (let i = 0; i < RAIN_DROPS_NUMBER; i++) {
      const speed = Math.floor(Math.random() * 15) + 15;
      this.drops.push({
        x: Math.floor(Math.random() * this.canvas.width),
        y: Math.floor(Math.random() * this.canvas.height),
        size: Math.floor(Math.random() * 15) + 7,
        speed,
        color: `rgba(255, 255, 255, ${speed / 20})`,
      });
    }

    setInterval(() => this.loop(), 50);
  }

  update() {
    this.drops.forEach(drop => {
      drop.x -= drop.speed * Math.cos(ANGLE);
      drop.y -= drop.speed * Math.sin(ANGLE);

      const isXOutside = drop.x < 0;
      const isYOutside = drop.y > this.canvas.height;

      if (isXOutside) {
        drop.x = this.canvas.width;
      }
      if (isYOutside) {
        drop.y = 0;
        drop.x = Math.floor(Math.random() * this.canvas.width);
      }
    });
  }

  loop() {
    this.update();
    this.ctx.lineWidth = 1;
    this.ctx.lineCap = "round";
    this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);

    this.drops.forEach(drop => {
      this.ctx.strokeStyle = !this.rainEnabled ? "transparent" : drop.color;
      this.ctx.beginPath();
      this.ctx.moveTo(drop.x, drop.y);
      const x2 = drop.x + (drop.size * Math.cos(ANGLE));
      const y2 = drop.y + (drop.size * Math.sin(ANGLE));
      this.ctx.lineTo(x2, y2);
      this.ctx.stroke();
    });
  }

  render() {
    this.shadowRoot.innerHTML = /* html */`
    <style>${RainCity.styles}</style>
    <canvas></canvas>
    `;
  }
}

customElements.define("rain-city", RainCity);

class CloudCity extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({ mode: "open" });
    this.x = Math.floor(Math.random() * 600);
    this.setY();
  }

  static get styles() {
    return /* css */`
      :host {
        display: inline-block;
        width: var(--width);
        height: var(--height);
        border-radius: 50px;
        opacity: var(--opacity);
        background: #fff;
        position: absolute;
        top: 0;
        left: 0;
        filter: blur(0.75px);
        transform: translate(var(--x, -200px), var(--y));
      }

      :host::before {
        content: "";
        display: inline-block;
        width: 40%;
        height: 50%;
        background: #fff;
        border-radius: 50px;
        position: absolute;
        transform: translate(50%, -50%);

      }

      :host::after {
        content: "";
        display: inline-block;
        width: 40%;
        height: 70%;
        background: #fff;
        border-radius: 50px;
        transform: translate(10%, -50%);
        position: absolute;
        top: 0;
        right: 25px;
      }
    `;
  }

  setY() {
    this.y = Math.floor(Math.random() * 300);
  }

  move() {
    setInterval(() => {
      const isOutside = this.x > 500;
      this.x += 1;

      if (isOutside) {
        this.x = -200;
        this.setY();
      }

      this.style.setProperty("--x", `${this.x}px`);
    }, 30);
  }

  connectedCallback() {
    this.render();
    this.move();

    const width = Math.floor(Math.random() * 50) + 100;
    this.style.setProperty("--width", `${width}px`);
    const height = Math.floor(Math.random() * 50) + 25;
    this.style.setProperty("--height", `${height}px`);

    this.style.setProperty("--y", `${this.y}px`);

    const opacity = (Math.floor(Math.random() * 5) / 10) + 0.5;
    this.style.setProperty("--opacity", opacity);

    const cloudSpeed = 20 + Math.floor(Math.random() * 20);
    this.style.setProperty("--cloud-speed", `${cloudSpeed}s`);
  }

  render() {
    this.shadowRoot.innerHTML = /* html */`
    <style>${CloudCity.styles}</style>
    <div class="container">
    </div>`;
  }
}

customElements.define("cloud-city", CloudCity);

              
            
!
999px

Console