Entering Font Properties into Existing Python Code



  • Hello All,

    A quick question, if I want to change my existing star properties to font properties, what might be the approach?

    I have attached my code for comment.

    def star(x, y, n, r1, r2):
        pts = []
        for i in range(n * 2):
            a = i * pi / n
            r = r2 if i % 2 else r1
            pts.append((x + r * sin(a), y + r * cos(a)))
        polygon(*pts)
    
    CANVAS = 500
    SQUARESIZE = 158
    NSQUARES = 50
    SQUAREDIST = 6
    
    width = NSQUARES * SQUAREDIST
    
    NFRAMES = 50
    
    for frame in range(NFRAMES):
        newPage(CANVAS, CANVAS)
        frameDuration(1/20)
        
        fill(0)
        rect(0, 0, CANVAS, CANVAS)
    
        phase = 2 * pi * frame / NFRAMES  
        startAngle = 90 * sin(phase)
        endAngle = 90 * sin(phase + 0.5 * pi)
    
        translate(CANVAS/2 - width / 2, CANVAS/2)
    
        fill(1)
        stroke(0)
    
        for i in range(NSQUARES + 1):
            f = i / NSQUARES
            save()
            translate(i * SQUAREDIST, 0)
            scale(0.7, 1)
            rotate(startAngle + f * (endAngle - startAngle))
    
            star(0, 0, 5, SQUARESIZE, SQUARESIZE * 0.5)
    
            restore()
    
    saveImage("StackOfStars.gif")
    

    Thank you.

    Robert



  • hello Robert,

    1. you can set text using text() and fontSize()
    2. you’ll need to calculate the center of the text box using textSize()

    here’s a modified version of your script. very little was changed. you can use a diff tool like quick diff to compare both versions.

    CANVAS = 500
    SQUARESIZE = 158
    NSQUARES = 50
    SQUAREDIST = 6
    
    width = NSQUARES * SQUAREDIST
    
    NFRAMES = 50
    
    for frame in range(NFRAMES):
        newPage(CANVAS, CANVAS)
        frameDuration(1/20)
    
        fill(0)
        rect(0, 0, CANVAS, CANVAS)
    
        phase = 2 * pi * frame / NFRAMES  
        startAngle = 90 * sin(phase)
        endAngle = 90 * sin(phase + 0.5 * pi)
    
        translate(CANVAS/2 - width / 2, CANVAS/2)
    
        fill(1, 0.1)
        stroke(None)
        fontSize(480)
    
        for i in range(NSQUARES + 1):
            f = i / NSQUARES
            save()
            translate(i * SQUAREDIST, 0)
            scale(0.7, 1)
            rotate(startAngle + f * (endAngle - startAngle))
    
            w, h = textSize('a')
            text('a', (-w * 0.5, -h * 0.5))
            restore()
    
    saveImage("StackOfChars.gif")
    

    StackOfChars.gif

    hope this helps… have fun!



  • Hello,

    Once again, thank you for being so helpful.

    Enjoy the day.

    Best.

    Robert


Log in to reply