基于three.js编写的一个项目类示例代码,three.js示例代码

by admin on 2019年9月3日

复制代码 代码如下:

复制代码 代码如下:

复制代码 代码如下:

遵照three.js编写的贰个连串类示例代码,three.js示例代码

WebVR

在始发在此之前,先对WebV奇骏举办介绍,WebVXC90是三个实验性的Javascript
API,允许HMD(head-mounted displays)连接到web
apps,同不经常候基本上能用那么些设备的地方和动作音信。这让使用Javascript开拓V中华V应用成为恐怕(当然已经有为数相当多接口API让Javascript作为开垦语言了,可是那并不影响大家为WebV陆风X8以为兴奋)。而让大家能够立时打开预览与体验,移动设备上的chrome已经支撑了WebV奥迪Q7并使手提式有线话机作为多个简练的HMD。手提式有线电话机能够把显示器分成左右眼视觉并利用手机中的加速度计、陀螺仪等感应器,你需求做的或许就只是买多个cardboard。上边话十分的少说了,来共同看看本文的正文:

这是一篇关于什么基于three.js进行可陈设的three.js的靶子创制的小说

体系地址

编辑叁个three.js的基类

那是创设的三个Three.js基类在那之中带有了意况,相机,渲染器,调整器以及部分情势

 // VRcore.js
 import * as THREE from 'three';
 const OrbitControls = require('three-orbit-controls')(THREE)
 let Scene, Camera, Renderer, Controls, loopID;

 function createScene({domContainer = document.body, fov = 50,far = 1000}){
 if (!(domContainer instanceof HTMLElement)) {
  throw new Error('domContainer is not a HTMLElement!');
 }
 // 初始化 scene
 Scene = new THREE.Scene();
 // 初始化 camera
 Camera = new THREE.PerspectiveCamera(fov, domContainer.clientWidth / domContainer.clientHeight, 1, far);
 Camera.position.set( 200, 200, 200 );
 Camera.lookAt(Scene.position);
 Scene.add(Camera);
 // 初始化 renderer
 Renderer = new THREE.WebGLRenderer({ canvas: domContainer, antialias: true, alpha: true } );
 Renderer.clear();
 Renderer.setClearColor( 0xeeeeee, 1); // 更改渲染器颜色
 Renderer.setSize(domContainer.clientWidth, domContainer.clientHeight);
 Renderer.shadowMap.Enabled = true;
 Renderer.setPixelRatio(domContainer.devicePixelRatio);
 initVR();
 }
 function initVR() {
  // 初始化控制器
  Controls = new OrbitControls(Camera, Renderer.domElement);;
  Controls.addEventListener('change', render);
  Controls.enableZoom = true;
 }
 function render() {
 Renderer.render(Scene, Camera);
 }
 function renderStart(callback) {
 loopID = 0; // 记录循环几次,后面有与清除场景中的物体
 if (loopID === -1) return;
 let animate = function(){
  loopID = requestAnimationFrame(animate);
  callback();
  Controls.update();
  render();
 }
 animate();
 }
 // 暂停动画渲染
 function renderStop() {
 if (loopID !== -1) {
  window.cancelAnimationFrame(loopID);
  loopID = -1;
 }
 }
 // 回收当前场景
 function clearScene() {
 for(let i = Scene.children.length - 1; i >= 0; i-- ) {
  Scene.remove(Scene.children[i]);
 }
 }
 // 清理页面
 function cleanPage() {
 renderStop();
 clearScene();
 }
 export {
 Scene,
 Camera,
 Renderer,
 Controls,
 createScene,
 initVR,
 renderStart,
 renderStop,
 clearScene,
 cleanPage
 }

始建贰个V奥德赛page基类

这是八个VCR-Vpage的基类,全数须要创制Three项目都亟需连续这一个类,然后生成叁个Three项目

 // VRpage.js
 import * as THREE from 'three';
 import * as VRcore from './VRcore.js';
 export default class VRpage {
 constructor(options) {
  // 创建场景
  VRcore.createScene(options);
  this.start();
  this.loadPage();
 }
 loadPage() {
  VRcore.renderStart(() => this.update());
  this.loaded();
 }
 initPage() {
  this.loadPage();
  this.start();
 }
 start() {}
 loaded() {}
 update() {}
 }

