html调用手机陀螺仪,前端基于THREE.js的3D全景,支持鼠标控制和手机陀螺仪的切换...

/p>

‘use strict’

;(function(_window) {

var navigatorUserAgent = navigator.userAgent

var iPhone = navigatorUserAgent.indexOf(‘iPhone’)

if (iPhone > -1) {

var dpr = Number(window.devicePixelRatio),

one_dpr = 1 / dpr

} else {

var dpr = 1,

one_dpr = 1

}

var writeText =

one_dpr +

‘,maximum-scale=’ +

one_dpr +

‘,minimum-scale=’ +

one_dpr +

‘,user-scalable=no”>\n

dpr +

‘”>’

document.write(writeText)

var

var F0 = 75

var getFontSize = function getFontSize() {

var windowWidth = window.innerWidth

}

getFontSize()

_window.addEventListener(‘resize’, getFontSize, false)

})(window)

body {

margin: 0;

}

html,

body,

#CanvasBody {

width: 100%;

height: 100%;

overflow: hidden;

}

#CanvasBody {

position: relative;

}

#onDevice {

position: absolute;

bottom: 0;

right: 0;

z-index: 2;

padding: 12px 18px;

font-size: 20px;

border: 1px solid #ddd;

background-color: #fff;

cursor: pointer;

-webkit-appearance: none;

}

THREE.OrbitControls = function(object, domElement) {

this.object = object

this.domElement =

domElement !== undefined ? domElement : document

this.enabled = true

this.target = new THREE.Vector3()

this.minDistance = 0

this.maxDistance = Infinity

this.minZoom = 0

this.maxZoom = Infinity

this.minPolarAngle = 0

this.maxPolarAngle = Math.PI

this.minAzimuthAngle = -Infinity

this.maxAzimuthAngle = Infinity

this.enableDamping = false

this.dampingFactor = 0.25

this.enableZoom = true

this.zoomSpeed = 1.0

this.enableRotate = true

this.rotateSpeed = 1.0

this.enablePan = true

this.keyPanSpeed = 7.0

this.autoRotate = false

this.autoRotateSpeed = 2.0

this.enableKeys = true

this.keys = { LEFT: 37, UP: 38, RIGHT: 39, BOTTOM: 40 }

this.mouseButtons = {

ORBIT: THREE.MOUSE.LEFT,

ZOOM: THREE.MOUSE.MIDDLE,

PAN: THREE.MOUSE.RIGHT,

}

this.target0 = this.target.clone()

this.position0 = this.object.position.clone()

this.zoom0 = this.object.zoom

this.getPolarAngle = function() {

return phi

}

this.getAzimuthalAngle = function() {

return theta

}

this.reset = function() {

scope.target.copy(scope.target0)

scope.object.position.copy(scope.position0)

scope.object.zoom = scope.zoom0

scope.object.updateProjectionMatrix()

scope.dispatchEvent(changeEvent)

scope.update()

state = STATE.NONE

}

this.update = (function() {

var offset = new THREE.Vector3()

var quat = new THREE.Quaternion().setFromUnitVectors(

object.up,

new THREE.Vector3(0, 1, 0),

)

var quatInverse = quat.clone().inverse()

var lastPosition = new THREE.Vector3()

var lastQuaternion = new THREE.Quaternion()

return function() {

var position = scope.object.position

offset.copy(position).sub(scope.target)

offset.applyQuaternion(quat)

spherical.setFromVector3(offset)

if (scope.autoRotate && state === STATE.NONE) {

rotateLeft(getAutoRotationAngle())

}

spherical.theta += sphericalDelta.theta

spherical.phi += sphericalDelta.phi

spherical.theta = Math.max(

scope.minAzimuthAngle,

Math.min(scope.maxAzimuthAngle, spherical.theta),

)

spherical.phi = Math.max(

scope.minPolarAngle,

Math.min(scope.maxPolarAngle, spherical.phi),

)

spherical.makeSafe()

spherical.radius *= scale

spherical.radius = Math.max(

scope.minDistance,

Math.min(scope.maxDistance, spherical.radius),

)

scope.target.add(panOffset)

offset.setFromSpherical(spherical)

offset.applyQuaternion(quatInverse)

position.copy(scope.target).add(offset)

scope.object.lookAt(scope.target)

if (scope.enableDamping === true) {

sphericalDelta.theta *= 1 – scope.dampingFactor

sphericalDelta.phi *= 1 – scope.dampingFactor

} else {

sphericalDelta.set(0, 0, 0)

}

scale = 1

panOffset.set(0, 0, 0)

if (

zoomChanged ||

lastPosition.distanceToSquared(

scope.object.position,

) > EPS ||

8 *

(1 –

lastQuaternion.dot(

scope.object.quaternion,

)) >

EPS

) {

scope.dispatchEvent(changeEvent)

lastPosition.copy(scope.object.position)

lastQuaternion.copy(scope.object.quaternion)

zoomChanged = false

return true

}

return false

}

})()

this.dispose = function() {

scope.domElement.removeEventListener(

‘contextmenu’,

onContextMenu,

false,

)

scope.domElement.removeEventListener(

‘mousedown’,

onMouseDown,

false,

)

scope.domElement.removeEventListener(

‘mousewheel’,

onMouseWheel,

false,

)

scope.domElement.removeEventListener(

‘MozMousePixelScroll’,

onMouseWheel,

false,

)

scope.domElement.removeEventListener(

‘touchstart’,

onTouchStart,

false,

)

scope.domElement.removeEventListener(

‘touchend’,

onTouchEnd,

false,

)

scope.domElement.removeEventListener(

‘touchmove’,

onTouchMove,

false

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值