I'd like to render a time x-axis on a HTML canvas
, that adapts when I pan (with click+drag) or when I zoom.
Charting JS libraries might exist to do this for curves/charts, but in my case, the data over the x-axis will not be a curve, so I need to do it from scratch.
Here when I click+drag on the background, the origin x
adapts, that is good (see snippet). But how to render a text x-axis like this on the bottom of the canvas
?
--------------------------------------------------
| | | | |
jan 02 jan 03 jan 04 jan 05 jan 06
var drag = false;
var x = 0;
var last_position = {};
document.getElementById('canvas').onmousedown = function() { drag = true; }
document.getElementById('canvas').onmouseup = function() { drag = false; }
document.getElementById('canvas').onmousemove = function(e) {
var deltaX = last_position.x - e.clientX,
deltaY = last_position.y - e.clientY;
if (drag && typeof(last_position.x) != 'undefined') {
x += deltaX;
document.getElementById('pos').innerHTML = x;
}
last_position = { x : e.clientX, y : e.clientY };
}
#canvas { width: 400px; height: 150px; background-color: #ccc; }
<canvas id="canvas"></canvas>
<div id="pos"></div>