Animation in mojo.Canvas

RoboFont Forums Help / General Animation in mojo.Canvas

This topic contains 2 replies, has 2 voices, and was last updated by  Roberto Arista 4 years, 7 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #6234

    Roberto Arista
    Participant

    Hi Frederik,
    I’d like to know if it is possible to generate an animation into a mojo.Canvas instance or into an NSView() (a kind of Processing void draw{}, which refresh itself to a given rate). I’d like to create something similar to the responsive lettering by Erik but keeping the animation into the application environment (no svg, no html).
    Any idea?

    All the best

    #6236

    frederik
    Keymaster

    Hi Roberto

    small example

    hope this helps :)

    from AppKit import NSTimer
    from random import random
    
    from vanilla import *
    
    from mojo.canvas import Canvas
    import mojo.drawingTools as dt
    
    from lib.baseObjects import CallbackWrapper
    
    class Test(object):
        
        def __init__(self):
            self.pos = 0, 0
            self.width = 400
            self.height = 400
            self.size = 100
            self.steps = 5
            self.addHorizontal = True
            self.directionX = 1
            self.directionY = 1
            
            framePerSecond = 30
            
            self.interval = framePerSecond / 1000.
            
            self.w = Window((400, 400))
            
            self.w.canvas = Canvas((0, 0, 400, 400), self, canvasSize=(self.width, self.height))
            self.w.open()
            
            self._callback = CallbackWrapper(self.redraw)
            self.sheduleTimer()
        
        def sheduleTimer(self):
            if self.w.getNSWindow() is not None:
                self.trigger = NSTimer.scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_(self.interval, self._callback, "action:", None, False)
        
        def redraw(self, timer):
            self.w.canvas.update()
            self.sheduleTimer()
        
        def draw(self):
            x, y = self.pos
            if self.addHorizontal:
                x += self.steps  * self.directionX
            else:
                y += self.steps  * self.directionY
            
            if x > (self.width - self.size):
                self.addHorizontal = False
                x = self.width - self.size
                self.directionX *= -1
            elif x < 0:
                self.addHorizontal = False
                x = 0     
                self.directionX *= -1       
            
            if y > (self.height - self.size):
                self.addHorizontal = True
                y = self.height - self.size
                self.directionY *= -1
            elif y < 0:
                self.addHorizontal = True
                y = 0
                self.directionY *= -1
            
            dt.fill(x/float(self.width), y/float(self.height), 1)
            dt.rect(x, y, self.size, self.size)
            
            self.pos = x, y
    
    Test()
        
    
    #6237

    Roberto Arista
    Participant

    OMG
    you made my day

    Thanks : )))))

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.