I am writing a javascript/canvas script that generates an image. I am experiencing radically different behaviour between browsers and I can't seem to understand why it happens or how to avoid it.
My code is essentially a version of this, assuming that X
and Y
contains the coordinates of the lines:
//this section of code is repeated a large number of times.
CTX.strokeStyle = 'rgba(0,0,0,0.05)';
CTX.lineWidth = 1 ;
for(var i = 0;i<NUM;i++){
for(var j = 0;j<NUM,j++){
if (!F[i*NUM+j]){
// i and j are not friends
// and should not have a line between them
continue;
}
var ax = X[i];
var ay = Y[i];
var bx = X[j];
var by = Y[j];
CTX.beginPath();
CTX.moveTo(ax,ay);
CTX.lineTo(bx,by);
//CTX.closePath(); // not needed. but removing does not fix problem.
CTX.stroke();
}
}
Note that the lines are tightly overlapping, which is why I set the alpha value so low. The image is supposed to be gradually exposed.
Here is an image that displays the behaviour:
Left: part of an image from firefox. Right: part of an image from chromium
The left example is the desired behaviour.
My assumption is that this has something to do with the antialising in the two browsers, but I can't find anywhere where this behaviour is discussed. I only find examples of people stating that you should round your coordinates to whole numbers if you want "pixel graphics", but that is a somewhat different matter. '
Questions:
- Do you know why this happens?
- Can I somehow avoid it?
Update
Here is a permanent link to the code as it is now: https://bitbucket.org/inconvergent/orbitals_js/src/30f33d11461f4b307fe4a09048bd1b3af4960d31/index.htm
I had written chrome. Turns out I was actually using chromium when i had this problem. on ubuntu 13.10. Will test in chrome on my machine and post back.