Pen Settings



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


Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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


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.


                <div class="p-2" style="min-width: 700px;">
  <div class="flex items-center mb-2">
    <label class="w-1/6">Spritesheet</label>
    <input class="input is-small mr-2" style="width:500px" type="text" id="input" value="" />
  <div class="flex mb-2">
    <div class="flex items-center w-1/3">
      <label class="w-1/2">Original Width</label>
      <input class="input is-small mr-2" style="width:50px" type="text" id="originalWidth" value="160" />
      <span class="text-gray-700">px</span>
    <div class="flex items-center w-1/3">
      <label class="w-1/2">Original Height</label>
      <input class="input is-small mr-2" style="width:50px" type="text" id="originalHeight" />
      <span class="text-gray-700">px</span>
  <div class="flex mb-2">
    <div class="flex items-center w-1/3">
      <label class="w-1/2">Width</label>
      <input class="input is-small mr-2" style="width:50px" type="text" id="width" value="100" />
      <span class="text-gray-700">px</span>
    <div class="flex items-center w-1/3">
      <label class="w-1/2">Height</label>
      <input class="input is-small mr-2" style="width:50px" type="text" id="height" />
      <span class="text-gray-700">px</span>
  <div class="flex items-center w-1/2 mb-2">
    <label class="w-1/3">Frame rate</label>
    <input class="input is-small mr-2" style="width:50px" type="text" id="fps" value="8" />
    <span class="text-gray-700">fps</span>
    <button class="button is-small" id="run">
      Make GIF




                async function run() {
  const spriteSheetURL = document.getElementById('input').value;
  const originalWidth = Number(document.getElementById('originalWidth').value);
  const originalHeight = Number(document.getElementById('originalHeight').value) || originalWidth;
  const width = Number(document.getElementById('width').value);
  const height = Number(document.getElementById('height').value) || width;  
  const fps = Number(document.getElementById('fps').value) || 10;

  const sprites = await makeCanvases(spriteSheetURL, originalWidth, originalHeight, width, height);
  const blob = await renderGif(sprites, fps);

  const url = URL.createObjectURL(blob);
  const img = document.createElement('img');
  img.setAttribute('src', url);
  img.className = 'mb-2';
  const btn = document.createElement('button');
  btn.innerHTML = '<i class="far fa-save mr-1"></i>Save';
  btn.className = 'button is-small';
  btn.onclick = () => {
    const a = document.createElement('a');
    a.href = url; = `${url.slice(url.lastIndexOf('/') + 1)}.gif`;;
  const item = document.createElement('div');

  const removeBtn = document.createElement('button');
  removeBtn.className = 'absolute'; = 0; = 0;
  removeBtn.innerHTML = '<i class="far fa-times-circle text-gray-500"></i>'
  removeBtn.onclick = () => {
  item.className = 'inline-block text-center relative';

async function renderGif(frames, fps) {
  const workerScript = await loadWorkerScript();

  const gif = new GIF({
    workers: 2,
    quality: 1,

  frames.forEach(frame => gif.addFrame(frame, {
    delay: 1000 / fps,

  return new Promise((resolve, reject) => {
    gif.on('finished', resolve);

    try {
    } catch(e) {

async function makeCanvases(spriteSheetUrl, originalWidth, originalHeight, canvasWidth, canvasHeight) {
  const spriteSheet = await loadImage(spriteSheetUrl);
  spriteSheet.crossOrigin = '';

  let x = 0;
  let y = 0;
  let sprites = [];

  while (y < spriteSheet.height) {
    x = 0;

    while (x < spriteSheet.width) {
      const canvas = document.createElement('canvas');
      canvas.width = canvasWidth;
      canvas.height = canvasHeight;

      const ctx = canvas.getContext('2d');
      ctx.drawImage(spriteSheet, x, y, originalWidth, originalHeight, 0, 0, canvasWidth, canvasHeight);

      const isEmpty = ctx.getImageData(0, 0, canvasWidth, canvasHeight).data.every(channel => channel === 0);

      if (!isEmpty) {
        ctx.fillStyle = '#fff';
        ctx.fillRect(0, 0, canvasWidth, canvasHeight);
        ctx.drawImage(spriteSheet, x, y, originalWidth, originalHeight, 0, 0, canvasWidth, canvasHeight);

      x += originalWidth;

    y += originalHeight;

  return sprites;

function loadImage(imgUrl) {
  const img = new Image();
  img.crossOrigin = 'Anonymous';

  return new Promise((resolve, reject) => {
    img.onload = function() {
      resolve (this);

    img.error = reject;

    img.src = imgUrl;

async function loadWorkerScript() {
  const { data } = await axios.get('', {
    responseType: 'blob',

  const content = await data.text();

  const blob = new Blob([content], {
    type: 'application/javascript',

  return URL.createObjectURL(blob);

document.getElementById('run').onclick = run;