I’d like to be able to extend the functionality of built-in p5.js
functions, keeping the same name but adding some additional code. I don’t want to create my own version of p5.js
, since maintaining that will be a pain. I think this is possible but can’t quite get my head around how to make it happen.
Let’s say I want to add the <canvas>
dashed line functionality. Normally, I’d call line()
like this:
line(x1,y1, x2,y2);
But I’d like to add optional arguments for dashed lines (the dash spacing and offset):
line(x1,y1, x2,y2, [10,5], 5);
I can make a function called line2()
that would look like this:
function line2(x1,y1, x2,y2, dash, offset) {
if (dash !== undefined) {
drawingContext.setLineDash(dash);
if (offset !== undefined) {
drawingContext.setDashOffset = offset;
}
}
line(x1,y1, x2,y2);
}
But this means I have to call line2()
instead of line()
. Not a huge deal but messy. Ideally, I’d like to create my own function called line()
that just like the one above and calls the original line()
inside it after changing the canvas settings. I’ve found a couple of SO posts that seem to point to this, but I can’t get them to work or they are meant for methods in classes.
Might not be possible but any pointers would be great! This is meant especially for my students, so switching to global mode or complicated hacks are probably more confusing than just creating a line2()
function.