Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

              
                #app
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Lato:300,900')

$mondrian-black = rgb(7, 9, 8)
$mondrian-yellow = rgb(248, 217, 45)
$mondrian-white = rgb(242, 245, 241)
$mondrian-blue = rgb(11, 84, 164)
$mondrian-red = rgb(214, 0, 20)

$colors = $mondrian-white $mondrian-yellow $mondrian-white $mondrian-blue $mondrian-white $mondrian-red

$blockSize = 50px
$height = 6 * $blockSize
$width = 5 * $blockSize

*
  box-sizing border-box
  transform-style preserve-3d

body
html
  background-color hsl(0, 100%, 98%)
  display grid
  place-items center
  min-height 100vh
  overflow hidden

h1
  position fixed
  color hsl(130, 13%, 18%)
  font-family sans-serif
  font-weight bold
  margin 0
  font-size 2.5rem
  right 1rem
  bottom 1rem
  
.mondrian
  border 2vmin solid transparent
  display grid
  grid-auto-flow dense
  grid-gap 2vmin
  grid-template-columns repeat(4, 10vmin)
  grid-template-rows repeat(5, 10vmin)
  width 50vmin
  height 62vmin
  grid-auto-rows 10vmin
  grid-auto-columns 10vmin
  transform rotate(180deg)

  &__block
    opacity 0
    grid-column span var(--columns)
    grid-row span var(--rows)

.generate-button
  cursor pointer
  padding 4px 12px
  border-radius 4px
  font-family 'Lato', sans-serif
  margin-top 30px
  background-color #fff

.cuboid {
  height: 100%;
  width: 100%;
  position: relative;
}

.cuboid__side {
  position: absolute;
  height: 100%;
  width: 100%;
  filter: brightness(
  var(--brightness, 1));
}

/* T, R, B, L, F, B */

.cuboid__side:nth-of-type(1) {
  --brightness: 0;
  top: 0;
  height: var(--depth, 50vmin);
  transform: translateY(-50%) rotateX(-90deg);
}

.cuboid__side:nth-of-type(2) {
  --brightness: 0.6;
  top: 50%;
  right: 0;
  width: var(--depth, 50vmin);
  transform: translate(50%, -50%) rotateY(90deg);
}

.cuboid__side:nth-of-type(3) {
  --brightness: 1.3;
  bottom: 0;
  height: var(--depth, 50vmin);
  transform: translateY(50%) rotateX(90deg);
}

.cuboid__side:nth-of-type(4) {
  --brightness: 0.6;
  top: 50%;
  width: var(--depth, 50vmin);
  transform: translate(-50%, -50%) rotateY(90deg);
}

.cuboid__side:nth-of-type(5) {
  --brightness: 0.75;
  transform: translateZ(calc(var(--depth, 50vmin) / -2));
}

.cuboid__side:nth-of-type(6) {
  --brightness: 1;
  transform: translateZ(calc(var(--depth, 50vmin) / 2)) rotateY(180deg);
}

#app
  transform translateZ(1000vmin)
  overflow visible
  height 100vh
  width 100vw
  display grid
  place-items center

.scene
  transform translateZ(1000vmin) rotateX(-24deg) rotateY(-32deg) rotateY(calc(var(--ry, 0) * 1deg))

.mondrian__block
  --depth 10vmin

  .cuboid__side
    background var(--bg)
    
.mondrian__frame
  --depth 2vmin
  position absolute
  height 100%
  width 100%
  transform rotate(180deg)

  .cuboid__side
    background hsl(0, 0%, 15%)
              
            
!

JS

              
                import React from "https://cdn.skypack.dev/react";
import ReactDOM from "https://cdn.skypack.dev/react-dom";
import gsap from "https://cdn.skypack.dev/gsap";

const ROOT_NODE = document.querySelector("#app");

const COLORS = {
  BLACK: "hsl(150, 13%, 3%)",
  WHITE: "hsl(105, 17%, 95%)",
  YELLOW: "hsl(51, 94%, 57%)",
  BLUE: "hsl(211, 87%, 34%)",
  RED: "hsl(354, 100%, 42%)"
};

