Fork me on GitHub

API Documentation


Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_animation_glge_animationcurve.js.html, line: 104 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29

Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_animation_glge_animationcurve.js.html, line: 104 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29

Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_animation_glge_animationcurve.js.html, line: 107 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29

Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_animation_glge_animationcurve.js.html, line: 107 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29

Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_animation_glge_animationcurve.js.html, line: 110 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29

Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_animation_glge_animationcurve.js.html, line: 110 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29

Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_animation_glge_animationcurve.js.html, line: 113 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29

Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_animation_glge_animationcurve.js.html, line: 113 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29

Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_animation_glge_animationcurve.js.html, line: 125 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29

Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_animation_glge_animationcurve.js.html, line: 125 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29

Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_animation_glge_animationcurve.js.html, line: 132 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29

Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_animation_glge_animationcurve.js.html, line: 132 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29

Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_animation_glge_animationcurve.js.html, line: 139 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29

Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_animation_glge_animationcurve.js.html, line: 139 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29

Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_animation_glge_animationcurve.js.html, line: 146 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29

Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_animation_glge_animationcurve.js.html, line: 146 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29

Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_animation_glge_animationcurve.js.html, line: 240 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29

Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_animation_glge_animationcurve.js.html, line: 240 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29

Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_animation_glge_animationcurve.js.html, line: 241 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29

Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_animation_glge_animationcurve.js.html, line: 241 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29

Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_animation_glge_animationcurve.js.html, line: 241 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29

Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_animation_glge_animationcurve.js.html, line: 241 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29

Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_animation_glge_animationcurve.js.html, line: 242 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29

Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_animation_glge_animationcurve.js.html, line: 242 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29

Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_animation_glge_animationcurve.js.html, line: 242 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29

Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_animation_glge_animationcurve.js.html, line: 242 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29
  1 /*
  2 GLGE WebGL Graphics Engine
  3 Copyright (c) 2010, Paul Brunt
  4 All rights reserved.
  5 
  6 Redistribution and use in source and binary forms, with or without
  7 modification, are permitted provided that the following conditions are met:
  8     * Redistributions of source code must retain the above copyright
  9       notice, this list of conditions and the following disclaimer.
 10     * Redistributions in binary form must reproduce the above copyright
 11       notice, this list of conditions and the following disclaimer in the
 12       documentation and/or other materials provided with the distribution.
 13     * Neither the name of GLGE nor the
 14       names of its contributors may be used to endorse or promote products
 15       derived from this software without specific prior written permission.
 16 
 17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 20 DISCLAIMED. IN NO EVENT SHALL PAUL BRUNT BE LIABLE FOR ANY
 21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 27 */
 28 
 29 /**
 30  * @fileOverview
 31  * @name glge_animationcurve.js
 32  * @author me@paulbrunt.co.uk
 33  */
 34 
 35 
 36 (function(GLGE){
 37 
 38 
 39 
 40 
 41 
 42 /**
 43 * @class A curve which interpolates between control points
 44 * @augments GLGE.QuickNotation
 45 * @augments GLGE.JSONLoader
 46 */
 47 GLGE.AnimationCurve=function(uid){
 48 	this.keyFrames=[];
 49 	this.solutions={};
 50 	this.caches={};
 51 	GLGE.Assets.registerAsset(this,uid);
 52 };
 53 GLGE.augment(GLGE.QuickNotation,GLGE.AnimationCurve);
 54 GLGE.augment(GLGE.JSONLoader,GLGE.AnimationCurve);
 55 GLGE.AnimationCurve.prototype.className="AnimationCurve";
 56 GLGE.AnimationCurve.prototype.keyFrames=null;
 57 /**
 58 * Adds a point to the curve
 59 * @param {object} point The point to add
 60 * @returns {Number} Index of the newly added point
 61 */
 62 GLGE.AnimationCurve.prototype.addPoint=function(point){
 63 	this.keyFrames.push(point);
 64 	return this.keyFrames.length-1;
 65 };
 66 GLGE.AnimationCurve.prototype.addStepPoint=GLGE.AnimationCurve.prototype.addPoint;
 67 GLGE.AnimationCurve.prototype.addLinearPoint=GLGE.AnimationCurve.prototype.addPoint;
 68 GLGE.AnimationCurve.prototype.addBezTriple=GLGE.AnimationCurve.prototype.addPoint;
 69 /**
 70 * Get the value of the curve at any point
 71 * @param {Number} frame The frame(x-coord) to return the value for
 72 * @returns {Number} The value of the curve at the given point
 73 */
 74 GLGE.AnimationCurve.prototype.coord=function(x,y){
 75 	return {x:x,y:y}
 76 }
 77 /**
 78 * Sets the animation channel this curve animates
 79 * @param {string} channel The property to animate
 80 */
 81 GLGE.AnimationCurve.prototype.setChannel=function(channel){
 82 	this.channel=channel
 83 }
 84 GLGE.AnimationCurve.prototype.getValue=function(frame){
 85 	if(this.keyFrames.length==0) return 0;
 86 	
 87 	if(this.caches[frame]) return this.caches[frame];
 88 	var startKey;
 89 	var endKey;
 90 	var preStartKey;
 91 	var preEndKey;
 92 	if(frame<this.keyFrames[0].x) return this.keyFrames[0].y;
 93 	for(var i=0; i<this.keyFrames.length;i++){
 94 		if(this.keyFrames[i].x==frame){
 95 			return this.keyFrames[i].y;
 96 		}
 97 		if(this.keyFrames[i].x<=frame && (startKey==undefined || this.keyFrames[i].x>this.keyFrames[startKey].x)){
 98 			preStartKey=startKey;
 99 			startKey=i;
100 		}else if(this.keyFrames[i].x<=frame && (preStartKey==undefined || this.keyFrames[i].x>this.keyFrames[preStartKey].x)){
101 			preStartKey=i;
102 		}
103 		if(this.keyFrames[i].x>frame && (endKey==undefined || this.keyFrames[i].x<=this.keyFrames[endKey].x)){
104 			preEndKey=endKey;
105 			endKey=i;
106 		}else if(this.keyFrames[i].x>frame && (preEndKey==undefined || this.keyFrames[i].x<=this.keyFrames[preEndKey].x)){
107 			preEndKey=i;
108 		}
109 	}
110 	if(startKey==undefined){
111 		startKey=endKey;
112 		endKey=preEndKey;
113 	}
114 	if(endKey==undefined){
115 		endKey=startKey;
116 		startKey=preStartKey;
117 	}
118 	if(this.keyFrames[startKey] instanceof GLGE.BezTriple && this.keyFrames[endKey] instanceof GLGE.BezTriple){
119 		var C1=this.coord(this.keyFrames[startKey].x,this.keyFrames[startKey].y);
120 		var C2=this.coord(this.keyFrames[startKey].x3,this.keyFrames[startKey].y3);
121 		var C3=this.coord(this.keyFrames[endKey].x1,this.keyFrames[endKey].y1);
122 		var C4=this.coord(this.keyFrames[endKey].x,this.keyFrames[endKey].y);
123 		return this.atX(frame,C1,C2,C3,C4).y;
124 	}
125 	if(this.keyFrames[startKey] instanceof GLGE.LinearPoint && this.keyFrames[endKey] instanceof GLGE.BezTriple){
126 		var C1=this.coord(this.keyFrames[startKey].x,this.keyFrames[startKey].y);
127 		var C2=this.coord(this.keyFrames[endKey].x1,this.keyFrames[endKey].y1);
128 		var C3=this.coord(this.keyFrames[endKey].x1,this.keyFrames[endKey].y1);
129 		var C4=this.coord(this.keyFrames[endKey].x,this.keyFrames[endKey].y);
130 		return this.atX(frame,C1,C2,C3,C4).y;
131 	}
132 	if(this.keyFrames[startKey] instanceof GLGE.BezTriple && this.keyFrames[endKey] instanceof GLGE.LinearPoint){
133 		var C1=this.coord(this.keyFrames[startKey].x,this.keyFrames[startKey].y);
134 		var C2=this.coord(this.keyFrames[startKey].x3,this.keyFrames[startKey].y3);
135 		var C3=this.coord(this.keyFrames[startKey].x3,this.keyFrames[startKey].y3);
136 		var C4=this.coord(this.keyFrames[endKey].x,this.keyFrames[endKey].y);
137 		return this.atX(frame,C1,C2,C3,C4).y;
138 	}
139 	if(this.keyFrames[startKey] instanceof GLGE.LinearPoint && this.keyFrames[endKey] instanceof GLGE.LinearPoint){
140 		var value=(frame-this.keyFrames[startKey].x)*(this.keyFrames[endKey].y-this.keyFrames[startKey].y)/(this.keyFrames[endKey].x-this.keyFrames[startKey].x)+this.keyFrames[startKey].y;
141 		return value;
142 	}
143 	if(this.keyFrames[startKey] instanceof GLGE.StepPoint){
144 		return this.keyFrames[startKey].y
145 	}
146 	if(!this.keyFrames.preStartKey) this.keyFrames.preStartKey=this.keyFrames[0].y;
147 	
148 	this.caches[frame]=this.keyFrames.preStartKey;
149 	
150 	return this.caches[frame];
151 };
152 /**
153 * Function used to calculate bezier curve
154 * @private
155 */
156 GLGE.AnimationCurve.prototype.B1=function(t) { return t*t*t };
157 /**
158 * Function used to calculate bezier curve
159 * @private
160 */
161 GLGE.AnimationCurve.prototype.B2=function(t) { return 3*t*t*(1-t) };
162 /**
163 * Function used to calculate bezier curve
164 * @private
165 */
166 GLGE.AnimationCurve.prototype.B3=function(t) { return 3*t*(1-t)*(1-t) };
167 /**
168 * Function used to calculate bezier curve
169 * @private
170 */
171 GLGE.AnimationCurve.prototype.B4=function(t) { return (1-t)*(1-t)*(1-t) };
172 /**
173 * Gets the value of a bezier curve at a given point
174 * @private
175 */
176 GLGE.AnimationCurve.prototype.getBezier=function(t,C1,C2,C3,C4) {
177 	var pos = {};
178 	pos.x = C1.x*this.B1(t) + C2.x*this.B2(t) + C3.x*this.B3(t) + C4.x*this.B4(t);
179 	pos.y = C1.y*this.B1(t) + C2.y*this.B2(t) + C3.y*this.B3(t) + C4.y*this.B4(t);
180 	return pos;
181 };
182 /**
183 * Solves cubic equation to get the parametic value of the curve at a specified point
184 * @private
185 */
186 GLGE.AnimationCurve.prototype.Quad3Solve=function(a,b,c,d){
187 	ref=a+"-"+b+"-"+"-"+c+"-"+d;
188 	if(this.solutions[ref]){
189 		return this.solutions[ref];
190 	}
191 	else
192 	{
193 		b /= a;c /= a;d /= a;
194 		var q, r, d1, s, t, t1, r13;
195 		q = (3.0*c - (b*b))/9.0;
196 		r = -(27.0*d) + b*(9.0*c - 2.0*(b*b));
197 		r /= 54.0;
198 		t1 = (b/3.0);
199 		discrim = q*q*q + r*r;
200 		result=[];
201 				
202 		if (discrim > 0) { 
203 		// one real, two complex
204 		 s = r + Math.sqrt(discrim);
205 		 s = ((s < 0) ? -Math.pow(-s, (1.0/3.0)) : Math.pow(s, (1.0/3.0)));
206 		 t = r - Math.sqrt(discrim);
207 		 t = ((t < 0) ? -Math.pow(-t, (1.0/3.0)) : Math.pow(t, (1.0/3.0)));
208 		 result[0] = -t1 + s + t;
209 		 t1 = t1 + (s + t)/2.0;
210 		 result[1] = result[2] = -t1;
211 		 t1 = Math.sqrt(3.0)*(-t + s)/2;
212 		} 
213 		else if (discrim == 0){ 
214 		// All roots real
215 		 r13 = ((r < 0) ? -Math.pow(-r,(1.0/3.0)) : Math.pow(r,(1.0/3.0)));
216 		 result[1] = -t1 + 2.0*r13;
217 		 result[1] = result[2]  = -(r13 + t1);
218 		} 
219 		else
220 		{
221 			q = -q;
222 			d1 = q*q*q;
223 			d1 = Math.acos(r/Math.sqrt(1));
224 			r13 = 2.0*Math.sqrt(q);
225 
226 
227 			result[0] = -t1 + r13*Math.cos(d1/3.0);
228 			result[1] = -t1 + r13*Math.cos((d1 + 2.0*Math.PI)/3.0);
229 			result[2] = -t1 + r13*Math.cos((d1 + 4.0*Math.PI)/3.0);
230 		}
231 		var toreturn=false;
232 		//determine which is the correct result
233 		if(result[0]>=0 && result[0]<=1) toreturn=result[0];
234 		if(!toreturn && result[1]>=0 && result[1]<=1) toreturn=result[1];
235 		if(!toreturn && result[2]>=0 && result[2]<=1) toreturn=result[2];
236 		//cache result for next time
237 		this.solutions[ref]=toreturn;
238 		
239 		return toreturn;
240 	}
241 };
242 /**
243 * Get the value of the a single bezier curve 
244 * @param {Number} x xcoord of point to get
245 * @param {Number} C1 First bezier control point
246 * @param {Number} C2 Second bezier control point
247 * @param {Number} C3 Third bezier control point
248 * @param {Number} C4 Forth bezier control point
249 * @returns {Number} The value of the curve at the given x
250 */
251 GLGE.AnimationCurve.prototype.atX=function(x,C1,C2,C3,C4){
252 	a=C1.x-C2.x*3+C3.x*3-C4.x;
253 	b=C2.x*3-C3.x*6+C4.x*3;
254 	c=C3.x*3-C4.x*3;
255 	d=C4.x-x;
256 	return this.getBezier(this.Quad3Solve(a,b,c,d),C1,C2,C3,C4);
257 };
258 
259 })(GLGE);
260