当前位置:首页 > 数码 > 全网疯传的前端量子纠缠成果源码大公开! (全网疯传的前一句是啥)

全网疯传的前端量子纠缠成果源码大公开! (全网疯传的前一句是啥)

admin4个月前 (05-09)数码29

昨天,很多群里都在疯传一个视频,视频展示了纯前端成功的量子纠缠成果,不少前端er示意:前端白学了。

视频作者昨晚开源一个简化版的成功源码(截止发文,该名目在上已取得超越1kStar),本文就来看看他是怎样成功的!

简化版

依据作者的形容,该名目是经常使用three.js和localStorage成功的在同一源上设置跨窗口的3D场景。

只管没有原视频那么炫酷,但基本原理当该差不多。

源码蕴含多个文件,最关键的文件如下:

在线体验:

源码

index.html文件中引入了three.js的紧缩包,以及main.js:

<!DOCTYPEhtml><html><head><title>3dexampleusingthree.jsandmultiple</title><script></script><style>*{margin:0;padding:0;}</style></head><body><script></script></body></html>

这没啥可说的,上方就来看看main.js中都写了点啥。代码如下:

importWindowManagerfrom'./WindowManager.js'constt=THREE;letcamera,scene,renderer,world;letnear,far;letpixR=window.devicePixelRatio?window.devicePixelRatio:1;letcubes=[];letsceneOffsetTarget={x:0,y:0};letsceneOffset={x:0,y:0};lettoday=newDate();today.setHours(0);today.setMinutes(0);today.setSeconds(0);today.setMilliseconds(0);today=today.getTime();letinternalTime=getTime();letwindowManager;letinitialized=false;////失掉从一天开局以来的秒数(以便一切窗口经常使用相反的期间)functiongetTime(){return(newDate().getTime()-today)/1000.0;}if(newURLSearchParams(window.location.search).get("clear")){localStorage.clear();}else{//在某些阅读器中防止在实践点击URL之前预加载页面内容document.addEventListener("visibilitychange",()=>{if(document.visibilityState!='hidden'&&!initialized){init();}});//确保在窗口齐全加载后,只要在页面可见时才口头初始化逻辑window.onload=()=>{if(document.visibilityState!='hidden'){init();}};//初始化操作functioninit(){initialized=true;//短期间内window.offsetX属性前往的值或者不准确,须要减少一个持久的提前,期待一段期间后再口头关系操作。setTimeout(()=>{setupScene();setupWindowManager();resize();updateWindowShape(false);render();window.addEventListener('resize',resize);},500)}//设置场景关系的性能functionsetupScene(){camera=newt.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=newt.Scene();scene.background=newt.Color(0.0);scene.add(camera);renderer=newt.WebGLRenderer({antialias:true,depthBuffer:true});renderer.setPixelRatio(pixR);world=newt.Object3D();scene.add(world);renderer.domElement.setAttribute("id","scene");document.body.endChild(renderer.domElement);}//设置窗口治理器的关系性能functionsetupWindowManager(){windowManager=newWindowManager();windowManager.setWinShapeChangeCallback(updateWindowShape);windowManager.setWinChangeCallback(windowsUpdated);letmetaData={foo:"bar"};//初始化窗口治理器(windowmanager)并将以后窗口减少到窗口池中。windowManager.init(metaData);windowsUpdated();}functionwindowsUpdated(){updateNumberOfCubes();}functionupdateNumberOfCubes(){letwins=windowManager.getWindows();cubes.forEach((c)=>{world.remove(c);})cubes=[];for(leti=0;i<wins.length;i++){letwin=wins[i];letc=newt.Color();c.setHSL(i*.1,1.0,.5);lets=100+i*50;letcube=newt.Mesh(newt.BoxGeometry(s,s,s),newt.MeshBasicMaterial({color:c,wireframe:true}));cube.position.x=win.shape.x+(win.shape.w*.5);cube.position.y=win.shape.y+(win.shape.h*.5);world.add(cube);cubes.push(cube);}}functionupdateWindowShape(easing=true){sceneOffsetTarget={x:-window.screenX,y:-window.screenY};if(!easing)sceneOffset=sceneOffsetTarget;}functionrender(){lett=getTime();windowManager.update();//依据以后位置和新位置之间的偏移量以及一个平滑系数来计算出窗口的新位置letfalloff=.05;sceneOffset.x=sceneOffset.x+((sceneOffsetTarget.x-sceneOffset.x)*falloff);sceneOffset.y=sceneOffset.y+((sceneOffsetTarget.y-sceneOffset.y)*falloff);world.position.x=sceneOffset.x;world.position.y=sceneOffset.y;letwins=windowManager.getWindows();//遍历立方体对象,并依据以后窗口位置的变动升级它们的位置。for(leti=0;i<cubes.length;i++){letcube=cubes[i];letwin=wins[i];let_t=t;//+i*.2;letposTarget={x:win.shape.x+(win.shape.w*.5),y:win.shape.y+(win.shape.h*.5)}cube.position.x=cube.position.x+(posTarget.x-cube.position.x)*falloff;cube.position.y=cube.position.y+(posTarget.y-cube.position.y)*falloff;cube.rotation.x=_t*.5;cube.rotation.y=_t*.3;};renderer.render(scene,camera);requestAnimationFrame(render);}//调整渲染器大小以适宜窗口大小functionresize(){letwidth=window.innerWidth;letheight=window.innerHeightcamera=newt.OrthographicCamera(0,width,0,height,-10000,10000);camera.updateProjectionMatrix();renderer.setSize(width,height);}}

