layout | permalink | title |
---|---|---|
main |
/index.html |
JS Light Meter |
A vertical light meter using JavaScript and HTML5 Canvas.
The library was originally created years ago for an online tool for TimelessTime, back in the day when I was new to the world of JavaScript.
- jCanvas
- jQuery
Try it out. Open up your console and use the 3 light meters that have been created. Try running some of the following.
lights1.flashUpTo(8)
lights1.flashDownTo(4)
lights1.flashTo(7)
lights1.changeValueBy(-4)
lights1.changeValueBy(3)
lights1.blinkLight(7)
Add a canvas tag to your HTML
<canvas id='myCanvas' width='20' height='365'></canvas>
Add jCanvas, jQuery and LightMeter to your page.
<script src="js/jQuery.js"></script>
<script src="js/jCanvas.js"></script>
<script src="js/LightMeter.js"></script>
Create a new LightMeter
instance, passing it in your canvas element & start using it.
var lights1 = new LightMeter( $("#myCanvas") );
lights1.flashUpTo();
lights1.flashDownTo();
Under the hood the the lights are controlled by pushing on/off functions to a queue which is processed every X
seconds using an interval.
A delay in the flashing is caused by pushing noop
functions on to the queue.
For example if the queue is processed every 200ms, the following queue would result in light 1 turning on, light 2 turning on, a pause and light 2 turning off with a 200ms delay between each event.
====
turn light 1 on
turn light 2 on
- noop -
turn light 2 off
===
Forces a redraw of the canvas.
Sets the interval time between processing events on the queue. A smaller interval results in a smaller delay between lights changing.
Returns the current interval time.
Adds the specified number of pauses to the queue. If the current interval is 200ms and the pauseFor(5) is called, a pause of 1s will be added to the queue.
Sets the lights to flash up to the specified value. If the value is less than the value on the end of the queue nothing will happen.
Sets the lights to flash down to the specified value. If the value is greater than the value on the end of the queue nothing will happen.
Flashes the light to a given value. Under the hood, uses flashUpTo & flashDownTo depending on the current value and the target value.
Flashes up/down to the new given value.
Sets the value of lights. Unlike changeValueBy
the lights will not change to this new value.
Returns the value of the lights at the end of the queue (i.e. the value after all current items on the queue have been processed.)
Blinks the light at the value one about the current value the specified number of times.
Stop processing items on the queue.
Start processing items on the queue.
The X position of the middle of each light in the canvas.
The number of lights to draw.
The radius of each light.
The vertical spacing between each light.
The update interval - how often to to process the queue.
The maximum value for colour1. If set to 3, lights 1-3 will be the colour of lightColour1
value.
The maximum value for colour2. See lightColour1Max
for details.
The maximum value for colour3. See lightColour1Max
for details.
If the light number if greater than this value the light colour will be white.
The light for the first set of lights.
The light for the second set of lights.
The light for the third set of lights.
The outline colour for each light.
The colour of each light when it's off.
NUMBER_OF_LIGHTS : 11,
LIGHT_RADIUS : 10,
LIGHT_SPACING : 12,
UPDATE_INTERVAL : 100,
LIGHT_X_POSITION : undefined, // null == use canvas width / 2
LIGHT_COLOUR_1_MAX : 3,
LIGHT_COLOUR_2_MAX : 7,
LIGHT_COLOUR_1 : "green",
LIGHT_COLOUR_2 : "orange",
LIGHT_COLOUR_3 : "red",
WHITE_LIGHT_COLOUR : "white",
LIGHT_OUTLINE_COLOUR : "black"
LIGHT_OFF_COLOUR : "white"