









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










Lecture 13 Animations
- An animation is a “moving” graphic. - Sometimes we say a time dependent graphic, since where it “moves” to is dependent upon time. - To create the illusion of “movement” we need draw frames with a different picture each frame. - A frame rate of about 30 frames a second is optimal - less than 15 20 appears to flicker - greater than 30 gives no apparent improvement - To draw a frame we need to erase the old frame before drawing the new frame. - All our drawings have been accumulative (we never erase anything, just draw “over” what’s already there). - There exist several strategies for frame drawing.
- getWindowTick :: Window -> IO() - Every window has an internal timer. getWindowTick “waits” for the next “tick” (since the last call to getWindowTick) before it returns. If the next “tick” has already occurred it returns immediately. - getTime^ ::^ IO Integer - Returns the current time, measured in milliseconds, counting from some arbitrary point. By itself, means nothing, but the difference between successive calls accurately measures elapsed time. - setGraphic^ :: Window -> Graphic -> IO() - Writes the graphic into the “free” video graphic buffer. At the next frame “tick” what’s in the “free” video buffer will be drawn, and the current buffer will become the free buffer. Usual tick rate = 30 times per second
Old interface: openWindow :: String -> Point -> IO Window e.g. openWindow “title” (width,height) Richer interface: openWindowEx :: String -> Maybe Point -> Maybe Point -> (Graphic -> DrawFun) -> Maybe Word32 -> IO Window openWindowEx “title” (Just(x,y)) -- upper left corner (Just(width,height)) drawBufferedGraphic -- drawing mode (Just 30) -- refresh rate
Shape pulses from t h is : t o t h is : t o t h is :
animate :: String -> Animation Graphic -> IO () animate title anim = runGraphics ( do w <- openWindowEx title (Just (0,0)) (Just(xWin,yWin)) drawBufferedGraphic (Just 30) t0 <- getTime let loop = do t <- getTime let ft = fromInteger (t-t0) / 1000 setGraphic w (anim ft) getWindowTick w loop loop)
revolvingBall :: Animation Region revolvingBall t = let ball = Shape (Ellipse 0.2 0.2) in Translate (sin t, cos t) ball planets :: Animation Picture planets t = let p1 = Region Red (Shape (rubberBall t)) p2 = Region Yellow (revolvingBall t) in p1 Over p tellTime :: Animation String tellTime t = "The time is: " ++ show t
main2 = animate "Animated Text” (text (100,200). tellTime) The time changes as time advances
picToGraphic :: Picture -> Graphic picToGraphic (Region c r) = withColor c (regionToGraphic r) picToGraphic (p1 Over p2) = picToGraphic p1 overGraphic picToGraphic p picToGraphic EmptyPic = emptyGraphic main4 = animate "Animated Picture" (picToGraphic. planets) Case analysis over structure of picture. Use the primitives overGraphic & emptyGraphic
- It's useful to define “time varying” primitives, e.g. type Anim = Animation Picture
emptyA :: Anim emptyA t = EmptyPic
- Combining time varying pictures overA :: Anim -> Anim -> Anim overA a1 a2 t = a1 t Over a2 t overManyA :: [Anim] -> Anim overManyA = foldr overA emptyA Recall Anim = Animation Picture = Time > Picture hence the time parameter t
rBall :: Anim rBall t = let ball = Shape (Ellipse 0.2 0.2) in Region Red (Translate (sin t, cos t) ball) rBalls :: Anim rBalls = overManyA [ timeTransA ((t*pi/4)+) rBall | t <- [0..7]] main5 = animate "Lots of Balls" (picToGraphic. rBalls) Each ball rotates pi/4 seconds behind the one in front of it