alexbol99 / flatten-js

Javascript library for 2d geometry
MIT License
546 stars 56 forks source link

return an instance of the current class when calling clone/translate/… #118

Open schelmo opened 2 years ago

schelmo commented 2 years ago

this allows to use extended classes with methods like clone/translate/rotate/transform/merge

a basic example

class Arc extends Flatten.Arc {
  customMethod() {/*...*/}
}
class Segment extends Flatten.Segment {
  customMethod() {/*...*/}
}
class Polygon exnteds Flatten.Polygon {
  customMethod() {/*...*/}
}

const poly = new Polygon([new Arc(), new Segment()])
// not working now
poly.clone().customMethod()

for (let edge of polygon.translate(Flatten.vector(1, 1)).edges) {
  // not working now
  edge.shape.customMethod()
}