home *** CD-ROM | disk | FTP | other *** search
/ com!online 2002 July / com!online0702.iso / software / livemotion / DATA1.CAB / Include / CubicBezier.js < prev    next >
Encoding:
Text File  |  2002-05-13  |  5.7 KB  |  157 lines

  1. /***************************************************************
  2. ADOBE SYSTEMS INCORPORATED 
  3. Copyright 2002 Adobe Systems Incorporated 
  4. All Rights Reserved 
  5.  
  6. NOTICE:  Adobe permits you to use, modify, and distribute this 
  7. file in accordance with the terms of the Adobe license agreement 
  8. accompanying it.  If you have received this file from a source 
  9. other than Adobe, then your use, modification, or distribution
  10. of it requires the prior written permission of Adobe. 
  11. ***************************************************************/
  12. /***************************************************************
  13. Author: Mary Obelnicki
  14. ***************************************************************/
  15.  
  16. /***************************************************************
  17.  
  18. This is the base constructor class for a Cubic Bezier object.  The 
  19. constructor takes four arguments: startPt, startControl, endControl,
  20. and endPt.  Each argument is assumed to be a 2-dimensional vector
  21. object. (See Vector.js)
  22.  
  23. startPt - The start vector point
  24. startControl - The control point for the start vector point
  25. endControl - The control point for the end vector point
  26. endPt - The end vector point
  27.  
  28. The constructor has three methods:
  29.  
  30. CubicBezier.xValue(t) and CubicBezier.yValue(t)
  31.     These methods return the x and y values (respectively) of
  32.     the bezier curve at time <t>.
  33.     
  34. CubicBezier.pointAt(t)
  35.     This method is based on the two above methods.  Instead of 
  36.     returning a short of the coordinate, pointAt() returns a
  37.     vector point object of the bezier curve at time <t>.
  38.     
  39. The class has two methods that work with LiveMotion automation
  40. scripting.  These two methods take in a LiveMotion path object
  41. and return a CubicBezier object.
  42.  
  43. CubicBezier.pathSegmentToBezier(pathObject, firstKnot, secondKnot)
  44.     returns a CubicBezier object representing the segment start
  45.     at <firstKnot>.  The returned bezier is in the same coordinate
  46.     space as <pathObject>.  The returned bezier reflects the base
  47.     shape of <pathObject>.  The bezier does not express any
  48.     post-transforms applied to <pathObject>.
  49.  
  50. CubicBezier.pathToBeziers(pathObject, pathIndex)
  51.     based on the above function, pathToBeziers() returns an array
  52.     of CubicBezier objects representing an entire path at <pathIndex>
  53.     in <pathObject>.  
  54.     Note: a LMPathObject may contain multiple paths.
  55.  
  56. ***************************************************************/
  57.  
  58. /***************************************************************
  59. DO NOT EDIT BELOW THIS LINE
  60. ***************************************************************/
  61.  
  62.  
  63. #include "Vector.js"
  64.  
  65. function CubicBezier(startPt, startControl, endControl, endPt)
  66. {
  67.     this.startX = startPt.x; 
  68.     this.startY = startPt.y; 
  69.     this.startControlX = startControl.x; 
  70.     this.startControlY = startControl.y; 
  71.     this.endControlX = endControl.x; 
  72.     this.endControlY = endControl.y; 
  73.     this.endX = endPt.x; 
  74.     this.endY = endPt.y;
  75.  
  76. }
  77.  
  78. CubicBezier.prototype.xValue =
  79.     function(t)
  80.     {        
  81.     var inverseT = 1 - t; 
  82.     var returnX = inverseT*inverseT*inverseT*(this.startX); 
  83.     returnX += 3*t*inverseT*inverseT*(this.startControlX);
  84.     returnX += 3*t*t*inverseT*(this.endControlX); 
  85.     returnX += t*t*t*(this.endX); 
  86.     return returnX; 
  87.     };
  88.  
  89. CubicBezier.prototype.yValue = 
  90.     function(t)
  91.     {
  92.     var inverseT = 1 - t; 
  93.     var returnY = inverseT*inverseT*inverseT*(this.startY); 
  94.     returnY += 3*t*inverseT*inverseT*(this.startControlY);
  95.     returnY += 3*t*t*inverseT*(this.endControlY); 
  96.     returnY += t*t*t*(this.endY); 
  97.     return returnY; 
  98.     };    
  99.  
  100. //returns a Vector representing the point at <t>
  101. CubicBezier.prototype.pointAt = 
  102.     function(t)
  103.     {
  104.     return new Vector(this.xValue(t), this.yValue(t)); 
  105.     }; 
  106.  
  107. // returns a CubicBezier representing the segment starting at knot, 
  108. // <firstKnot> and ending with <secondKnot>.  The returned bezier is
  109. // in the same coordinate space as <pathObject>. The returned bezier
  110. // reflects the base shape of the pathObject, it does not express any
  111. // transforms applied to the path object. 
  112. CubicBezier.pathSegmentToBezier = 
  113.     function(pathObject, firstKnot, secondKnot)
  114.     {
  115.     var degToRads = Math.PI/180;  
  116.  
  117.     // maintains positive y as down.
  118.     var startPt = new Vector(pathObject.position.x + firstKnot.position.x, pathObject.position.y + firstKnot.position.y); 
  119.     var startControl = new Vector(pathObject.position.x + firstKnot.position.x + firstKnot.leaveHandle.radius*Math.cos(firstKnot.leaveHandle.angle*degToRads), 
  120.                       pathObject.position.y + firstKnot.position.y - firstKnot.leaveHandle.radius*Math.sin(firstKnot.leaveHandle.angle*degToRads)); 
  121.     var endControl = new Vector(pathObject.position.x + secondKnot.position.x + secondKnot.enterHandle.radius*Math.cos(secondKnot.enterHandle.angle*degToRads), 
  122.                     pathObject.position.y + secondKnot.position.y - secondKnot.enterHandle.radius*Math.sin(secondKnot.enterHandle.angle*degToRads)); 
  123.     var endPt =  new Vector(pathObject.position.x + secondKnot.position.x, pathObject.position.y + secondKnot.position.y); 
  124.     
  125.     return new CubicBezier(startPt, startControl, endControl, endPt); 
  126.  
  127.     }
  128.  
  129. // returns an array of CubicBeziers representing path at <pathIndex>
  130. // in <pathObject>. 
  131.  
  132. CubicBezier.pathToBeziers = 
  133.     function(pathObject, pathIndex)
  134.     {
  135.     var returnBeziers = new Array; 
  136.     var thePath = pathObject.paths[pathIndex]; 
  137.     var theKnots = thePath.knots; 
  138.     var i=0; 
  139.     while(i < theKnots.length - 1)
  140.     {
  141.         returnBeziers[i] = CubicBezier.pathSegmentToBezier(pathObject, theKnots[i], theKnots[i+1]); 
  142.         i++; 
  143.     }
  144.     //if the path is closed
  145.     if(thePath.closed){
  146.         returnBeziers[i] = CubicBezier.pathSegmentToBezier(pathObject, theKnots[i], theKnots[0]); 
  147.     }
  148.     return returnBeziers; 
  149.     }
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.