Animation: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added a task on animation)
 
(common lisp with ltk)
Line 3: Line 3:


Create a window containing the string “<code>Hello World! </code>” (the trailing space is significant). Make the text appear to be rotating right by periodically removing one letter from the end of the string and attaching it to the front. When the user clicks on the text, it should reverse its direction.
Create a window containing the string “<code>Hello World! </code>” (the trailing space is significant). Make the text appear to be rotating right by periodically removing one letter from the end of the string and attaching it to the front. When the user clicks on the text, it should reverse its direction.

=={{header|Common Lisp}}==
The ltk package provides a lisp interface to Tk for creating graphical interfaces. Assuming ''ltk'' has been installed somewhere the following will work as per the Tcl example.
{{libheader|Tk}}
<lang lisp>(use-package ':ltk)
(use-package :ext)

(defparameter *message* "Hello World! ")
(defparameter *direction* :left)
(defun animate (label)
(let* ((n (length *message*))
(i (if (eq *direction* :left) 0 (1- n)))
(c (string (char *message* i))))
(if (eq *direction* :left)
(setq *message* (string-concat (substring *message* 1 n) c))
(setq *message* (string-concat c (substring *message* 0 (1- n)))))
(setf (ltk:text label) *message*)
(ltk:after 125 (lambda () (animate label)))))
(defun basic-animation ()
(ltk:with-ltk ()
(let* ((label (make-instance 'label
:font "Courier 14")))
(setf (text label) *message*)
(ltk:bind label "<Button-1>"
(lambda (event)
(declare (ignore event))
(cond
((eq *direction* :left) (setq *direction* :right))
((eq *direction* :right) (setq *direction* :left)))))
(ltk:pack label)
(animate label)
(ltk:mainloop))))

(basic-animation)</lang>


=={{header|Tcl}}==
=={{header|Tcl}}==

Revision as of 23:01, 14 June 2009

Task
Animation
You are encouraged to solve this task according to the task description, using any language you may know.

Animation is the foundation of a great many parts of graphical user interfaces, including both the fancy effects when things change used in window managers, and of course games. The core of any animation system is a scheme for periodically changing the display while still remaining responsive to the user. This task demonstrates this.

Create a window containing the string “Hello World! ” (the trailing space is significant). Make the text appear to be rotating right by periodically removing one letter from the end of the string and attaching it to the front. When the user clicks on the text, it should reverse its direction.

Common Lisp

The ltk package provides a lisp interface to Tk for creating graphical interfaces. Assuming ltk has been installed somewhere the following will work as per the Tcl example.

Library: Tk

<lang lisp>(use-package ':ltk) (use-package :ext)

(defparameter *message* "Hello World! ") (defparameter *direction* :left) (defun animate (label)

 (let* ((n (length *message*))
        (i (if (eq *direction* :left) 0 (1- n)))
        (c (string (char *message* i))))
   (if (eq *direction* :left)
       (setq *message* (string-concat (substring *message* 1 n) c))
     (setq *message* (string-concat c (substring *message* 0 (1- n)))))
   (setf (ltk:text label) *message*)
   (ltk:after 125 (lambda () (animate label)))))
       

(defun basic-animation ()

 (ltk:with-ltk ()
     (let* ((label (make-instance 'label 
                                  :font "Courier 14")))
       (setf (text label) *message*)
       (ltk:bind label "<Button-1>"
                 (lambda (event)
                   (declare (ignore event))
                   (cond
                    ((eq *direction* :left) (setq *direction* :right))
                    ((eq *direction* :right) (setq *direction* :left)))))
       (ltk:pack label)
       (animate label)
       (ltk:mainloop))))

(basic-animation)</lang>

Tcl

Library: Tk

<lang tcl>package require Tk set s "Hello World! " set dir 0

  1. Periodic animation callback

proc animate {} {

   global dir s
   if {$dir} {
       set s [string range $s 1 end][string index $s 0]
   } else {
       set s [string index $s end][string range $s 0 end-1]
   }
   # We will run this code ~8 times a second (== 125ms delay)
   after 125 animate

}

  1. Make the label (constant width font looks better)

pack [label .l -textvariable s -font {Courier 14}]

  1. Make a mouse click reverse the direction

bind .l <Button-1> {set dir [expr {!$dir}]}

  1. Start the animation

animate</lang>