Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #34845

Re: Triying to do 3D in Javascript

From Joao Rodrigues <groups_jr-1@yahoo.com.br>
Newsgroups comp.lang.javascript
Subject Re: Triying to do 3D in Javascript
Date 2018-04-16 10:16 -0300
Organization Aioe.org NNTP Server
Message-ID <pb27mu$1mcd$1@gioia.aioe.org> (permalink)
References <f268863c-f1a9-4337-8012-08454b2a639f@googlegroups.com>

Show all headers | View raw


bit-naughty@hotmail.com wrote:
> Hi,
> I'm just trying to do some 3D in JS.
> 
> All I want is for a triangle to rotate in 3D.
> Here is my code, what am I doing wrong?: (I'm just drawing a poly, rotating it around the Y axis, perspective projecting it,...and repeat..)
> 
> 
> Is this code even *right*? I'm not sure....
> 
> 
>      <html>
>      
>      
>      <head>
>      <style>
>      
>      canvas {
>      	border: 1px solid black;
>      }
>      </style>
>      
>      <script>
>      
>      
>      
>      window.onload = function() {
>      	var canvas = document.getElementById("drawingCanvas");
>      	var context = canvas.getContext("2d");
>      
>      function drawpoly (twodpoly) {
>      	
>      	context.moveTo (twodpoly[0].x,twodpoly[0].y);
>      	context.lineTo (twodpoly[1].x,twodpoly[1].y);
>      	context.lineTo (twodpoly[2].x,twodpoly[2].y);
>      	context.closePath();
>      	
>      	
>      	context.fill();
>      	
>      	
>      }
>      
>      
>    
>      
>      var threedpoly = [
>      	{
>      		x:250,
>      		y:50,
>      		z:1
>      	},
>      	
>      	{
>      		x:50,
>      		y:250,
>      		z:1
>      	},
>      	
>      	{
>      		x:450,
>      		y:250,
>      		z:1
>      	}
>      ];
>      
>      
>      
>      
>      function perspproject(threedpoly) {
>      
>      
>      var twodpoly = [{
>      	x:0,
>      	y:0
>      	},
>      	
>      	{
>      	x:0,
>      	y:0
>      	},
>      	
>      	{
>      	x:0,
>      	y:0
>      	
>      }]
>      
>      
>      
>      for (var i=0; i<3; i++) {
>      
>      
>      	twodpoly[i].x = threedpoly[i].x/threedpoly[i].z ;
>      	twodpoly[i].y = (threedpoly[i].y/threedpoly[i].z) * -1 ;
>      
>      	};		
>      
>      return twodpoly;
>      
>      };
>      
>      function rotatethreedpoly(threedpoly, angle) {
>      	
>      	var rotatedthreedpoly = [
>      	{
>      		x:0,
>      		y:0,
>      		z:0
>      	},
>      	
>      	{
>      		x:0,
>      		y:0,
>      		z:0
>      	},
>      	
>      	{
>      		x:0,
>      		y:0,
>      		z:0
>      	}
>      ];
>      	
>      	
>      	for (var i=0; i<3; i++) {
>      		rotatedthreedpoly[i].x = (Math.cos(angle) * threedpoly[i].x) + (Math.sin(angle) * threedpoly[i].z) ;
>      		rotatedthreedpoly[i].y = threedpoly[i].y ;
>      		rotatedthreedpoly[i].z = (Math.sin(angle) * threedpoly[i].x) + (Math.cos(angle) * threedpoly[i].z) ;
>      		
>      	}
>      	
>      return rotatedthreedpoly ;
>      
>      };
>      
>      	
>      
>      
>      // DRAW SHIT - MAIN LOOP
>      
>      
>      for (var i=1; i<360; i++) {
>      
>       var rad = i * 0.017453293;
>      
>      
>      var foo = rotatethreedpoly(threedpoly, rad);
>      
>      var bar = perspproject(foo);
>      
>      drawpoly(bar);
>      
>      //context.clearRect(0, 0, canvas.width, canvas.height);
>      
>      }
>      
>      
>      
>      
>      }
>      
>      </script>
>      </head>
>      
>      <body>
>      <canvas id = "drawingCanvas" width="500" height="300">
>      </canvas>
>      </body>
>      
>      </html>
> 

Hopefully Mr. Know It All Thomas 'PointedEars' Lahn will answer your 
question, after he stops bugging other participants LOL

-- 
Joao Rodrigues

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Triying to do 3D in Javascript bit-naughty@hotmail.com - 2018-04-13 00:13 -0700
  Re: Triying to do 3D in Javascript "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-04-13 14:07 -0700
    Re: Triying to do 3D in Javascript bit-naughty@hotmail.com - 2018-04-14 07:34 -0700
      Re: Triying to do 3D in Javascript Joao Rodrigues <groups_jr-1@yahoo.com> - 2018-04-14 15:01 -0300
        Re: Triying to do 3D in Javascript "Chris M. Thomasson" <invalid_chris_thomasson@invalid.invalid> - 2018-04-14 15:58 -0700
        Re: Triying to do 3D in Javascript Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-04-15 01:06 +0200
          Re: Triying to do 3D in Javascript "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2018-04-15 11:09 +0200
            Re: Triying to do 3D in Javascript bit-naughty@hotmail.com - 2018-04-16 00:03 -0700
              Re: Triying to do 3D in Javascript "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2018-04-16 11:14 +0200
          Re: Triying to do 3D in Javascript Joao Rodrigues <groups_jr-1@yahoo.com> - 2018-04-15 11:11 -0300
            Re: Triying to do 3D in Javascript Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-04-16 02:08 +0200
              Re: Triying to do 3D in Javascript Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2018-04-16 10:02 -0300
                Re: Triying to do 3D in Javascript Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-04-17 17:57 +0200
                Re: Triying to do 3D in Javascript Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2018-04-17 14:19 -0300
                Re: Triying to do 3D in Javascript Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-04-17 19:44 +0200
                Re: Triying to do 3D in Javascript Gene Wirchenko <genew@telus.net> - 2018-04-17 11:22 -0700
                Re: Triying to do 3D in Javascript Julio Di Egidio <julio@diegidio.name> - 2018-04-17 11:42 -0700
                Re: Triying to do 3D in Javascript Jon Ribbens <jon+usenet@unequivocal.eu> - 2018-04-18 13:48 +0000
                Re: Triying to do 3D in Javascript John G Harris <niam@jghnorth.org.uk.invalid> - 2018-04-17 19:27 +0100
                Re: Triying to do 3D in Javascript Jon Ribbens <jon+usenet@unequivocal.eu> - 2018-04-18 13:48 +0000
      Re: Triying to do 3D in Javascript "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-04-20 11:52 -0700
  Re: Triying to do 3D in Javascript bit-naughty@hotmail.com - 2018-04-16 00:00 -0700
  Re: Triying to do 3D in Javascript Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2018-04-16 10:16 -0300
  Re: Triying to do 3D in Javascript Julio Di Egidio <julio@diegidio.name> - 2018-05-24 06:35 -0700

csiph-web