Adventures in Rotation
Bill Marriott
wjm at wjm.org
Sun Dec 4 19:16:29 EST 2005
Ok, as our regular viewers know, I'm trying to mimic with Rev a pretty
little clock distributed with Konfabulator. The way K. does this clock is to
create little PNGs for each of the elements (second hand, minute hand, rim,
background), package them up in XML, and animate (rotate) them in
JavaScript.
I decided to start by writing a stack to rotate a clock hand 360 degrees
around.
Our story thus far:
Attempt 1: "rotate" a PNG
Code used: rotate image "hourHand" by -30
Result: Obliterates the image. Gets progressively slower. Eats up more than
512MB RAM.
Attempt 2: set the "angle" of the PNG
Code used: set the angle of image "hourHand" to the angle of image
"hourHand" - 30
Result: ugly jaggies at non-orthogonal angles. Slow. Can't set origin of
rotation.
The only way I'll be able to employ a bitmap graphic for the hands is to
flip through sequential frames in which the hand is prepositioned at the
correct angle.
Now I thought ... hey what about using a "graphic?" That is what Revolution
calls objects created with its built-in tools for simple vector objects.
I'll use one of those.
Attempt 3: rotate a rectangle
The "seconds" hand in the K. clock is a simple, red line. So I made a new
stack and drew a 200 wide x 4 high rectangle, then set the fill to red. Then
I used the message box.
Code used: set the angle of graphic 1 to 30
Result: Nothing happened. I researched this in the help file and discovered:
"A regular polygon can be rotated between zero and 360°. Increasing the
angleInDegrees rotates the polygon clockwise. You can specify the angle of a
graphic that is not a regular polygon, but it has no effect on the graphic's
appearance."
A rectangle is not a regular polygon. In fact, not even a perfect square
drawn with the rectangle tool is considered a "regular polygon" (even though
it is). It has to be an object *drawn* with the regular polygon tool, or
more precisely, a graphic with the style/type set to "Regular Polygon."
Attempt 4: revRotatePoly a rectangle
I noticed under the "see also" section an interesting command,
"revRotatePoly" and thought perhaps this is what I am looking for. Browsing
to this page in help I read I can,
"Use the revRotatePoly command to rotate a line, curve, or irregular polygon
graphic."
But not a rectangle. But, no problem. I'll just make my minute hand with the
freehand polygon tool. I wanted to make a fancy-looking minute hand anyway.
Attempt 4 (revised): revRotatePoly an irregular polygon
So, I drew myself a pretty minute hand and then wrote a script.
Code used:
on mouseUp
repeat with i = 1 to 360
revRotatePoly the long ID of graphic "minuteHand",1
wait for 50 milliseconds
end repeat
end mouseUp
(I know the "wait for" method was not what I was going to use in my eventual
stack, for performance reasons, but I just wanted to test out spinning it.)
Well, running this script was a shocker. It had the effect of crumpling my
pretty minute hand into a little ball. If you'd like to see what happens, go
to revOnline under my space, "MerryOtter" -- the stack is, "Fun with
revRotatePoly"
My clock hand *is* pretty fancy, it's got 56 points. But the same thing
happens with graphics of only 3 points. Open the stack and try the simplest
kind of clock hand you could imagine with
100,10
300,20
100,30
in the coordinate list, then click "Reset."
--------------------
So, this simple experiment touches on a few issues from my original post:
#4) 3D support. I guess we shouldn't expect 3D support when 2D is still not
really there!
#10) Graphics support. I honestly haven't tried to do much with graphics in
Rev in a long time. I see that things really are still quite limited, at
least with respect to rotation. :) Both bitmaps and "objects" are not
handled well.
#13) General flakiness. I would definitely say revCrumplePoly is a better
name for the revRotatePoly command. Is there any situation where
revRotatePoly could be useful? Not if it does this. Totally bugged. (I
suspect the reason why is that Rev doesn't have fractional coordinates?
Could such a limitation really result in such dramatic distortion? If this
is the case, then Rev should store fractionals, but render to integers.)
#14) Advancing HyperTalk. Why do we need three mutually exclusive methods of
manipulating rotation (rotate command, angle property, reRotatePoly) each
with different limitations, none able to turn a simple rectangle reliably,
and none working particularly well at what they do? We need a rotate command
(to turn something) and an angle properly (to inspect or directly set a
particular angle). They should work on almost all objects (certainly images
and graphics, ideally fields [at least at 90 degrees], and potentially other
elements).
Bill
More information about the use-livecode
mailing list