VR element space tilt calculator

In this example below you will see how to do a VR element space tilt calculator with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by nsena, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright nsena ©
  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>VR element space tilt calculator</title>
  
  
  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  
<canvas id="canvas" width="1000" height="1000">
 </canvas>
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/nsena/vr-element-space-tilt-calculator-ZBvMZK */
canvas {
    border: 1px dotted black;
}

/*Downloaded from https://www.codeseek.co/nsena/vr-element-space-tilt-calculator-ZBvMZK */
// Set up!
var a_canvas = document.getElementById("canvas");
var context = a_canvas.getContext("2d");

var xCenter = 500;
var yCenter = 300;
var magnifier = 75;
var smallRadius = 2 * magnifier;
var bigRadius = 3 * magnifier;
var realimgWidth = 1;
var imgWidth = magnifier;
var numIMGs = 3;
var changeAngle;

//Sidebar
context.font = "20px Garamond";
context.fillText("radius =" + smallRadius/magnifier, 0, 30);
context.font = "20px Garamond";
context.fillText("Image Width =" + realimgWidth, 0, 60);
context.font = "20px Garamond";
context.fillText("Number of Images =" + numIMGs, 0, 90);
context.font = "20px Garamond";
context.fillText("Angle between IMG centerpoints =" + 360/numIMGs, 0, 120);


//Diagram
var drawCircle = function( xPos, yPos, radius, colour){
  context.fillStyle = colour;
  context.beginPath();
  context.arc(xPos, yPos, radius, 0, 2*Math.PI);
  context.closePath();
  context.fill();
  context.lineWidth = 1;
  context.stroke();
  context.fillStyle = "black";
  console.log("circle drawn at: " + xPos + ", " + yPos);
}

var drawLine = function(xStart, yStart, xFin, yFin){
  context.beginPath();
  context.moveTo(xStart, yStart);
  context.lineTo(xFin, yFin);
  context.stroke();
}

drawCircle(xCenter, yCenter, bigRadius, "red");
drawCircle(xCenter, yCenter, smallRadius, "yellow");
drawLine(xCenter,yCenter, xCenter, yCenter -smallRadius);
drawLine(xCenter - imgWidth/2, yCenter - smallRadius, xCenter + imgWidth/2, yCenter - smallRadius);
 //white line
    context.strokeStyle = "#FFFFFF";
    context.beginPath();
    context.moveTo(xCenter, yCenter);
    context.lineTo(xCenter + imgWidth/2, yCenter - smallRadius);
    context.stroke();

    context.beginPath();
    context.moveTo(xCenter, yCenter);
    context.lineTo(xCenter - imgWidth/2, yCenter - smallRadius);
    context.stroke();
//context.font = "15px Garamond";
//context.fillText("imgWidth", xCenter - 30, yCenter - smallRadius - 5);

// Word Diagram
context.font = "30px Garamond";
context.fillText("Diagram", xCenter,yCenter + bigRadius + 50);
context.font = "10px Garamond";
context.fillText("0, 0", xCenter,yCenter);
//function takes degrees and outputs radians
var toRadians = function(angle) {
  return angle * (Math.PI/180);
}


var angleCounter = 150;

for (i = 0; i < numIMGs; i++) { 
    var xDelta = smallRadius * Math.cos(toRadians(i *(360/numIMGs)  -90));
    var yDelta = smallRadius * Math.sin(toRadians(i*(360/numIMGs) - 90));
    var newX = xCenter + xDelta;
    var newY = yCenter + yDelta;
    console.log("newX: " + newX + ", newY: " +newY);
    console.log("angle " + (i + 1) + " " + xCenter + ", " + xDelta + ", " + yCenter + ", " + yDelta);
    drawCircle(newX, newY, 2, "black");
  var anglePlusOne = i +1;
  var realX = (newX - xCenter)/magnifier;
  var realY = (newY - yCenter)/magnifier;
  context.font = "10px Garamond";
  context.fillText("angle " + anglePlusOne, newX, newY);
  context.fillText("angle " + anglePlusOne + ": " + realX + ", " + realY, 10, angleCounter);
  angleCounter +=10;
}

Comments