Ticket #1483: trac_1483_animate.patch

File trac_1483_animate.patch, 2.9 KB (added by jhpalmieri, 9 months ago)

(This is the same as "trac_1483_animate.py", but it has the right name :p)

  • sage/plot/animate.py

    # HG changeset patch
    # User J. H. Palmieri <palmieri@math.washington.edu>
    # Date 1246318936 25200
    # Node ID 644051ea215abd2c7f2a5ffcdbee83e3d18f0966
    # Parent  d35be627d29580f364da6bd2703b715f9d077d8a
    use javascript for animations
    
    diff -r d35be627d295 -r 644051ea215a sage/plot/animate.py
    a b  
    350350            print "" 
    351351            print "See www.imagemagick.org, for example." 
    352352 
     353    def js(self, delay=20, iterations=0): 
     354        """ 
     355        Produce javascript code for this animation. 
     356 
     357        INPUT: 
     358         
     359        -  ``delay`` - (default: 20) delay in hundredths of a 
     360           second between frames 
     361         
     362        -  ``iterations`` - integer (default: 0); number of 
     363           iterations of animation. If 0, loop forever. 
     364        """ 
     365        msec = delay*10  # delay in milliseconds 
     366        d = self.png() 
     367        from sage.misc.prandom import randint 
     368        r = randint(1,1000) 
     369        file_list = os.listdir(d) 
     370        file_list.sort() 
     371        cell_dir = os.path.join(os.path.dirname(os.path.abspath("")), "anim%s"%r) 
     372        os.makedirs(cell_dir) 
     373        link_path = os.path.join("/home", cell_dir[cell_dir.find("admin"):]) 
     374        v = [] 
     375        for f in file_list: 
     376            shutil.copy(os.path.join(d, f), os.path.join(cell_dir, f)) 
     377 
     378        v = [os.path.join(link_path, f) for f in file_list] 
     379 
     380        s = '<BODY onload="animate()">' 
     381        s += """ 
     382        <SCRIPT language="javascript"> 
     383        <!-- 
     384       Animation = new Array(%s)\n"""%len(v) 
     385        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]) 
     388        s +=""" 
     389        var ii=1; 
     390        var jj=1; 
     391        var timerID=null; 
     392 
     393        function animate() 
     394        { 
     395           if (ii<21) 
     396                ii++; 
     397           else 
     398             {""" 
     399        if iterations > 0: 
     400            s += """ 
     401               if (jj<%s)"""%iterations 
     402            s += """ 
     403                 { 
     404                    jj++; 
     405                    ii=1; 
     406                 }""" 
     407        else: 
     408            s += """ 
     409                    ii=1;""" 
     410        s += """ 
     411             } 
     412             document.x%s.src=Animation[ii].src; 
     413             timerID=setTimeout("animate()", %s); 
     414        } 
     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 
     427 
     428         
    353429    def show(self, delay=20, iterations=0): 
    354430        r""" 
    355431        Show this animation.