Skip to content

Commit

Permalink
Fix onChange types (#43)
Browse files Browse the repository at this point in the history
* fix: Fix onChange type change from #42

* Change version to 1.0.12
  • Loading branch information
TiagoCavalcante committed Apr 3, 2024
1 parent c40c2fb commit 0cfb50c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
7 changes: 5 additions & 2 deletions lib/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ var STATE = {
var partialScope = {
camera: undefined,
enabled: true,
target: new three.Vector3(),
// We will override this later. A new vector ins't created here because it
// could cause problems when there is more than one controls on the screen
// (which could share the same `target` object, if we created it here).
target: undefined,
minZoom: 0,
maxZoom: Infinity,
// How far you can orbit vertically, upper and lower limits.
Expand All @@ -82,7 +85,7 @@ var partialScope = {
};
function createControls() {
var height = 0;
var scope = __assign(__assign({}, partialScope), { onChange: function (event) { } });
var scope = __assign(__assign({}, partialScope), { target: new three.Vector3(), onChange: function (event) { } });
var internals = {
moveStart: new three.Vector3(),
rotateStart: new three.Vector2(),
Expand Down
4 changes: 2 additions & 2 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as react_native from "react-native"
import { GestureResponderEvent, LayoutChangeEvent } from "react-native"
import React from "react"
import { PerspectiveCamera, OrthographicCamera, Vector3, Matrix4 } from "three"
import { Vector3, PerspectiveCamera, OrthographicCamera, Matrix4 } from "three"

declare const partialScope: {
camera: PerspectiveCamera | OrthographicCamera | undefined
Expand All @@ -24,10 +24,10 @@ declare const partialScope: {
}
declare function createControls(): {
scope: {
target: Vector3
onChange: (event: { target: typeof partialScope }) => void
camera: PerspectiveCamera | OrthographicCamera | undefined
enabled: boolean
target: Vector3
minZoom: number
maxZoom: number
minPolarAngle: number
Expand Down
7 changes: 5 additions & 2 deletions lib/index.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ var STATE = {
var partialScope = {
camera: undefined,
enabled: true,
target: new Vector3(),
// We will override this later. A new vector ins't created here because it
// could cause problems when there is more than one controls on the screen
// (which could share the same `target` object, if we created it here).
target: undefined,
minZoom: 0,
maxZoom: Infinity,
// How far you can orbit vertically, upper and lower limits.
Expand All @@ -80,7 +83,7 @@ var partialScope = {
};
function createControls() {
var height = 0;
var scope = __assign(__assign({}, partialScope), { onChange: function (event) { } });
var scope = __assign(__assign({}, partialScope), { target: new Vector3(), onChange: function (event) { } });
var internals = {
moveStart: new Vector3(),
rotateStart: new Vector2(),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "r3f-native-orbitcontrols",
"version": "1.0.11",
"version": "1.0.12",
"description": "OrbitControls for React Three Fiber in React Native",
"type": "module",
"types": "./lib/index.d.ts",
Expand Down
29 changes: 17 additions & 12 deletions src/OrbitControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ const partialScope = {

enabled: true,

// We will override this later. A new vector ins't created here because it
// could cause problems when there is more than one controls on the screen
// (which could share the same `target` object, if we created it here).
target: undefined as unknown as Vector3,

minZoom: 0,
maxZoom: Infinity,

Expand Down Expand Up @@ -148,7 +153,7 @@ export function createControls() {
if (event.nativeEvent.touches.length === 1) {
internals.rotateStart.set(
event.nativeEvent.touches[0].locationX,
event.nativeEvent.touches[0].locationY
event.nativeEvent.touches[0].locationY,
)
} else if (event.nativeEvent.touches.length === 2) {
const x =
Expand Down Expand Up @@ -183,7 +188,7 @@ export function createControls() {
if (event.nativeEvent.touches.length === 1) {
internals.panStart.set(
event.nativeEvent.touches[0].locationX,
event.nativeEvent.touches[0].locationY
event.nativeEvent.touches[0].locationY,
)
} else if (event.nativeEvent.touches.length === 2) {
const x =
Expand Down Expand Up @@ -237,7 +242,7 @@ export function createControls() {
if (event.nativeEvent.touches.length === 1) {
internals.rotateEnd.set(
event.nativeEvent.locationX,
event.nativeEvent.locationY
event.nativeEvent.locationY,
)
} else if (event.nativeEvent.touches.length === 2) {
const x =
Expand Down Expand Up @@ -282,7 +287,7 @@ export function createControls() {

internals.dollyEnd = distance
this.dollyOut(
Math.pow(internals.dollyEnd / internals.dollyStart, scope.zoomSpeed)
Math.pow(internals.dollyEnd / internals.dollyStart, scope.zoomSpeed),
)
internals.dollyStart = internals.dollyEnd
}
Expand Down Expand Up @@ -331,7 +336,7 @@ export function createControls() {
// we use only height here so aspect ratio does not distort speed
this.panLeft(
(2 * deltaX * targetDistance) / height,
scope.camera.matrix
scope.camera.matrix,
)
this.panUp((2 * deltaY * targetDistance) / height, scope.camera.matrix)
}
Expand All @@ -341,7 +346,7 @@ export function createControls() {
if (event.nativeEvent.touches.length === 1) {
internals.panEnd.set(
event.nativeEvent.locationX,
event.nativeEvent.locationY
event.nativeEvent.locationY,
)
} else if (event.nativeEvent.touches.length === 2) {
const x =
Expand Down Expand Up @@ -405,7 +410,7 @@ export function createControls() {
// so camera.up is the orbit axis
const quat = new Quaternion().setFromUnitVectors(
scope.camera.up,
new Vector3(0, 1, 0)
new Vector3(0, 1, 0),
)
const quatInverse = quat.clone().invert()

Expand Down Expand Up @@ -437,7 +442,7 @@ export function createControls() {
if (min <= max) {
internals.spherical.theta = Math.max(
min,
Math.min(max, internals.spherical.theta)
Math.min(max, internals.spherical.theta),
)
} else {
internals.spherical.theta =
Expand All @@ -450,7 +455,7 @@ export function createControls() {
// restrict phi to be between desired limits
internals.spherical.phi = Math.max(
scope.minPolarAngle + EPSILON,
Math.min(scope.maxPolarAngle - EPSILON, internals.spherical.phi)
Math.min(scope.maxPolarAngle - EPSILON, internals.spherical.phi),
)

if ((scope.camera as PerspectiveCamera).isPerspectiveCamera) {
Expand All @@ -459,17 +464,17 @@ export function createControls() {
scope.camera.zoom = Math.max(
Math.min(
scope.camera.zoom / (internals.scale * scope.zoomSpeed),
scope.maxZoom
scope.maxZoom,
),
scope.minZoom
scope.minZoom,
)
scope.camera.updateProjectionMatrix()
}

// restrict radius to be between desired limits
internals.spherical.radius = Math.max(
scope.minZoom,
Math.min(scope.maxZoom, internals.spherical.radius)
Math.min(scope.maxZoom, internals.spherical.radius),
)

// move target to panned location
Expand Down

0 comments on commit 0cfb50c

Please sign in to comment.