Turtle and draw a tree with Transcrypt
<p>today I want draw a tree in browser with Transcrypt.
I have code in Python which is work in Skulpt:</p>

<pre><code>import random
import turtle

def tree(size, myTurtle):
    myTurtle.pensize(size / 20)

    if size &lt; random.randint(1,2) * 20:
        myTurtle.color("green")
    else:
        myTurtle.color("brown")

    if size &gt; 5:
        myTurtle.forward(size)
        myTurtle.left(25)
        tree(size - random.randint(10, 20), myTurtle)
        myTurtle.right(50)
        tree(size - random.randint(10, 20), myTurtle)
        myTurtle.left(25)
        myTurtle.penup()
        myTurtle.backward(size)
        myTurtle.pendown()

window = turtle.Screen()
window.setup(800,600)
window.bgcolor("white")

myTurtle = turtle.Turtle()
myTurtle.color("brown", "blue")
myTurtle.left(90)
myTurtle.speed(0)
myTurtle.penup()
myTurtle.setpos(0, -250)
myTurtle.pendown()
</code></pre>

<p>And I want to run it in browser to get this effect:</p>

<p><a href="http://i.stack.imgur.com/NpcHB.png" rel="nofollow"><img src="http://i.stack.imgur.com/NpcHB.png" alt="image"></a>
âDon't worry about text over the tree, is in polish ;) 
I run this in Skulpt, maybe you hear about it, effect you have above.
Now I want to run this in Transcrypt and compare it to Skulpt and Brython.</p>

<p>As you can see here:
<a href="http://www.transcrypt.org/live/turtle_site/turtle_site.html" rel="nofollow">http://www.transcrypt.org/live/turtle_site/turtle_site.html</a></p>

<p>Transcrypt somehow can draw with turtle.</p>

<p>What change in this code, to work with Transcrypt?</p>

<p>Can you help me with this?</p>

<p>First: you need some modification in code because some functions in <code>Transcrypt</code> have different names or don't exist. You have to add <code>turtle.done()</code> to dislay result.</p>

<p><strong>turtle_tree.py</strong></p>

<pre><code>import random
import turtle

def tree(size, myTurtle):
    myTurtle.pensize(size / 20)

    if size &lt; random.randint(1,2) * 20:
        myTurtle.color("green")
    else:
        myTurtle.color("brown")

    if size &gt; 5:
        myTurtle.forward(size)
        myTurtle.left(25)
        tree(size - random.randint(10, 20), myTurtle)
        myTurtle.right(50)
        tree(size - random.randint(10, 20), myTurtle)
        myTurtle.left(25)
        myTurtle.up()       # penup()
        myTurtle.back(size) # backward(size)
        myTurtle.down()     # pendown()

#window = turtle.Screen()   # doesn't exists
#window.setup(800,600)      # doesn't exists
#window.bgcolor("white")    # doesn't exists

myTurtle = turtle.Turtle()
myTurtle.color("brown", "blue")
myTurtle.left(90)
myTurtle.speed(0)
myTurtle.up()         # penup()
myTurtle.goto(0, 250) # setpos(0, -250)     
myTurtle.down()       # pendown()

tree(135, myTurtle)

myTurtle.done() # display 
</code></pre>

<p>Install <code>Transcrypt</code> using <code>pip</code></p>

<pre><code>pip install transcrypt
</code></pre>

<p>Compile Python into JavaScript</p>

<pre><code>transcrypt turtle_tree.py
</code></pre>

<p>You get folder <code>__javascript__</code> with file <code>turtle_tree.js</code> (and <code>turtle_tree.min.js</code>, <code>turtle_tree.mod.js</code> but you don't need it now) </p>

<p>You need HTML file which loads <code>turtle_tree.js</code> and has <code>&lt;div id="__turtlegraph__"&gt;</code> to display result.</p>

<p><strong>turtle_tree.html</strong></p>

<pre><code>&lt;!DOCTYPE html&gt;

&lt;html&gt;

&lt;head&gt;
    &lt;meta charset="utf-8"/&gt;
    &lt;title&gt;Turtle Tree&lt;/title&gt;

    &lt;style&gt;
        #__turtlegraph__ {
            height: 600px;
            width: 800px;
        }
    &lt;/style&gt;
&lt;/head&gt;

&lt;body&gt;

    &lt;div id="__turtlegraph__"&gt;&lt;/div&gt;
    &lt;script src='turtle_tree.js'&gt;&lt;/script&gt;

&lt;/body&gt;

&lt;/html&gt;
</code></pre>

<p>Put HTML file in <code>__javascript__</code> folder and open it in browser.</p>

<hr>

<p>Tested on Python 3.5.2 / Linux Mint 17.3 / Firefox 48.0 - it draws in 1 second (or less).</p>

<p><a href="http://i.stack.imgur.com/sh3Fe.png" rel="nofollow"><img src="http://i.stack.imgur.com/sh3Fe.png" alt="enter image description here"></a></p>

python transcrypt