three实现多窗体共享的三维小球旋转动画效果代码
代码语言:html
所属分类:三维
代码描述:three实现多窗体共享的三维小球旋转动画效果代码,打开新的窗体,新增小球会实时显示。
代码标签: three 多窗体 共享 三维 小球 旋转 动画
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en"> <head> <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/three.124.js"></script> <style type="text/css"> * { margin: 0; padding: 0; } </style> </head> <body> <script type="module" > class WindowManager { #windows; #count; #id; #winData; #winShapeChangeCallback; #winChangeCallback; constructor () { let that = this; // event listener for when localStorage is changed from another window addEventListener("storage", (event) => { if (event.key == "windows") { let newWindows = JSON.parse(event.newValue); let winChange = that.#didWindowsChange(that.#windows, newWindows); that.#windows = newWindows; if (winChange) { if (that.#winChangeCallback) that.#winChangeCallback(); } } }); // event listener for when current window is about to ble closed window.addEventListener('beforeunload', function (e) { let index = that.getWindowIndexFromId(that.#id); //remove this window from the list and update local storage that.#windows.splice(index, 1); that.updateWindowsLocalStorage(); }); } // check if theres any changes to the window list #didWindowsChange (pWins, nWins) { if (pWins.length != nWins.length) { return true; } else { let c = false; for (let i = 0; i < pWins.length; i++) { if (pWins[i].id != nWins[i].id) c = true; } return c; } } // initiate current window (add metadata for custom data to store with each window instance) init (metaData) { this.#windows = JSON.parse(localStorage.getItem("windows")) || []; this.#count= localStorage.getItem("count") || 0; this.#count++; this.#id = this.#count; let shape = this.getWinShape(); this.#winData = {id: this.#id, shape: shape, metaData: metaData}; this.#windows.push(this.#winData); localStorage.setItem("count", this.#count); this.updateWindowsLocalStorage(); } getWinShape () { let shape = {x: window.screenLeft, y: window.screenTop, w: window.innerWidth, h: window.innerHeight}; return shape; } getWindowIndexFromId (id) { let index = -1; for (let i = 0; i < this.#windows.length; i++) { if (this.#windows[i].id == id) index = i; } return index; } updateWindowsLocalStorage () { localStorage.setItem("windows", JSON.stringify(this.#windows)); } update () { //console.log(step); let winShape = this.getWinShape(); //console.log(winShape.x, winShape.y); if (winShape.x != this.#winData.shape.x || winShape.y != this.#winData.shape.y || winShape.w != this.#winData.shape.w || winShape.h != this.#winData.shape.h) { this.#winData.shape = winShape; let index = this.getWindowIndexFromId(this.#id); this.#windows[index].shape = winShape; //console.log(windows); if (this.#winShapeChangeCallback) this.#winShapeChangeCallback(); this.updateWindowsLocalStorage(); } } setWinShapeChangeCallback (callback) { this.#winShapeChangeCallback = callback; } setWinChangeCallback (callback) { this.#winChangeCallback = callback; } getWindows () { return this.#windows; } getThisWindowData () { return this.#winData; } getThisWindowID () { return this.#id; } } const t = THREE; let camera, scene, renderer, world; let near, far; let pixR = window.devicePixelRatio ? window.devicePixelRatio : 1; let cubes = []; let sceneOffsetTarget = {x: 0, y: 0}; let sceneOffset = {x: 0, y: 0}; let today = new Date(); today.setHours(0); today.setMinutes(0); today.setSeconds(0); today.setMilliseconds(0); today = today.getTime(); let internalTime = getTime(); let windowManager; let initialized = false; // get time in seconds since beginning of the day (so that all windows use the same time) function getTime () { return (new Date().getTime() - today) / 1000.0; } if (new URLSearchParams(window.location.search).get("clear")) { localStorage.clear(); } else { // this code is essential to circumvent that some browsers preload the content of some pages before you actually hit the url document.addEventListener("visibilitychange", () => { if (document.visibilityState != 'hidden' && !initialized) { init(); } }); window.onload = () => { if (document.visibilityState != 'hidden') { init(); } }; function init () { initialized = true; // add a short timeout because window.offsetX reports wrong values before a short period setTimeout(() => { setupScene(); // createParticleSystem(); setupWindowManager(); resize(); updateWindowShape(false); render(); window.addEventListener('resize', resize); }, 500) } function setupScene () { camera = new t.OrthographicCamera(0, 0, window.innerWidth, window.innerHeight, -10000, 10000); camera.position.z = 2.5; near = camera.position.z - .5; far = camera.position.z + 0.5; scene = new t.Scene(); scene.background = new t.Color(0.0); scene.add( camera ); var starGeometry = new THREE.Geometry(); for (let i = 0; i < 5000; i++) { var star = new THREE.Vector3(); star.x = Math.random() * 5000 - 2000; star.y = Math.random() * 5000 - 2000; star.z = Math.random() * 5000 - 2000; starGeometry.vertices.push(star); var color = new THREE.Color(); if (Math.random() < 0.5) { color.setHSL(0.16, 0.5, Math.random() * 0.5 + 0.25); } else { color.setHSL(0.0, 0.0, Math.random() * 0.5 + 0.5); } starGeometry.colors.push(color); } var starMaterial = new THREE.PointsMaterial({ size: 2, vertexColors: THREE.VertexColors }); var starField = new THREE.Points(starGeometry, starMaterial); scene.add(starField); renderer = new t.WebGLRenderer({antialias: true, depthBuffer: true}); renderer.setPixelRatio(pixR); world = new t.Object3D(); scene.add(world); renderer.domElement.setAttribute("id", "scene"); document.body.appendChild( renderer.domElement ); // Lights var light = new THREE.AmbientLight( 0x404040 ); // soft white light scene.add( light ); var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 ); directionalLight.position.set( 0, 128, 128 ); scene.add( directionalLight ); } function setupWindowManager () { windowManager = new WindowManager(); windowManager.setWinShapeChangeCallback(updateWindowShape); windowManager.setWinChangeCallback(windowsUpdated); // here you can .........完整代码请登录后点击上方下载按钮下载查看
网友评论0