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

              
                <div class="scene">
  <div class="canvas-container">
    <canvas width="150" height="75" id="canvas" class="canvas canvas1"></canvas>
    <canvas width="150" height="75" id="canvas2" class="canvas canvas2"></canvas>
  </div>
  <div class="foreground"></div>
</div>
              
            
!

CSS

              
                body {
  height: 100vh;
  display: grid;
  place-items: center;
  background: black;
}

.scene {
  position: relative;
  width: clamp(0px, 800px, 80vw);
  height: clamp(0px, 610px, 61vw);
}

.canvas-container {
  position: absolute;
  width: 100%;
  height: 99%;
  overflow: hidden;
  perspective: 400px;
}

.canvas {
  position: absolute;
  vertical-align: middle;
  image-rendering: pixelated;
  transform-style: preserve-3d;
}

.canvas1 {
  top: 47%;
  width: 60%;
  height: 30%;
  transform-origin: center right;
  transform:
    rotateY(50deg)
    rotateZ(10deg);
}

.canvas2 {
  top: 42%;
  right: 0;
  width: 45%;
  height: 35%;
  transform-origin: bottom left;
  transform:
    translateX(7%)
    rotateY(-50deg)
    rotateZ(-7deg)
}

.foreground {
  position: absolute;
  width: 100%;
  height: 100%;
  background: url(https://assets.codepen.io/25387/torni-clear-sm.png) no-repeat center center;
  background-size: contain;
}
              
            
!

JS

              
                // https://codepen.io/pavlovsk/pen/VRbGVP
const { abs, floor, ceil, round, sin, PI: π } = Math
const random = (x = 1) => Math.random() * x
const rgb = (...values) => `rgb(${values.join(',')})`
const rgba = (...values) => `rgba(${values.join(',')})`

function nArray(n) {
  return [...Array(n).keys()]
}

function generateGrid(size, cellCallback) {
  return nArray(size).map((row, y) => (
    nArray(size).map((cell, x) => cellCallback(x, y))
  ))
}

function generateField({ density, offset = 0, width, height }) {
  const pixels = nArray(width * height)
  return pixels
    .map((px, i) => {
      const star = {
        x: offset + px % width,
        y: floor(px / width)
      }
      // Put some colorful points if debug is enabled
      if (Settings.debug) {
        const isMiddle = i === (width * height) / 2
        const isLast = i === width * height - 1
        if (isLast) {
          return Object.assign(star, { color: 'red' })
        }
        if (isMiddle) {
          return Object.assign(star, { color: 'blue' })
        }
      }
      if (random() > 1 - density) {
        return star
      }
      return null
    })
    .filter(Boolean)
}

function drawSky(ctx, width, state) {
  ctx.fillStyle = Settings.sky.bg
  ctx.fillRect(0, 0, width, width)
  state.sky.shift += Settings.sky.shiftSpeed
}

function generateStars(ctx, width, state) {
  const { stars, fields, shift } = state.sky
  if (!stars.length || width * fields - shift <= width) {
    const newStars = generateField({
      density: Settings.sky.starDensity,
      offset: width * fields,
      width: width,
      height: width
    }).map(star => ({
      ...star,
      color: star.color || Settings.sky.starColor
    }))
    stars.splice(0, 1)
    stars.push(...newStars)
    state.sky.fields++
  }
}

function drawStars(ctx, width, state) {
  const stars = state.sky.stars
  stars.forEach(({ x, y, color }) => {
    ctx.fillStyle = color
    const shiftedX = x - state.sky.shift
    ctx.fillRect(shiftedX, y, 1, 1)
  })
}

function draw(ctx, width, state) {
  generateStars(ctx, width, state)
  drawSky(ctx, width, state)
  drawStars(ctx, width, state)
  requestAnimationFrame(() => {
    draw(ctx, width, state)
  })
}

function init(state, canvas) {
  const ctx = canvas.getContext('2d')
  draw(ctx, canvas.width, state)
}

const Settings = {
  debug: false,
  sky: {
    bg: '#f5f5f5',
    get shiftSpeed() {
      if (Settings.debug) return 0.8
      return 0.05
    },
    starDensity: 0.1,
    get starColor() {
      if (random() >= .5) {
        return 'black'
      }
      if (random() >= .66) {
        return 'hotpink'
      }
      if (random() >= .66) {
        return 'crimson'
      }
      if (random() >= .66) {
        return 'mediumspringgreen'
      }
      if (random() >= .25) {
        return '#fd5'
      }
      return 'palevioletred'
    }
  }
}

const State = {
  sky: {
    shift: 0,
    fields: 0,
    stars: []
  }
}

const image = new Image()
image.src = 'https://assets.codepen.io/25387/torni-clear-sm.png'
image.onload = () => {
  init(State, canvas)
  init(State, canvas2)
}

              
            
!
999px

Console