Ticket #1483: trac_1483-part2.patch

File trac_1483-part2.patch, 3.2 KB (added by was, 9 months ago)

modified the previous posted code so it actually works in the notebook and fixed some bugs

  • sage/plot/animate.py

    # 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 b  
    361361         
    362362        -  ``iterations`` - integer (default: 0); number of 
    363363           iterations of animation. If 0, loop forever. 
     364 
     365        EXAMPLES:: 
     366            sage: a = animate([sin(x + float(k)) for k in srange(0,pi,0.3)], xmin=0, xmax=2*pi, figsize=[2,1]) 
     367            sage: a.js() 
     368            <html>...</html> 
    364369        """ 
    365370        msec = delay*10  # delay in milliseconds 
    366371        d = self.png() 
    367372        from sage.misc.prandom import randint 
    368         r = randint(1,1000) 
     373        r = randint(1,1000000000) 
    369374        file_list = os.listdir(d) 
    370375        file_list.sort() 
    371376        cell_dir = os.path.join(os.path.dirname(os.path.abspath("")), "anim%s"%r) 
     
    376381            shutil.copy(os.path.join(d, f), os.path.join(cell_dir, f)) 
    377382 
    378383        v = [os.path.join(link_path, f) for f in file_list] 
     384        v.sort() 
    379385 
    380         s = '<BODY onload="animate()">' 
    381         s += """ 
    382         <SCRIPT language="javascript"> 
    383         <!-- 
    384        Animation = new Array(%s)\n"""%len(v) 
     386        s = """ 
     387        <script> 
     388        Animation%s = new Array(%s)\n"""%(r,len(v)) 
    385389        for i in range(len(v)): 
    386             s += '       Animation[%s] = new Image();\n'%(i+1) 
    387             s += '       Animation[%s].src="%s"\n'%(i+1, v[i]) 
     390            s += '       Animation%s[%s] = "%s";\n'%(r, i+1, v[i]) 
     391 
    388392        s +=""" 
    389393        var ii=1; 
    390394        var jj=1; 
    391395        var timerID=null; 
    392396 
    393         function animate() 
     397        function animate%s() 
    394398        { 
    395            if (ii<21) 
     399           if (ii<%s) 
    396400                ii++; 
    397401           else 
    398              {""" 
     402             {"""%(r, len(file_list)) 
     403         
    399404        if iterations > 0: 
    400405            s += """ 
    401406               if (jj<%s)"""%iterations 
     
    409414                    ii=1;""" 
    410415        s += """ 
    411416             } 
    412              document.x%s.src=Animation[ii].src; 
    413              timerID=setTimeout("animate()", %s); 
     417             var e=get_element('animation%s'); 
     418             if (e) e.src=Animation%s[ii]; 
     419             timerID=setTimeout("animate%s()", %s); 
    414420        } 
    415         </SCRIPT>"""%(r, msec) 
    416         s += """         
    417                 <IMG NAME=x%s SRC="%s" BORDER="0"> 
    418 </BODY>"""%(r, v[0]) 
    419         # ideally, delete the following three lines 
    420         f = open(os.path.join(os.path.abspath(""), "animate.html"), 'w') 
    421         f.write(s) 
    422         f.close() 
    423         # the following should be good enough... 
    424         print "<html>%s</html>"%s 
    425         return 
    426 #         return s   # uncomment for debugging the javascript 
     421        animate%s(); 
     422        </script>"""%(r, r, r, msec, r) 
     423        s = """ 
     424        <img src="" id="animation%s"> 
     425        """%r + s 
     426        s = '<html>%s</html>'%s 
     427        #print '<html>%s</html>'%s 
     428        return s 
    427429 
    428430         
    429431    def show(self, delay=20, iterations=0):