Here’s how to change the pixel color along a line based on the stroke and existing pixels.
First you need to get the XY coordinates along that the line from start to end:
Given:
var startPt={x:50,y:50}
var endPt={x:85,y:25}
You can get an XY along that line which is proportionally (p) between 0.00 and 1.00 like this:
// this function gets the xy along a line
// that is proportionally (p) from 0.00 to 1.00
function getLineXY(startPt,endPt,p) {
var dx = endPt.x-startPt.x;
var dy = endPt.y-startPt.y;
var X = startPt.x + dx*p;
var Y = startPt.y + dy*p;
return( {x:X,y:Y} );
}
This is a loop that will get XY’s along the line and examine the pixel color at that xy:
If the colors do not match, you can use your setpixelcolour to change it as you desire.
for(var i=0;i<=1;i+=.005){
// get the xy of a point along the line
var pt=getLineXY(i);
// get the rgb of that pixel at xy
var ptColour=getpixelcolour(pt.x,pt.y);
// if the strokeColor is not the ptColor, change the pixel's colour
if(
!(strokeColour.r == ptColor.r &&
strokeColour.g == ptColor.g &&
strokeColour.b == ptColor.b)
)
{
setpixelcolour(pt.x,pt.y,newColor);
}
}
One “gotcha” in this process:
Canvas Context will add anti-aliasing to the strokes and fills it draws.
That anti-aliasing will mess up your color comparisons.