Back to Using Web Gauges

Making the Web Gauges

To make the previous gauge, we need an image. The image needs to be 320 x 320. Any image of any kind will do, as long as it can be used in the background of a div. I used Gimp (similar to Photoshop, but free), and made huge 3200 x 3200 dials, then reduced them. A dial image does not need to be a dial. It could be your cat. Whatever you want. This is where you get artsy and I leave you to it.

The needle, in my case, is 20 pixels wide, by 102 pixels high. In the JavaScript code, you define the point on the needle around which it will rotate. For my needle that was 10x, 102y. That is the bottom center point in the needle. The needle background must be transparent, so I used a png file. If you make a needle that is 100 pixels long and 50 pixels wide, you would choose 25x, 100y for your needle center.

A Second Gauge

This gauge is smaller, but uses the same code. The gauge is 250 pixels square, and the needle is 115 pixels long by 16 pixels wide. The code for it is seen below. Notice we set the "needleCenter" property to the x,y coordinates of the rotational center of the needle.

<script>
var windGauge = new gauge(
	"wind-dir",
	'http://33a7546c658c6cca1d4c-839a3c61562bcdd35ab5709b8af9ebd1.r41.cf1.rackcdn.com/a/dca/images/projects/needle-black.png',
	0,0,1);

windGauge.needleCenter = [8,105];

var position = 0;
var dir = 1;
setInterval(function(){moveNeedle()},50);

function moveNeedle() {
	if (dir == 1) {
		if (++position > 359) dir = 0;
	} else {
		if (--position < 1) dir = 1;
	}
	windGauge.setPosition(position);
}
</script>

And Finally

This gauge is silly, and is only to show that this has nothing to do with gauges - it is just one image spinning over another. The gauge JavaScript takes an input of your units and converts it to degrees, then positions the pointer there. It doesn't care what the units are, and it doesn't care what number you pass. There is no range checking, so you can show 1000°F on a 100°F thermometer - it will just have wrapped 10 times before ending somewhere.