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

              
                <!--  Easy -->

<section class="easy" data-level="easy">

  <button class="close" data-level="easy">
    <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
      <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clip-rule="evenodd" />
    </svg>

  </button>

  <button class="title" data-level="easy">
    <div class="grid"></div>
    <span>3x3</span>
  </button>

  <div class="puzzle"></div>
  <div class="preview"></div>

  <div class="buttons">
    <button class="start">
      <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M14.752 11.168l-3.197-2.132A1 1 0 0010 9.87v4.263a1 1 0 001.555.832l3.197-2.132a1 1 0 000-1.664z" />
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
      </svg>
    </button>
    <button class="reset">
      <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
        <path fill-rule="evenodd" d="M4 2a1 1 0 011 1v2.101a7.002 7.002 0 0111.601 2.566 1 1 0 11-1.885.666A5.002 5.002 0 005.999 7H9a1 1 0 010 2H4a1 1 0 01-1-1V3a1 1 0 011-1zm.008 9.057a1 1 0 011.276.61A5.002 5.002 0 0014.001 13H11a1 1 0 110-2h5a1 1 0 011 1v5a1 1 0 11-2 0v-2.101a7.002 7.002 0 01-11.601-2.566 1 1 0 01.61-1.276z" clip-rule="evenodd" />
      </svg>
    </button>
  </div>

</section>

<!-- medium -->
<section class="medium" data-level="medium">

  <button class="close" data-level="medium">
    <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
      <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clip-rule="evenodd" />
    </svg>

  </button>

  <button class="title" data-level="medium">
    <div class="grid"></div>
    <span>4x4</span>
  </button>

  <div class="puzzle"></div>
  <div class="preview"></div>

  <div class="buttons">
    <button class="start">
      <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M14.752 11.168l-3.197-2.132A1 1 0 0010 9.87v4.263a1 1 0 001.555.832l3.197-2.132a1 1 0 000-1.664z" />
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
      </svg>
    </button>
    <button class="reset">
      <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
        <path fill-rule="evenodd" d="M4 2a1 1 0 011 1v2.101a7.002 7.002 0 0111.601 2.566 1 1 0 11-1.885.666A5.002 5.002 0 005.999 7H9a1 1 0 010 2H4a1 1 0 01-1-1V3a1 1 0 011-1zm.008 9.057a1 1 0 011.276.61A5.002 5.002 0 0014.001 13H11a1 1 0 110-2h5a1 1 0 011 1v5a1 1 0 11-2 0v-2.101a7.002 7.002 0 01-11.601-2.566 1 1 0 01.61-1.276z" clip-rule="evenodd" />
      </svg>
    </button>
  </div>

</section>

<!-- hard -->
<section class="hard" data-level="hard">

  <button class="close" data-level="hard">
    <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
      <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clip-rule="evenodd" />
    </svg>

  </button>

  <button class="title" data-level="hard">

    <div class="grid"></div>
    <span>5x5</span>
  </button>

  <div class="puzzle"></div>
  <div class="preview"></div>

  <div class="buttons">
    <button class="start">
      <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M14.752 11.168l-3.197-2.132A1 1 0 0010 9.87v4.263a1 1 0 001.555.832l3.197-2.132a1 1 0 000-1.664z" />
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
      </svg>
    </button>
    <button class="reset">
      <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
        <path fill-rule="evenodd" d="M4 2a1 1 0 011 1v2.101a7.002 7.002 0 0111.601 2.566 1 1 0 11-1.885.666A5.002 5.002 0 005.999 7H9a1 1 0 010 2H4a1 1 0 01-1-1V3a1 1 0 011-1zm.008 9.057a1 1 0 011.276.61A5.002 5.002 0 0014.001 13H11a1 1 0 110-2h5a1 1 0 011 1v5a1 1 0 11-2 0v-2.101a7.002 7.002 0 01-11.601-2.566 1 1 0 01.61-1.276z" clip-rule="evenodd" />
      </svg>
    </button>
  </div>

</section>

