Apart from issues of formatting, you do have the functions defining global variables, which is almost certainly responsible for clobbering variables.
Declare them as variables, so that they don’t end up affecting any similar global variables.
draw.drawLine = function(acanvas, x, y, endx, endy) {
var start = new point(x, y),
end = new point(endx, endy),
aline = new line(start, end),
c = document.getElementById(acanvas),
cxt = c.getContext("2d");
cxt.moveTo(x, y);
...
};
Functions that are constructors should have a capital initial letter for their name:
function Point(x, y) {
this.x = x;
this.y = y;
}
and likewise for the Line constructor.
Also, the last line of the Line constructor doesn’t appear to assign anything to the context.
this.context;
Instead of using new Object() it’s preferred to use {} instead
draw = {};
And there’s a stroke statement in the addPointRelative function that’s missing parenthesis:
aline.context.stroke;
Here’s a cleaned-up version of the code, but without a test page to help exercise the code, it’s unknown whether this will help with your problem.
function Point(x, y) {
this.x = x;
this.y = y;
}
function Line(start, end) {
this.start = start;
this.end = end;
}
var draw = {};
draw.drawLine = function (acanvas, x, y, endx, endy) {
var start = new Point(x, y),
end = new Point(endx, endy),
aline = new Line(start, end),
c = document.getElementById(acanvas),
cxt = c.getContext("2d");
cxt.moveTo(x, y);
cxt.lineTo(endx, endy);
cxt.stroke();
aline.context = cxt;
return aline;
};
draw.addPoint = function (aline, x, y) {
var start = new Point(aline.end.x, aline.end.y),
end = new Point(x, y),
newline = new Line(start, end);
aline.context.lineTo(x, y);
aline.context.stroke();
return newline;
};
draw.addPointRelative = function (aline, x, y) {
var start = new Point(aline.end.x, aline.end.y),
end = new Point(x, y),
newline = new Line(start, end);
aline.context.lineTo(aline.end.x, aline.end.y); //THIS IS LINE 53
aline.context.stroke();
return newline;
};