cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

Code Indentation

     

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.

            
              <h2>getUserMedia meets Voxel CSS by <a href="https://twitter.com/wesbos">@wesbos</a></h2>
  <p>Drag it around!</p>

  <div id="scene"></div>
  <canvas width="320" height="240" id="paint1"></canvas>
  <canvas width="320" height="240" id="paint2"></canvas>
  <canvas width="320" height="240" id="paint3"></canvas>
  <canvas width="320" height="240" id="paint4"></canvas>
  <video id="webcam"></video>
            
          
!
            
               video, canvas {
      display: none;
    }

    html {
      text-align: center;
      background:#223545;
      font-family: 'Helvetica Neue', sans-serif;
      color:white;
      font-weight: 100;
    }
    
    body {
      border-top: 20px solid #F6C602;
      padding: 0;
      margin: 0;
    }

    h2 {
      font-weight: 100;
      font-size: 20px;
    }

    a {
      color:#F6C602;
      text-decoration: none;
    }
            
          
!
            
                 /*  
      Quick getUserMedia + Voxel thing I whipped up.
      learn from it!
    */

/* getUserMedia changed again - fix that for older browsers */
var promisifiedOldGUM = function(constraints, successCallback, errorCallback) {

  // First get ahold of getUserMedia, if present
  var getUserMedia = (navigator.getUserMedia ||
      navigator.webkitGetUserMedia ||
      navigator.mozGetUserMedia);

  // Some browsers just don't implement it - return a rejected promise with an error
  // to keep a consistent interface
  if(!getUserMedia) {
    return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
  }

  // Otherwise, wrap the call to the old navigator.getUserMedia with a Promise
  return new Promise(function(successCallback, errorCallback) {
    getUserMedia.call(navigator, constraints, successCallback, errorCallback);
  });
		
}

// Older browsers might not implement mediaDevices at all, so we set an empty object first
if(navigator.mediaDevices === undefined) {
  navigator.mediaDevices = {};
}

// Some browsers partially implement mediaDevices. We can't just assign an object
// with getUserMedia as it would overwrite existing properties.
// Here, we will just add the getUserMedia property if it's missing.
if(navigator.mediaDevices.getUserMedia === undefined) {
  navigator.mediaDevices.getUserMedia = promisifiedOldGUM;
}


// lets go!

    var video = document.querySelector('#webcam');
    var faces = {};

    function getWebCam() {
      navigator.mediaDevices.getUserMedia({video: true, audio: false})
        .then(function(mediaStream) {
          handleStream(mediaStream);
        })
        .catch(function(err) {
          console.error(err);
        });
    }

    function handleStream(mediaStream) {
      video.src = window.URL.createObjectURL(mediaStream);
      video.load();
      video.play();
      voxItUp();
      paintToCanvas();
    }

    function paintToCanvas() {
      setInterval(function() {
        var w = 320;
        var h = 240;
        var offsetX = 0;
        var offsetY = 0;
        for (var i = 1; i < 5; i++) {
          var canvas = document.querySelector('#paint' + i);
          var ctx = canvas.getContext('2d');
          ctx.drawImage(video, offsetX,offsetY,w,h , 0, 0, w,h);
          var image = canvas.toDataURL('image/jpeg');        
          faces['face' + i].setSource(image);
          offsetX += w;
          // restart to hit the lower 2 rows
          if(offsetX > w) {
            offsetY += h;
            offsetX = 0;
          }
        };
      }, 50);
    }


    function voxItUp() {
      var scene = new voxelcss.Scene();
      scene.rotate(-Math.PI / 8, Math.PI / 4, 0);
      scene.attach(document.querySelector('#scene'));

      var lightSource = new voxelcss.LightSource(300, 300, 300, 750, 0.5, 1);
      scene.addLightSource(lightSource);

      var world = new voxelcss.World(scene);
      var editor = new voxelcss.Editor(world);
      
      var coords = [[0,250],[250,250], [0,0], [250,0]];

      coords.forEach(function(coord, i) {
        var index = i+1;
        faces['face' + index] = new voxelcss.ImageFace();
        var mesh = new voxelcss.Mesh(faces['face' + index]);
        var vox = new voxelcss.Voxel(coord[0], coord[1], 0, 200, { mesh: mesh });
        editor.add(vox);
      });
    }

    getWebCam();
            
          
!
999px
Loading ..................

Console