I'm creating a canvas via javascript on a webproject.
The canvas has graphical representations on the x-y plane.
I am trying to add the horizontal-scrolling feature to the canvas.
I have investigated a few methodologies:-
1) draw 12 months worth of data on the canvas, when the mouse scrolls forward, 1st month's data disappears, and at the end a new months data is added, new canvas is drawn.
Con:- Everytime the mouse scrolls to pan thru the timeline - a new SQL query has to be made, making my web application very slow.
2) maybe I can draw say 10 years worth of data on the canvas via 1 SQL query, but only show 12 months worth of the data. masking the rest of the 9 years. Now when the client scrolls, I capture the scroll event and move to the appropriate part of the canvas. Is this possible? If so then how?
Can anyone advise?
My current representation of the canvas = with only 12 months worth of data
To be more specific on the scrolling, I would like to have a feeling such as the following widget for my client-side scrolling action:-
/
I'm creating a canvas via javascript on a webproject.
The canvas has graphical representations on the x-y plane.
I am trying to add the horizontal-scrolling feature to the canvas.
I have investigated a few methodologies:-
1) draw 12 months worth of data on the canvas, when the mouse scrolls forward, 1st month's data disappears, and at the end a new months data is added, new canvas is drawn.
Con:- Everytime the mouse scrolls to pan thru the timeline - a new SQL query has to be made, making my web application very slow.
2) maybe I can draw say 10 years worth of data on the canvas via 1 SQL query, but only show 12 months worth of the data. masking the rest of the 9 years. Now when the client scrolls, I capture the scroll event and move to the appropriate part of the canvas. Is this possible? If so then how?
Can anyone advise?
My current representation of the canvas = with only 12 months worth of data
To be more specific on the scrolling, I would like to have a feeling such as the following widget for my client-side scrolling action:-
http://www.simile-widgets.org/timeline/
Share Improve this question edited Apr 9, 2016 at 1:36 pancake 3,4503 gold badges23 silver badges35 bronze badges asked Jan 16, 2013 at 22:04 PhiloPhilo 1,98912 gold badges40 silver badges80 bronze badges 2 |2 Answers
Reset to default 14Here's a pretty basic implementation: http://jsfiddle.net/CQPeU/
var can = document.getElementById("can"),
ctx = can.getContext('2d'),
dragging = false,
lastX = 0,
translated = 0;
// these two lines will make the y-axis grow upwards.
ctx.scale(1,-1);
ctx.translate(0, -400);
can.onmousedown = function(e){
var evt = e || event;
dragging = true;
lastX = evt.offsetX;
}
window.onmousemove = function(e){
var evt = e || event;
if (dragging){
var delta = evt.offsetX - lastX;
translated += delta;
ctx.translate(delta, 0); // translate the context.
lastX = evt.offsetX;
draw(); // redraw
}
}
window.onmouseup = function(){
dragging = false;
}
function draw() {
ctx.clearRect(-translated, 0, 600, 400); // this is why we need to keep track of how much we've translated
for (var i = 0; i < plot.length; i++) {
ctx.beginPath();
ctx.arc(plot[i].x, plot[i].y, 5, 0, 2 * Math.PI); // note we don't have to futz with the x/y values, and can use them directly.
ctx.fill();
}
}
To create a grid, you could do something like this:
var grid = (function(dX, dY){
var can = document.createElement("canvas"),
ctx = can.getContext('2d');
can.width = dX;
can.height = dY;
// fill canvas color
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, dX, dY);
// x axis
ctx.strokeStyle = 'orange';
ctx.moveTo(.5, 0.5);
ctx.lineTo(dX + .5, 0.5);
ctx.stroke();
// y axis
ctx.moveTo(.5, .5);
ctx.lineTo(.5, dY + .5);
ctx.stroke();
return ctx.createPattern(can, 'repeat');
})(100, 50);
Which would be used like this:
function draw() {
ctx.clearRect(-translated, 0, 600, 400);
ctx.rect(-translated, 0, 600, 400);
ctx.fillStyle = grid;
ctx.fill();
ctx.fillStyle = "#fff";
for (var i = 0; i < plot.length; i++) {
ctx.beginPath();
ctx.arc(plot[i].x, plot[i].y, 5, 0, 2 * Math.PI);
ctx.fill();
}
}
Updated demo: http://jsfiddle.net/CQPeU/2/
To avoid the redraw on every Mouse Move event in the answer from @Shmiddty, you can draw the entire canvas oversize, then change the CSS margin property. This is a significant performance gain when the content of the canvas gets more complex.
Here is a demo: https://jsfiddle.net/ax7n8944/
HTML:
<div id="canvasdiv" style="width: 500px; height: 250px; overflow: hidden">
<canvas id="canvas" width="10000px" height="250px"></canvas>
</div>
JS:
var canvas = document.getElementById("canvas");
var context = canvas.getContext('2d');
var dragging = false;
var lastX;
var marginLeft = 0;
for (var i = 0; i < 1000; i++) {
context.beginPath();
context.arc(Math.random() * 10000, Math.random() * 250, 20.0, 0, 2 * Math.PI, false);
context.stroke();
}
canvas.addEventListener('mousedown', function(e) {
var evt = e || event;
dragging = true;
lastX = evt.clientX;
e.preventDefault();
}, false);
window.addEventListener('mousemove', function(e) {
var evt = e || event;
if (dragging) {
var delta = evt.clientX - lastX;
lastX = evt.clientX;
marginLeft += delta;
canvas.style.marginLeft = marginLeft + "px";
}
e.preventDefault();
}, false);
window.addEventListener('mouseup', function() {
dragging = false;
}, false);
context.translate(-x, 0)
to simulate the effect. This way, you won't have to change the x-coordinates of your data points. – Shmiddty Commented Jan 16, 2013 at 22:46