JSFiddle - React, Tailwind, and code Playground

by Michael Prosser

JavaScript

var AITorpedo = function(){

	var t = this;
	
  t.groundWidth = 1000;
  t.groundHeight = 1000;
  
  t.width = 512;
  t.height = 512;
  
  t.rw = t.width/t.groundWidth;
  t.rh = t.height/t.groundHeight;
  
  t.playerPosition = { x: 0, y: 100, z: 0 };
  
	var canvas = document.createElement("canvas");
	canvas.width = t.width;
	canvas.height = t.height;
	var ctx = canvas.getContext("2d");
	ctx.fillStyle = '#000000';
	ctx.fillRect(0, 0, canvas.width, canvas.height);
    
  document.querySelector('body').appendChild(canvas);
  
  t.position = { x: 200, y: 100, z: 300 };
    
	t.plotCoordinates = function(position){
  
  		return { x: (position.x + (t.groundWidth/2))*t.rw, y: (position.z + (t.groundHeight/2))*t.rh };
  
  }
  
  t.angleOnPlane = function(p1,p2){
  
  	var angleRadians = Math.atan2(p2.z - p1.z, p2.x - p1.x);
  
  }
  
  t.drawBeacon = function(){
  
    // clear the display
    
   // ctx.fillStyle = '#000000';
	 // ctx.fillRect(0, 0, canvas.width, canvas.height);
  
    var p1 = t.plotCoordinates(t.position);
    
    //console.log(p1);
    
  	ctx.fillStyle = "red";
    ctx.arc(p1.x, p1.y, 5, 0, 2 * Math.PI);
    ctx.fill();
  
  }
  
  setInterval(function(){
  
  	t.position.x -= 1;
  
  	t.drawBeacon();
  
  },100);
  
  
  
}

var ait = new AITorpedo();