home *** CD-ROM | disk | FTP | other *** search
/ com!online 2002 July / com!online0702.iso / software / livemotion / DATA1.CAB / Automation / Scripts / Animate On Path.js next >
Encoding:
Text File  |  2002-05-13  |  7.8 KB  |  235 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 script is used to create a key framed animation of an array
  19. of objects traveling along a bezier curve path.
  20.  
  21. To use:
  22.     Create a path.  Group it with one or more objects.  The
  23.     objects will be animated in sequence.  With the group
  24.     selected, execute the script.
  25.  
  26.  
  27. Function:
  28. animateGroupToCurve(group, pathIndex, frames, track, stagger)
  29.  
  30. Arguments:
  31.     <group> LMGroupObject: a group with both the object to 
  32.         animate and the path to animate it on the path used 
  33.         as the base for    the animation will be the top-most 
  34.         path object in the group, the object will be the 
  35.         top-most object that is not the animation path
  36.     <pathIndex> integer: which path in the path object to use, 
  37.         this value is usually 0. 
  38.     <frames> integer: how many key frames of animation to create
  39.     <track> boolean: the rotation of the object track the 
  40.         curve. true = yes, false = no 
  41.  
  42. ***************************************************************/
  43.  
  44. #include "../../Include/CubicBezier.js"
  45.  
  46. /***************************************************************
  47. To change the behavior of this script, 
  48. make your changes below
  49. ***************************************************************/
  50.  
  51. animateGroupToCurve(application.currentComposition.objects[0], 0, 24, true, 1);
  52.  
  53. /***************************************************************
  54. DO NOT EDIT BELOW THIS LINE
  55. ***************************************************************/
  56.  
  57. function animateToCurve(object, curves, frames, track)
  58. //object: the object that will be animated
  59. //curves: an array of parameterize curves.  Each curve must have a function pointAt()
  60. //        and be parameterize for 0 <= t <= 1.
  61. //frames: how many frames the animation will occupy. 
  62. //        if you want an animation that takes 2 seconds at 12 fps, frames must equal 25.
  63. //track: a boolean.  If true, <objects>'s rotation will follow changes
  64. //       in the angle of the line tangent to the path
  65. {
  66.  
  67.     if (curves.length < 1)
  68.     return; 
  69.  
  70.     frames--;
  71.     var startFrame = object.currentFrame; 
  72.     var tPerFrame = curves.length/frames; 
  73.     var oriObjRot; 
  74.     var oriPathAngle; 
  75.  
  76.     object.stopwatch.position = true;
  77.  
  78.     if(track)
  79.     {
  80.         object.stopwatch.rotation = true; 
  81.         oriObjRot = object.rotation;
  82.         var vectorOne = curves[0].pointAt(0.0); 
  83.         var vectorTwo = curves[0].pointAt(0.001); 
  84.         oriPathAngle = Vector.subtract(vectorTwo, vectorOne).polarAngle();  
  85.     }
  86.     
  87.     var curveIndex; 
  88.     for(curveIndex = 0; curveIndex < curves.length; curveIndex++)
  89.     {
  90.         var firstFrame = Math.ceil(curveIndex*frames/curves.length); 
  91.         var lastFrame = Math.floor((curveIndex+1)*frames/curves.length); 
  92.         var frame; 
  93.  
  94.         for(frame = firstFrame; frame <= lastFrame ; frame++)
  95.         {
  96.             object.currentFrame = startFrame +frame; 
  97.             var currentT = (frame*tPerFrame) - curveIndex;
  98.             var currentPoint = curves[curveIndex].pointAt(currentT); 
  99.             object.position.x = currentPoint.x; 
  100.             object.position.y = currentPoint.y; 
  101.  
  102.             if(track)
  103.             {
  104.                 //get two points on the curve
  105.                 //currentPoint is the first point            
  106.                 //get the angle tangent to the curve
  107.                 var otherPoint; 
  108.                 var currentPathAngle; 
  109.                 if((currentT +.01)>1.0)
  110.                 {
  111.                     otherPoint = curves[curveIndex].pointAt(currentT - 0.001);
  112.                     currentPathAngle = Vector.subtract(currentPoint, otherPoint).polarAngle(); 
  113.                     //            currentPathAngle = getPolarAngle(otherPoint, currentPoint);
  114.                 }
  115.                 else
  116.                 {
  117.                     otherPoint = curves[curveIndex].pointAt(currentT + 0.001);
  118.                     currentPathAngle = Vector.subtract(otherPoint, currentPoint).polarAngle(); 
  119.                     //    currentPathAngle = getPolarAngle(currentPoint, otherPoint); 
  120.                 }
  121.                 //set the angle for the object
  122.                 object.rotation = oriObjRot + (currentPathAngle - oriPathAngle); 
  123.  
  124.             }
  125.         }
  126.     }
  127. }
  128.  
  129. function animateGroupToCurve(group, pathIndex, frames, track, stagger){
  130. //group: a group with both the object to animate and the path to animate it on
  131. //       the path used as the base for the animation will be the top-most path object in
  132. //       the group, the object will be the top-most object that is not the animation path
  133. //pathIndex: which path in the path object to use, this value is usually 0. 
  134. //frames: how many key frames of animation to create
  135. //track: should the rotation of the object track the curve. true = yes, false = no 
  136.  
  137.     //find the pathobject
  138.     var pathObjectIndex; 
  139.     var objectIndex; 
  140.     var i; 
  141.     var startFrame = group.currentFrame; 
  142.     var objectCount = 0; 
  143.     for (i = 0; i < group.objects.length; i++)
  144.     {
  145.         if (group.objects[i].paths) // only a path object has paths
  146.         {
  147.             pathObjectIndex = i;
  148.             break; 
  149.         }
  150.     }
  151.  
  152.     var beziers = CubicBezier.pathToBeziers(group.objects[pathObjectIndex], pathIndex); 
  153.     //animate everything put the first path object
  154.     for (i = 0; i < group.objects.length; i++)
  155.     {
  156.         if (i != pathObjectIndex)
  157.         {
  158.             group.objects[i].currentFrame = startFrame + objectCount*stagger;  
  159.             animateToCurve(group.objects[i], beziers, frames, track);
  160.             objectCount++; 
  161.         }
  162.     }
  163.  
  164. function curveTextEffect(group, path, pathframes, endframes, track, stagger, forward, startRot){
  165.  
  166.     //find the pathobject
  167.     var pathObjectIndex; 
  168.     var objectIndex; 
  169.     var i; 
  170.     var startFrame = group.currentFrame; 
  171.     var endFrame = (startFrame - 1) + pathframes + endframes;
  172.     var objectCount = 0; 
  173.     for (i = 0; i < group.objects.length; i++)
  174.     {
  175.         if (group.objects[i].paths) // only a path object has paths
  176.         {
  177.             pathObjectIndex = i;
  178.             break; 
  179.         }
  180.     }
  181.     //save all the object offsets from the first object that is not the animation path; 
  182.     var baseIndex; 
  183.     if(forward)
  184.     {
  185.         if (pathObjectIndex == (group.objects.length - 1))
  186.             baseIndex = group.objects.length-2;
  187.         else
  188.             baseIndex = group.objects.length-1; 
  189.     }
  190.     else
  191.     {
  192.         if (pathObjectIndex == 0)
  193.             baseIndex = 1; 
  194.         else
  195.             baseIndex = 0; 
  196.     }
  197.  
  198.     var xoffset = new Array; 
  199.     var yoffset = new Array; 
  200.     var oriRot  = new Array; 
  201.     for (i = 0; i < group.objects.length; i++)
  202.     {
  203.         xoffset[i] = group.objects[i].position.x - group.objects[baseIndex].position.x; 
  204.         yoffset[i] = group.objects[i].position.y - group.objects[baseIndex].position.y;
  205.         oriRot[i] = group.objects[i].rotation; 
  206.     }
  207.  
  208.     //find the object
  209.     for (i = 0; i < group.objects.length; i++)
  210.     {
  211.         var j; 
  212.         if(forward)
  213.             j = i;
  214.         else 
  215.             j = group.objects.length - 1 - i; 
  216.         if (j != pathObjectIndex)
  217.         {
  218.             group.objects[j].currentFrame = startFrame + objectCount*stagger; 
  219.             group.objects[j].startFrame = group.objects[j].currentFrame; 
  220.             group.objects[j].rotation = startRot; 
  221.             animateToCurve(group.objects[j], group.objects[pathObjectIndex], path, pathframes, track);
  222.  
  223.  
  224.             //put the object back in place
  225.             group.objects[j].currentFrame = endFrame + objectCount*stagger; 
  226.             group.objects[j].position.x = group.objects[j].position.x + xoffset[j]; 
  227.             group.objects[j].position.y = group.objects[j].position.y + yoffset[j];
  228.             group.objects[j].rotation = oriRot[j]; 
  229.             objectCount++; 
  230.         }
  231.     }
  232. }
  233.  
  234.