I have two visible canvases one which uses drawImage and another one where I copy pixels from a hidden (buffer) canvas. Except that everything is the same but when I move the object by some non-integer value the objects starts to stutter. I suspect that flooring while copying pixels is the problem but I'd like to ask how to do this to produce the same result as drawImage?
I set up the jsfiddle.
(object on the right side stutters)
Function which copies pixels.
function draw2()
{
var canvasData = ctx2.createImageData(canvas2.width, canvas2.height),
cData = canvasData.data;
for (var w = 0; w < imgToDraw.width; w++)
{
for (var h = 0; h < imgToDraw.height; h++)
{
if (elm.x + w < canvas2.width && elm.x + w > 0 &&
elm.y + h > 0 && elm.y + h < canvas2.height)
{
var iData = (h * imgToDraw.width + w) * 4;
var pData = (Math.floor(elm.x + w) + Math.floor(elm.y + h) * canvas2.width) * 4;
cData[pData] = imagePixData[iData];
cData[pData + 1] = imagePixData[iData + 1];
cData[pData + 2] = imagePixData[iData + 2];
cData[pData + 3] = imagePixData[iData + 3];
}
}
}
ctx2.putImageData(canvasData, 0, 0);
}
I suspect line 14 (jsfiddle line 102):
var pData = (~~ (elm.x + w) + ~~ (elm.y + h) * canvas2.width) * 4;
I could use Math.round or Math.ceil instead of ~~ to get the desired result but I don't know which would be better or how drawImage handles this?