Reputation: 195
PaperJS has many static constructors (like Rectangle) on their base constructor functions such as Shape. I'd like to extend one of those static constructors like this,
class Rect extends paper.Shape.Rectangle {
constructor() {
super(...arguments);
}
customMethod() {}
}
const A = new Rect();
But what I get in variable A is an instance of class "Shape" which doesn't have the "customMethod".
What is a solution?
Upvotes: 0
Views: 61
Reputation: 8830
Paper.js don't seem to use standard ecmascript
classes; their class system is based on
straps.js.
You can see it in action
in the source code of Rectangle.js,
that is the class available as paper.Rectangle
.
If you wanted to extend Rectangle
, you could
use its method .extend
, like this:
const Rect = paper.Rectangle.extend({
customMove(dx, dy){
this.x += dx;
this.y += dy;
}
});
const rect = new Rect(10, 10, 100, 100);
rect.customMove(30, 10);
As for Shape.Rectangle
it can't
be properly extended, since it is just
a method of the class Shape
- see
the source code.
You could extend Shape
itself to add a new static
method:
const Shape1 = paper.Shape.extend({
statics:{
Rect(){
//... access to this.Rectangle
}
}
});
And in some cases, it is feasible to just add
a new method directly to Shape
:
paper.Shape.Rect = function(){
//... access this.Rectangle, or paper.Shape.Rectangle)
}
Upvotes: 1