1 / 6

Timer-Based Animation

Timer-Based Animation. Jerry Cain CS 106AX October 2, 2019 slides leveraged from those written by Eric Roberts. Timer Events. The programs we discussed last time respond to mouse events by installing event listeners into the GWindow object.

garroyo
Download Presentation

Timer-Based Animation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Timer-Based Animation Jerry Cain CS 106AX October 2, 2019 slides leveraged from those written by Eric Roberts

  2. Timer Events • The programs we discussed last time respond to mouse events by installing event listeners into the GWindow object. • JavaScript also allows you to listen for timer events, which occur after a specified time interval. • As with mouse events, you specify the listener for a timer event in the form of a callback function that is automatically invoked at the end of the time interval. • You can add animation to a JavaScript program by setting a timer for a short interval and having the callback function make small updates to the graphical objects in the window. • If the time interval is short enough (typically between 20 and 30 milliseconds), the animation will appear smooth to the human eye.

  3. Timeouts • An interval timer invokes its callback function repeatedly at regular intervals. You create an interval timer by calling • JavaScript supports two kinds of timers. A one-shot timer invokes its callback function once after a specified delay. You create a one-shot timer by calling setInterval(function, delay); The setInterval function returns a numeric value that you can later use to stop the timer by calling clearInterval with that numeric value as an argument. setTimeout(function, delay); where function is the callback function and delay is the time interval in milliseconds.

  4. A Simple Example of Animation gw dx dy square stepCount step timer 1 4 2 0 ... 1729 AnimatedSquare

  5. Exercise: Exploding Circles • Write a program that draws ten filled, randomly shaded circles at various locations within a graphics window, as described below: • The center coordinate and full radius of the first circle are randomly generated so the entire circle fits within the canvas. • The circle is initially drawn with radius zero, but the radius is slowly increased over several time steps until the radius matches that generated at the outset. • Each circle is introduced in the same manner, but the second circle is only placed after the first circle has fully expanded, the third circle is only placed after the second circle has fully expanded, and so forth. • The program ends after the tenth circle has been placed and fully expanded.

  6. The End

More Related