css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <html>
	<body>
		<canvas id="mycanvas" width="1000" height="600" style="width:100%"></canvas>
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
		<script src="https://d28qoto45d39ov.cloudfront.net/arrastre/js/lodash.min.js"></script>
		<script src="https://d28qoto45d39ov.cloudfront.net/arrastre/js/d3.v3.min.js"></script>
		<script src="https://d28qoto45d39ov.cloudfront.net/arrastre/js/arrastre-util.js"></script>
		<script src="https://d28qoto45d39ov.cloudfront.net/arrastre/js/getdata.js"></script>
   </body>
</html>
            
          
!
            
              body {
  background: #000;
  margin: 0 auto;
  text-align: center;
  width: 96%;
}
            
          
!
            
              /*
DEBUGGING

This animation consists of 5 stages, starting with a 'buggy' animation and finishing with a 'debugged' dancer.

At each step, the function getTrianglesOfSkeleton is called which returns an array of triangles. Each triangle is an array of vertices. We then draw these vertices in differing ways, depending on which stage of the dance we're at.
*/

/*------------
INITIALISATION
------------*/
var canvas = document.getElementById('mycanvas');
var ctx = canvas.getContext('2d');

var width = 1000, height = 600;

// Set up D3 scale functions for colour and position
var color = d3.scale.category20();
var xScale = d3.scale.linear().domain([-1.5, 1.5]).range([0, width]);
var yScale = d3.scale.linear().domain([-1.5, 1.5]).range([height, 0]);

// Draw black background
ctx.fillStyle = "rgb(0,0,0)";
ctx.fillRect(0, 0, width, height);


/*-----
DRAWING FUNCTIONS
We have five different drawing functions which will automatically play in sequence, starting with draw1
-----*/
function draw1(json, err) {
  // 1. Draw the triangles as one big path (this is not meant to look good!)

  var triangles = getTrianglesOfSkeleton(json, 0);

  ctx.beginPath();
  ctx.fillStyle = '#aec7e8';

  _.each(triangles, function(triangle, index) {
    _.each(triangle, function(vertex) {
      var x = xScale(vertex.X);
      var y = yScale(vertex.Y);
      ctx.lineTo(x, y);
    });
  });
 
  // Draw head
  _.each(json.Skeletons[0].Joints, function(j) {
    if(j.JointType !== 'Head')
      return;
    var x = xScale(j.Position.X);
    var y = yScale(j.Position.Y);
    ctx.lineTo(x,y);
  });

  ctx.fill();
}

function draw2(json, err) {
  // 2. Same as before, but this time clear the canvas between each frame.

  var triangles = getTrianglesOfSkeleton(json, 0);

  // Clear the canvas slightly
  ctx.fillStyle = "rgba(0, 0, 0, 0.01)";
  ctx.fillRect(0, 0, width, height);

  ctx.beginPath();
  ctx.fillStyle = '#aec7e8';
  _.each(triangles, function(triangle, index) {
    _.each(triangle, function(vertex) {
      var x = xScale(vertex.X);
      var y = yScale(vertex.Y);
      ctx.lineTo(x, y);
    });
  });

  // Draw head
  _.each(json.Skeletons[0].Joints, function(j) {
    if(j.JointType !== 'Head')
      return;
    var x = xScale(j.Position.X);
    var y = yScale(j.Position.Y);
    ctx.lineTo(x,y);
  });

  ctx.fill();
}


function draw3(json, err) {
  // 3. Now draw each triangle as an individual path

  var triangles = getTrianglesOfSkeleton(json, 0);

  ctx.fillStyle = "rgba(0, 0, 0, 0.03)";
  ctx.fillRect(0, 0, width, height);

  ctx.fillStyle = '#aec7e8';

  _.each(triangles, function(triangle) {
    ctx.beginPath();
    _.each(triangle, function(vertex) {
      var x = xScale(vertex.X);
      var y = yScale(vertex.Y);
      ctx.lineTo(x, y);
    });
    ctx.fill();
  });

  // Draw head
  _.each(json.Skeletons[0].Joints, function(j) {
    if(j.JointType !== 'Head')
      return;

    var x = xScale(j.Position.X);
    var y = yScale(j.Position.Y);
    drawCircle(ctx, x, y, 18);
  });
}

function draw4(json, err) {
  // 4. Same as before but colour each triangle
  
  var triangles = getTrianglesOfSkeleton(json, 0);

  ctx.fillStyle = "rgba(0, 0, 0, 0.05)";
  ctx.fillRect(0, 0, width, height);

  _.each(triangles, function(triangle, index) {
    ctx.fillStyle = color(index);
    ctx.beginPath();
    _.each(triangle, function(vertex) {
      var x = xScale(vertex.X);
      var y = yScale(vertex.Y);
      ctx.lineTo(x, y);
    });
    ctx.fill();
  });

  // Draw head
  _.each(json.Skeletons[0].Joints, function(j) {
    if(j.JointType !== 'Head')
      return;

    ctx.fillStyle = color(15);
    var x = xScale(j.Position.X);
    var y = yScale(j.Position.Y);
    drawCircle(ctx, x, y, 18);
  });
}


function draw5(json, err) {
  // 5. Now let's fade between each frame more than before so that the animation is cleaner.

  var triangles = getTrianglesOfSkeleton(json, 0);

  ctx.fillStyle = "rgba(0, 0, 0, 0.2)";
  ctx.fillRect(0, 0, width, height);

  _.each(triangles, function(triangle, index) {
    ctx.fillStyle = color(index);
    ctx.beginPath();
    _.each(triangle, function(vertex) {
      var x = xScale(vertex.X);
      var y = yScale(vertex.Y);
      ctx.lineTo(x, y);
    });
    ctx.fill();
  });

  // Draw head
  _.each(json.Skeletons[0].Joints, function(j) {
    if(j.JointType !== 'Head')
      return;

    ctx.fillStyle = color(15);
    var x = xScale(j.Position.X);
    var y = yScale(j.Position.Y);
    drawCircle(ctx, x, y, 18);
  });
}



// Start with drawing function draw1
var drawFunction = draw1;

// Set timers to switch between the different draw functions
setTimeout(function() {drawFunction = draw2;}, 8000);
setTimeout(function() {drawFunction = draw3;}, 16000);
setTimeout(function() {drawFunction = draw4;}, 24000);
setTimeout(function() {drawFunction = draw5;}, 32000);
  
  
// You can choose one of these to visualise a different dance
var DATASET_ALGORITHM = 'https://d10u56tzn9e9rq.cloudfront.net/algorithm_';

var DATASET_BINARY = 'https://d10u56tzn9e9rq.cloudfront.net/binary_';

var DATASET_WHOLEPIECE = 'https://d10u56tzn9e9rq.cloudfront.net/wholepiece_';

var dataFetcher = new ChunkedDataFetcher(DATASET_ALGORITHM);
dataFetcher.drawFunction = function(json) {
      drawFunction(json, null);
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console