I'm trying to draw a rectangle (or other path) at the cursors position.
The issue is, if you move your mouse fast enough, the drawing lags behind the cursor considerably (chases it).
To reproduce/ test the issue, I've tried to produce code that is as lean as possible. However there's still a noticeable gap [between the cursor and the rectangle] due to rendering latency, even on a computer with decent specs (Chrome Beta 37, Fedora 20, i7 4770k, etc)
Can anyone posit to the cause, or suggest improvements to the following code to reduce latency:
var canvas = document.getElementsByTagName('canvas')[0];
var canvasDim = {
width: canvas.width,
height: canvas.height
};
var canvasOffset = canvas.getBoundingClientRect();
var context = canvas.getContext('2d');
context.stroke = "#000000";
context.fill = "#000000";
var currentPosition = {x:0, y:0};
var previousPosition = currentPosition;
var onlyClearPreviousPositon = true;
canvas.onmousemove = function(e){
currentPosition = {
x: e.clientX - canvasOffset.left,
y: e.clientY - canvasOffset.top
};
};
function drawAtCursor(){
if (onlyClearPreviousPositon){
// experiment - try not clearing the whole canvas
context.clearRect(previousPosition.x - 4, previousPosition.y - 4, 8, 8);
previousPosition = currentPosition;
} else {
context.clearRect(0, 0, canvasDim.width, canvasDim.height);
}
context.fillRect(currentPosition.x - 4, currentPosition.y - 4, 8, 8);
window.requestAnimationFrame(drawAtCursor);
}
drawAtCursor();