Jose Ferrao 6 years ago
parent
commit
6ada2456c7
6 changed files with 179 additions and 5 deletions
  1. 33 3
      index.html
  2. 28 0
      source/Object2D.js
  3. 5 0
      source/Viewport.js
  4. 1 1
      source/math/Box2.js
  5. 1 1
      source/math/Matrix3.js
  6. 111 0
      source/math/Matrix32.js

+ 33 - 3
index.html

@@ -6,18 +6,48 @@
 </head>
 <body>
 	<script type="text/javascript" src="source/EventManager.js"></script>
+	<script type="text/javascript" src="source/Object2D.js"></script>
+	<script type="text/javascript" src="source/Viewport.js"></script>
+
+	<script type="text/javascript" src="source/math/Vector3.js"></script>
+	<script type="text/javascript" src="source/math/Vector2.js"></script>
+	<script type="text/javascript" src="source/math/Matrix3.js"></script>
+	<script type="text/javascript" src="source/math/Box2.js"></script>
+	<script type="text/javascript" src="source/math/UUID.js"></script>
+	<script type="text/javascript" src="source/math/Matrix32.js"></script>
+
 	<script type="text/javascript">
 		var canvas = document.createElement("canvas");
 		canvas.width = 800;
 		canvas.height = 600;
 		document.body.appendChild(canvas);
 
-		var context 
+		//var m = matrix.elements;
+		///context.setTransform(m[0], m[3], m[1], m[4], m[2], m[5]);
+		
+		var a = new Matrix32();
+		a.rotate(1);
+
+		var b = new Matrix32();
+		b.translate(50, 20);
 
-		class CanvasObject()
+		var context = canvas.getContext("2d");
+
+		function loop()
 		{
-			
+			context.clearRect(0, 0, canvas.width, canvas.height);
+
+			a.rotate(0.01);
+			a.setContextTransform(context);
+			context.fillRect(0, 0, 40, 60);
+
+			requestAnimationFrame(loop);
 		}
+
+		loop();
+
+
+
 	</script>
 </body>
 </html>

+ 28 - 0
source/Object2D.js

@@ -37,6 +37,13 @@ function Object2D()
 	 */
 	this.rotation = 0.0;
 
+	/**
+	 * Layer of this object, objects are sorted by layer value.
+	 *
+	 * Lower layer value is draw first.
+	 */
+	this.layer = 0;
+
 	/**
 	 * Local transformation matrix applied to the object. 
 	 */
@@ -55,4 +62,25 @@ Object2D.prototype.add = function(object)
 {
 	object.parent = this;
 	this.children.push(object);
+};
+
+/**
+ * Remove object from the children list.
+ */
+Object2D.prototype.remove = function(object)
+{
+	var index = this.children.indexOf(object);
+	if(index !== -1)
+	{
+		this.children[index].parent = null;
+		this.children.splice(index, 1)
+	}
+};
+
+/**
+ * Draw the object into the canvas.
+ */
+Object2D.prototype.draw = function(context)
+{
+
 };

+ 5 - 0
source/Viewport.js

@@ -1,5 +1,10 @@
 "use strict";
 
+/**
+ * Used to indicate how the user views the content inside of the canvas.
+ *
+ * @class
+ */
 function Viewport()
 {
 	

+ 1 - 1
source/math/Box2.js

@@ -172,4 +172,4 @@ Object.assign(Box2.prototype,
 	}
 });
 
-export {Box2};
+//export {Box2};

+ 1 - 1
source/math/Matrix3.js

@@ -327,4 +327,4 @@ Object.assign(Matrix3.prototype,
 	}
 });
 
-export {Matrix3};
+//export {Matrix3};

+ 111 - 0
source/math/Matrix32.js

@@ -0,0 +1,111 @@
+
+const Matrix32 = (() =>
+{
+	// private
+	let self;
+	let m = [1, 0, 0, 1, 0, 0];
+
+	const reset = () =>
+	{
+		const m = [1,0,0,1,0,0];
+	};
+
+	const multiply = mat =>
+	{
+		const m0=m[0]*mat[0]+m[2]*mat[1];
+		const m1=m[1]*mat[0]+m[3]*mat[1];
+		const m2=m[0]*mat[2]+m[2]*mat[3];
+		const m3=m[1]*mat[2]+m[3]*mat[3];
+		const m4=m[0]*mat[4]+m[2]*mat[5]+m[4];
+		const m5=m[1]*mat[4]+m[3]*mat[5]+m[5];
+		m=[m0,m1,m2,m3,m4,m5];
+	};
+
+	const screenPoint=(transformedX, transformedY) =>
+	{
+		// invert
+		const d =1/(m[0]*m[3]-m[1]*m[2]);
+		im = [ m[3]*d, -m[1]*d, -m[2]*d, m[0]*d, d*(m[2]*m[5]-m[3]*m[4]), d*(m[1]*m[4]-m[0]*m[5]) ];
+
+		// point
+		return(
+		{
+			x:transformedX*im[0]+transformedY*im[2]+im[4],
+			y:transformedX*im[1]+transformedY*im[3]+im[5]
+		});
+	};
+
+	const transformedPoint=(screenX, screenY) => ({
+		x:screenX*m[0] + screenY*m[2] + m[4],
+		y:screenX*m[1] + screenY*m[3] + m[5]
+	});
+
+	// public
+	class Matrix32
+	{
+		constructor()
+		{
+			self = this;
+		}
+
+		// shared methods
+		translate(x, y)
+		{
+			const mat = [1, 0, 0, 1, x, y];
+			multiply(mat);
+		}
+
+		rotate(rAngle)
+		{
+			const c = Math.cos(rAngle);
+			const s = Math.sin(rAngle);
+			const mat = [c, s, -s, c, 0, 0];    
+			multiply(mat);
+		}
+
+		scale(x, y)
+		{
+			const mat = [x, 0, 0, y, 0, 0];        
+			multiply(mat);
+		}
+
+		skew(radianX, radianY)
+		{
+			const mat=[ 1, Math.tan(radianY), Math.tan(radianX), 1, 0, 0 ];
+			multiply(mat);
+		}
+
+		reset()
+		{
+			reset();
+		}
+
+		setContextTransform(ctx)
+		{
+			ctx.setTransform(m[0], m[1], m[2], m[3], m[4], m[5]);
+		}
+
+		resetContextTransform(ctx)
+		{
+			ctx.setTransform(1,0,0,1,0,0);
+		}
+
+		getTransformedPoint(screenX, screenY)
+		{
+			return transformedPoint(screenX, screenY);
+		}
+
+		getScreenPoint(transformedX, transformedY)
+		{
+			return screenPoint(transformedX, transformedY);
+		}
+
+		getMatrix()
+		{
+			const clone = [m[0], m[1], m[2], m[3], m[4], m[5]];
+			return(clone);
+		}
+	}
+
+	return(Matrix32);
+})();