DomButton.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { Point } from "../spatial/Point.js";
  2. export class DomButton {
  3. constructor(x, y, view, text, classData) {
  4. this.center = new Point(x, y);
  5. this.element = document.createElement("button")
  6. this.element.innerText = text
  7. this.element.className = classData
  8. this.viewer = view
  9. this.canvasBounds = null
  10. this.clickHandler = () => {}
  11. }
  12. attach() {
  13. this.viewer.appendChild(this.element)
  14. }
  15. draw(ctx, scaledCanvas) {
  16. this.canvasBounds = scaledCanvas.bounds;
  17. }
  18. update(delta) {
  19. let bounds = this.element.getBoundingClientRect()
  20. this.element.style.left = `${this.center.x - bounds.width / 2}px`
  21. this.element.style.top = `${this.center.y - bounds.height / 2}px`
  22. }
  23. setPosition(x, y) {
  24. this.center.x = x
  25. this.center.y = y
  26. }
  27. onClick(handler) {
  28. this.element.removeEventListener("click", this.clickHandler)
  29. this.clickHandler = handler
  30. this.element.addEventListener("click", this.clickHandler)
  31. }
  32. remove() {
  33. this.element.removeEventListener("click", this.clickHandler)
  34. this.viewer.removeChild(this.element)
  35. }
  36. }