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 id="preview" class="waiting">
  <p>Drop an SVG file here!</p>
</div>
<div id="options">
  <h2>options</h2>
  <div>
    <label>movement</label>
    <div class="radio">
      <button id="auto">auto</button>
      <button id="mouse">mouse</button>
      <button id="manual">manual</button>
    </div>
  </div>
  <div class="if-auto">
    <label>x speed</label>
    <input id="xv" type="range" min="0.1" max="4" value="1.98" step="any" />
  </div>
  <div class="if-auto">
    <label>y speed</label>
    <input id="yv" type="range" min="0.1" max="4" value="1.23" step="any" />
  </div>
  <div class="if-manual">
    <label>x angle</label>
    <input id="xa" type="range" min="-1" max="1" value="1" step="any" />
  </div>
  <div class="if-manual">
    <label>y angle</label>
    <input id="ya" type="range" min="-1" max="1" value="1" step="any" />
  </div>
  <hr />
  <div>
    <label>x scale</label>
    <input id="xs" type="range" min="1" max="25" value="5" />
  </div>
  <div>
    <label>y scale</label>
    <input id="ys" type="range" min="1" max="25" value="3" />
  </div>
  <div>
    <label>centerpoint</label>
    <input id="zo" type="range" min="0" max="1" value="0.3" step="any" />
  </div>
</div>
              
            
!

CSS

              
                body {
  display: flex;
  justify-content: space-around;
  margin: 0;
  padding: 0;
  width: 100vw;
  height: 100vh;
  background: #212121;
  color: #eee;
  
  font-family: sans-serif;
}

body > * {
  margin: 0.5rem;
  padding: 0.5rem;
}

#preview {
  flex: 1 1 0;
  position: relative;
  border: 0.4rem solid #eeeeee;
  text-align: center;
  font-size: 4em;
}

#preview > svg {
  width: 100%;
  height: 100%;
  pointer-events: none;
}

#options {
  flex: 0 0 auto;
}

#options > div {
  display: flex;
  margin: 0.2em 0;
  justify-content: space-between;
}

#options > div > label {
  flex: 1;
  margin-right: 1em;
}

#options > div > :not(label) {
  width: 150px;
}

.radio {
  display: flex;
}
.radio > button {
  flex: 1 0 auto;
  margin: 0;
  padding: 0.4em;
  font-size: 0.8em;
  border: none;
  color: #eee;
  background: #212121;
  border: 1px solid #eee;
}
.radio > button:disabled {
  background: #eee;
  color: #212121;
}

#options > .if-auto, #options > .if-manual {
  display: none;
}
#options.mode-auto > .if-auto,
#options.mode-manual > .if-manual {
  display: flex;
}
              
            
!

JS

              
                const log = console.log.bind(console);
const $$ = id => document.getElementById(id);
const $ = id => +$$(id).value;

const dt = 1 / 60;
const modes = ['auto', 'mouse', 'manual'];
let mode;
let layers;
let time = 0;
let mouse = [0, 0];

const chmode = _mode => {
  mode = _mode;
  $$('options').className = `mode-${mode}`;
  modes.forEach(m => { $$(m).disabled = mode === m});
};
modes.forEach(m => { $$(m).onclick = () => chmode(m)});
chmode('mouse');


document.body.onmousemove = evt => {
  const rect = $$('preview').getBoundingClientRect();
  mouse = [
    2 * (evt.pageX - rect.x) / rect.width - 1,
    2 * (evt.pageY - rect.y) / rect.height - 1,
  ];
  console.log(mouse);
};
$$('preview').ondragover = evt => evt.preventDefault();
$$('preview').ondrop = evt => {
  evt.preventDefault();
  console.log(evt);

  if (evt.dataTransfer.items) {
    if (evt.dataTransfer.items.length !== 1) {
      log('error', 'drag exactly one file please!');
      return;
    }
    const item = evt.dataTransfer.items[0];
    if (item.kind !== 'file') {
      log('error', 'drag an SVG file please!');
      return;
    }
    open(item.getAsFile())
  } else {
    if (evt.dataTransfer.files.length !== 1) {
      log('error', 'drag exactly one file please!');
      return;
    }
    open(evt.dataTransfer.files[0]);
    log('note', `opening ${file.name}`);
  }
};

const open = file => {
  log('note', `opening ${file.name}...`);
  
  const reader = new FileReader();
  reader.onload = () => {
    layers = [];
    $$('preview').innerHTML = reader.result;
    for (const e of $$('preview').firstElementChild.children) {
      if (e.tagName !== 'g')
        continue;

      layers.push(e);
    }
    layers.reverse();
    log('note', `done, loaded ${layers.length} layers`);
  };
  
  reader.readAsText(file);
}

const camera = () => {
  switch (mode) {
    case 'auto':
      return [
        Math.sin(time*$('xv')),
        Math.cos(time*$('yv')),
      ];
    case 'manual':
      return [
        $('xa'),
        $('ya'),
      ];
    case 'mouse':
      return mouse;
  }
}

const dist = i => (i / (layers.length - 1)) - $('zo');

const tick = () => {
  if (!layers) {
    requestAnimationFrame(tick);
    return;
  }
  
  time += dt;
  const [x, y] = camera();
  
  layers.forEach((layer, i) => {
    const f = dist(i);
    layer.style.transform = `translate(${x * $('xs') * f}px, ${y * $('ys') * f}px)`;
  });
  
  requestAnimationFrame(tick);
}
tick();
              
            
!
999px

Console