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>
	<style>
	</style>
	<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%;
}
            
          
!
            
              /*
Algorithms & Data structures

The dance itself is based on sorting algorithms whilst the visualisation is based on data-structures, in this particular case: trees.

The human body can be thought of as a tree structure, with the spine at the root. See the variable 'tree' below for the full structure. This visualisation starts by drawing a tree and then slowly morphing it into a dancer.
*/

/*------------
INITIALISATION
------------*/
// Set-up positional scale functions
var xScale = d3.scale.linear().domain([-0.8, 2.2]).range([0, 800]);
var yScale = d3.scale.linear().domain([-2, 1]).range([800, 0]);

// Set-up radius and colour scales
var radiusScale = d3.scale.linear().domain([5, 0]).range([8, 30]);
var colorScale = d3.scale.linear().domain([0, 5]).range(['brown', 'yellow']);

// This scale function determines the balance between tree and dancer. If 0 is output, the tree data is used and if 1 is output, the dancer data is used.
var interpolationScale = d3.scale.linear().domain([0, 32000, 156000, 200000, 330000]).range([0, 0, 0.1, 1, 1]).clamp(true);

// This scale function determines how many nodes to draw
var depthScale = d3.scale.threshold().domain([0, 15500, 23000, 30800, 46200, 61800]).range([-1, 0, 1, 2, 3, 4, 5, 6]);

// These scale functions change alpha values over time
var alphaScale = d3.scale.linear().domain([0, 330000]).range([0.01, 0.2]).clamp(true);
var linkAlphaScale = d3.scale.linear().domain([0, 330000]).range([0, 1]).clamp(true);


var canvas = document.getElementById('mycanvas');
var ctx = canvas.getContext('2d');

ctx.fillStyle = "rgb(0,0,0)";
ctx.fillRect(0, 0, 1200, 800);

var treeNodes, treeLinks;
var startTime = Date.now();
var depth = 0;


// Define the tree structure
var tree = {
  name: 'Spine',
  children: [
    {
      name: 'HipCenter',
      children: [
        {
          name: 'HipLeft',
          children: [
            {
              name: 'KneeLeft',
              children: [
                {
                  name: 'AnkleLeft',
                  children: [
                    {
                      name: 'FootLeft'
                    }
                  ]
                }
              ]
            }
          ]
        },
        {
          name: 'HipRight',
          children: [
            {
              name: 'KneeRight',
              children: [
                {
                  name: 'AnkleRight',
                  children: [
                    {
                      name: 'FootRight'
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    },
    {
      name: 'ShoulderCenter',
      children: [
        {
          name: 'Head'
        },
        {
          name: 'ShoulderLeft',
          children: [
            {
              name: 'ElbowLeft',
              children: [
                {
                  name: 'WristLeft',
                  children: [
                    {
                      name: 'HandLeft'
                    }
                  ]
                }
              ]
            }
          ]
        },
        {
          name: 'ShoulderRight',
          children: [
            {
              name: 'ElbowRight',
              children: [
                {
                  name: 'WristRight',
                  children: [
                    {
                      name: 'HandRight'
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  ]
};


/*------------
TREE FUNCTIONS

Functions for dealing with the tree structure, interpolation etc.
------------*/
function getJointsOfSkeleton(json, skeleton) {
  // Transform Kinect data into object
  var joints = {};
  if(json.Skeletons === undefined)
    return;
  if(skeleton > json.Skeletons.length - 1)
    return;
  _.each(json.Skeletons[skeleton].Joints, function(joint) {
    joints[joint.JointType] = joint;
  });
  return joints;
}

function initialiseTree() {
  // Compute tree node positions

  var treeLayout = d3.layout.tree()
  .size([0.6 * Math.PI, 500]);

  var treeNodesArray = treeLayout(tree);
  var treeLinksArray = treeLayout.links(treeNodesArray);

  // keep tabs on original positions
  treeNodesArray = _.map(treeNodesArray, function(node) {
    node.startX = node.y * Math.sin(node.x - 0.3 * Math.PI) + 200;
    node.startY = -(node.y * Math.cos(node.x - 0.3 * Math.PI) - 450);
    return node;
  });

  // convert to object
  treeNodes = {};
  _.each(treeNodesArray, function(node) {
    treeNodes[node.name] = node;
  })

  treeLinks = treeLinksArray;
}

function updateNodes(joints) {
  // Interpolate between treeNodes and joints

  function linearInterpolate(p0, p1, u) {
    return (1 - u) * p0 + u * p1;
  }

  _.each(joints, function(joint) {
    var jointType = joint.JointType;
    var node = treeNodes[jointType];

    var u = interpolationScale(Date.now() - startTime);
    node.x = linearInterpolate(node.startX, xScale(joint.Position.X), u);
    node.y = linearInterpolate(node.startY, yScale(joint.Position.Y), u);
  });
}


/*---------------
DRAWING FUNCTIONS
---------------*/
function clear() {
  var alpha = alphaScale(Date.now() - startTime);
  ctx.fillStyle = "rgba(0,0,0," + alpha + ")";
  ctx.fillRect(0, 0, 1200, 800);
}

function drawTree() {
  ctx.save();

  ctx.strokeStyle = 'orange';
  ctx.lineWidth = 0.5;
  ctx.translate(300, 100);
  ctx.globalAlpha = linkAlphaScale(Date.now() - startTime);

  _.each(treeLinks, function(link) {
    var node0 = treeNodes[link.source.name];
    var node1 = treeNodes[link.target.name];

    if(node0.depth > depth || node1.depth > depth)
      return;

    drawLine(ctx, node0.x, node0.y, node1.x, node1.y);
  });

  ctx.globalAlpha = 0.9;

  _.each(treeNodes, function(node) {
    if(node.depth > depth)
      return;

    ctx.fillStyle = colorScale(node.depth);
    drawCircle(ctx, node.x, node.y, radiusScale(node.depth));
  });

  ctx.restore();
}




// Initialise the tree
initialiseTree();


// 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) {
  depth = Math.floor(depthScale(Date.now() - startTime));

  var joints = getJointsOfSkeleton(json, 0);
  if(!joints)
    return; 

  ctx.globalAlpha = 1;
  clear();
  updateNodes(joints);
  drawTree();
}
            
          
!
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