<!-- extreme  -->
<section class="extreme" data-level="extreme">

  <button class="close" data-level="extreme">
    <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
      <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clip-rule="evenodd" />
    </svg>

  </button>

  <button class="title" data-level="extreme">
    <div class="grid"></div>
    <span>6x6</span>
  </button>

  <div class="puzzle"></div>
  <div class="preview"></div>

  <div class="buttons">
    <button class="start">
      <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M14.752 11.168l-3.197-2.132A1 1 0 0010 9.87v4.263a1 1 0 001.555.832l3.197-2.132a1 1 0 000-1.664z" />
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
      </svg>
    </button>
    <button class="reset">
      <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
        <path fill-rule="evenodd" d="M4 2a1 1 0 011 1v2.101a7.002 7.002 0 0111.601 2.566 1 1 0 11-1.885.666A5.002 5.002 0 005.999 7H9a1 1 0 010 2H4a1 1 0 01-1-1V3a1 1 0 011-1zm.008 9.057a1 1 0 011.276.61A5.002 5.002 0 0014.001 13H11a1 1 0 110-2h5a1 1 0 011 1v5a1 1 0 11-2 0v-2.101a7.002 7.002 0 01-11.601-2.566 1 1 0 01.61-1.276z" clip-rule="evenodd" />
      </svg>
    </button>
  </div>

</section>
              
            
!

CSS

              
                html,
body {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  height: 100%;
}

*,
*:before,
*:after {
  box-sizing: border-box;
}

svg {
  max-width: 100%;
  display: block;
  pointer-events: none;
}

button {
  all: unset;
  z-index: 10;

  color: white;
  border-radius: 0.5rem;
  font-family: Playfair Display;
  font-size: 1.5rem;
  cursor: pointer;

  & span {
    pointer-events: none;
  }
}

.hidden {
  opacity: 0;
}

body {
  --body-color: hsl(220, 80%, 15%);
  --section-color: hsl(220, 75%, 13%);

  overflow-x: hidden;
  max-width: 70rem;
  margin-inline: auto;

  display: grid;
  gap: 1.15rem;
  grid-auto-rows: 1fr;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 30rem), 1fr));

  background: hsl(220, 70%, 95%);
  padding: 5vh 3vw;

  &.full {
    grid-auto-columns: 1fr;
    grid-template-columns: unset;
  }
}

.preview,
.buttons,
.puzzle,
.close {
  display: none;
}

section {
  --url: "";
  --square: 40px;
  --size: 3;

  --left: 2 / 4;
  --right: 11 / -1;

  overflow: hidden;
  position: relative;

  perspective: 800px;
  box-shadow: 0 0 5px black;
  background: linear-gradient(
    34deg,
    #081d4a 0%,
    #092052 16%,
    #0a245a 33%,
    #0b2762 50%,
    #0c2a6a 66%,
    #0d2e72 83%,
    #0f317a 100%
  );

  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: 1fr;
  place-items: center;
  column-gap: 1.25rem;

  & > * {
    grid-area: 1 / 1 / -1 / -1;
  }

  &.selected {
    grid-row: 1 / 6;
    grid-column: span 3;

    grid-template-columns: repeat(13, 1fr);
    grid-template-rows: repeat(13, 1fr);

    & .title {
      grid-column: var(--left);
      grid-row: 1 / 4;
    }

    & .close {
      grid-column: var(--right);
      grid-row: 1 / 4;
    }

    & .buttons {
      grid-column: var(--left);
      grid-row: 7;
    }

    & .preview {
      grid-column: var(--right);
      grid-row: 7;
    }
  }
}

.close {
  height: 3vmin;
  width: 3vmin;

  & > svg {
    fill: white;
  }
}

.preview {
  height: 10vmin;
  width: 10vmin;

  background: var(--url);
  background-size: contain;
  background-repeat: no-repeat;
}

.title {
  --transition: transform 1s cubic-bezier(0.87, 0, 0.13, 1);

  position: relative;
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: 1fr;
  place-items: center;

  filter: drop-shadow(0 0 15px black);

  & > span {
    position: absolute;
    top: -50%;
    font-size: clamp(1.3rem, 1rem + 5vw, 3.5rem);
    text-shadow: 1px 1px 5px black;
    transition: var(--transition);
  }

  &:hover .grid {
    transform: rotate3d(1, 1, 1, 45deg) skew(150deg) rotate(360deg);
  }

  &:hover > span {
    transform: translateY(-10px);
  }
}

.grid {
  --grid: 60px;
  --rotate: 0deg;

  height: var(--grid);
  width: var(--grid);

  pointer-events: none;

  display: grid;
  grid-template-columns: repeat(var(--size), 1fr);
  grid-template-rows: repeat(var(--size), 1fr);

  transform: rotate3d(1, 1, 1, 45deg) skew(150deg) rotate(var(--rotate));
  transition: var(--transition);

  & > span {
    filter: drop-shadow(0 0 1px black);
    border: 2px solid var(--section-color);
  }
}

