I'm working on some code I'm unfamiliar with, and trying to troubleshoot a strange bug. Essentially there are different sections to the canvas, and the goal is to calculate the percentage of the section that's filled. There are two different colors (red and yellow, with specific RGB and Hex values) that could be used to draw a line in the section, used like a brush stroke. When I draw over the entire section in a single color, the % of section filled shows correctly at 100%. However, if I go back over and draw a line of the other color into the section, a small number of pixels are neither of the specific RGB values and so the % turns to 99%. When I used an image viewer and blew up the image, I could see that the bulk of the drawn line is fine, but the outer edges are gradient-like that fade into the other color.
This is the code used to draw onto the canvas:
handleMouseMove() {
const canvas = this.props.canvasInfo;
const context = canvas.getContext('2d');
if (!(this.props.canvasObject && context && this.image)) {
setCanvasError(this.props.dispatch, Constants.CANVAS_GENERIC_ERROR);
return;
}
const { isDrawing, mode, brushSize, brushColor } = this.props.canvasObject;
if (isDrawing && mode && brushSize && brushColor) {
context.strokeStyle = brushColor;
context.lineJoin = 'round';
context.lineWidth = brushSize;
context.globalCompositeOperation = 'source-over';
if (mode === Constants.ERASER_MODE) {
context.strokeStyle = Constants.CANVAS_BACKGROUND_COLOR;
}
context.beginPath();
const localPos = {
x: this.lastPointerPosition.x - this.image.x(),
y: this.lastPointerPosition.y - this.image.y(),
};
context.moveTo(localPos.x, localPos.y);
const stage = this.image.getStage();
const pos = stage.getPointerPosition();
localPos.x = pos.x - this.image.x();
localPos.y = pos.y - this.image.y();
context.lineTo(localPos.x, localPos.y);
context.closePath();
context.stroke();
this.lastPointerPosition = pos;
this.image.getLayer().draw();
}
I'm not sure why the pixels aren't all just either of the two specified colors. I saw at html 5 canvas LineTo() line color issues that this could be antialiasing, but I'm not sure how to fix it.