这段代码关键成功以下几点:

接上去看看最外围的成功:WindowManager,代码如下:

classWindowManager{#windows;#count;#id;#winData;#winShapeChangeCallback;#winChangeCallback;constructor(){letthat=this;//监听localStorage能否被其余窗口更改addEventListener("storage",(event)=>{if(event.key=="windows"){letnewWindows=JSON.parse(event.newValue);letwinChange=that.#didWindowsChange(that.#windows,newWindows);that.#windows=newWindows;if(winChange){if(that.#winChangeCallback)that.#winChangeCallback();}}});//监听以后窗口能否行将封锁window.addEventListener('beforeunload',function(e){letindex=that.getWindowIndexFromId(that.#id);//从窗口列表中移除以后窗口并升级localStoragethat.#windows.splice(index,1);that.updateWindowsLocalStorage();});}//审核窗口列表能否有变动#didWindowsChange(pWins,nWins){if(pWins.length!=nWins.length){returntrue;}else{letc=false;for(leti=0;i<pWins.length;i++){if(pWins[i].id!=nWins[i].id)c=true;}returnc;}}//初始化以后窗口(减少元数据以将自定义数据存储在每个窗口实例中)init(metaData){this.#windows=JSON.parse(localStorage.getItem("windows"))||[];this.#count=localStorage.getItem("count")||0;this.#count++;this.#id=this.#count;letshape=this.getWinShape();this.#winData={id:this.#id,shape:shape,metaData:metaData};this.#windows.push(this.#winData);localStorage.setItem("count",this.#count);this.updateWindowsLocalStorage();}getWinShape(){letshape={x:window.screenLeft,y:window.screenTop,w:window.innerWidth,h:window.innerHeight};returnshape;}getWindowIndexFromId(id){letindex=-1;for(leti=0;i<this.#windows.length;i++){if(this.#windows[i].id==id)index=i;}returnindex;}updateWindowsLocalStorage(){localStorage.setItem("windows",JSON.stringify(this.#windows));}update(){letwinShape=this.getWinShape();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;letindex=this.getWindowIndexFromId(this.#id);this.#windows[index].shape=winShape;if(this.#winShapeChangeCallback)this.#winShapeChangeCallback();this.updateWindowsLocalStorage();}}setWinShapeChangeCallback(callback){this.#winShapeChangeCallback=callback;}setWinChangeCallback(callback){this.#winChangeCallback=callback;}getWindows(){returnthis.#windows;}getThisWindowData(){returnthis.#winData;}getThisWindowID(){returnthis.#id;}}exportdefaultWindowManager;

这段代码定义了一个WindowManager类,用于治理窗口的创立、升级和删除等操作,并将其作为模块导出。

该类蕴含以下私有属性:

该类蕴含以下公共方法:

可以看到,作者经常使用window.screenLeft、window.screenTop、window.innerWidth和window.innerHeight这些属性来计算立方体的位置和大小信息,经过localstorage来在不同学口之间共享不同的位置信息。

全网疯传的前一句是啥

当新增一个窗口时,就将其保留到localstorage中,每个窗口经常使用惟一的id启动标志,并贮存立方体的位置和大小信息。不同阅读器窗口都可以取得一切的窗口信息,以确保实时升级。

当窗口的位置,即screenTop、screenLeft出现变动时,就升级立方体。

这里就不再详细解释了,可以检查完整源码:


vb编写使窗体保持最前端的代码 像QQ那样

在VB6.0里要先在最前面进行如下声明Private Declare Function SetWindowPos Lib user32 (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long在过程中加入SetWindowPos , -1, 0, 0, 0, 0, 3就可以了,还原把 -1 改为 -2 。如果是就容易了,直接在窗体属性中设置TopMost属性就行了要显示窗体 0,主窗体

希望能帮助到你祝你好运!

2017年我国的科技成就有哪些?

1、C919首次试飞2017年5月5日,中国首架具有自主知识产权的大型喷气式干线民用飞机C919的首飞完满结束。 这标志着大型民用客机的垄断市场在A(Airbus空中客车)和B(Boeing波音)之外,正式迎来了新的参与者C(C919)。 2、可燃冰稳定试采成功2017年5月18日,我国在南海北部海域的可燃冰试采成功。 可燃冰作为一种储量惊人的清洁能源,其储量几乎可以达到煤、石油、天然气总和的两倍。 可燃冰是公认的下一代清洁替代能源。 此次试采标志着我国成为全球实现了在海域可燃冰试开采中获得连续稳定产气第一名的国家。 3、北斗导航第三阶段组网2017年5月23日,中国卫星导航系统管理办公室宣布,我国将全面启动北斗系统第三步建设,即北斗三号系统,2017年下半年发射6~8颗全球组网卫星。 正式标志着北斗系统第三阶段任务:全球组网正式开始。 接下来的2018年将前后发射18颗北斗卫星。 北斗卫星系统将率先为“一带一路”沿线国家提供基本服务,直到2020年我国建成世界一流的全球卫星导航系统,最终形成全球服务能力。 4、全球首列无轨列车2017年6月2日,中车株洲所成功研制的一款名为“智能轨道快运系统”的全新交通产品在株洲惊艳亮相,这款产品昵称“智轨”。 这是一款融合现代有轨电车和公共汽车各自优势的跨界之作。 既保持轨道列车载人多的优势,又不需要建造专有钢轨,还能像公交车一样灵活穿行的城市交通工具。 5、量子通讯试验阶段性成功2017年8月10日,全球首颗量子科学实验卫星“墨子号”圆满完成了三大科学实验任务:量子纠缠分发、量子密钥分发、量子隐形传态。 使得我国在量子通讯领域达到新高度。 作为安全等级最高的加密技术,量子加密早已成为各大国争相追逐的目标。 早在10年前,IBM、微软等知名外企就率先开展研发,国内仅用于个别科学实验。 谁能想到呢,只用了10年,中国在这个领域已经变成了当之无愧的领头羊。

免责声明:本文转载或采集自网络,版权归原作者所有。本网站刊发此文旨在传递更多信息,并不代表本网赞同其观点和对其真实性负责。如涉及版权、内容等问题,请联系本网,我们将在第一时间删除。同时,本网站不对所刊发内容的准确性、真实性、完整性、及时性、原创性等进行保证,请读者仅作参考,并请自行核实相关内容。对于因使用或依赖本文内容所产生的任何直接或间接损失,本网站不承担任何责任。

标签: 源码

“全网疯传的前端量子纠缠成果源码大公开! (全网疯传的前一句是啥)” 的相关文章

源码安全性-保护你的源码免受攻击-黑客克星 (源代码安全管理工具)

源码安全性-保护你的源码免受攻击-黑客克星 (源代码安全管理工具)

在当今数字化时代,源码安全性已成为企业和开发者不可忽视的问题。源码泄漏不仅会导致企业商业机密泄露,还可能导致黑客攻击、恶意代码注入等安全问题。因此,保护源码安全已成为企业和开发者必须重视的事项。本...