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

              
                
<body class="max-w-xl mx-auto">
  <header>
    <h1 class="mt-5 mb-5 text-3xl">Record a 2 second gif with your webcam</h1>
    <ol class="list-decimal list-inside mb-5">
      <li>Allow the camera sharing to see an instant preview.</li>
      <li>When you're ready, press the button below to start recording your gif.</li>
      <li>Preview the gif, and download it if you like it.</li>
    </ol>
  </header>

  <main>
    <video></video>
    <p></p>
    <button onclick="record()" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded w-full mt-5 mb-10" >Please allow camera sharing to enable recording</button>
    <div id="preview" class="pb-10"></div>

  </main>
  
  <footer>
    <ul class="mb-5 text-gray-700 text-sm">
      <li>No tracking is used on this website. So tweet me your feedback!</li>
      <li>The gifs are only stored temporarily in your browser, no server is ever storing them.</li>
    </ul>
  </footer>

  <a href="https://twitter.com/thibpat">
    <footer class="fixed bottom-0 right-0 px-4 py-3 bg-gray-200 rounded-t-lg">
      <img src="https://yt3.ggpht.com/a/AGF-l795KioT7lGfMJIuFLPdtuBMIxuPtfH2HpcUoQ=s88-c-k-c0x00ffffff-no-rj" class="w-6 inline align-middle rounded-full"/> <span class="ml-2">By @thibpat</span>
    </footer>
  </a>
  <div class="w-0 h-0 overflow-hidden">
    <canvas id="frame-preview"/>
  </div>
  <script type="text/javascript" src="https://cdn.rawgit.com/antimatter15/jsgif/master/LZWEncoder.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/antimatter15/jsgif/master/NeuQuant.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/antimatter15/jsgif/master/GIFEncoder.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/antimatter15/jsgif/master/b64.js"></script>

</body>
              
            
!

CSS

              
                body { font-family: 'Open Sans', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";}
              
            
!

JS

              
                const DEFAULT_RECORD_TEXT = 'Record a 2 seconds animated gif';
let globalStream;

navigator.mediaDevices.getUserMedia({
  video: true,
}).then(async function(stream) {

  var video = document.querySelector('video');

  video.srcObject = globalStream = stream;
  video.onloadedmetadata = function(e) {
    video.play();
    document.querySelector('button').innerText = DEFAULT_RECORD_TEXT;
  };
});

let recording = false;

function snap() {
  var video = document.querySelector('video');
  var canvas = document.createElement("canvas");
  const coefficient = 360/video.videoWidth;
  canvas.width = video.videoWidth*coefficient;
  canvas.height = video.videoHeight*coefficient;

  canvas.getContext('2d').drawImage(video, 0, 0, video.videoWidth, video.videoHeight, 0, 0, canvas.width, canvas.height);
  return canvas.getContext('2d');
}

function record() {
  if(!globalStream || recording) return;

  recording = true;
  document.querySelector('button').innerText = 'Get ready to record...';
  
  var encoder = new GIFEncoder();
  encoder.setRepeat(0);
  encoder.setDelay(100);
  encoder.start();

  document
    .querySelector('button')
    .innerText = 'Recording... (2 seconds left)';
  setTimeout(() => {
    document
      .querySelector('button')
      .innerText = 'Recording... (1 seconds left)';
  }, 1000);

  let frames = 20;
  encoder.addFrame(snap());
  frames--;
  let inter = setInterval(() => {
    encoder.addFrame(snap());
    frames--;
    if(frames<=0) {
      clearInterval(inter);
      encoder.finish();
      document
        .querySelector('button')
        .innerText = DEFAULT_RECORD_TEXT;
      recording = false;
      var binary_gif = encoder.stream().getData();
      var data_url = 'data:image/gif;base64,'+encode64(binary_gif);
       document
      .querySelector('#preview').innerHTML = `
<a href=${data_url}
download="gifit.gif"
titl="Click to download the gif!"
class="mb-10">
<img src="${data_url}"/>
<p>Click to download the gif!</p>
</a>
`;
    }
  }, 100);
}
              
            
!
999px

Console