.buttons {
  display: grid;
  gap: 1rem;

  opacity: 0;

  & > button {
    height: 3vmin;
    width: 3vmin;
  }
}

.puzzle {
  display: grid;
  grid-template-columns: repeat(var(--size), var(--square));
  grid-template-rows: repeat(var(--size), var(--square));
  gap: 0.35rem;

  & .tile {
    background-image: var(--url);
    box-shadow: 0 0 5px black;
  }
}

              
            
!

JS

              
                /**
 * REFERENCES
 *
 * -- handle movement -- https://www.smashingmagazine.com/2016/02/javascript-ai-html-sliding-tiles-puzzle/
 */

/**
 * TODO
 *
 * -- make different size of grid -- ✔️
 * -- make it a slide puzzle -- 🚧
 * -- multiple difficulty lvl -- ✔️
 * -- button change image / refresh button
 * -- handle image position depending on size -- ✔️
 * -- clean code
 * -- improve design
 * -- sound ?
 * -- from puzzle to puzzle
 * -- responsive image
 *
 * -- shuffle should be handle in an other way because I think actually we can have an impossible puzzle
 *
 *
 */

/**
 * DESIGN
 *
 * -- grid 2*2
 * -- one cell per lvl
 * -- Flip in once clicked ?
 *
 */

/**
 * SLIDE PUZZLE
 *
 * -- create empty tile -- ✔️
 * -- store empty tile coord -- ✔️
 * -- store  clicked tile coord -- ✔️
 *
 * -- check if clicked tile and empty tile are neighbours -- ✔️
 *     -- animate if they are
 *
 * -- check if data-pos is the same as data.newPos -- ✔️ ? 🚧
 *    -- if so, win
 *
 * -- add time and score
 *    -- localstorage ?
 *
 * -- add numbers as hint
 *    -- btn to add hint during party
 *
 * -- add lvl -- ✔️
 *    -- make function generic
 *    -- easy 3*3
 *    -- medium 4*4
 *    -- hard 5*5
 *    -- extreme 6*6
 *
 * -- add grid on preview
 */

// start by registering the plugin
gsap.registerPlugin(Flip);

/**
 *--------------------------------------------------------------------------
 * Init
 *--------------------------------------------------------------------------
 */

const LEVELS = {
  easy: 3,
  medium: 4,
  hard: 5,
  extreme: 6
};

const SIZES = {
  easy: 100,
  medium: 80,
  hard: 70,
  extreme: 60
};

Object.keys(LEVELS).forEach((level) => initLevel(level));
setGlobalAnimation();

/**
 * Init
 */
function initLevel(level) {
  const btnLevel = document.querySelector(`[data-level="${level}"] .title`);
  const btnStart = document.querySelector(`[data-level="${level}"] .start`);
  const btnReset = document.querySelector(`[data-level="${level}"] .reset `);
  const btnClose = document.querySelector(`[data-level="${level}"] .close `);

  setPuzzle({ level, gridSize: LEVELS[level], squareSize: SIZES[level] });
  setGrid({ level, gridSize: LEVELS[level] });

  btnLevel.addEventListener("click", showGame);
  btnClose.addEventListener("click", hideGame);
  btnStart.addEventListener("click", () => shufflePuzzle(level));
  btnReset.addEventListener("click", () => reset(level));
}

/**
 * global settings animation
 *
 */
function setGlobalAnimation() {
  const grids = [...document.querySelectorAll(".grid")];
  gsap.set(".close,.preview,.puzzle,.buttons", {
    opacity: 0,
    y: -10,
    visibility: "hidden",
    display: "none"
  });

  gsap.set(".grid > span", { opacity: 0 });

  grids.forEach((g) =>
    gsap.to(g.children, {
      keyframes: { opacity: [0, 1] },
      stagger: 0.03,
      delay: 0.7
    })
  );
}

/**
 * create grid square
 */
function setGrid({ level, gridSize }) {
  const grid = document.querySelector(`[data-level="${level}"] .grid`);
  const frag = document.createDocumentFragment();

  Array.from({ length: gridSize * gridSize }, (_, k) => {
    const span = document.createElement("span");
    span.style.setProperty(
      "background-color",
      `hsl(${220 + k}, 80%, ${45 + k}%)`
    );
    frag.appendChild(span);
  });

  grid.appendChild(frag);
}

/**
 * create squares
 */
