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

              
                #app
              
            
!

CSS

              
                *
  box-sizing border-box

body
  background #111
  min-height 100vh
  display flex
  align-items center
  justify-content center

.glitch-image
  --cell-size calc(100 / var(--cells))
  height calc(var(--size, 300) * 1px)
  width calc(var(--size, 300) * 1px)
  display grid
  grid-template-columns repeat(var(--cells), 1fr)
  grid-template-rows repeat(var(--cells), 1fr)
  position relative
  overflow hidden

  & > img
    opacity 0
    height 0
    width 0

  &__cell
    position relative
    overflow hidden

    &:after
      content ''
      animation glitch-switch calc(var(--speed, 0.2) * 1s) calc(var(--delay, 0.5) * 1s) var(--count) backwards steps(1)
      background-image var(--src)
      background-size calc(var(--size) * 1px) calc(var(--size) * 1px)
      position absolute
      top 0
      left 0
      transform translate(calc((var(--x) * var(--cell-size)) * -1%), calc((var(--y) * var(--cell-size)) * -1%))
      height calc(var(--size) * 1px)
      width calc(var(--size) * 1px)


// Worth noting here if we write about this that FireFox requires
// the explicit background-position. Else, we could just update
// --x and --y on each keyframes
@keyframes glitch-switch
  0%
    filter grayscale(1)
    transform translate(calc((var(--x-0) * var(--cell-size)) * -1%), calc((var(--y-0) * var(--cell-size)) * -1%))
  10%
    transform translate(calc((var(--x-1) * var(--cell-size)) * -1%), calc((var(--y-1) * var(--cell-size)) * -1%))
  20%
    transform translate(calc((var(--x-2) * var(--cell-size)) * -1%), calc((var(--y-2) * var(--cell-size)) * -1%))
  30%
    transform translate(calc((var(--x-3) * var(--cell-size)) * -1%), calc((var(--y-3) * var(--cell-size)) * -1%))
  40%
    transform translate(calc((var(--x-4) * var(--cell-size)) * -1%), calc((var(--y-4) * var(--cell-size)) * -1%))
  50%
    transform translate(calc((var(--x-5) * var(--cell-size)) * -1%), calc((var(--y-5) * var(--cell-size)) * -1%))
  60%
    transform translate(calc((var(--x-6) * var(--cell-size)) * -1%), calc((var(--y-6) * var(--cell-size)) * -1%))
  70%
    transform translate(calc((var(--x-7) * var(--cell-size)) * -1%), calc((var(--y-7) * var(--cell-size)) * -1%))
  80%
    transform translate(calc((var(--x-8) * var(--cell-size)) * -1%), calc((var(--y-8) * var(--cell-size)) * -1%))
  90%
    transform translate(calc((var(--x-9) * var(--cell-size)) * -1%), calc((var(--y-9) * var(--cell-size)) * -1%))
  100%
    filter grayscale(0)
    transform translate(calc((var(--x-0) * var(--cell-size)) * -1%), calc((var(--y-0) * var(--cell-size)) * -1%))
              
            
!

JS

              
                const {
  dat: { GUI },
  React,
  React: { useEffect, useRef, useState },
  ReactDOM: { render },
} = window

const ALT = 'jh3y logo - cartoon bear with snapback cap on'
const SRC =
  'https://s3-us-west-2.amazonaws.com/s.cdpn.io/605876/bear-with-cap.svg'
// Try something random like https://source.unsplash.com/300x300?bear

const CONFIG = {
  cells: 5,
  count: 5,
  size: 300,
  speed: 0.2,
  delay: 0.5,
  src: SRC,
}

const BOUNDS = {
  cells: [2, 20],
  count: [2, 20],
  size: [50, 300],
  speed: [0.1, 1, 0.01],
  delay: [0, 1, 0.01],
}

const randomInRange = (min, max) =>
  Math.floor(Math.random() * max - min + 1) + min

// const generateStyles = (index, cells) => {
//   const X = index % Math.sqrt(cells)
//   const Y = Math.floor(index / Math.sqrt(cells))
//   const COUNT = randomInRange(1, CONFIG.count)
//   const glitches = new Array(10)
//     .fill()
//     .map(
//       (_, index) =>
//         `--x-${index}: ${Math.floor(
//           Math.random() * Math.sqrt(cells)
//         )}; --y-${index}: ${Math.floor(Math.random() * Math.sqrt(cells))};`
//     )
//   return `--x: ${X}; --y: ${Y}; --count: ${COUNT}; ${glitches.join(' ')}`
// }

const generateStyles = (index, cells) => {
  const result = {
    '--x': index % Math.sqrt(cells),
    '--y': Math.floor(index / Math.sqrt(cells)),
    '--count': randomInRange(1, CONFIG.count),
  }
  for (let g = 0; g < 10; g++) {
    result[`--x-${g}`] = Math.floor(Math.random() * Math.sqrt(cells))
    result[`--y-${g}`] = Math.floor(Math.random() * Math.sqrt(cells))
  }
  return result
}

const App = () => {
  const [model, setModel] = useState(0)
  const datRef = useRef(null)
  useEffect(() => {
    if (!datRef.current) {
      datRef.current = new GUI({ name: 'Glitchy Image Config' })
      for (const spec in BOUNDS) {
        datRef.current
          .add(
            CONFIG,
            spec,
            BOUNDS[spec][0],
            BOUNDS[spec][1],
            BOUNDS[spec][2] ? BOUNDS[spec][2] : 1
          )
          .onFinishChange(() => setModel(new Date().getTime()))
      }
      datRef.current
        .add(CONFIG, 'src')
        .onChange(() => setModel(new Date().getTime()))
    }
  }, [model])

  return (
    <div
      className="glitch-image"
      style={{
        '--src': `url(${CONFIG.src})`,
        '--size': CONFIG.size,
        '--cells': CONFIG.cells,
        '--speed': CONFIG.speed,
        '--delay': CONFIG.delay,
      }}>
      {new Array(CONFIG.cells * CONFIG.cells).fill().map((_, index) => (
        <div
          className="glitch-image__cell"
          key={`glitch-image-cell-${model}--${index}`}
          ariaHidden={true}
          style={generateStyles(index, CONFIG.cells * CONFIG.cells)}></div>
      ))}
      <img src={CONFIG.src} alt={ALT} />
    </div>
  )
}

render(<App />, document.getElementById('app'))

              
            
!
999px

Console