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

              
                <div id="scene"></div>
<details class="settings-panel">
  <summary class="settings-panel-title">Settings</summary>
  <div id="settings"></div>
  <button id="print">Print to console</button>
</details>

<template id="range-setting-template">
  <label class="setting">{id}: <span id="{id}-value">{value}</span>
    <br />
    <input id="{id}-input" type="range" min="{min}" max="{max}" value="{value}" />
  </label>
</template>

<template id="color-setting-template">
  <label class="setting">{id}: <span id="{id}-value">{value}</span>
    <br />
    <input id="{id}-input" type="color" value="{value}" />
  </label>
</template>
              
            
!

CSS

              
                :root {
  --rotation: 90deg;
  --container-width: 300px;
  --container-height: 50px;
  --element-width: 96%;
  --element-height: 101%;
  --element-bottom: -30%;
  --element-right: -24%;
  --outline-width: 1px;
  --body-background-color: #0a061e;
  --border-top-color: #ce92af;
  --border-right-color: #00ffcc;
}


body {
  margin: 0;
  overflow: hidden;
  background: var(--body-background-color);
  font-family: "Operator Mono", menlo, monaco, monospace;
  font-size: 14px;
  color: white;
}

.settings-panel {
  /* Remove this to enter adjustment mode */
  display: none;
  position: absolute;
  max-width: 200px;
  max-height: 435px;
  overflow: auto;
  padding: .5em;
  background: #00000099;
  border-radius: 6px;
}

.settings-panel-title {
  cursor: pointer;
}

.settings-panel[open] .settings-panel-title {
  cursor: default;
}

#settings {
  position: relative;
  z-index: 2;
}

.setting {
  display: block;
  margin-bottom: .5em;
}

.setting:first-of-type {
  margin-top: 1em;
}

#scene {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

#scene div {
  width: var(--container-width);
  height: var(--container-height);
  border-top: var(--outline-width) solid var(--border-top-color);
  border-right: var(--outline-width) solid var(--border-right-color);
  position: relative;
  border-radius: 100%;
  transform: translateZ(0) rotate(var(--rotation));
  animation:
    portal-width 2.5s infinite,
    portal-height 10s infinite;
}

#scene div div {
  width: var(--element-width);
  height: var(--element-height);
  position: absolute;
  bottom: var(--element-bottom);
  right: var(--element-right);
  animation: portal-element 2.5s infinite linear;
}

@keyframes portal-width {
  50% {
    width: 100px;
  }
}

@keyframes portal-height {
  50% {
    height: 300px;
  }
}

@keyframes portal-element {
  50% {
    width: 102%;
  }
}
              
            
!

JS

              
                const getNestedMarkup = (tagName, amount) => {
  return [
    Array(amount).fill(`<${tagName}>`).join(''),
    Array(amount).fill(`</${tagName}>`).join('')
  ].join('')
}

const query = document.querySelector.bind(document)

const setCSSProperty = (key, value, scope = window.document.documentElement) => {
  scope.style.setProperty(key, value)
}

const renderTemplate = (template, data) => {
  Object.keys(data).forEach(key => {
    const pattern = new RegExp(`{${key}}`, 'g')
    template = template.replace(pattern, data[key])
  })
  return template
}

const settings = [{
  id: 'rotation',
  type: 'range',
  min: -360,
  max: 360,
  value: 90,
  hasCSSProperty: true,
  get formattedValue() {
    return `${this.value}deg`
  }
}, {
  id: 'container-width',
  type: 'range',
  max: 1000,
  value: 300,
  hasCSSProperty: true,
  get formattedValue() {
    return `${this.value}px`
  },
}, {
  id: 'container-height',
  type: 'range',
  max: 1000,
  value: 50,
  hasCSSProperty: true,
  get formattedValue() {
    return `${this.value}px`
  }
}, {
  id: 'element-width',
  type: 'range',
  max: 200,
  value: 96,
  hasCSSProperty: true,
  get formattedValue() {
    return `${this.value}%`
  }
}, {
  id: 'element-height',
  type: 'range',
  max: 200,
  value: 101,
  hasCSSProperty: true,
  get formattedValue() {
    return `${this.value}%`
  }
}, {
  id: 'element-bottom',
  type: 'range',
  min: -100,
  max: 100,
  value: -30,
  hasCSSProperty: true,
  get formattedValue() {
    return `${this.value}%`
  }
}, {
  id: 'element-right',
  type: 'range',
  min: -100,
  max: 100,
  value: -24,
  hasCSSProperty: true,
  get formattedValue() {
    return `${this.value}%`
  }
}, {
  id: 'elements',
  type: 'range',
  min: 1,
  max: 100,
  value: 100,
  fn() {
    const container = query('#scene')
    container.innerHTML = getNestedMarkup('div', +this.value)
  }
}, {
  id: 'outline-width',
  type: 'range',
  min: 1,
  max: 50,
  value: 1,
  hasCSSProperty: true,
  get formattedValue() {
    return `${this.value}px`
  }
}, {
  id: 'body-background-color',
  type: 'color',
  value: '#0a061e',
  hasCSSProperty: true
}, {
  id: 'border-top-color',
  type: 'color',
  value: '#ce92af',
  hasCSSProperty: true
}, {
  id: 'border-right-color',
  type: 'color',
  value: '#00ffcc',
  hasCSSProperty: true
}]

const settingTemplates = {
  range: query('#range-setting-template').innerHTML,
  color: query('#color-setting-template').innerHTML
}

const applySetting = (setting) => {
  const currentValue = setting.formattedValue || setting.value
  query(`#${setting.id}-value`).innerText = currentValue
  if (setting.hasCSSProperty) {
    setCSSProperty(`--${setting.id}`, currentValue)
  }
  if (setting.fn) {
    setting.fn(currentValue)
  }
}

const renderSettings = () => {
  const renderedSettings = settings
    .map(setting => {
      const template = settingTemplates[setting.type]
      return renderTemplate(template, setting)
    })
    .join('')

  query('#settings').innerHTML = renderedSettings
  settings.forEach(applySetting)

  query('#print').addEventListener('click', () => {
    const settingsText = JSON.stringify(settings, null, 2)
    console.log('const settings =', settingsText)

    const variablesText = query('html').style.cssText
      .split(';')
      .map(v => ` ${v}`)
      .join(';\n')
      .trim()
    console.log(':root {\n', variablesText, '\n}')
  })
}

const listenSettings = () => {
  settings.forEach(setting => {
    query(`#${setting.id}-input`).addEventListener('input', e => {
      setting.value = e.currentTarget.value
      applySetting(setting)
    })
  })
}

const animateRotation = (increment, interval) => {
  const rotation = settings.find(s => s.id === 'rotation')
  return setInterval(() => {
    rotation.value += increment
    applySetting(rotation)
  }, interval)
}

renderSettings()
listenSettings()
animateRotation(0.01, 1000 / 60)
              
            
!
999px

Console