function setPuzzle({ level, gridSize, squareSize }) {
  const frag = document.createDocumentFragment();
  const imgSize = squareSize * gridSize;
  const url = `url(https://picsum.photos/${imgSize})`;
  const section = document.querySelector(`[data-level="${level}"]`);
  const puzzle = document.querySelector(`[data-level="${level}"] .puzzle`);

  section.style.setProperty("--size", gridSize);
  section.style.setProperty("--square", `${squareSize}px`);
  section.style.setProperty("--url", url);

  for (let i = 0; i < gridSize; i++) {
    for (let j = 0; j < gridSize; j++) {
      const span = document.createElement("span");

      const x = `calc(100% / ${gridSize - 1} * ${j})`;
      const y = `calc(100% / ${gridSize - 1} * ${i})`;

      // css purpose
      span.classList.add("tile");

      // set grid coordinates
      span.dataset.pos = `${j},${i}`;

      span.style.setProperty("background-position-x", x);
      span.style.setProperty("background-position-y", y);
      span.addEventListener("click", (e) => moveTile(e, level));

      frag.appendChild(span);
    }
  }
  puzzle.appendChild(frag);
}

/**
 *--------------------------------------------------------------------------
 * Usage
 *--------------------------------------------------------------------------
 */

/**
 * show grid
 */

function showGame(event) {
  const level = event.target.dataset.level;
  const section = document.querySelector(`[data-level="${level}"]`);
  const sections = [...document.querySelectorAll(`section[data-level]`)];
  const sizeGrid = [SIZES[level], SIZES[level]];

  const state = Flip.getState(sections);
  const titleState = Flip.getState(`[data-level="${level}"] .title`);

  sections.forEach((section) => {
    section.classList.contains("selected") &&
      section.classList.remove("selected");
    section.dataset.level === level && section.classList.add("selected");
  });

  document.body.classList.add("full");

  Flip.from(titleState, {
    duration: 0.3,
    ease: "linear",
    toggleClass: "hidden",
    onComplete: () => {
      gsap.to(".title", { scale: 0.9 });
    }
  });

  Flip.from(state, {
    duration: 0.3,
    ease: "linear",
    absolute: true
  })
    .set(`[data-level="${level}"] :is(.close,.preview,.buttons,.puzzle)`, {
      visibility: "visible",
      display: "grid"
    })
    .to(`[data-level="${level}"] :is(.close,.preview,.buttons,.puzzle)`, {
      opacity: 1,
      y: 0,
      stagger: 0.3
    })
    .to(
      `[data-level="${level}"] .puzzle > span`,
      {
        keyframes: {
          filter: ["brightness(3)", "brightness(1)"],
          scale: [0.875, 1]
        },
        stagger: {
          grid: sizeGrid,
          from: "edges",
          amount: 0.3,
          axis: "x"
        }
      },
      "<+0.5"
    );
}

/**
 * hide grid
 */
function hideGame(event) {
  const level = event.target.dataset.level;
  const sections = [...document.querySelectorAll(`section[data-level]`)];

  const state = Flip.getState(sections);
  const titleState = Flip.getState(".title");

  sections.forEach((s) => s.classList.remove("selected"));
  document.body.classList.remove("full");

  reset(level);

  gsap.to(".close,.preview,.buttons,.puzzle", {
    opacity: 0,
    y: -10,
    visibility: "hidden",
    display: "none"
  });

  Flip.from(titleState, {
    duration: 0.5,
    ease: "linear",
    toggleClass: "hidden",
    onComplete: () => {
      gsap.to(".title", { scale: 1 });
    }
  });

  Flip.from(state, {
    duration: 0.6,
    absolute: true
  });
}

/**
 * handle logic movement tiles
 */
