Is there a recommended way to extend classes in Paper.js? In particular, I am interested in extending Path
Pardon if my terminology is incorrect, but I am essentailly asking the same question about paper that is being asked about three here
Is there a recommended way to extend classes in Paper.js? In particular, I am interested in extending Path
Pardon if my terminology is incorrect, but I am essentailly asking the same question about paper that is being asked about three here
Based on your comment to the initial version of my answer, you are looking for the 'extend' function (oops, that was exactly what you meant) to do subclassing. In an email to the paper.js mailing list, Jürg Lehni (one of the creators) said:
A complication is that there are no
paper.Path.Rectangle
objects. There are paths, and there are rectangles, but when you callnew paper.Path.Rectangle()
it creates a newPath
using initialization code (createRectangle
) that creates a rectangular shape.So we would need to extend
paper.Path
. Unfortunately, when you callnew paper.Path.Rectangle
it callscreatePath
, which always returns aPath
(not your extension). It may be possible to do something like:...and with correctly substituting/overriding for
createRectangle
orcreatePath
get a subclass to work. Unfortunately, I have not been able to manage it.My first working recommendation is to make a factory and add your functions to the objects in that factory (jsbin here):
Similarly, you can use the factory to just change the prototype for your SuperRectangles, having added your functions to that prototype object (and making its prototype the one from
paper.Path.__proto__
) (jsbin here):Alternatively, you can make an object that encapsulates the Path (jsbin here):
Unfortunately, then to access the path you have to use the
theRect
variable.Initial incorrect answer follows:
I don't think you mean "extending classes". In Javascript you can extend objects so that they have more functions, so extending the Path "class" would mean all Path objects have the same new functions. Javascript object extension is further described here.
If I'm wrong, and you do want to extend Path, then you can use:
However, I think you are actually talking about creating new objects that mostly behave like Path objects but have different functionality from each other. If that is the case, then you may want to look at this answer about Javascript using prototypical inheritance. For example, here I create two Rectangle objects that behave differently when I ask them to
doSomething
(jsbin here):A couple of things.
1) You can wrap the original paperjs object but this is very much a hack paperjs playground
2) I may clone & create a paper 2017 which operates off of es6 so that you can use the
extend
keyword.3) I wrote an application called Flavas but it never gained a following so I just kind of left it. That being said, I have been playing with it lately; upgrading it to es6. With it you can do what you're talking about.
I wrote all this kind of quick so feel free to hit me up with Q's.