diff --git a/.eslintrc.json b/.eslintrc.json index 887350b0..1c60b238 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -36,7 +36,7 @@ "import/namespace": "off", "import/default": "off", "@typescript-eslint/explicit-module-boundary-types": "off", - "no-unused-vars": ["warn"], + "no-unused-vars": ["off"], "no-var": "error", "react/jsx-uses-react": "error", "react/jsx-uses-vars": "error", @@ -44,8 +44,7 @@ "@typescript-eslint/no-use-before-define": "off", "@typescript-eslint/no-empty-function": "off", "@typescript-eslint/no-empty-interface": "off", - "@typescript-eslint/no-explicit-any": "off", - "jest/consistent-test-it": ["error", { "fn": "it", "withinDescribe": "it" }] + "@typescript-eslint/no-explicit-any": "off" }, "settings": { "react": { @@ -68,6 +67,13 @@ "parserOptions": { "project": ["./tsconfig.json", "./storybook/tsconfig.json"] } + }, + { + // enable the rule specifically for TypeScript files + "files": ["*.ts", "*.tsx"], + "rules": { + "@typescript-eslint/explicit-function-return-type": ["error", { "allowHigherOrderFunctions": true }] + } } ] } diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 043b3574..081bd25c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -24,7 +24,7 @@ jobs: # there's a build step for post installation so we don't run build when "testing" run: yarn install - name: Lint test 🧪 - run: yarn eslint:ci + run: yarn lint:ci - name: Prettier test run: yarn prettier:check - name: Type check diff --git a/demos/ssr/components/Lights.tsx b/demos/ssr/components/Lights.tsx index 3b7f2cf2..0241f0a4 100644 --- a/demos/ssr/components/Lights.tsx +++ b/demos/ssr/components/Lights.tsx @@ -1,4 +1,4 @@ -export const Lights = () => { +export const Lights = (): JSX.Element => { return ( diff --git a/demos/ssr/components/Scene.tsx b/demos/ssr/components/Scene.tsx index d791294a..96debb2d 100644 --- a/demos/ssr/components/Scene.tsx +++ b/demos/ssr/components/Scene.tsx @@ -5,7 +5,7 @@ import { Mesh } from 'three' import { Lights } from './Lights' import { Controls } from './Controls' -export const Scene = () => { +export const Scene = (): JSX.Element => { const meshRef = useRef() useFrame((_, delta) => { diff --git a/demos/ssr/pages/_app.tsx b/demos/ssr/pages/_app.tsx index f96aa436..db3af8d0 100644 --- a/demos/ssr/pages/_app.tsx +++ b/demos/ssr/pages/_app.tsx @@ -1,5 +1,6 @@ import React from 'react' import { createGlobalStyle } from 'styled-components' +import type { AppProps } from 'next/app' const GlobalStyle = createGlobalStyle` *, @@ -45,7 +46,7 @@ const GlobalStyle = createGlobalStyle` } ` -const App = ({ Component, pageProps }) => { +const App = ({ Component, pageProps }: AppProps): JSX.Element => { return ( <> diff --git a/demos/ssr/pages/_document.tsx b/demos/ssr/pages/_document.tsx index 9c9b1d68..a2b9a912 100644 --- a/demos/ssr/pages/_document.tsx +++ b/demos/ssr/pages/_document.tsx @@ -1,15 +1,21 @@ -import Document, { Html, Head, Main, NextScript } from 'next/document' +import Document, { Html, Head, Main, NextScript, DocumentContext } from 'next/document' import { ServerStyleSheet } from 'styled-components' export default class Doc extends Document { - static async getInitialProps(ctx) { + static async getInitialProps( + ctx: DocumentContext, + ): Promise<{ + styles: JSX.Element + html: string + head?: (JSX.Element | null)[] | undefined + }> { const sheet = new ServerStyleSheet() const originalRenderPage = ctx.renderPage try { - ctx.renderPage = () => + ctx.renderPage = (): ReturnType => originalRenderPage({ - enhanceApp: (App) => (props) => sheet.collectStyles(), + enhanceApp: (App) => (props): React.ReactElement => sheet.collectStyles(), }) const initialProps = await Document.getInitialProps(ctx) @@ -27,7 +33,7 @@ export default class Doc extends Document { } } - render() { + render(): JSX.Element { return ( diff --git a/demos/ssr/pages/index.tsx b/demos/ssr/pages/index.tsx index f770eefb..7418f8cb 100644 --- a/demos/ssr/pages/index.tsx +++ b/demos/ssr/pages/index.tsx @@ -3,7 +3,7 @@ import styled from 'styled-components' import { Scene } from '../components/Scene' -export default function IndexPage() { +export default function IndexPage(): JSX.Element { return ( diff --git a/package.json b/package.json index 070558c7..e6911c78 100644 --- a/package.json +++ b/package.json @@ -9,8 +9,8 @@ "build": "rollup -c && npm run copy", "build-storybook": "build-storybook -s ./.storybook/public", "copy": "copyfiles package.json README.md LICENSE dist && json -I -f dist/package.json -e \"this.private=false; this.devDependencies=undefined; this.optionalDependencies=undefined; this.scripts=undefined; this.husky=undefined; this.prettier=undefined; this.jest=undefined; this['lint-staged']=undefined;\"", - "eslint": "eslint --cache --fix src/**/*.ts", - "eslint:ci": "eslint src/**/*.ts", + "lint": "eslint --cache --fix src/**/*.ts", + "lint:ci": "eslint src/**/*.ts", "prebuild": "rimraf dist && npm run typegen", "prepare": "npm run build", "prettier:write": "prettier --write .", @@ -36,6 +36,9 @@ "url": "git+https://github.com/pmndrs/three-stdlib.git" }, "author": "Paul Henschel", + "maintainers": [ + "Josh Ellis (https://github.com/joshuaellis" + ], "license": "MIT", "bugs": { "url": "https://github.com/pmndrs/three-stdlib/issues" @@ -60,8 +63,8 @@ "@storybook/html": "^6.1.21", "@storybook/preset-typescript": "^3.0.0", "@types/three": "^0.126.0", - "@typescript-eslint/eslint-plugin": "^4.15.2", - "@typescript-eslint/parser": "^4.15.2", + "@typescript-eslint/eslint-plugin": "^4.18.0", + "@typescript-eslint/parser": "^4.18.0", "babel-eslint": "^10.1.0", "babel-loader": "^8.2.2", "babel-plugin-transform-react-remove-prop-types": "^0.4.24", diff --git a/src/controls/DeviceOrientationControls.ts b/src/controls/DeviceOrientationControls.ts index 9ef8de8f..83e2418f 100644 --- a/src/controls/DeviceOrientationControls.ts +++ b/src/controls/DeviceOrientationControls.ts @@ -11,7 +11,7 @@ class DeviceOrientationControls extends EventDispatcher { private EPS = 0.000001 enabled = true - deviceOrientation = { alpha: 0, beta: 0, gamma: 0 } + deviceOrientation: Partial = { alpha: 0, beta: 0, gamma: 0 } screenOrientation: string | number = 0 alphaOffset = 0 // radians @@ -24,11 +24,11 @@ class DeviceOrientationControls extends EventDispatcher { this.connect() } - private onDeviceOrientationChangeEvent = (event: any) => { + private onDeviceOrientationChangeEvent = (event: DeviceOrientationEvent): void => { this.deviceOrientation = event } - private onScreenOrientationChangeEvent = () => { + private onScreenOrientationChangeEvent = (): void => { this.screenOrientation = window.orientation || 0 } @@ -44,14 +44,14 @@ class DeviceOrientationControls extends EventDispatcher { beta: number, gamma: number, orient: number, - ) => { + ): void => { this.euler.set(beta, alpha, -gamma, 'YXZ') // 'ZXY' for the device, but 'YXZ' for us quaternion.setFromEuler(this.euler) // orient the device quaternion.multiply(this.q1) // camera looks out the back of the device, not the top quaternion.multiply(this.q0.setFromAxisAngle(this.zee, -orient)) // adjust for screen orientation } - connect = () => { + connect = (): void => { this.onScreenOrientationChangeEvent() // run once on load // iOS 13+ @@ -78,7 +78,7 @@ class DeviceOrientationControls extends EventDispatcher { this.enabled = true } - disconnect = () => { + disconnect = (): void => { window.removeEventListener('orientationchange', this.onScreenOrientationChangeEvent) window.removeEventListener('deviceorientation', this.onDeviceOrientationChangeEvent) @@ -86,7 +86,7 @@ class DeviceOrientationControls extends EventDispatcher { } private lastQuaternion = new Quaternion() - update = () => { + update = (): void => { if (this.enabled === false) return const device = this.deviceOrientation @@ -106,7 +106,7 @@ class DeviceOrientationControls extends EventDispatcher { } } - dispose = () => this.disconnect() + dispose = (): void => this.disconnect() } export { DeviceOrientationControls } diff --git a/src/controls/DragControls.ts b/src/controls/DragControls.ts index 1c7a36f0..df051a88 100644 --- a/src/controls/DragControls.ts +++ b/src/controls/DragControls.ts @@ -30,7 +30,7 @@ class DragControls extends EventDispatcher { this.activate() } - activate = () => { + activate = (): void => { this._domElement.addEventListener('pointermove', this.onPointerMove) this._domElement.addEventListener('pointerdown', this.onPointerDown) this._domElement.addEventListener('pointerup', this.onPointerCancel) @@ -40,7 +40,7 @@ class DragControls extends EventDispatcher { this._domElement.addEventListener('touchend', this.onTouchEnd) } - deactivate = () => { + deactivate = (): void => { this._domElement.removeEventListener('pointermove', this.onPointerMove) this._domElement.removeEventListener('pointerdown', this.onPointerDown) this._domElement.removeEventListener('pointerup', this.onPointerCancel) @@ -53,11 +53,11 @@ class DragControls extends EventDispatcher { } // TODO: confirm if this can be removed? - dispose = () => this.deactivate() + dispose = (): void => this.deactivate() - getObjects = () => this._objects + getObjects = (): Object3D[] => this._objects - private onMouseMove = (event: MouseEvent) => { + private onMouseMove = (event: MouseEvent): void => { const rect = this._domElement.getBoundingClientRect() this._mouse.x = ((event.clientX - rect.left) / rect.width) * 2 - 1 @@ -104,7 +104,7 @@ class DragControls extends EventDispatcher { } } - private onMouseDown = (event: MouseEvent) => { + private onMouseDown = (event: MouseEvent): void => { event.preventDefault() this._intersections.length = 0 @@ -126,7 +126,7 @@ class DragControls extends EventDispatcher { } } - private onMouseCancel = (event: MouseEvent) => { + private onMouseCancel = (event: MouseEvent): void => { event.preventDefault() if (this._selected) { @@ -138,7 +138,7 @@ class DragControls extends EventDispatcher { this._domElement.style.cursor = this._hovered ? 'pointer' : 'auto' } - private onPointerMove = (event: PointerEvent) => { + private onPointerMove = (event: PointerEvent): void => { event.preventDefault() switch (event.pointerType) { @@ -151,7 +151,7 @@ class DragControls extends EventDispatcher { } } - private onPointerDown = (event: PointerEvent) => { + private onPointerDown = (event: PointerEvent): void => { event.preventDefault() switch (event.pointerType) { @@ -164,7 +164,7 @@ class DragControls extends EventDispatcher { } } - private onPointerCancel = (event: PointerEvent) => { + private onPointerCancel = (event: PointerEvent): void => { event.preventDefault() switch (event.pointerType) { @@ -177,7 +177,7 @@ class DragControls extends EventDispatcher { } } - private onTouchMove = (event: TouchEvent) => { + private onTouchMove = (event: TouchEvent): void => { event.preventDefault() const newEvent = event.changedTouches[0] @@ -199,7 +199,7 @@ class DragControls extends EventDispatcher { } } - private onTouchStart = (event: TouchEvent) => { + private onTouchStart = (event: TouchEvent): void => { event.preventDefault() const newEvent = event.changedTouches[0] @@ -232,7 +232,7 @@ class DragControls extends EventDispatcher { } } - private onTouchEnd = (event: TouchEvent) => { + private onTouchEnd = (event: TouchEvent): void => { event.preventDefault() if (this._selected) { diff --git a/src/controls/FirstPersonControls.ts b/src/controls/FirstPersonControls.ts index 8e520db7..ecd0e79d 100644 --- a/src/controls/FirstPersonControls.ts +++ b/src/controls/FirstPersonControls.ts @@ -77,7 +77,7 @@ class FirstPersonControls extends EventDispatcher { window.addEventListener('keyup', this.onKeyUp) } - dispose = () => { + dispose = (): void => { this.domElement.removeEventListener('contextmenu', this.contextmenu) ;(this.domElement as HTMLElement).removeEventListener('mousedown', this.onMouseDown) ;(this.domElement as HTMLElement).removeEventListener('mousemove', this.onMouseMove) @@ -87,7 +87,7 @@ class FirstPersonControls extends EventDispatcher { window.removeEventListener('keyup', this.onKeyUp) } - handleResize = () => { + handleResize = (): void => { if (this.domElement instanceof Document) { this.viewHalfX = window.innerWidth / 2 this.viewHalfY = window.innerHeight / 2 @@ -97,7 +97,7 @@ class FirstPersonControls extends EventDispatcher { } } - private onMouseDown = (event: MouseEvent) => { + private onMouseDown = (event: MouseEvent): void => { if (this.domElement instanceof HTMLElement) { this.domElement.focus() } @@ -118,7 +118,7 @@ class FirstPersonControls extends EventDispatcher { this.mouseDragOn = true } - private onMouseUp = (event: MouseEvent) => { + private onMouseUp = (event: MouseEvent): void => { event.preventDefault() if (this.activeLook) { @@ -135,7 +135,7 @@ class FirstPersonControls extends EventDispatcher { this.mouseDragOn = false } - private onMouseMove = (event: MouseEvent) => { + private onMouseMove = (event: MouseEvent): void => { if (this.domElement instanceof Document) { this.mouseX = event.pageX - this.viewHalfX this.mouseY = event.pageY - this.viewHalfY @@ -145,7 +145,7 @@ class FirstPersonControls extends EventDispatcher { } } - private onKeyDown = (event: KeyboardEvent) => { + private onKeyDown = (event: KeyboardEvent): void => { //event.preventDefault(); switch (event.code) { @@ -178,7 +178,7 @@ class FirstPersonControls extends EventDispatcher { } } - private onKeyUp = (event: KeyboardEvent) => { + private onKeyUp = (event: KeyboardEvent): void => { switch (event.code) { case 'ArrowUp': case 'KeyW': @@ -209,7 +209,7 @@ class FirstPersonControls extends EventDispatcher { } } - lookAt = (x: Vector3 | number, y?: number, z?: number) => { + lookAt = (x: Vector3 | number, y?: number, z?: number): this => { if (x instanceof Vector3) { this.target.copy(x) } else if (y && z) { @@ -224,7 +224,8 @@ class FirstPersonControls extends EventDispatcher { } private targetPosition = new Vector3() - update = (delta: number) => { + + update = (delta: number): void => { if (this.enabled === false) return if (this.heightSpeed) { @@ -280,9 +281,9 @@ class FirstPersonControls extends EventDispatcher { this.object.lookAt(this.targetPosition) } - private contextmenu = (event: Event) => event.preventDefault() + private contextmenu = (event: Event): void => event.preventDefault() - private setOrientation = (controls: FirstPersonControls) => { + private setOrientation = (controls: FirstPersonControls): void => { const quaternion = controls.object.quaternion this.lookDirection.set(0, 0, -1).applyQuaternion(quaternion) diff --git a/src/controls/FlyControls.ts b/src/controls/FlyControls.ts index 0844a702..e256f4b4 100644 --- a/src/controls/FlyControls.ts +++ b/src/controls/FlyControls.ts @@ -1,6 +1,6 @@ import { Camera, EventDispatcher, Quaternion, Vector3 } from 'three' -function contextmenu(event: Event) { +function contextmenu(event: Event): void { event.preventDefault() } @@ -56,10 +56,9 @@ class FlyControls extends EventDispatcher { } this.domElement.addEventListener('contextmenu', contextmenu) - - this.domElement.addEventListener('mousemove', this.mousemove as any) - this.domElement.addEventListener('mousedown', this.mousedown as any) - this.domElement.addEventListener('mouseup', this.mouseup as any) + ;(this.domElement as HTMLElement).addEventListener('mousemove', this.mousemove) + ;(this.domElement as HTMLElement).addEventListener('mousedown', this.mousedown) + ;(this.domElement as HTMLElement).addEventListener('mouseup', this.mouseup) window.addEventListener('keydown', this.keydown) window.addEventListener('keyup', this.keyup) @@ -68,7 +67,7 @@ class FlyControls extends EventDispatcher { this.updateRotationVector() } - private keydown = (event: KeyboardEvent) => { + private keydown = (event: KeyboardEvent): void => { if (event.altKey) { return } @@ -128,7 +127,7 @@ class FlyControls extends EventDispatcher { this.updateRotationVector() } - private keyup = (event: KeyboardEvent) => { + private keyup = (event: KeyboardEvent): void => { switch (event.code) { case 'ShiftLeft': case 'ShiftRight': @@ -182,7 +181,7 @@ class FlyControls extends EventDispatcher { this.updateRotationVector() } - private mousedown = (event: MouseEvent) => { + private mousedown = (event: MouseEvent): void => { if (this.domElement !== document && !(this.domElement instanceof Document)) { this.domElement.focus() } @@ -205,7 +204,7 @@ class FlyControls extends EventDispatcher { } } - private mousemove = (event: MouseEvent) => { + private mousemove = (event: MouseEvent): void => { if (!this.dragToLook || this.mouseStatus > 0) { const container = this.getContainerDimensions() const halfWidth = container.size[0] / 2 @@ -218,7 +217,7 @@ class FlyControls extends EventDispatcher { } } - private mouseup = (event: MouseEvent) => { + private mouseup = (event: MouseEvent): void => { event.preventDefault() if (this.dragToLook) { @@ -244,7 +243,7 @@ class FlyControls extends EventDispatcher { private lastQuaternion = new Quaternion() private lastPosition = new Vector3() - public update = (delta: number) => { + public update = (delta: number): void => { const moveMult = delta * this.movementSpeed const rotMult = delta * this.rollSpeed @@ -267,7 +266,7 @@ class FlyControls extends EventDispatcher { } } - private updateMovementVector = () => { + private updateMovementVector = (): void => { const forward = this.moveState.forward || (this.autoForward && !this.moveState.back) ? 1 : 0 this.moveVector.x = -this.moveState.left + this.moveState.right @@ -275,13 +274,16 @@ class FlyControls extends EventDispatcher { this.moveVector.z = -forward + this.moveState.back } - private updateRotationVector = () => { + private updateRotationVector = (): void => { this.rotationVector.x = -this.moveState.pitchDown + this.moveState.pitchUp this.rotationVector.y = -this.moveState.yawRight + this.moveState.yawLeft this.rotationVector.z = -this.moveState.rollRight + this.moveState.rollLeft } - private getContainerDimensions = () => { + private getContainerDimensions = (): { + size: number[] + offset: number[] + } => { if (this.domElement != document && !(this.domElement instanceof Document)) { return { size: [this.domElement.offsetWidth, this.domElement.offsetHeight], @@ -295,11 +297,11 @@ class FlyControls extends EventDispatcher { } } - public dispose = () => { + public dispose = (): void => { this.domElement.removeEventListener('contextmenu', contextmenu) - this.domElement.removeEventListener('mousemove', this.mousemove as any) - this.domElement.removeEventListener('mousedown', this.mousedown as any) - this.domElement.removeEventListener('mouseup', this.mouseup as any) + ;(this.domElement as HTMLElement).removeEventListener('mousemove', this.mousemove) + ;(this.domElement as HTMLElement).removeEventListener('mousedown', this.mousedown) + ;(this.domElement as HTMLElement).removeEventListener('mouseup', this.mouseup) window.removeEventListener('keydown', this.keydown) window.removeEventListener('keyup', this.keyup) diff --git a/src/controls/OrbitControls.ts b/src/controls/OrbitControls.ts index b1ee5eab..10260dd4 100644 --- a/src/controls/OrbitControls.ts +++ b/src/controls/OrbitControls.ts @@ -1,3 +1,4 @@ +/* eslint-disable */ import { Camera, EventDispatcher, @@ -78,12 +79,10 @@ class OrbitControls extends EventDispatcher { getPolarAngle: () => number getAzimuthalAngle: () => number - // eslint-disable-next-line no-unused-vars - listenToKeyEvents: (domElement: any) => void + listenToKeyEvents: (domElement: HTMLElement) => void saveState: () => void reset: () => void update: () => void - // eslint-disable-next-line no-unused-vars connect: (domElement: HTMLElement) => void dispose: () => void @@ -101,22 +100,22 @@ class OrbitControls extends EventDispatcher { // public methods // - this.getPolarAngle = () => spherical.phi + this.getPolarAngle = (): number => spherical.phi - this.getAzimuthalAngle = () => spherical.theta + this.getAzimuthalAngle = (): number => spherical.theta - this.listenToKeyEvents = (domElement) => { + this.listenToKeyEvents = (domElement: HTMLElement): void => { domElement.addEventListener('keydown', onKeyDown) this._domElementKeyEvents = domElement } - this.saveState = () => { + this.saveState = (): void => { scope.target0.copy(scope.target) scope.position0.copy(scope.object.position) scope.zoom0 = scope.object instanceof PerspectiveCamera ? scope.object.zoom : 1 } - this.reset = () => { + this.reset = (): void => { scope.target.copy(scope.target0) scope.object.position.copy(scope.position0) if (scope.object instanceof PerspectiveCamera) { @@ -132,7 +131,7 @@ class OrbitControls extends EventDispatcher { } // this method is exposed, but perhaps it would be better if we can make it private... - this.update = (() => { + this.update = ((): (() => void) => { const offset = new Vector3() // so camera.up is the orbit axis @@ -144,7 +143,7 @@ class OrbitControls extends EventDispatcher { const twoPI = 2 * Math.PI - return function update() { + return function update(): boolean { const position = scope.object.position offset.copy(position).sub(scope.target) @@ -248,7 +247,7 @@ class OrbitControls extends EventDispatcher { })() // https://github.com/mrdoob/three.js/issues/20575 - this.connect = (domElement: HTMLElement) => { + this.connect = (domElement: HTMLElement): void => { if ((domElement as any) === document) { console.error( 'THREE.OrbitControls: "document" should not be used as the target "domElement". Please use "renderer.domElement" instead.', @@ -263,7 +262,7 @@ class OrbitControls extends EventDispatcher { scope.domElement.addEventListener('touchmove', onTouchMove) } - this.dispose = () => { + this.dispose = (): void => { scope.domElement?.removeEventListener('contextmenu', onContextMenu) scope.domElement?.removeEventListener('pointerdown', onPointerDown) scope.domElement?.removeEventListener('wheel', onMouseWheel) @@ -323,19 +322,19 @@ class OrbitControls extends EventDispatcher { const dollyEnd = new Vector2() const dollyDelta = new Vector2() - function getAutoRotationAngle() { + function getAutoRotationAngle(): number { return ((2 * Math.PI) / 60 / 60) * scope.autoRotateSpeed } - function getZoomScale() { + function getZoomScale(): number { return Math.pow(0.95, scope.zoomSpeed) } - function rotateLeft(angle: number) { + function rotateLeft(angle: number): void { sphericalDelta.theta -= angle } - function rotateUp(angle: number) { + function rotateUp(angle: number): void { sphericalDelta.phi -= angle } diff --git a/src/controls/PointerLockControls.ts b/src/controls/PointerLockControls.ts index 0ee45c4a..b2d55e2f 100644 --- a/src/controls/PointerLockControls.ts +++ b/src/controls/PointerLockControls.ts @@ -35,7 +35,7 @@ class PointerLockControls extends EventDispatcher { this.connect() } - private onMouseMove = (event: MouseEvent) => { + private onMouseMove = (event: MouseEvent): void => { if (this.isLocked === false) return const movementX = event.movementX || (event as any).mozMovementX || (event as any).webkitMovementX || 0 @@ -53,7 +53,7 @@ class PointerLockControls extends EventDispatcher { this.dispatchEvent(this.changeEvent) } - private onPointerlockChange = () => { + private onPointerlockChange = (): void => { if (this.domElement.ownerDocument.pointerLockElement === this.domElement) { this.dispatchEvent(this.lockEvent) @@ -65,34 +65,34 @@ class PointerLockControls extends EventDispatcher { } } - private onPointerlockError = () => { + private onPointerlockError = (): void => { console.error('THREE.PointerLockControls: Unable to use Pointer Lock API') } - connect = () => { + connect = (): void => { this.domElement.ownerDocument.addEventListener('mousemove', this.onMouseMove) this.domElement.ownerDocument.addEventListener('pointerlockchange', this.onPointerlockChange) this.domElement.ownerDocument.addEventListener('pointerlockerror', this.onPointerlockError) } - disconnect = () => { + disconnect = (): void => { this.domElement.ownerDocument.removeEventListener('mousemove', this.onMouseMove) this.domElement.ownerDocument.removeEventListener('pointerlockchange', this.onPointerlockChange) this.domElement.ownerDocument.removeEventListener('pointerlockerror', this.onPointerlockError) } - dispose = () => { + dispose = (): void => { this.disconnect() } - private getObject = () => + private getObject = (): Camera => // retaining this method for backward compatibility this.camera private direction = new Vector3(0, 0, -1) - getDirection = (v: Vector3) => v.copy(this.direction).applyQuaternion(this.camera.quaternion) + getDirection = (v: Vector3): Vector3 => v.copy(this.direction).applyQuaternion(this.camera.quaternion) - moveForward = (distance: number) => { + moveForward = (distance: number): void => { // move forward parallel to the xz-plane // assumes this.camera.up is y-up @@ -103,17 +103,17 @@ class PointerLockControls extends EventDispatcher { this.camera.position.addScaledVector(this.vec, distance) } - moveRight = (distance: number) => { + moveRight = (distance: number): void => { this.vec.setFromMatrixColumn(this.camera.matrix, 0) this.camera.position.addScaledVector(this.vec, distance) } - lock = () => { + lock = (): void => { this.domElement.requestPointerLock() } - unlock = () => { + unlock = (): void => { this.domElement.ownerDocument.exitPointerLock() } } diff --git a/src/controls/TrackballControls.ts b/src/controls/TrackballControls.ts index 245da98d..f1b26b49 100644 --- a/src/controls/TrackballControls.ts +++ b/src/controls/TrackballControls.ts @@ -114,7 +114,7 @@ class TrackballControls extends EventDispatcher { private onScreenVector = new Vector2() - private getMouseOnScreen = (pageX: number, pageY: number) => { + private getMouseOnScreen = (pageX: number, pageY: number): Vector2 => { this.onScreenVector.set( (pageX - this.screen.left) / this.screen.width, (pageY - this.screen.top) / this.screen.height, @@ -125,7 +125,7 @@ class TrackballControls extends EventDispatcher { private onCircleVector = new Vector2() - private getMouseOnCircle = (pageX: number, pageY: number) => { + private getMouseOnCircle = (pageX: number, pageY: number): Vector2 => { this.onCircleVector.set( (pageX - this.screen.width * 0.5 - this.screen.left) / (this.screen.width * 0.5), (this.screen.height + 2 * (this.screen.top - pageY)) / this.screen.width, // screen.width intentional @@ -142,7 +142,7 @@ class TrackballControls extends EventDispatcher { private moveDirection = new Vector3() private angle: number = 0 - private rotateCamera = () => { + private rotateCamera = (): void => { this.moveDirection.set(this._moveCurr.x - this._movePrev.x, this._moveCurr.y - this._movePrev.y, 0) this.angle = this.moveDirection.length() @@ -179,7 +179,7 @@ class TrackballControls extends EventDispatcher { this._movePrev.copy(this._moveCurr) } - private zoomCamera = () => { + private zoomCamera = (): void => { let factor if (this._state === this.STATE.TOUCH_ZOOM_PAN) { @@ -220,7 +220,7 @@ class TrackballControls extends EventDispatcher { private objectUp = new Vector3() private pan = new Vector3() - private panCamera = () => { + private panCamera = (): void => { this.mouseChange.copy(this._panEnd).sub(this._panStart) if (this.mouseChange.lengthSq()) { @@ -251,7 +251,7 @@ class TrackballControls extends EventDispatcher { } } - private checkDistances = () => { + private checkDistances = (): void => { if (!this.noZoom || !this.noPan) { if (this._eye.lengthSq() > this.maxDistance * this.maxDistance) { this.object.position.addVectors(this.target, this._eye.setLength(this.maxDistance)) @@ -265,7 +265,7 @@ class TrackballControls extends EventDispatcher { } } - private handleResize = () => { + private handleResize = (): void => { const box = this.domElement.getBoundingClientRect() // adjustments come from similar code in the jquery offset() function const d = this.domElement.ownerDocument.documentElement @@ -275,7 +275,7 @@ class TrackballControls extends EventDispatcher { this.screen.height = box.height } - public update = () => { + public update = (): void => { this._eye.subVectors(this.object.position, this.target) if (!this.noRotate) { @@ -316,7 +316,7 @@ class TrackballControls extends EventDispatcher { } } - public reset = () => { + public reset = (): void => { this._state = this.STATE.NONE this._keyState = this.STATE.NONE @@ -337,7 +337,7 @@ class TrackballControls extends EventDispatcher { this.lastZoom = this.object.zoom } - private keydown = (event: KeyboardEvent) => { + private keydown = (event: KeyboardEvent): void => { if (this.enabled === false) return window.removeEventListener('keydown', this.keydown) @@ -353,7 +353,7 @@ class TrackballControls extends EventDispatcher { } } - private onPointerDown = (event: PointerEvent) => { + private onPointerDown = (event: PointerEvent): void => { if (this.enabled === false) return switch (event.pointerType) { @@ -366,7 +366,7 @@ class TrackballControls extends EventDispatcher { } } - private onPointerMove = (event: PointerEvent) => { + private onPointerMove = (event: PointerEvent): void => { if (this.enabled === false) return switch (event.pointerType) { @@ -379,7 +379,7 @@ class TrackballControls extends EventDispatcher { } } - private onPointerUp = (event: PointerEvent) => { + private onPointerUp = (event: PointerEvent): void => { if (this.enabled === false) return switch (event.pointerType) { @@ -392,7 +392,7 @@ class TrackballControls extends EventDispatcher { } } - private keyup = () => { + private keyup = (): void => { if (this.enabled === false) return this._keyState = this.STATE.NONE @@ -400,7 +400,7 @@ class TrackballControls extends EventDispatcher { window.addEventListener('keydown', this.keydown) } - private onMouseDown = (event: MouseEvent) => { + private onMouseDown = (event: MouseEvent): void => { event.preventDefault() if (this._state === this.STATE.NONE) { @@ -441,7 +441,7 @@ class TrackballControls extends EventDispatcher { this.dispatchEvent(this.startEvent) } - private onMouseMove = (event: MouseEvent) => { + private onMouseMove = (event: MouseEvent): void => { if (this.enabled === false) return event.preventDefault() @@ -458,7 +458,7 @@ class TrackballControls extends EventDispatcher { } } - private onMouseUp = (event: MouseEvent) => { + private onMouseUp = (event: MouseEvent): void => { if (this.enabled === false) return event.preventDefault() @@ -471,7 +471,7 @@ class TrackballControls extends EventDispatcher { this.dispatchEvent(this.endEvent) } - private mousewheel = (event: WheelEvent) => { + private mousewheel = (event: WheelEvent): void => { if (this.enabled === false) return if (this.noZoom === true) return @@ -499,7 +499,7 @@ class TrackballControls extends EventDispatcher { this.dispatchEvent(this.endEvent) } - private touchstart = (event: TouchEvent) => { + private touchstart = (event: TouchEvent): void => { if (this.enabled === false) return event.preventDefault() @@ -528,7 +528,7 @@ class TrackballControls extends EventDispatcher { this.dispatchEvent(this.startEvent) } - private touchmove = (event: TouchEvent) => { + private touchmove = (event: TouchEvent): void => { if (this.enabled === false) return event.preventDefault() @@ -552,7 +552,7 @@ class TrackballControls extends EventDispatcher { } } - private touchend = (event: TouchEvent) => { + private touchend = (event: TouchEvent): void => { if (this.enabled === false) return switch (event.touches.length) { @@ -570,13 +570,13 @@ class TrackballControls extends EventDispatcher { this.dispatchEvent(this.endEvent) } - private contextmenu = (event: MouseEvent) => { + private contextmenu = (event: MouseEvent): void => { if (this.enabled === false) return event.preventDefault() } - public dispose = () => { + public dispose = (): void => { this.domElement.removeEventListener('contextmenu', this.contextmenu) this.domElement.removeEventListener('pointerdown', this.onPointerDown) diff --git a/src/controls/TransformControls.ts b/src/controls/TransformControls.ts index b5f4d914..b34de91f 100644 --- a/src/controls/TransformControls.ts +++ b/src/controls/TransformControls.ts @@ -20,6 +20,7 @@ import { Quaternion, Raycaster, SphereGeometry, + Intersection, TorusGeometry, Vector3, } from 'three' @@ -151,7 +152,7 @@ class TransformControls extends Object3D { } // Defined getter, setter and store for a property - function defineProperty(propName: string, defaultValue: TValue) { + function defineProperty(propName: string, defaultValue: TValue): void { Object.defineProperty(this, propName, { get: function () { return this[propName] @@ -174,7 +175,11 @@ class TransformControls extends Object3D { } } - private intersectObjectWithRay = (object: Object3D, raycaster: Raycaster, includeInvisible?: boolean) => { + private intersectObjectWithRay = ( + object: Object3D, + raycaster: Raycaster, + includeInvisible?: boolean, + ): false | Intersection => { const allIntersections = raycaster.intersectObject(object, true) for (let i = 0; i < allIntersections.length; i++) { @@ -203,7 +208,7 @@ class TransformControls extends Object3D { return this } - public updateMatrixWorld = () => { + public updateMatrixWorld = (): void => { if (this._object !== undefined) { this._object.updateMatrixWorld() @@ -227,7 +232,7 @@ class TransformControls extends Object3D { Object3D.prototype.updateMatrixWorld.call(this) } - private pointerHover = (pointer: TransformControlsPointerObject) => { + private pointerHover = (pointer: TransformControlsPointerObject): void => { if (this._object === undefined || this._dragging === true) return this.raycaster.setFromCamera(pointer, this._camera) @@ -241,7 +246,7 @@ class TransformControls extends Object3D { } } - private pointerDown = (pointer: TransformControlsPointerObject) => { + private pointerDown = (pointer: TransformControlsPointerObject): void => { if (this._object === undefined || this._dragging === true || pointer.button !== 0) return if (this._axis !== null) { @@ -287,7 +292,7 @@ class TransformControls extends Object3D { } } - private pointerMove = (pointer: TransformControlsPointerObject) => { + private pointerMove = (pointer: TransformControlsPointerObject): void => { const axis = this._axis const mode = this._mode const object = this._object @@ -472,7 +477,7 @@ class TransformControls extends Object3D { this.dispatchEvent(this.objectChangeEvent) } - private pointerUp = (pointer: TransformControlsPointerObject) => { + private pointerUp = (pointer: TransformControlsPointerObject): void => { if (pointer.button !== 0) return if (this._dragging && this._axis !== null) { @@ -506,7 +511,7 @@ class TransformControls extends Object3D { } } - private onPointerHover = (event: Event) => { + private onPointerHover = (event: Event): void => { if (!this._enabled) return switch ((event as PointerEvent).pointerType) { @@ -517,7 +522,7 @@ class TransformControls extends Object3D { } } - private onPointerDown = (event: Event) => { + private onPointerDown = (event: Event): void => { if (!this._enabled) return ;(this.domElement as HTMLElement).style.touchAction = 'none' // disable touch scroll this.domElement.ownerDocument?.addEventListener('pointermove', this.onPointerMove) @@ -526,13 +531,13 @@ class TransformControls extends Object3D { this.pointerDown(this.getPointer(event)) } - private onPointerMove = (event: Event) => { + private onPointerMove = (event: Event): void => { if (!this._enabled) return this.pointerMove(this.getPointer(event)) } - private onPointerUp = (event: Event) => { + private onPointerUp = (event: Event): void => { if (!this._enabled) return ;(this.domElement as HTMLElement).style.touchAction = '' this.domElement.ownerDocument?.removeEventListener('pointermove', this.onPointerMove) @@ -540,29 +545,29 @@ class TransformControls extends Object3D { this.pointerUp(this.getPointer(event)) } - getMode = () => this._mode + getMode = (): TransformControls['_mode'] => this._mode - setMode = (mode: 'translate' | 'rotate' | 'scale') => { + setMode = (mode: TransformControls['_mode']): void => { this._mode = mode } - setTranslationSnap = (translationSnap: number) => { + setTranslationSnap = (translationSnap: number): void => { this._translationSnap = translationSnap } - setRotationSnap = (rotationSnap: number) => { + setRotationSnap = (rotationSnap: number): void => { this._rotationSnap = rotationSnap } - setScaleSnap = (scaleSnap: number) => { + setScaleSnap = (scaleSnap: number): void => { this._scaleSnap = scaleSnap } - setSize = (size: number) => { + setSize = (size: number): void => { this._size = size } - setSpace = (space: string) => { + setSpace = (space: string): void => { this._space = space } @@ -572,7 +577,7 @@ class TransformControls extends Object3D { ) } - public dispose = () => { + public dispose = (): void => { this.domElement.removeEventListener('pointerdown', this.onPointerDown) this.domElement.removeEventListener('pointermove', this.onPointerHover) this.domElement.ownerDocument?.removeEventListener('pointermove', this.onPointerMove) @@ -728,7 +733,7 @@ class TransformControlsGizmo extends Object3D { const lineGeometry = new BufferGeometry() lineGeometry.setAttribute('position', new Float32BufferAttribute([0, 0, 0, 1, 0, 0], 3)) - const CircleGeometry = (radius: number, arc: number) => { + const CircleGeometry = (radius: number, arc: number): BufferGeometry => { const geometry = new BufferGeometry() const vertices = [] @@ -743,7 +748,7 @@ class TransformControlsGizmo extends Object3D { // Special geometry for transform helper. If scaled with position vector it spans from [0,0,0] to position - const TranslateHelperGeometry = () => { + const TranslateHelperGeometry = (): BufferGeometry => { const geometry = new BufferGeometry() geometry.setAttribute('position', new Float32BufferAttribute([0, 0, 0, 1, 1, 1], 3)) @@ -918,7 +923,7 @@ class TransformControlsGizmo extends Object3D { // Creates an Object3D with gizmos described in custom hierarchy definition. // this is nearly impossible to Type so i'm leaving it - const setupGizmo = (gizmoMap: any) => { + const setupGizmo = (gizmoMap: any): Object3D => { const gizmo = new Object3D() for (let name in gizmoMap) { @@ -986,7 +991,7 @@ class TransformControlsGizmo extends Object3D { } // updateMatrixWorld will update transformations and appearance of individual handles - public updateMatrixWorld = () => { + public updateMatrixWorld = (): void => { let space = this._space if (this._mode === 'scale') space = 'local' // scale always oriented to local rotation @@ -1318,7 +1323,7 @@ class TransformControlsPlane extends Mesh { private _mode: 'translate' | 'rotate' | 'scale' = 'translate' private _space = 'world' - updateMatrixWorld = () => { + updateMatrixWorld = (): void => { let space = this._space this.position.copy(this._worldPosition) diff --git a/src/controls/experimental/CameraControls.ts b/src/controls/experimental/CameraControls.ts index a20d454d..bdeb0bcb 100644 --- a/src/controls/experimental/CameraControls.ts +++ b/src/controls/experimental/CameraControls.ts @@ -1,3 +1,4 @@ +/* eslint-disable no-unused-vars */ import { EventDispatcher, MOUSE, @@ -11,11 +12,11 @@ import { Vector3, } from 'three' -type CHANGE_EVENT = { +export type CHANGE_EVENT = { type: 'change' | 'start' | 'end' } -enum STATE { +export enum STATE { NONE = -1, ROTATE = 0, DOLLY = 1, @@ -212,18 +213,18 @@ class CameraControls extends EventDispatcher { this.saveState() } - getPolarAngle = () => this.spherical.phi + getPolarAngle = (): number => this.spherical.phi - getAzimuthalAngle = () => this.spherical.theta + getAzimuthalAngle = (): number => this.spherical.theta - saveState = () => { + saveState = (): void => { this.target0.copy(this.target) this.position0.copy(this.object.position) this.quaternion0.copy(this.object.quaternion) this.zoom0 = this.object.zoom } - reset = () => { + reset = (): void => { this.target.copy(this.target0) this.object.position.copy(this.position0) this.object.quaternion.copy(this.quaternion0) @@ -237,7 +238,7 @@ class CameraControls extends EventDispatcher { this.state = STATE.NONE } - dispose = () => { + dispose = (): void => { this.domElement.removeEventListener('contextmenu', this.onContextMenu, false) this.domElement.removeEventListener('mousedown', this.onMouseDown, false) this.domElement.removeEventListener('wheel', this.onMouseWheel, false) @@ -254,7 +255,7 @@ class CameraControls extends EventDispatcher { //this.dispatchEvent( { type: 'dispose' } ); // should this be added here? } - private update = () => { + private update = (): boolean => { const position = this.object.position this.offset.copy(position).sub(this.target) @@ -373,22 +374,26 @@ class CameraControls extends EventDispatcher { return false } - private getAutoRotationAngle = () => ((2 * Math.PI) / 60 / 60) * this.autoRotateSpeed + private getAutoRotationAngle = (): number => ((2 * Math.PI) / 60 / 60) * this.autoRotateSpeed - private getZoomScale = () => Math.pow(0.95, this.zoomSpeed) + private getZoomScale = (): number => Math.pow(0.95, this.zoomSpeed) - private rotateLeft = (angle: number) => (this.sphericalDelta.theta -= angle) + private rotateLeft = (angle: number): void => { + this.sphericalDelta.theta -= angle + } - private rotateUp = (angle: number) => (this.sphericalDelta.phi -= angle) + private rotateUp = (angle: number): void => { + this.sphericalDelta.phi -= angle + } - private panLeft = (distance: number, objectMatrix: Matrix4) => { + private panLeft = (distance: number, objectMatrix: Matrix4): void => { this.v.setFromMatrixColumn(objectMatrix, 0) // get X column of objectMatrix this.v.multiplyScalar(-distance) this.panOffset.add(this.v) } - private panUp = (distance: number, objectMatrix: Matrix4) => { + private panUp = (distance: number, objectMatrix: Matrix4): void => { if (this.screenSpacePanning === true) { this.v.setFromMatrixColumn(objectMatrix, 1) } else { @@ -402,7 +407,7 @@ class CameraControls extends EventDispatcher { } // deltaX and deltaY are in pixels; right and down are positive - private pan = (deltaX: number, deltaY: number) => { + private pan = (deltaX: number, deltaY: number): void => { const element = this.domElement if (this.object instanceof PerspectiveCamera) { @@ -434,7 +439,7 @@ class CameraControls extends EventDispatcher { } } - private dollyIn = (dollyScale: number) => { + private dollyIn = (dollyScale: number): void => { // TODO: replace w/.isPerspectiveCamera ? if (this.object instanceof PerspectiveCamera) { this.scale /= dollyScale @@ -449,7 +454,7 @@ class CameraControls extends EventDispatcher { } } - private dollyOut = (dollyScale: number) => { + private dollyOut = (dollyScale: number): void => { // TODO: replace w/.isPerspectiveCamera ? if (this.object instanceof PerspectiveCamera) { this.scale *= dollyScale @@ -466,20 +471,20 @@ class CameraControls extends EventDispatcher { // event callbacks - update the object state - private handleMouseDownRotate = (event: MouseEvent) => { + private handleMouseDownRotate = (event: MouseEvent): void => { this.rotateStart.set(event.clientX, event.clientY) } // TODO: confirm if worthwhile to return the Vector2 instead of void - private handleMouseDownDolly = (event: MouseEvent) => { + private handleMouseDownDolly = (event: MouseEvent): void => { this.dollyStart.set(event.clientX, event.clientY) } - private handleMouseDownPan = (event: MouseEvent) => { + private handleMouseDownPan = (event: MouseEvent): void => { this.panStart.set(event.clientX, event.clientY) } - private handleMouseMoveRotate = (event: MouseEvent) => { + private handleMouseMoveRotate = (event: MouseEvent): void => { this.rotateEnd.set(event.clientX, event.clientY) this.rotateDelta.subVectors(this.rotateEnd, this.rotateStart).multiplyScalar(this.rotateSpeed) @@ -495,7 +500,7 @@ class CameraControls extends EventDispatcher { this.update() } - private handleMouseMoveDolly = (event: MouseEvent) => { + private handleMouseMoveDolly = (event: MouseEvent): void => { this.dollyEnd.set(event.clientX, event.clientY) this.dollyDelta.subVectors(this.dollyEnd, this.dollyStart) @@ -511,7 +516,7 @@ class CameraControls extends EventDispatcher { this.update() } - private handleMouseMovePan = (event: MouseEvent) => { + private handleMouseMovePan = (event: MouseEvent): void => { this.panEnd.set(event.clientX, event.clientY) this.panDelta.subVectors(this.panEnd, this.panStart).multiplyScalar(this.panSpeed) @@ -523,11 +528,11 @@ class CameraControls extends EventDispatcher { this.update() } - private handleMouseUp(/*event*/) { + private handleMouseUp(/*event*/): void { // no-op } - private handleMouseWheel = (event: WheelEvent) => { + private handleMouseWheel = (event: WheelEvent): void => { if (event.deltaY < 0) { this.dollyOut(this.getZoomScale()) } else if (event.deltaY > 0) { @@ -537,7 +542,7 @@ class CameraControls extends EventDispatcher { this.update() } - private handleKeyDown = (event: KeyboardEvent) => { + private handleKeyDown = (event: KeyboardEvent): void => { let needsUpdate = false // TODO: keyCode deprecated? @@ -571,7 +576,7 @@ class CameraControls extends EventDispatcher { } } - private handleTouchStartRotate = (event: TouchEvent) => { + private handleTouchStartRotate = (event: TouchEvent): void => { if (event.touches.length == 1) { this.rotateStart.set(event.touches[0].pageX, event.touches[0].pageY) } else { @@ -582,7 +587,7 @@ class CameraControls extends EventDispatcher { } } - private handleTouchStartPan = (event: TouchEvent) => { + private handleTouchStartPan = (event: TouchEvent): void => { if (event.touches.length == 1) { this.panStart.set(event.touches[0].pageX, event.touches[0].pageY) } else { @@ -593,7 +598,7 @@ class CameraControls extends EventDispatcher { } } - private handleTouchStartDolly = (event: TouchEvent) => { + private handleTouchStartDolly = (event: TouchEvent): void => { const dx = event.touches[0].pageX - event.touches[1].pageX const dy = event.touches[0].pageY - event.touches[1].pageY @@ -602,19 +607,19 @@ class CameraControls extends EventDispatcher { this.dollyStart.set(0, distance) } - private handleTouchStartDollyPan = (event: TouchEvent) => { + private handleTouchStartDollyPan = (event: TouchEvent): void => { if (this.enableZoom) this.handleTouchStartDolly(event) if (this.enablePan) this.handleTouchStartPan(event) } - private handleTouchStartDollyRotate = (event: TouchEvent) => { + private handleTouchStartDollyRotate = (event: TouchEvent): void => { if (this.enableZoom) this.handleTouchStartDolly(event) if (this.enableRotate) this.handleTouchStartRotate(event) } - private handleTouchMoveRotate = (event: TouchEvent) => { + private handleTouchMoveRotate = (event: TouchEvent): void => { if (event.touches.length == 1) { this.rotateEnd.set(event.touches[0].pageX, event.touches[0].pageY) } else { @@ -635,7 +640,7 @@ class CameraControls extends EventDispatcher { this.rotateStart.copy(this.rotateEnd) } - private handleTouchMovePan = (event: TouchEvent) => { + private handleTouchMovePan = (event: TouchEvent): void => { if (event.touches.length == 1) { this.panEnd.set(event.touches[0].pageX, event.touches[0].pageY) } else { @@ -652,7 +657,7 @@ class CameraControls extends EventDispatcher { this.panStart.copy(this.panEnd) } - private handleTouchMoveDolly = (event: TouchEvent) => { + private handleTouchMoveDolly = (event: TouchEvent): void => { const dx = event.touches[0].pageX - event.touches[1].pageX const dy = event.touches[0].pageY - event.touches[1].pageY @@ -667,19 +672,19 @@ class CameraControls extends EventDispatcher { this.dollyStart.copy(this.dollyEnd) } - private handleTouchMoveDollyPan = (event: TouchEvent) => { + private handleTouchMoveDollyPan = (event: TouchEvent): void => { if (this.enableZoom) this.handleTouchMoveDolly(event) if (this.enablePan) this.handleTouchMovePan(event) } - private handleTouchMoveDollyRotate = (event: TouchEvent) => { + private handleTouchMoveDollyRotate = (event: TouchEvent): void => { if (this.enableZoom) this.handleTouchMoveDolly(event) if (this.enableRotate) this.handleTouchMoveRotate(event) } - private handleTouchEnd(/*event*/) { + private handleTouchEnd(/*event*/): void { // no-op } @@ -687,7 +692,7 @@ class CameraControls extends EventDispatcher { // event handlers - FSM: listen for events and reset state // - private onMouseDown = (event: MouseEvent) => { + private onMouseDown = (event: MouseEvent): void => { if (this.enabled === false) return // Prevent the browser from scrolling. @@ -774,7 +779,7 @@ class CameraControls extends EventDispatcher { } } - private onMouseMove = (event: MouseEvent) => { + private onMouseMove = (event: MouseEvent): void => { if (this.enabled === false) return event.preventDefault() @@ -803,7 +808,7 @@ class CameraControls extends EventDispatcher { } } - private onMouseUp = (event: MouseEvent) => { + private onMouseUp = (): void => { if (this.enabled === false) return // this.handleMouseUp() @@ -816,7 +821,7 @@ class CameraControls extends EventDispatcher { this.state = STATE.NONE } - private onMouseWheel = (event: WheelEvent) => { + private onMouseWheel = (event: WheelEvent): void => { if ( this.enabled === false || this.enableZoom === false || @@ -834,13 +839,13 @@ class CameraControls extends EventDispatcher { this.dispatchEvent(this.endEvent) } - private onKeyDown = (event: KeyboardEvent) => { + private onKeyDown = (event: KeyboardEvent): void => { if (this.enabled === false || this.enableKeys === false || this.enablePan === false) return this.handleKeyDown(event) } - private onTouchStart = (event: TouchEvent) => { + private onTouchStart = (event: TouchEvent): void => { if (this.enabled === false) return event.preventDefault() @@ -907,7 +912,7 @@ class CameraControls extends EventDispatcher { } } - private onTouchMove = (event: TouchEvent) => { + private onTouchMove = (event: TouchEvent): void => { if (this.enabled === false) return event.preventDefault() @@ -954,7 +959,7 @@ class CameraControls extends EventDispatcher { } } - private onTouchEnd = (event: TouchEvent) => { + private onTouchEnd = (): void => { if (this.enabled === false) return // this.handleTouchEnd() @@ -964,7 +969,7 @@ class CameraControls extends EventDispatcher { this.state = STATE.NONE } - private onContextMenu = (event: Event) => { + private onContextMenu = (event: Event): void => { if (this.enabled === false) return event.preventDefault() diff --git a/src/lines/LineSegments2.ts b/src/lines/LineSegments2.ts index cac19c68..65802bac 100644 --- a/src/lines/LineSegments2.ts +++ b/src/lines/LineSegments2.ts @@ -26,7 +26,7 @@ class LineSegments2 extends Mesh { private distStart = new Vector3() private distEnd = new Vector3() - computeLineDistances = () => { + computeLineDistances = (): this => { const geometry = this.geometry const instanceStart = geometry.attributes.instanceStart as InterleavedBufferAttribute @@ -58,7 +58,7 @@ class LineSegments2 extends Mesh { private line = new Line3() private closestPoint = new Vector3() - raycast = (raycaster: Raycaster, intersects: Array) => { + raycast = (raycaster: Raycaster, intersects: Array): void => { if (raycaster.camera === null) { console.error('LineSegments2: "Raycaster.camera" needs to be set in order to raycast against LineSegments2.') } diff --git a/src/lines/LineSegmentsGeometry.ts b/src/lines/LineSegmentsGeometry.ts index 8d004612..6cccc4c4 100644 --- a/src/lines/LineSegmentsGeometry.ts +++ b/src/lines/LineSegmentsGeometry.ts @@ -133,7 +133,7 @@ class LineSegmentsGeometry extends InstancedBufferGeometry { private box = new Box3() - computeBoundingBox = () => { + computeBoundingBox = (): void => { if (this.boundingBox === null) { this.boundingBox = new Box3() } @@ -152,7 +152,7 @@ class LineSegmentsGeometry extends InstancedBufferGeometry { private vector = new Vector3() - computeBoundingSphere = () => { + computeBoundingSphere = (): void => { if (this.boundingSphere === null) { this.boundingSphere = new Sphere() } @@ -192,11 +192,11 @@ class LineSegmentsGeometry extends InstancedBufferGeometry { } } - toJSON = () => { + toJSON = (): void => { // todo } - applyMatrix = (matrix: Matrix4) => { + applyMatrix = (matrix: Matrix4): this => { console.warn('THREE.LineSegmentsGeometry: applyMatrix() has been renamed to applyMatrix4().') return this.applyMatrix4(matrix) diff --git a/src/math/SimplexNoise.ts b/src/math/SimplexNoise.ts index 32f5de8b..5d67343c 100644 --- a/src/math/SimplexNoise.ts +++ b/src/math/SimplexNoise.ts @@ -157,11 +157,11 @@ export class SimplexNoise { return g[0] * x + g[1] * y + g[2] * z } - dot4 = (g: number[], x: number, y: number, z: number, w: number) => { + dot4 = (g: number[], x: number, y: number, z: number, w: number): number => { return g[0] * x + g[1] * y + g[2] * z + g[3] * w } - noise = (xin: number, yin: number) => { + noise = (xin: number, yin: number): number => { let n0 let n1 let n2 // Noise contributions from the three corners diff --git a/src/misc/WebGL.ts b/src/misc/WebGL.ts index d333156a..07271815 100644 --- a/src/misc/WebGL.ts +++ b/src/misc/WebGL.ts @@ -1,5 +1,5 @@ export const WEBGL = { - isWebGLAvailable: function () { + isWebGLAvailable: function (): boolean { try { const canvas = document.createElement('canvas') return !!(window.WebGLRenderingContext && (canvas.getContext('webgl') || canvas.getContext('experimental-webgl'))) @@ -8,7 +8,7 @@ export const WEBGL = { } }, - isWebGL2Available: function () { + isWebGL2Available: function (): boolean { try { const canvas = document.createElement('canvas') return !!(window.WebGL2RenderingContext && canvas.getContext('webgl2')) @@ -17,15 +17,15 @@ export const WEBGL = { } }, - getWebGLErrorMessage: function () { + getWebGLErrorMessage: function (): HTMLDivElement { return this.getErrorMessage(1) }, - getWebGL2ErrorMessage: function () { + getWebGL2ErrorMessage: function (): HTMLDivElement { return this.getErrorMessage(2) }, - getErrorMessage: function (version: 1 | 2) { + getErrorMessage: function (version: 1 | 2): HTMLDivElement { const names = { 1: 'WebGL', 2: 'WebGL 2', diff --git a/src/modifiers/CurveModifier.ts b/src/modifiers/CurveModifier.ts index 7b872308..dfb3a73e 100644 --- a/src/modifiers/CurveModifier.ts +++ b/src/modifiers/CurveModifier.ts @@ -116,11 +116,11 @@ export function modifyShader( material: ModifiedMaterial, uniforms: CurveModifierUniforms, numberOfCurves = 1, -) { +): void { if (material.__ok) return material.__ok = true - material.onBeforeCompile = (shader: Shader & { __modified: boolean }) => { + material.onBeforeCompile = (shader: Shader & { __modified: boolean }): void => { if (shader.__modified) return shader.__modified = true diff --git a/src/modifiers/EdgeSplitModifier.ts b/src/modifiers/EdgeSplitModifier.ts index 0de0b8e7..be96a038 100644 --- a/src/modifiers/EdgeSplitModifier.ts +++ b/src/modifiers/EdgeSplitModifier.ts @@ -95,7 +95,7 @@ class EdgeSplitModifier { return result } - private edgeSplit = (indexes: number[], cutOff: number, original: number | null = null) => { + private edgeSplit = (indexes: number[], cutOff: number, original: number | null = null): void => { if (indexes.length === 0) return const groupResults: EdgeSplitToGroupsResult[] = [] @@ -124,7 +124,7 @@ class EdgeSplitModifier { } } - public modify = (geometry: BufferGeometry, cutOffAngle: number, tryKeepNormals = true) => { + public modify = (geometry: BufferGeometry, cutOffAngle: number, tryKeepNormals = true): BufferGeometry => { let hadNormals = false if (geometry.attributes.normal) { diff --git a/src/modifiers/SimplifyModifier.ts b/src/modifiers/SimplifyModifier.ts index 73f56c51..6731f791 100644 --- a/src/modifiers/SimplifyModifier.ts +++ b/src/modifiers/SimplifyModifier.ts @@ -4,11 +4,11 @@ import * as BufferGeometryUtils from '../utils/BufferGeometryUtils' const cb = new Vector3(), ab = new Vector3() -function pushIfUnique(array: TItem[], object: TItem) { +function pushIfUnique(array: TItem[], object: TItem): void { if (array.indexOf(object) === -1) array.push(object) } -function removeFromArray(array: TItem[], object: TItem) { +function removeFromArray(array: TItem[], object: TItem): void { const k = array.indexOf(object) if (k > -1) array.splice(k, 1) } @@ -150,7 +150,7 @@ class Triangle { class SimplifyModifier { constructor() {} - private computeEdgeCollapseCost = (u: Vertex, v: Vertex) => { + private computeEdgeCollapseCost = (u: Vertex, v: Vertex): number => { // if we collapse edge uv by moving u to v then how // much different will the model change, i.e. the "error". @@ -202,7 +202,7 @@ class SimplifyModifier { return amt } - private removeVertex(v: Vertex, vertices: Vertex[]) { + private removeVertex(v: Vertex, vertices: Vertex[]): void { console.assert(v.faces.length === 0) while (v.neighbors.length) { @@ -213,7 +213,7 @@ class SimplifyModifier { removeFromArray(vertices, v) } - private computeEdgeCostAtVertex = (v: Vertex) => { + private computeEdgeCostAtVertex = (v: Vertex): void => { // compute the edge collapse cost for all edges that start // from vertex v. Since we are only interested in reducing // the object by selecting the min cost edge at each step, we @@ -258,7 +258,7 @@ class SimplifyModifier { // v.collapseCost = v.minCost; } - private removeFace = (f: Triangle, faces: Triangle[]) => { + private removeFace = (f: Triangle, faces: Triangle[]): void => { removeFromArray(faces, f) if (f.v1) removeFromArray(f.v1.faces, f) @@ -280,7 +280,7 @@ class SimplifyModifier { } } - private collapse = (vertices: Vertex[], faces: Triangle[], u: Vertex, v: Vertex) => { + private collapse = (vertices: Vertex[], faces: Triangle[], u: Vertex, v: Vertex): void => { // u and v are pointers to vertices of an edge // Collapse the edge uv by moving vertex u onto v @@ -318,7 +318,7 @@ class SimplifyModifier { } } - private minimumCostEdge = (vertices: Vertex[]) => { + private minimumCostEdge = (vertices: Vertex[]): Vertex => { // O(n * n) approach. TODO optimize this let least = vertices[0] @@ -332,7 +332,7 @@ class SimplifyModifier { return least } - modify = (geometry: BufferGeometry, count: number) => { + modify = (geometry: BufferGeometry, count: number): BufferGeometry => { geometry = geometry.clone() const attributes = geometry.attributes diff --git a/src/modifiers/TessellateModifier.ts b/src/modifiers/TessellateModifier.ts index e4f8f4e9..02367d58 100644 --- a/src/modifiers/TessellateModifier.ts +++ b/src/modifiers/TessellateModifier.ts @@ -74,7 +74,7 @@ class TessellateModifier { let iteration = 0 let tessellating = true - function addTriangle(a: number, b: number, c: number) { + function addTriangle(a: number, b: number, c: number): void { const v1 = vs[a] const v2 = vs[b] const v3 = vs[c] diff --git a/src/types/helpers.ts b/src/types/helpers.ts index 648658ec..aa268e17 100644 --- a/src/types/helpers.ts +++ b/src/types/helpers.ts @@ -1 +1 @@ -export const getWithKey = (obj: T, key: K) => obj[key] +export const getWithKey = (obj: T, key: K): T[K] => obj[key] diff --git a/src/utils/BufferGeometryUtils.ts b/src/utils/BufferGeometryUtils.ts index 35101f46..2efd9c32 100644 --- a/src/utils/BufferGeometryUtils.ts +++ b/src/utils/BufferGeometryUtils.ts @@ -598,7 +598,7 @@ export function computeMorphedAttributes(object: Mesh | Line | Points): Computed b: number, c: number, modifiedAttributeArray: Float32Array, - ) { + ): void { _vA.fromBufferAttribute(attribute, a) _vB.fromBufferAttribute(attribute, b) _vC.fromBufferAttribute(attribute, c) diff --git a/yarn.lock b/yarn.lock index 1d85829d..cdd41d46 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2338,13 +2338,13 @@ dependencies: "@types/yargs-parser" "*" -"@typescript-eslint/eslint-plugin@^4.15.2": - version "4.15.2" - resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-4.15.2.tgz#981b26b4076c62a5a55873fbef3fe98f83360c61" - integrity sha512-uiQQeu9tWl3f1+oK0yoAv9lt/KXO24iafxgQTkIYO/kitruILGx3uH+QtIAHqxFV+yIsdnJH+alel9KuE3J15Q== +"@typescript-eslint/eslint-plugin@^4.18.0": + version "4.18.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-4.18.0.tgz#50fbce93211b5b690895d20ebec6fe8db48af1f6" + integrity sha512-Lzkc/2+7EoH7+NjIWLS2lVuKKqbEmJhtXe3rmfA8cyiKnZm3IfLf51irnBcmow8Q/AptVV0XBZmBJKuUJTe6cQ== dependencies: - "@typescript-eslint/experimental-utils" "4.15.2" - "@typescript-eslint/scope-manager" "4.15.2" + "@typescript-eslint/experimental-utils" "4.18.0" + "@typescript-eslint/scope-manager" "4.18.0" debug "^4.1.1" functional-red-black-tree "^1.0.1" lodash "^4.17.15" @@ -2352,15 +2352,15 @@ semver "^7.3.2" tsutils "^3.17.1" -"@typescript-eslint/experimental-utils@4.15.2": - version "4.15.2" - resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-4.15.2.tgz#5efd12355bd5b535e1831282e6cf465b9a71cf36" - integrity sha512-Fxoshw8+R5X3/Vmqwsjc8nRO/7iTysRtDqx6rlfLZ7HbT8TZhPeQqbPjTyk2RheH3L8afumecTQnUc9EeXxohQ== +"@typescript-eslint/experimental-utils@4.18.0": + version "4.18.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-4.18.0.tgz#ed6c955b940334132b17100d2917449b99a91314" + integrity sha512-92h723Kblt9JcT2RRY3QS2xefFKar4ZQFVs3GityOKWQYgtajxt/tuXIzL7sVCUlM1hgreiV5gkGYyBpdOwO6A== dependencies: "@types/json-schema" "^7.0.3" - "@typescript-eslint/scope-manager" "4.15.2" - "@typescript-eslint/types" "4.15.2" - "@typescript-eslint/typescript-estree" "4.15.2" + "@typescript-eslint/scope-manager" "4.18.0" + "@typescript-eslint/types" "4.18.0" + "@typescript-eslint/typescript-estree" "4.18.0" eslint-scope "^5.0.0" eslint-utils "^2.0.0" @@ -2374,28 +2374,28 @@ eslint-scope "^5.0.0" eslint-utils "^2.0.0" -"@typescript-eslint/parser@^4.15.2": - version "4.15.2" - resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-4.15.2.tgz#c804474321ef76a3955aec03664808f0d6e7872e" - integrity sha512-SHeF8xbsC6z2FKXsaTb1tBCf0QZsjJ94H6Bo51Y1aVEZ4XAefaw5ZAilMoDPlGghe+qtq7XdTiDlGfVTOmvA+Q== +"@typescript-eslint/parser@^4.18.0": + version "4.18.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-4.18.0.tgz#a211edb14a69fc5177054bec04c95b185b4dde21" + integrity sha512-W3z5S0ZbecwX3PhJEAnq4mnjK5JJXvXUDBYIYGoweCyWyuvAKfGHvzmpUzgB5L4cRBb+cTu9U/ro66dx7dIimA== dependencies: - "@typescript-eslint/scope-manager" "4.15.2" - "@typescript-eslint/types" "4.15.2" - "@typescript-eslint/typescript-estree" "4.15.2" + "@typescript-eslint/scope-manager" "4.18.0" + "@typescript-eslint/types" "4.18.0" + "@typescript-eslint/typescript-estree" "4.18.0" debug "^4.1.1" -"@typescript-eslint/scope-manager@4.15.2": - version "4.15.2" - resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-4.15.2.tgz#5725bda656995960ae1d004bfd1cd70320f37f4f" - integrity sha512-Zm0tf/MSKuX6aeJmuXexgdVyxT9/oJJhaCkijv0DvJVT3ui4zY6XYd6iwIo/8GEZGy43cd7w1rFMiCLHbRzAPQ== +"@typescript-eslint/scope-manager@4.18.0": + version "4.18.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-4.18.0.tgz#d75b55234c35d2ff6ac945758d6d9e53be84a427" + integrity sha512-olX4yN6rvHR2eyFOcb6E4vmhDPsfdMyfQ3qR+oQNkAv8emKKlfxTWUXU5Mqxs2Fwe3Pf1BoPvrwZtwngxDzYzQ== dependencies: - "@typescript-eslint/types" "4.15.2" - "@typescript-eslint/visitor-keys" "4.15.2" + "@typescript-eslint/types" "4.18.0" + "@typescript-eslint/visitor-keys" "4.18.0" -"@typescript-eslint/types@4.15.2": - version "4.15.2" - resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.15.2.tgz#04acf3a2dc8001a88985291744241e732ef22c60" - integrity sha512-r7lW7HFkAarfUylJ2tKndyO9njwSyoy6cpfDKWPX6/ctZA+QyaYscAHXVAfJqtnY6aaTwDYrOhp+ginlbc7HfQ== +"@typescript-eslint/types@4.18.0": + version "4.18.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.18.0.tgz#bebe323f81f2a7e2e320fac9415e60856267584a" + integrity sha512-/BRociARpj5E+9yQ7cwCF/SNOWwXJ3qhjurMuK2hIFUbr9vTuDeu476Zpu+ptxY2kSxUHDGLLKy+qGq2sOg37A== "@typescript-eslint/typescript-estree@2.34.0": version "2.34.0" @@ -2410,25 +2410,25 @@ semver "^7.3.2" tsutils "^3.17.1" -"@typescript-eslint/typescript-estree@4.15.2": - version "4.15.2" - resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-4.15.2.tgz#c2f7a1e94f3428d229d5ecff3ead6581ee9b62fa" - integrity sha512-cGR8C2g5SPtHTQvAymEODeqx90pJHadWsgTtx6GbnTWKqsg7yp6Eaya9nFzUd4KrKhxdYTTFBiYeTPQaz/l8bw== +"@typescript-eslint/typescript-estree@4.18.0": + version "4.18.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-4.18.0.tgz#756d3e61da8c16ab99185532c44872f4cd5538cb" + integrity sha512-wt4xvF6vvJI7epz+rEqxmoNQ4ZADArGQO9gDU+cM0U5fdVv7N+IAuVoVAoZSOZxzGHBfvE3XQMLdy+scsqFfeg== dependencies: - "@typescript-eslint/types" "4.15.2" - "@typescript-eslint/visitor-keys" "4.15.2" + "@typescript-eslint/types" "4.18.0" + "@typescript-eslint/visitor-keys" "4.18.0" debug "^4.1.1" globby "^11.0.1" is-glob "^4.0.1" semver "^7.3.2" tsutils "^3.17.1" -"@typescript-eslint/visitor-keys@4.15.2": - version "4.15.2" - resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-4.15.2.tgz#3d1c7979ce75bf6acf9691109bd0d6b5706192b9" - integrity sha512-TME1VgSb7wTwgENN5KVj4Nqg25hP8DisXxNBojM4Nn31rYaNDIocNm5cmjOFfh42n7NVERxWrDFoETO/76ePyg== +"@typescript-eslint/visitor-keys@4.18.0": + version "4.18.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-4.18.0.tgz#4e6fe2a175ee33418318a029610845a81e2ff7b6" + integrity sha512-Q9t90JCvfYaN0OfFUgaLqByOfz8yPeTAdotn/XYNm5q9eHax90gzdb+RJ6E9T5s97Kv/UHWKERTmqA0jTKAEHw== dependencies: - "@typescript-eslint/types" "4.15.2" + "@typescript-eslint/types" "4.18.0" eslint-visitor-keys "^2.0.0" "@webassemblyjs/ast@1.9.0":