function moveTile(event, level) {
  const tiles = [
    ...document.querySelectorAll(`[data-level="${level}"] .puzzle > span`)
  ];

  const state = Flip.getState(tiles);

  const clickedTile = event.target;
  const emptyTile = tiles[tiles.length - 1];
  const clickedTilePos = clickedTile.dataset.newPos;
  const emptyTilePos = emptyTile.dataset.newPos;
  const xClicked = parseInt(clickedTilePos.split(",")[0]);
  const yClicked = parseInt(clickedTilePos.split(",")[1]);
  const xEmpty = parseInt(emptyTilePos.split(",")[0]);
  const yEmpty = parseInt(emptyTilePos.split(",")[1]);

  function revert() {
    let emptyOrder = emptyTile.dataset.newOrder;
    let clickedOrder = clickedTile.dataset.newOrder;

    // swap order value
    [emptyOrder, clickedOrder] = [clickedOrder, emptyOrder];

    // revert style order
    clickedTile.style.order = clickedOrder;
    emptyTile.style.order = emptyOrder;

    // change data order
    emptyTile.dataset.newOrder = emptyOrder;
    clickedTile.dataset.newOrder = clickedOrder;
  }

  // Move up
  if (xClicked === xEmpty && yClicked + 1 === yEmpty) {
    revert();
    // change pos
    emptyTile.dataset.newPos = `${xEmpty},${yEmpty - 1}`;
    clickedTile.dataset.newPos = `${xClicked},${yClicked + 1}`;
  }

  // Move down
  if (xClicked === xEmpty && yClicked - 1 === yEmpty) {
    revert();
    // change pos
    emptyTile.dataset.newPos = `${xEmpty},${yEmpty + 1}`;
    clickedTile.dataset.newPos = `${xClicked},${yClicked - 1}`;
  }

  // Move left
  if (yClicked === yEmpty && xClicked + 1 === xEmpty) {
    revert();
    // change pos
    emptyTile.dataset.newPos = `${xEmpty - 1},${yEmpty}`;
    clickedTile.dataset.newPos = `${xClicked + 1},${yClicked}`;
  }

  // Move right
  if (yClicked === yEmpty && xClicked - 1 === xEmpty) {
    revert();
    // change pos
    emptyTile.dataset.newPos = `${xEmpty + 1},${yEmpty}`;
    clickedTile.dataset.newPos = `${xClicked - 1},${yClicked}`;
  }

  function checkWin() {
    return tiles.every((tile) => {
      tile.dataset.order === tile.dataset.newOrder;
    });
  }

  Flip.from(state, {
    duration: 0.2,
    ease: "linear",
    onComplete: () => {
      if (checkWin()) {
        gsap.to(tiles, { opacity: 1, duration: 0.2 });
        console.log("WIN");
      }
    }
  });
}

/**
 * shuffle the grid
 */
function shufflePuzzle(level) {
  const tiles = [
    ...document.querySelectorAll(`[data-level="${level}"] .puzzle > span`)
  ];

  const state = Flip.getState(tiles);
  const shuffleOrder = getRandomOrder(tiles.length);
  const coordinates = getGridCoordinates(Math.sqrt(tiles.length));

  tiles.forEach((tile, idx) => {
    tile.style.order = shuffleOrder[idx];

    tile.dataset.order = idx;
    tile.dataset.newOrder = shuffleOrder[idx];
    tile.dataset.newPos = coordinates[shuffleOrder[idx]];
  });

  // create empty tile
  tiles[tiles.length - 1].dataset.empty = true;

  Flip.from(state, {
    duration: 1,
    ease: "back.out(0.7)",
    onComplete: () => {
      gsap.to(tiles[tiles.length - 1], { opacity: 0, duration: 0.2 });
      console.log("game start");
    }
  });
}

/**
 * reset grid
 */
function reset(level) {
  const tiles = [
    ...document.querySelectorAll(`[data-level="${level}"] .puzzle > span`)
  ];
  const state = Flip.getState(tiles);
  const coordinates = getGridCoordinates(Math.sqrt(tiles.length));

  tiles.forEach((tile, idx) => {
    tile.style.order = idx + 1;
    tile.dataset.pos = coordinates[idx];
    tile.dataset.newOrder = idx;
    tile.dataset.newPos = coordinates[idx];
  });

  Flip.from(state, {
    duration: 0.5,
    ease: "back.out(1.7)",
    onComplete: () => {
      console.log("reset");
      gsap.to(tiles, { opacity: 1 });
    }
  });
}

/**
 *--------------------------------------------------------------------------
 * Helpers
 *--------------------------------------------------------------------------
 */

/**
 * create an array of coordinates for the grid
 */
function getGridCoordinates(gridSize) {
  let coord = [];

  for (let i = 0; i < gridSize; i++) {
    for (let j = 0; j < gridSize; j++) {
      coord.push(`${j},${i}`);
    }
  }

  return coord;
}

/**
 * get a random interger
 */
function getRandomNumber(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

/**
 * create array of random number with no duplicates
 */
function getRandomOrder(max) {
  const array = Array.from({ length: max }, (_, x) => x);
  return array.sort((a, b) => 0.5 - Math.random());
}

              
            
!
999px

Console