Rotating Objects

Teodor Filimon, Gadget Developer
August 2007
"Give me a place to stand on, and i will move the Earth!"  -  Archimedes


Introduction

In the physical world, we interact with all kinds of objects every day, basing this interaction mostly on our eyesight. We think we see 3D stuff, but actually a 2D image is projected on our retina, only to be processed later into a spatial perspective. That's why when we see a drawing of a cube on paper, although the paper is 2D, we immediately think of the 3D object.

This article is about rotating elements in a gadget. I hope you find these tips helpful; links to source code of all the examples discussed here can be found at the end of this article.

How To Understand Rotation

A gadget can be very similar to a sheet of paper on which the developer is drawing his UI (User Interface) elements. Mathematically, there are many ways of locating a point in a plane, but one of them which is relevant to this article is the polar system: an axis, an angle and a length. When you place an <img> object in your gadget the position, height and width of the image are automatically determined (you can change them explicitly as well).

What we're interested in is the angle, which has a default value of 0 - no rotation. A positive value will turn the image clockwise around its axis (the upper-left corner), and a negative value will turn it counter-clockwise. An example is shown in Figure 1.

Rotation angles
Figure 1: Examples of rotation values

When we add an <img> object, we usually specify at least two things: the position and the image source. So, it's only natural that the specified coordinates will become the axis. As i've said in the previous paragraphs, this means that each rotation will take place around the upper-left corner of the image, like in Figure 2.

Rotation axis
Figure 2: The default rotation axis

It's important to know that we can do this not only for images, but other types of elements as well, the rotation property being inherited as part of the basicElement.

I said that the upper-right corner is the default axis when the only things we have are the x and y coordinates. But we can change the rotation axis by giving a certain value to the pinX and pinY properties, which are also inherited by every element. That way we can achieve certain effects like clock hand movement or rotating status indicators very easily (Figure 5 and Figure 7). When pinX and pinY are not set, their value is considered zero, so the axis will naturally become the point located at the (0,0) position.

But how do we actually rotate objects? The answer comes in the next section of the article.

Rotating UI Elements

Basically, the rotation property of an element can be changed both in the XML and the Javascript code. An initial angle is usually set in the XML (if it's different from zero), and then, if the gadget purpose requires it, the rotation can be handled dynamically in the Javascript code, like below. This example simulates in a very simple way the behavior of a tachometer - you can see it in cars, measuring engine RPM :)

//in main.xml

<img name="needle" src="needle.png"
	x="67" y="67" rotation="135" />

//since the needle is horizontal at first,
//and positioned in the center of the gauge,
//we need to put it into the right position
//in main.js

function accelerate(){
	needle.rotation+=1;
	//we can also set a limit
	if (needle.rotation==315){
		needle.rotation=135;
		alert("Engine overheated!");
	}
}

function decelerate(){
	if (needle.rotation>135)
		needle.rotation-=1;
}

Notice how in this example we're taking advantage of the fact that rotation is a read/write property. This allows us to relatively increase or decrease it, without necessarily having to know its value at a certain moment.

Possible Uses

Some possible uses are the following 3, although many more can be found since they are very flexible and easy to implement:

We can use rotation not only to improve functionality, but looks as well:

Conclusion

In this article i tried to show you how rotating objects can improve not only the interface look, but the functionality behind it as well, also bringing an opportunity to optimize your code in certain situations. As described, to emphasize change in a value, an analog indicator which rotates to point to it can sometimes be a lot better than a digital display, even in the 3rd millennium :)

Many gadgets, everyone!

Resources


Author Bio

Teodor Filimon

I'm a natural born programmer. My first contact with a techno-gadget was Star Trek (remember those cool sensors?). I used to fill up a whole room with drawings of them when I was only 3 years old. Generally, I find a lot of inspiration for intuitive interfaces in things with "star" in their names (like Star Wars, Stargate,... :-) . I like the border between interface and function—it's the essence of a program, I think. Anyway, I'm a software engineering student now, and you can learn more about me and what I'm thinking and doing at my website or blog.