EllipseBuilder.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * EllipseBuilder.js
  3. *
  4. * @author realor
  5. */
  6. import { ObjectBuilder } from './ObjectBuilder.js'
  7. import { ProfileBuilder } from './ProfileBuilder.js'
  8. import { ProfileGeometry } from '../core/ProfileGeometry.js'
  9. import * as THREE from '../lib/three.module.js'
  10. class EllipseBuilder extends ProfileBuilder {
  11. constructor(xradius = 1, yradius = 0.5, segments = 32) {
  12. super()
  13. this.xradius = xradius
  14. this.yradius = yradius
  15. this.segments = segments
  16. }
  17. performBuild(profile) {
  18. const shape = new THREE.Shape()
  19. this.drawEllipse(shape, this.xradius, this.yradius, this.segments)
  20. profile.updateGeometry(new ProfileGeometry(shape))
  21. return true
  22. }
  23. drawEllipse(path, xradius, yradius, segments) {
  24. const incr = (2 * Math.PI) / segments
  25. path.moveTo(xradius, 0)
  26. for (let rad = incr; rad < 2 * Math.PI; rad += incr) {
  27. path.lineTo(xradius * Math.cos(rad), yradius * Math.sin(rad))
  28. }
  29. path.closePath()
  30. }
  31. copy(source) {
  32. this.xradius = source.xradius
  33. this.yradius = source.yradius
  34. this.segments = source.segments
  35. return this
  36. }
  37. }
  38. ObjectBuilder.addClass(EllipseBuilder)
  39. export { EllipseBuilder }