Author Topic: Wind direction arrow using canvas  (Read 4314 times)

0 Members and 1 Guest are viewing this topic.

Offline FourOhFour

  • Member
  • *
  • Posts: 15
    • skigod.us
Wind direction arrow using canvas
« on: November 24, 2007, 06:57:00 PM »
OK, don't I feel dumb. I posted this in weather website help, but it really belongs here. Deleted that one (no replies), reposting here.

--

Well, I was looking at my site doing a redesign... and thought putting the wind direction as an arrow in a circle would look neat. Now, I could have done this with a bunch of images (one for each direction)... but that limits you to as many directions as images you make.

Instead, I decided to use a little javascript and a little (non-standard) HTML: the <canvas> tag. If you are using Opera, Safari, or Firefox, you'll see this if you visit skigod.us. If you're using IE, you likely won't see it (but it'll degrade nicely and show the direction as an abbreviation).

Now, this code isn't very good as a drop-in thing... you'll have to fiddle with it to fit your site design. But if you want it... cool. Have fun. At a minimum, you'll have to go in and set your foreground color.

The code for the canvas is the functions setup_wind_canvas, draw_arrow, clear_arrow, update_arrow, and unglow_arrow in this file.

Maybe some day I'll make the arrow move smoothly instead of pop to the new position...

Offline WeatherBeacon

  • Chief
  • Forecaster
  • *****
  • Posts: 1369
    • http://www.wxbeacon.com
Re: Wind direction arrow using canvas
« Reply #1 on: November 24, 2007, 07:20:48 PM »

FourOhFour:

I downloaded your script (thanks for graciously making it available!) and skimmed it. What data file(s) does it read? It looks like it need CSV files?

Thanks!

Regards,

Kevin...
Mae govannen!
Kevin  (Member AMS) http://www.wxbeacon.com               Genesee County, Michigan
Hardware:  Davis Vantage Pro Wireless, Midland WR-300
Software: VWS 14.01p43, WeatherFlash, & GRLevel3

Offline FourOhFour

  • Member
  • *
  • Posts: 15
    • skigod.us
Re: Wind direction arrow using canvas
« Reply #2 on: November 24, 2007, 07:55:04 PM »

FourOhFour:

I downloaded your script (thanks for graciously making it available!) and skimmed it. What data file(s) does it read? It looks like it need CSV files?

The script reads the data.csv generated by VWS, but the functions for the wind direction arrow should be easy to add to any ajax-related weather script.

setup_wind_canvas() - no arguments. Sets up the canvas for our use. If successful, adds a 'with_canvas' class to the body tag, so your stylesheet can make any changes necessary for the canvas.

draw_arrow(dir, glow) - dir should be the wind direction in degrees, if glow is 1, the arrow is drawn in a highlight color.

clear_arrow() - no arguments. Clears the arrow from the display.

update_arrow(dir) - dir should be the wind direction in degrees. This is the function that needs to be plugged into an existing update script. If the new direction is different from the old one, clears the old arrow and draws a new one with the highlight. Safely does nothing if the canvas couldn't be set up (so there is no need to test for that)

unglow_arrow() - no arguments. Redraws the arrow in the normal color.