变动三个Three.js的品种

上边包车型大巴公文是贰个接续了V揽胜极光page类的叁个类,然后我们重写了start方法以及update方法,start方法中大家向场景中增加了一个正方体,update方法是我们给那些正方体的二个变形动画,他会构成VEnclavecore.js里面包车型大巴renderStart方法来进展动画效果

 // page.js
 import * as THREE from 'three';
 import VRpage from '../../utils/VRpage.js';
 import * as VRcore from '../../utils/VRcore.js';
 export default class Page extends VRpage {
 start() { // 启动渲染之前,创建场景3d模型
  let geometry = new THREE.CubeGeometry(100,100,100);
  let material = new THREE.MeshLambertMaterial( { color:0x0000ff} );
  this.box = new THREE.Mesh(geometry,material);
  this.box.position.set(3, -2, -3);
  const PointLight = new THREE.PointLight(0xffffff);
  PointLight.position.set(500, 500, 500);
  const AmbientLight = new THREE.AmbientLight( 0x404040 ); // soft white light
  VRcore.Scene.add(PointLight);
  VRcore.Scene.add(AmbientLight);
  VRcore.Scene.background = new THREE.Color( 0xeeeeee ); // 更改场景背景色
  VRcore.Scene.add(this.box);
 }
 update() {
  this.box.rotation.y += 0.01;
 }
 }

此地本身动用的是react的框架

 // index.js
 import React, { Component } from 'react';
 import PropTypes from 'prop-types';
 import Page from './Page.js';
 class Oho extends Component {
 constructor() {
  super();
  this.init = this.init.bind(this);
 }
 componentDidMount() {
  const dom = document.querySelector('#Oho');
  this.init(dom);
 }
 init(dom) {
  const page = new Page({domContainer: dom});
 }
 render() {
  return (
  <div className="three-demo">
   <canvas id="Oho" ref="camera"></canvas>
  </div>
  );
 }
 }
 export default Oho;

总结

以上正是那篇文章的全部内容了,希望本文的剧情对我们的读书也许工作具备一定的参照学习价值,倘诺有疑点我们能够留言交流,多谢大家对帮客之家的支撑。

