News:

Thanks to everyone for the feedback on what errors you encountered from the forum database changes made in Fall 2023. Let us know if you discover anymore.

Main Menu

Javascript help wanted: drawing map tiles on HTML5 canvas

Started by yakra, July 10, 2018, 09:50:02 PM

Previous topic - Next topic

yakra

I've been experimenting with drawing OSM map tiles on the HTML5 canvas, the idea being to do so in some TravelMapping-related utils once I get things working.

Here's where progress is at so far:
http://yakra.teresco.org/tmtools_demos/tile/NS-tile.htm (118.8 k)
http://yakra.teresco.org/tmtools_demos/tile/RI-tile.htm (13.1 k)
The tiles do not display when viewing an area/zoom level that hasn't been viewed before. This includes when the map is first loaded.
To get the tiles to display, you need to redraw the map at an already-viewed area/zoom level. There are several ways to do this:
- Pan the map
- Zoom out & back in, or vice versa
- Click Render or NullXform, or type 'n' or 'N'
- Reset the map to its initial state, via the button or typing 'r' or 'R'

No big surprises here; I'm drawing the images before they're fully loaded.
What I want to do is use image.onload. It's there in the code; just uncomment lines 126-128 & comment out line 131, and away we go.
But it doesn't work. Nothing loads. Ever. Same results if I try different, non-OSM, tilesets.
And I've gotten image.onload to work just fine on several other occasions when using code from various references/tutorials/blogs/etc. around the web.

What am I doing wrong?
"Officer, I'm always careful to drive the speed limit no matter where I am and that's what I was doin'." Said "No, you weren't," she said, "Yes, I was." He said, "Madam, I just clocked you at 22 MPH," and she said "That's the speed limit," he said "No ma'am, that's the route numbah!"  - Gary Crocker


hotdogPi

Side note: It should not be possible to zoom infinitely inward or infinitely outward.
Clinched

Traveled, plus
US 13, 44, 50
MA 22, 40, 107, 109, 117, 119, 126, 141, 159
NH 27, 111A(E); CA 133; NY 366; GA 42, 140; FL A1A, 7; CT 32; VT 2A, 5A; PA 3, 51, 60, QC 162, 165, 263; 🇬🇧A100, A3211, A3213, A3215, A4222; 🇫🇷95 D316

yakra

Quote from: 1 on July 10, 2018, 09:52:14 PM
Side note: It should not be possible to zoom infinitely inward or infinitely outward.
True. The tiles displayed are scaled to match the scale of the roads being drawn on the canvas atop them.
Nonetheless, there's still a bug in my code where it will attempt to load tiles from negative or too-large zoom levels. Gotta limit the min & max zoom-level tiles to load. I wrote a note in my phone a week ago to remember to fix this, but never moved it to a GitHub issue where I'd be more likely to keep track of it. So thanks for the reminder. :)
Edit: Fixed. Only zoom levels from 0 to 19 are used.
"Officer, I'm always careful to drive the speed limit no matter where I am and that's what I was doin'." Said "No, you weren't," she said, "Yes, I was." He said, "Madam, I just clocked you at 22 MPH," and she said "That's the speed limit," he said "No ma'am, that's the route numbah!"  - Gary Crocker



Opinions expressed here on belong solely to the poster and do not represent or reflect the opinions or beliefs of AARoads, its creators and/or associates.