public
Created

  • Download Gist
3d_transforms.patch
Diff
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
From ec4ee0a508c3b4573b805cafda8c8f35388f9ffc Mon Sep 17 00:00:00 2001
From: Benedict Eastaugh <benedict@eastaugh.net>
Date: Sat, 24 Jul 2010 15:43:57 +0100
Subject: [PATCH] Just generating a patch so I can hack on stuff.
 
---
src/firmin.js | 115 +++++++++++++++++++++++++++++++++++++++++-----
test/3d-transforms.html | 109 ++++++++++++++++++++++++++++++++++++++++++++
test/index.html | 1 +
3 files changed, 212 insertions(+), 13 deletions(-)
create mode 100644 test/3d-transforms.html
 
diff --git a/src/firmin.js b/src/firmin.js
index b5af7b9..4ffcb54 100644
--- a/src/firmin.js
+++ b/src/firmin.js
@@ -110,11 +110,12 @@ Firmin.Transform.multiply = function(a, b) {
};
Firmin.Transform.methods = [
- "translate", "translateX", "translateY",
- "scale", "scaleX", "scaleY",
- "rotate",
+ "translate", "translate3d", "translateX", "translateY", "translateZ",
+ "scale", "scale3d", "scaleX", "scaleY", "scaleZ",
+ "rotate", "rotate3d", "rotateX", "rotateY", "rotateZ",
"skew", "skewX", "skewY",
- "matrix"
+ "matrix", "matrix3d",
+ "perspective"
];
/*
@@ -163,6 +164,7 @@ Firmin.Transform.parse = function(description, context) {
};
Firmin.Transform.prototype.matrix3d = function(m) {
+ console.log(m);
this.ctm = Firmin.Transform.multiply(this.ctm, m);
};
@@ -219,6 +221,22 @@ Firmin.Transform.prototype.translate = function(distances) {
this.matrix(1, 0, 0, 1, x, y);
};
+Firmin.Transform.prototype.translate3d = function(distances) {
+ var vector = Firmin.pointToVector(distances),
+ x = vector[0],
+ y = vector[1],
+ z = vector[2];
+
+ if (typeof x !== "number") x = parseInt(x, 10) || 0;
+ if (typeof y !== "number") y = parseInt(y, 10) || 0;
+ if (typeof z !== "number") z = parseInt(z, 10) || 0;
+
+ this.matrix3d([1, 0, 0, 0,
+ 0, 1, 0, 0,
+ 0, 0, 1, 0,
+ x, y, z, 1]);
+};
+
Firmin.Transform.prototype.translateX = function(distance) {
this.translate([distance, 0]);
};
@@ -227,29 +245,50 @@ Firmin.Transform.prototype.translateY = function(distance) {
this.translate([0, distance]);
};
+Firmin.Transform.prototype.translateZ = function(distance) {
+ this.translate3d([0, 0, distance]);
+};
+
+Firmin.Transform.prototype.scale3d = function(magnitudes) {
+ var vector = Firmin.pointToVector(magnitudes),
+ x = vector[0],
+ y = vector[1],
+ z = vector[2];
+
+ if (typeof x !== "number") x = 1;
+ if (typeof y !== "number") y = 1;
+ if (typeof z !== "number") z = 1;
+
+ this.matrix3d([x, 0, 0, 0,
+ 0, y, 0, 0,
+ 0, 0, z, 0,
+ 0, 0, 0, 1])
+};
+
Firmin.Transform.prototype.scale = function(magnitudes) {
- var vector, x, y;
+ var vector;
if (typeof magnitudes === "number") {
- x = y = magnitudes;
+ vector = [magnitudes, magnitudes, 1];
} else {
vector = Firmin.pointToVector(magnitudes);
- x = vector[0];
- y = vector[1];
- if (typeof x !== "number") x = 1;
- if (typeof y !== "number") y = 1;
+ if (typeof vector[2] !== "number") vector[2] = 1;
}
- this.matrix(x, 0, 0, y, 0, 0);
+ this.scale3d(vector);
};
Firmin.Transform.prototype.scaleX = function(magnitude) {
- this.scale([magnitude, 1]);
+ this.scale3d([magnitude, 1, 1]);
};
Firmin.Transform.prototype.scaleY = function(magnitude) {
- this.scale([1, magnitude]);
+ this.scale3d([1, magnitude, 1]);
+};
+
+Firmin.Transform.prototype.scaleZ = function(magnitude) {
+ this.scale3d([1, 1, magnitude]);
};
Firmin.Transform.prototype.skew = function(magnitudes) {
@@ -296,6 +335,52 @@ Firmin.Transform.prototype.rotate = function(angle) {
);
};
+Firmin.Transform.prototype.rotate3d = function(params) {
+ var x = params.x,
+ y = params.y,
+ z = params.z,
+ a = params.angle,
+ cos = Math.cos,
+ sin = Math.sin;
+
+ if (typeof x !== "number") x = 0;
+ if (typeof y !== "number") y = 0;
+ if (typeof z !== "number") z = 0;
+
+ a = Firmin.angleToRadians.apply(null, Firmin.Parser.parseAngle(a));
+
+ this.matrix3d([
+ 1 + (1 - cos(a)) * (x * x - 1),
+ -z * sin(a) + (1 - cos(a)) * x * y,
+ y * sin(a) + (1 - cos(a)) * x * z,
+ 0,
+ z * sin(a) + (1 - cos(a)) * x * y,
+ 1 + (1 - cos(a)) * (y * y - 1),
+ -x * sin(a) + (1 - cos(a)) * y * z,
+ 0,
+ -y * sin(a) + (1 - cos(a)) * x * z,
+ x * sin(a) + (1 - cos(a)) * y * z,
+ 1 + (1 - cos(a)) * (z * z - 1),
+ 0,
+ 0,
+ 0,
+ 0,
+ 1
+ ]);
+};
+
+Firmin.Transform.prototype.rotateX = function(angle) {
+ this.rotate3d({x: 1, angle: angle});
+};
+
+Firmin.Transform.prototype.rotateY = function(angle) {
+ this.rotate3d({y: 1, angle: angle});
+};
+
+Firmin.Transform.prototype.rotateZ = function(angle) {
+ this.rotate3d({z: 1, angle: angle});
+};
+
Firmin.Transform.prototype.origin = function(origin) {
var vector = Firmin.pointToVector(origin);
@@ -304,6 +389,10 @@ Firmin.Transform.prototype.origin = function(origin) {
if (vector[2]) this.centre[2] = vector[2];
};
+Firmin.Transform.prototype.perspective = function(depth) {
+
+};
+
/*
CSS transitions are the basic mechanism behind animation in Firmin, and
diff --git a/test/3d-transforms.html b/test/3d-transforms.html
new file mode 100644
index 0000000..70dc278
--- /dev/null
+++ b/test/3d-transforms.html
@@ -0,0 +1,109 @@
+<!DOCTYPE html>
+<html><head>
+ <title>Firmin 3D transform tests</title>
+
+ <style type="text/css" media="screen">
+ html, body {font-family:Helvetica, Arial, sans-serif; color:#000; background:#fff; margin:0; border:none; padding:0;}
+ body {margin:50px;}
+
+ .container {
+ position:relative;
+ float:left;
+ width:200px;
+ height:200px;
+ margin:0 20px 20px 0;
+ border:1px solid #ccc;
+ padding:50px;
+ }
+
+ .legend {
+ margin:0;
+ position:absolute;
+ top:10px;
+ left:10px;
+ font-weight:bold;
+ background:#fff;
+ }
+
+ .transform-css, .transform-js {
+ position:absolute;
+ top:50px;
+ left:50px;
+ width:200px;
+ height:200px;
+ opacity:0.5;
+ }
+
+ .transform-css {
+ background:-webkit-gradient(linear, 0 0, 0 100%, from(#f00), to(#fff));
+ }
+
+ .transform-js {
+ background:-webkit-gradient(linear, 0 0, 0 100%, from(#00f), to(#fff));
+ }
+
+ #test-translate .transform-css {
+ -webkit-transform:translate3d(20px, 20px, 20px);
+ }
+
+ #test-rotate .transform-css {
+ -webkit-transform:rotate3d(0, 1, 1, 2rad);
+ }
+
+ #test-scale .transform-css {
+ -webkit-transform:rotateZ(1rad) scale3d(1, 1, 2);
+ }
+ </style>
+
+ <script type="text/javascript" src="lib/firmin-min.js"></script>
+
+ <script type="text/javascript" charset="utf-8">
+ window.$ = function(query) {
+ return document.querySelector(query);
+ };
+ </script>
+</head><body>
+ <h1 id="description">Transform tests</h1>
+
+ <p>Red boxes are transformed with CSS, blue boxes with Firmin.</p>
+
+ <p>
+ Boxes not lining up exactly (producing a purple box) indicates a
+ regression.
+ </p>
+
+ <div class="container" id="test-translate">
+ <div class="transform-css"></div>
+ <div class="transform-js"></div>
+ <p class="legend">Translate</p>
+ </div>
+
+ <script type="text/javascript">
+ Firmin.translate3d($('#test-translate .transform-js'), ["20px", "20px", "20px"]);
+ </script>
+
+ <div class="container" id="test-rotate">
+ <div class="transform-css"></div>
+ <div class="transform-js"></div>
+ <p class="legend">Rotate</p>
+ </div>
+
+ <script type="text/javascript">
+ Firmin.rotate3d($('#test-rotate .transform-js'), {
+ x: 0, y: 1, z: 1, angle: "2rad"
+ });
+ </script>
+
+ <div class="container" id="test-scale">
+ <div class="transform-css"></div>
+ <div class="transform-js"></div>
+ <p class="legend">Scale</p>
+ </div>
+
+ <script type="text/javascript">
+ Firmin.animate($('#test-scale .transform-js'), {
+ rotateZ: "1rad",
+ scale3d: {x: 1, y: 1, z: 2}
+ });
+ </script>
+</body></html>
diff --git a/test/index.html b/test/index.html
index ea2ddfd..3433420 100644
--- a/test/index.html
+++ b/test/index.html
@@ -8,6 +8,7 @@
<li><a href="animate.html">Animation</a></li>
<li><a href="transition.html">Transitions</a></li>
<li><a href="transforms.html">Transforms</a></li>
+ <li><a href="3d-transforms.html">3D Transforms</a></li>
<li><a href="chain.html">Chaining</a></li>
<li><a href="parser.html">Parsing</a></li>
</ul>
--
1.7.1

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.