WebV库罗德 在始发从前,先对WebV中华V举办介绍,WebVLacrosse是三个实验性的Javascript
API,允许HMD(h…

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”

<script type=”text/javascript”>
        function getTxt1CursorPosition(){
            var oTxt1 = document.getElementById(“txt1”);
            var cursurPosition=-1;
            if(oTxt1.selectionStart){//非IE浏览器
                cursurPosition= oTxt1.selectionStart;
            }else{//IE
                var range = document.selection.createRange();
                range.moveStart(“character”,-oTxt1.value.length);
                cursurPosition=range.text.length;
            }
            alert(cursurPosition);
        }

<script type=”text/javascript”>
        function getTxt1CursorPosition(){
            var oTxt1 = document.getElementById(“txt1”);
            var cursurPosition=-1;
            if(oTxt1.selectionStart){//非IE浏览器
                cursurPosition= oTxt1.selectionStart;
            }else{//IE
                var range = document.selection.createRange();
                range.moveStart(“character”,-oTxt1.value.length);
                cursurPosition=range.text.length;
            }
            alert(cursurPosition);
        }

“;
<html xmlns=”;
<head>
<title>js切换光标</title>
<script type=”text/javascript”>
var t;
function a()
{
document.getElementById(‘input1’).focus();
t=setTimeout(“b()”,3000);
//clearTimeout(t);
//document.getElementById(‘input2’).focus();
//document.getElementById(‘input1’).focus();
}
function b()
{
clearTimeout(t);
document.getElementById(‘input2’).focus();
}
function c()
{
setTimeout(“document.getElementById(‘input3’).focus()”,500);
}
</script>
</head>
<body>
<input id=”testButton” type=”button” onclick=”a()” value=’btn1′>
<input type=”text” id=”input1″>
<input type=”text” id=”input2″>

        function setTxt1CursorPosition(i){
            var oTxt1 = document.getElementById(“txt2”);
            var cursurPosition=-1;
            if(oTxt1.selectionStart){//非IE浏览器
                oTxt1.selectionStart=i;
            }else{//IE
                var range = oTxt1.createTextRange();
                range.move(“character”,i);
                range.select();
            }
        }

        function setTxt1CursorPosition(i){
            var oTxt1 = document.getElementById(“txt2”);
            var cursurPosition=-1;
            if(oTxt1.selectionStart){//非IE浏览器
                oTxt1.selectionStart=i;
            }else{//IE
                var range = oTxt1.createTextRange();
                range.move(“character”,i);
                range.select();
            }
        }

<input id=”testButton1″ type=”button” onclick=”c()”
value=’btn2′>
<input type=”text” id=”input3″>
</body>
</html>

        function getTa1CursorPosition(){
            var evt
=window.event?window.event:getTa1CursorPosition.caller.arguments[0];
            var oTa1 = document.getElementById(“ta1”);
            var cursurPosition=-1;
            if(oTa1.selectionStart){//非IE浏览器
                cursurPosition= oTa1.selectionStart;
            }else{//IE
                var range = oTa1.createTextRange();
                range.moveToPoint(evt.x,evt.y);
                range.moveStart(“character”,-oTa1.value.length);
                cursurPosition=range.text.length;
            }
            alert(cursurPosition);
        }

        function getTa1CursorPosition(){
            var evt
=window.event?window.event:getTa1CursorPosition.caller.arguments[0];
            var oTa1 = document.getElementById(“ta1”);
            var cursurPosition=-1;
            if(oTa1.selectionStart){//非IE浏览器
                cursurPosition= oTa1.selectionStart;
            }else{//IE
                var range = oTa1.createTextRange();
                range.moveToPoint(evt.x,evt.y);
                range.moveStart(“character”,-oTa1.value.length);
                cursurPosition=range.text.length;
            }
            alert(cursurPosition);
        }

光标停留一段时间,跳到别二个方本框中去……

        function setTa1CursorPosition(i){
            var oTa2 = document.getElementById(“ta2”);
            if(oTa2.selectionStart){//非IE浏览器
                oTa2.selectionStart=i;
                oTa2.selectionEnd=i;
            }else{//IE
                var range = oTa2.createTextRange();
                range.move(“character”,i);
                range.select();
            }
        }
    </script>

        function setTa1CursorPosition(i){
            var oTa2 = document.getElementById(“ta2”);
            if(oTa2.selectionStart){//非IE浏览器
                oTa2.selectionStart=i;
                oTa2.selectionEnd=i;
            }else{//IE
                var range = oTa2.createTextRange();
                range.move(“character”,i);
                range.select();
            }
        }
    </script>

代码如下: !DOCTYPE html PUBLIC
“-//W3C//DTD XHTML 1.0 Transitional//EN”
“” html
xmlns=”…

代码如下: script type=”text/javascript”
function getTxt1CursorPosition(){ var oTxt1 =
document.getElementById(“txt1”); var cursurPosition=-1;
if(oTxt1.selectionSta…

你或许感兴趣的文章:

  • JavaScript自定义文本框光标
  • Javascript贯彻获取及安装光标地方的章程
  • js光标定位文本框回车表单提交难题的缓慢解决办法
  • JS在可编写制定的div中的光标地点插入内容的章程
  • js落成获取关节后光标在字符串后
  • 在页面中js获取光标/鼠标的坐标及光标的像素坐标
  • js切换光标示例代码
  • js/html光标定位的完毕代码
  • javascript
    textarea光标定位方法(包容IE和FF)
  • JavaScript中在光标处插入添Gavin本标签节点的详实措施

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图