Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// get closest body
var closestBodyIndex = getClosestBodyIndex(frame.body.bodies);
if(closestBodyIndex !== trackedBodyIndex) {
if(closestBodyIndex > -1) {
kinect.trackPixelsForBodyIndices([closestBodyIndex]);
} else {
kinect.trackPixelsForBodyIndices(false);
}
}
else {
if(closestBodyIndex > -1) {
//measure distance from floor
if(frame.body.floorClipPlane)
{
//get position of left hand
var joint = frame.body.bodies[closestBodyIndex].joints[Kinect2.JointType.handLeft];
//https://social.msdn.microsoft.com/Forums/en-US/594cf9ed-3fa6-4700-872c-68054cac5bf0/angle-of-kinect-device-and-effect-on-xyz-positional-data?forum=kinectv2sdk
var cameraAngleRadians= Math.atan(frame.body.floorClipPlane.z / frame.body.floorClipPlane.y);
var cosCameraAngle = Math.cos(cameraAngleRadians);
var sinCameraAngle = Math.sin(cameraAngleRadians);
var yprime = joint.cameraY * cosCameraAngle + joint.cameraZ * sinCameraAngle;
var jointDistanceFromFloor = frame.body.floorClipPlane.w + yprime;
//show height in canvas
showHeight(context, joint, jointDistanceFromFloor);
showHeight(outputContext, joint, jointDistanceFromFloor);
//send height data to remote
var jointDataToSend = {joint: joint, distance: jointDistanceFromFloor};
sendToPeer('floorHeightTracker', jointDataToSend);
function getClosestBodyIndex(bodies) {
var closestZ = Number.MAX_VALUE;
var closestBodyIndex = -1;
for(var i = 0; i < bodies.length; i++) {
if(bodies[i].tracked && bodies[i].joints[Kinect2.JointType.spineMid].cameraZ < closestZ) {
closestZ = bodies[i].joints[Kinect2.JointType.spineMid].cameraZ;
closestBodyIndex = i;
}
}
return closestBodyIndex;
}
}
}
var pixelWidth = calculatePixelWidth(frame.bodyIndexColor.horizontalFieldOfView, frame.body.bodies[closestBodyIndex].joints[Kinect2.JointType.spineMid].cameraZ * 1000);
scale = 0.3 * pixelWidth;
//head joint is in middle of head, add area (y-distance from neck to head joint) above
topJoint = {
colorX: topJoint.colorX,
colorY: Math.min(topJoint.colorY, frame.body.bodies[closestBodyIndex].joints[Kinect2.JointType.head].colorY - (frame.body.bodies[closestBodyIndex].joints[Kinect2.JointType.neck].colorY - frame.body.bodies[closestBodyIndex].joints[Kinect2.JointType.head].colorY))
};
var srcRect = {
x: leftJoint.colorX * canvas.width,
y: topJoint.colorY * canvas.height,
width: (rightJoint.colorX - leftJoint.colorX) * canvas.width,
height: (frame.body.bodies[closestBodyIndex].joints[Kinect2.JointType.spineMid].floorColorY - topJoint.colorY) * canvas.height
};
var dstRect = {
x: outputCanvas.width * 0.5,
y: outputCanvas.height - (srcRect.height * scale),
width: srcRect.width * scale,
height: srcRect.height * scale
};
//center the user horizontally - is not minus half width of image as user might reach to one side or the other
//do minus the space on the left size of the spine
var spaceLeft = frame.body.bodies[closestBodyIndex].joints[Kinect2.JointType.spineMid].colorX - leftJoint.colorX;
dstRect.x -= (spaceLeft * canvas.width * scale);
newPixelData = frame.bodyIndexColor.bodies[closestBodyIndex].buffer;
for (var i = 0; i < imageDataSize; i++) {
imageDataArray[i] = newPixelData[i];
joint.depthY * inCanvas.height,
10,
10
);
}
//draw hand states
updateHandState(
inContext,
body.leftHandState,
body.joints[Kinect2.JointType.handLeft]
);
updateHandState(
inContext,
body.rightHandState,
body.joints[Kinect2.JointType.handRight]
);
}
}
kinect.on('multiSourceFrame', function(frame){
var closestBodyIndex = getClosestBodyIndex(frame.body.bodies);
if(closestBodyIndex !== trackedBodyIndex) {
if(closestBodyIndex > -1) {
kinect.trackPixelsForBodyIndices([closestBodyIndex]);
} else {
kinect.trackPixelsForBodyIndices(false);
}
}
else {
if(closestBodyIndex > -1) {
//get body ground position - when use jumps this point stays on the ground
if(frame.body.bodies[closestBodyIndex].joints[Kinect2.JointType.spineMid].floorColorY)
{
//calculate the source rectangle
var leftJoint = frame.body.bodies[closestBodyIndex].joints[0],
topJoint = frame.body.bodies[closestBodyIndex].joints[0],
rightJoint = frame.body.bodies[closestBodyIndex].joints[0];
for(var i = 1; i < frame.body.bodies[closestBodyIndex].joints.length; i++) {
var joint = frame.body.bodies[closestBodyIndex].joints[i];
if(joint.colorX < leftJoint.colorX) {
leftJoint = joint;
}
if(joint.colorX > rightJoint.colorX) {
rightJoint = joint;
}
if(joint.colorY < topJoint.colorY) {
topJoint = joint;
}
for (var jointType in body.joints) {
var joint = body.joints[jointType];
inContext.fillStyle = colors[index];
inContext.fillRect(
joint.depthX * inCanvas.width,
joint.depthY * inCanvas.height,
10,
10
);
}
//draw hand states
updateHandState(
inContext,
body.leftHandState,
body.joints[Kinect2.JointType.handLeft]
);
updateHandState(
inContext,
body.rightHandState,
body.joints[Kinect2.JointType.handRight]
);
}
}