DomButton.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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.alpha = 1
  11. this.clickHandler = () => {}
  12. }
  13. attach() {
  14. this.alpha = 1
  15. this.viewer.appendChild(this.element)
  16. }
  17. draw(ctx, scaledCanvas) {
  18. this.canvasBounds = scaledCanvas.bounds;
  19. }
  20. update(delta) {
  21. let bounds = this.element.getBoundingClientRect()
  22. this.element.style.left = `${this.center.x - bounds.width / 2}px`
  23. this.element.style.top = `${this.center.y - bounds.height / 2}px`
  24. this.element.style.opacity = this.alpha
  25. }
  26. setPosition(x, y) {
  27. this.center.x = x
  28. this.center.y = y
  29. }
  30. onClick(handler) {
  31. this.element.removeEventListener("click", this.clickHandler)
  32. this.clickHandler = handler
  33. this.element.addEventListener("click", this.clickHandler)
  34. }
  35. remove() {
  36. this.element.removeEventListener("click", this.clickHandler)
  37. this.viewer.removeChild(this.element)
  38. }
  39. }