# HG changeset patch
# User William Stein <wstein@gmail.com>
# Date 1246372477 -7200
# Node ID 0fe38a2b742aca6d79ce9457da8536adc29a3048
# Parent  0ee125b9c62b838cf034e1de0c81313def06049f
trac #1483 -- javascript animation in the notebook

diff -r 0ee125b9c62b -r 0fe38a2b742a sage/plot/animate.py
--- a/sage/plot/animate.py	Mon Jun 29 16:42:16 2009 -0700
+++ b/sage/plot/animate.py	Tue Jun 30 16:34:37 2009 +0200
@@ -361,11 +361,16 @@
         
         -  ``iterations`` - integer (default: 0); number of
            iterations of animation. If 0, loop forever.
+
+        EXAMPLES::
+            sage: a = animate([sin(x + float(k)) for k in srange(0,pi,0.3)], xmin=0, xmax=2*pi, figsize=[2,1])
+            sage: a.js()
+            <html>...</html>
         """
         msec = delay*10  # delay in milliseconds
         d = self.png()
         from sage.misc.prandom import randint
-        r = randint(1,1000)
+        r = randint(1,1000000000)
         file_list = os.listdir(d)
         file_list.sort()
         cell_dir = os.path.join(os.path.dirname(os.path.abspath("")), "anim%s"%r)
@@ -376,26 +381,26 @@
             shutil.copy(os.path.join(d, f), os.path.join(cell_dir, f))
 
         v = [os.path.join(link_path, f) for f in file_list]
+        v.sort()
 
-        s = '<BODY onload="animate()">'
-        s += """
-        <SCRIPT language="javascript">
-        <!--
-       Animation = new Array(%s)\n"""%len(v)
+        s = """
+        <script>
+        Animation%s = new Array(%s)\n"""%(r,len(v))
         for i in range(len(v)):
-            s += '       Animation[%s] = new Image();\n'%(i+1)
-            s += '       Animation[%s].src="%s"\n'%(i+1, v[i])
+            s += '       Animation%s[%s] = "%s";\n'%(r, i+1, v[i])
+
         s +="""
         var ii=1;
         var jj=1;
         var timerID=null;
 
-        function animate()
+        function animate%s()
         {
-           if (ii<21)
+           if (ii<%s)
                 ii++;
            else
-             {"""
+             {"""%(r, len(file_list))
+        
         if iterations > 0:
             s += """
                if (jj<%s)"""%iterations
@@ -409,21 +414,18 @@
                     ii=1;"""
         s += """
              }
-             document.x%s.src=Animation[ii].src;
-             timerID=setTimeout("animate()", %s);
+             var e=get_element('animation%s');
+             if (e) e.src=Animation%s[ii];
+             timerID=setTimeout("animate%s()", %s);
         }
-        </SCRIPT>"""%(r, msec)
-        s += """        
-                <IMG NAME=x%s SRC="%s" BORDER="0">
-</BODY>"""%(r, v[0])
-        # ideally, delete the following three lines
-        f = open(os.path.join(os.path.abspath(""), "animate.html"), 'w')
-        f.write(s)
-        f.close()
-        # the following should be good enough...
-        print "<html>%s</html>"%s
-        return
-#         return s   # uncomment for debugging the javascript
+        animate%s();
+        </script>"""%(r, r, r, msec, r)
+        s = """
+        <img src="" id="animation%s">
+        """%r + s
+        s = '<html>%s</html>'%s
+        #print '<html>%s</html>'%s
+        return s
 
         
     def show(self, delay=20, iterations=0):
