I am trying to add my own object parameter to a canvas element as in jquery (like it adds element_id.draggable, element_id.resizable etc to the dom elements). My piece of code looks like this:
window.addEventListener("DOMContentLoaded",handler,false);
function handler()
{
HTMLCanvasElement.prototype.animateLine=function(x1,x2,y1,y2,params) {
if(params)
init(this,x1,x2,y1,y2,params.lineColour,params.lineWidth,params.divisions,params.totalTime,params.callback);
else
init(this,x1,x2,y1,y2);
};
}
So that when a user declares a canvas tag, he can directly call canvas_id.animateLine()
to execute the above function.
But the problem occurring is that when the user call animateLine() in body onload or window onload, then it does not work (says animateLine is undefined).
I wanted to know that is this the right way to implement this? Also how can I remove the error from the above piece of code since I too have to call it on body/window onload?
EDIT: Updated code to call function on DOM ready, still it will not work if the user calls animateLine on DOM Ready. Any solution to this?