Skip to content

Commit

Permalink
feat: turn utils to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
WoodNeck committed Nov 9, 2020
1 parent 62a3b11 commit e34ca66
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
File renamed without changes.
10 changes: 6 additions & 4 deletions src/utils/browserFeature.js → src/utils/browserFeature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) 2015 NAVER Corp.
* egjs projects are licensed under the MIT license
*/
import {window as win, document as doc} from "./browser";
import { window as win, document as doc } from "./browser";

win.Float32Array = (typeof win.Float32Array !== "undefined") ? win.Float32Array : win.Array;

Expand All @@ -14,7 +14,7 @@ const SUPPORT_DEVICEMOTION = "ondevicemotion" in win;
const DeviceMotionEvent = win.DeviceMotionEvent;
const devicePixelRatio = win.devicePixelRatio;

const TRANSFORM = (function() {
const TRANSFORM = (() => {
const docStyle = doc.documentElement.style;
const target = ["transform", "webkitTransform", "msTransform", "mozTransform"];

Expand All @@ -33,18 +33,20 @@ const SUPPORT_WILLCHANGE = win.CSS && win.CSS.supports &&
let WEBXR_SUPPORTED = false;

const checkXRSupport = () => {
const navigator = window.navigator as any;

if (!navigator.xr) {
return;
}

if (navigator.xr.isSessionSupported) {
navigator.xr.isSessionSupported("immersive-vr").then(res => {
WEBXR_SUPPORTED = res;
}).catch(() => {});
}).catch(() => {}); // tslint:disable-line no-empty
} else if (navigator.xr.supportsSession) {
navigator.xr.supportsSession("immersive-vr").then(res => {
WEBXR_SUPPORTED = res;
}).catch(() => {});
}).catch(() => {}); // tslint:disable-line no-empty
}
}

Expand Down
26 changes: 14 additions & 12 deletions src/utils/math-util.js → src/utils/math-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,36 @@ THE SOFTWARE. */
// Some minimal math functionality borrowed from gl-Matrix and stripped down
// for the purposes of this library.

import {vec2, vec3, quat} from "gl-matrix";
import { vec2, vec3, quat } from "gl-matrix";

function quatToVec3(quaternion) {
function quatToVec3(quaternion: quat) {
const baseV = vec3.fromValues(0, 0, 1);

vec3.transformQuat(baseV, baseV, quaternion);
return baseV;
}

function toDegree(a){
function toDegree(a: number) {
return a * 180 / Math.PI;
}

const util = {};
const util: any = {};

util.isPowerOfTwo = function(n) {
util.isPowerOfTwo = (n: number) => {
return n && (n & (n - 1)) === 0;
};

util.extractPitchFromQuat = function(quaternion) {
util.extractPitchFromQuat = (quaternion: quat) => {
const baseV = quatToVec3(quaternion);

return -1 * Math.atan2(
baseV[1],
Math.sqrt(Math.pow(baseV[0], 2) + Math.pow(baseV[2], 2)));
};

util.hypot = Math.hypot || function(x, y) {
util.hypot = Math.hypot || ((x: number, y : number) => {
return Math.sqrt(x * x + y * y);
};
});

// implement reference
// the general equation of a plane : http://www.gisdeveloper.co.kr/entry/평면의-공식
Expand Down Expand Up @@ -134,7 +134,7 @@ function getRotationDelta(prevQ, curQ, rotateKind) {
const coefficientA = vecN[0];
const coefficientB = vecN[1];
const coefficientC = vecN[2];
// const coefficientD = -1 * vec3.dot(vecN, meshPoint1);
// const coefficientD = -1 * vec3.dot(vecN, meshPoint1);

// a point on the plane
curPoint = vec3.fromValues(meshPoint[0], meshPoint[1], meshPoint[2]);
Expand Down Expand Up @@ -162,7 +162,9 @@ function getRotationDelta(prevQ, curQ, rotateKind) {
(vec3.length(projectedPrevPoint) * vec3.length(curPoint));

// defensive block
trigonometricRatio > 1 && (trigonometricRatio = 1);
if (trigonometricRatio > 1) {
trigonometricRatio = 1;
}

const theta = Math.acos(trigonometricRatio);

Expand All @@ -186,13 +188,13 @@ function getRotationDelta(prevQ, curQ, rotateKind) {
return toDegree(deltaRadian);
}

function angleBetweenVec2(v1, v2) {
function angleBetweenVec2(v1: vec2, v2: vec2) {
const det = v1[0] * v2[1] - v2[0] * v1[1];
const theta = -Math.atan2(det, vec2.dot(v1, v2));
return theta;
}

util.yawOffsetBetween = function(viewDir, targetDir) {
util.yawOffsetBetween = (viewDir: number, targetDir: number) => {
const viewDirXZ = vec2.fromValues(viewDir[0], viewDir[2]);
const targetDirXZ = vec2.fromValues(targetDir[0], targetDir[2]);

Expand Down
File renamed without changes.

0 comments on commit e34ca66

Please sign in to comment.