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

              
                <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>
              
            
!

CSS

              
                body {
  background: #000;
  margin: 0 auto;
  text-align: center;
  width: 96%;
}
              
            
!

JS

              
                /*
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

Console