const GENERATE_BLOCKS = (NUMBER) =>
  new Array(NUMBER).fill().map(() => ({
    columns: gsap.utils.random(1, 3, 1),
    rows: gsap.utils.random(1, 3, 1),
    bg: [COLORS.RED, COLORS.BLUE, COLORS.YELLOW][gsap.utils.random(0, 2, 1)]
  }));

const Mondrian = () => {
  const containerRef = React.useRef(null);
  const spinRef = React.useRef(null);
  const [round, setRound] = React.useState(1);
  const [blocks, setBlocks] = React.useState(GENERATE_BLOCKS(10));

  React.useEffect(() => {
    gsap.to(".scene", {
      "--ry": 360,
      duration: 20,
      ease: "none",
      repeat: -1
    });
  }, []);

  React.useEffect(() => {
    if (containerRef.current && containerRef.current.children.length) {
      const BOUNDS = containerRef.current.getBoundingClientRect();
      const BLOCKS = containerRef.current.querySelectorAll(".mondrian__block");
      const TL = gsap.timeline();
      const VMIN = Math.min(window.innerHeight, window.innerWidth) / 100;
      BLOCKS.forEach((BLOCK, index) => {
        const { top, right, bottom, left } = BLOCK.getBoundingClientRect();
        const outRight = left < BOUNDS.left;
        const outLeft = right > BOUNDS.right;
        const outTop = top < BOUNDS.top;
        const outBottom = bottom > BOUNDS.bottom;
        const outOfBounds = outLeft || outRight || outTop || outBottom;
        gsap.set(BLOCK, {
          opacity: outOfBounds ? 0 : 1,
          attr: {
            "data-live": outOfBounds ? "false" : "true"
          },
          z: Math.random() > 0.5 ? -10 * VMIN : 10 * VMIN
        });

        if (!outOfBounds)
          TL.add(
            gsap
              .timeline()

              .fromTo(
                BLOCK,
                {
                  yPercent: "+=1000"
                },
                {
                  yPercent: 0,
                  ease: "power1.inOut"
                }
              )
              .to(BLOCK, { z: 0 }),
            index * 0.1
          );
      });
    }
  }, [blocks]);

  const generate = () => {
    gsap.to(containerRef.current.querySelectorAll('[data-live="true"]'), {
      yPercent: "-=1000",
      stagger: 0.1,
      ease: "power1.inOut",
      onComplete: () => {
        setRound(round + 1)
        setBlocks(GENERATE_BLOCKS(10));
      }
    });
  };

  React.useEffect(() => {
    window.addEventListener("click", generate);
    return () => {
      window.removeEventListener("click", generate);
    };
  }, []);

  return (
    <>
      <h1>Tap!</h1>
      <div className="scene">
        <div className="mondrian__frame">
          <div className="cuboid">
            <div className="cuboid__side"></div>
            <div className="cuboid__side"></div>
            <div className="cuboid__side"></div>
            <div className="cuboid__side"></div>
            <div className="cuboid__side"></div>
            <div className="cuboid__side"></div>
          </div>
        </div>
        <div className="mondrian" ref={containerRef} key={new Date().getTime()}>
          {blocks.map((block, index) => (
            <div
              key={`${round}-${new Date().getTime()}-${index}`}
              className="mondrian__block"
              style={{
                "--columns": block.columns,
                "--rows": block.rows,
                "--bg": block.bg
              }}
            >
              <div className="cuboid">
                <div className="cuboid__side"></div>
                <div className="cuboid__side"></div>
                <div className="cuboid__side"></div>
                <div className="cuboid__side"></div>
                <div className="cuboid__side"></div>
                <div className="cuboid__side"></div>
              </div>
            </div>
          ))}
        </div>
      </div>
    </>
  );
};

ReactDOM.render(<Mondrian />, ROOT_NODE);

              
            
!
999px

Console