Advertisement
Guest User

ThreeJS Tooltips Typescript

a guest
Sep 9th, 2022
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export class MouseInput {
  2.     mouse: THREE.Vector2 // carries current position information to the mouse object
  3.     raycaster: THREE.Raycaster
  4.     camera: THREE.Camera  
  5.     observables: THREE.Object3D[]
  6.     renderer: THREE.Renderer
  7.     latestIntersectedPoint: THREE.Vector3
  8.     latestIntersectedObject: THREE.Object3D
  9.     tooltip: Tooltip
  10.  
  11.     constructor(renderer: THREE.Renderer, camera: THREE.Camera){
  12.         this.mouse = new THREE.Vector2()
  13.         this.raycaster = new THREE.Raycaster()
  14.         this.renderer = renderer
  15.         this.camera = camera
  16.         this.observables = []
  17.         this.tooltip = new Tooltip(renderer, camera)
  18.         window.addEventListener('mousemove', this.onMouseMove, false);
  19.     }
  20.  
  21.     onMouseMove = (event: MouseEvent) => {
  22.         this.mouse.x = ((event.clientX - this.renderer.domElement.offsetLeft + 0.5) / window.innerWidth) * 2 - 1;
  23.         this.mouse.y = -((event.clientY - this.renderer.domElement.offsetTop + 0.5) / window.innerHeight) * 2 + 1;        
  24.     }
  25.  
  26.     observe(object: THREE.Object3D) {
  27.         this.observables.push(object)
  28.     }
  29.  
  30.     // we calculate intersection during the frame loop
  31.     // after any objects and the mouse have moved around, and right before render probably
  32.     currentIntersection(): THREE.Intersection | null {
  33.         this.raycaster.setFromCamera(this.mouse, this.camera);
  34.  
  35.         const intersected = this.raycaster.intersectObjects(this.observables)[0];
  36.         if (intersected) {
  37.             const tooltipContents = intersected.object.userData.tooltipContents
  38.             if (tooltipContents) {
  39.                 this.tooltip.show(tooltipContents, intersected.point)            
  40.             } else {
  41.                 this.tooltip.hide()
  42.             }
  43.             return intersected
  44.         } else {
  45.             this.tooltip.hide()
  46.             return null
  47.         }
  48.  
  49.     }
  50.  
  51. }
  52.  
  53. export class Tooltip {
  54.     domElement: HTMLElement
  55.     renderer: THREE.Renderer
  56.     position: THREE.Vector3
  57.     camera: THREE.Camera
  58.  
  59.     constructor(renderer: THREE.Renderer, camera: THREE.Camera){
  60.         this.domElement = document.getElementById('tooltip')!
  61.         this.renderer = renderer
  62.         this.camera = camera
  63.         this.position = new THREE.Vector3
  64.     }
  65.  
  66.     show(contents: string, point: THREE.Vector3){
  67.         this.domElement.style.display = 'block';
  68.  
  69.         const canvasHalfWidth = this.renderer.domElement.offsetWidth / 2;
  70.         const canvasHalfHeight = this.renderer.domElement.offsetHeight / 2;
  71.         this.position.copy(point).project(this.camera);
  72.         this.position.x = (this.position.x * canvasHalfWidth) + canvasHalfWidth + this.renderer.domElement.offsetLeft;
  73.         this.position.y = -(this.position.y * canvasHalfHeight) + canvasHalfHeight + this.renderer.domElement.offsetTop;
  74.  
  75.         const tootipWidth = this.domElement.offsetWidth;
  76.         const tootipHeight = this.domElement.offsetHeight;
  77.         this.domElement.style.left = `${this.position.x - tootipWidth/2}px`
  78.         this.domElement.style.top = `${this.position.y - tootipHeight - 5}px`
  79.  
  80.         this.domElement.innerHTML = contents
  81.         console.log('show', contents, point)
  82.     }
  83.  
  84.     hide(){
  85.         this.domElement.style.display = 'none';
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement