多视角3D可旋转的HTML5 Logo动画
更新时间:2023-04-03 05:39:39 阅读: 评论:0
这是一款基于html5和css3的3d动画特效,与之前的3d特效不同的是,这款是完全用html5特性实现的,而并非用多张图片叠加起来。这款3d动画还有一个特点是可以无限的旋转,从而可以从多个视角来观察html5 logo。
html代码
xml/html code
复制内容到剪贴板
<divclass=“link-overlay”> </div> <canvasid=“canvas”style=“background-color:#ddd”></canvas> javascript代码
javascript code
复制内容到剪贴板
//bindtowindowonloadhandler window.addeventlistener(‘load’,onloadhandler,fal); /** *windowonloadhandler */ functiononloadhandler() { varcanvas=document.getelementbyid(‘canvas’); canvas.width=window.innerwidth; canvas.height=window.innerheight; vark3dmain=newk3d.controller(canvas,true); //generate3dobjects varobj1=newk3d.k3dobject(); with(obj1) { drawmode=“solid”; shademode=“lightsource”; sortmode=“unsorted”; addphi=-0.5; abouty=-90; perslevel=1000; init( [{x:-80,y:180,z:0},{x:0,y:180,z:-80},{x:0,y:0,z:-80},{x:-80,y:20,z:0},{x:-50,y:150,z:-30},{x:0,y:150,z:-80},{x:0,y:130,z:-80},{x:-30,y:130,z:-50},{x:-28,y:110,z:-52},{x:0,y:110,z:-80},{x:0,y:90,z:-80},{x:-45,y:90,z:-35},{x:-44,y:80,z:-36},{x:-25,y:80,z:-55},{x:-22,y:66,z:-58},{x:0,y:60,z:-80},{x:0,y:40,z:-80},{x:-40,y:50,z:-40}], [], [{color:[227,76,38],vertices:[0,1,2,3,0]},{infrontcolor:[235,235,235],vertices:[4,5,6,7,8,9,10,11,4]},{color:[235,235,235],vertices:[12,13,14,15,16,17,12]}] ); } k3dmain.addk3dobject(obj1); varobj2=newk3d.k3dobject(); with(obj2) { drawmode=“solid”; shademode=“lightsource”; sortmode=“unsorted”; addphi=-0.5; abouty=-90; perslevel=1000; init( [{x:0,y:180,z:-80},{x:80,y:180,z:0},{x:80,罗汉果水y:20,z:0},{x:0,y:0,z:-80},{x:0,y:165,z:-80},{x:68,y:165,z:-12},{x:55,y:35,z:-25},{x:0,y:20,z:-80},{x:0,y:150,z:-80},{x:50,y:150,z:-30},{x:47,y:130,z:-33},{x:0,y:130,z:-80},{x:0,y:110,z:-80},{x:45,y:110,z:-35},{x:40,y:50,z:-40},{x:0,y:40,z:-80},{x:0,y:60,z:-80},{x:20,y:66,z:-60},{x:23,y:90,z:-57},{x:0,y:90,z:-80}], []母亲节是什么时候, [{color:[227,76,38],vertices:[0,1,2,3,0]},{color:[240,101,41],vertices:[4,5,6,7,4]},{color:[235,235,235],vertices:[8,9,10,11,8]},{color:[235,235,235],vertices:[12,13,14,15,16,17,18,19,12]}] ); } k3dmain.addk3dobject(obj2); varobj3=newk3d.k3dobject(); with(obj3) { drawmode=“solid”; shademode=“lightsource”; s贫困户申请书ortmode=“unsorted”; addphi=-0.5; abouty=-90; perslevel=1000; init( [{x:80,y:180,z:0},{x:0,y:180,z:80},{x:0,y:0,z:80},{x:80,y:20,z:0},{x:50,y:150,z:30},{x:0,y:150,z:80},{x:0,y:130,z:80},{x:30,y:130,z:50},{x:28,y:110,z:52},{x:0,y:110,z:80},{x:0,y:90,z:80},{x:45,y:90,z:35},{x:44,y:80,z:36},{x:25,y:80,z:55},{x:22,y:66,z:58},{x:0,y:60,z:80},{x:0,y:40,z:80},{x:40,y:50,z:40}], [], [{color:[227,76,38],vertices:[0,1,2,3,0]},{color:[235,235,235],vertices:[4,5,6,7,8,9,10,11,4]},{color:[235,235,235],vertices:[12,13,14,15,16,17,12]}] ); } k3dmain.addk3dobject(obj3); varobj4=newk3d.k3dobject(); with(obj4) { drawmode=“solid”; shademode=“lightsource”; sortmode=“unsorted”; addphi=-0.5; abouty=-90; perslevel=1000; init( [{x:0,y:180,z:80},{x:-80,y:180,z:0},{x:-80,y:20,z:0},{x:0,y:0,z:80},{x:0,y:165,z:80},{x:-68,y:165,z:12},{x:-55,y:35,z:25},{x:0,y:20,z:80},{x:0,y:150,z:80},{x:-50,y:150,z:30},{x:-47,y:130,z:33},{x:0,y:130,z:80},{x:0,y:110,z:80},{x:-45,y:110,z:35},{x:-40,y:50,z:40},{x:0,y:40,z:80},{x:0,y:60,z:80},{x:-20,y:66,z:60},{x:-23,y:90,z:57},{x:0,y:90,z:80}], [], [{color:[227,76,38],vertices:[0,1,2,3,0]},{color:[240,101,41],vertices:[4,5,6,7,4]},{color:[235,235,235],vertices:[8,9,10,11,8]},{color:[235,235,235],vertices:[12,13,14,15,16,17,18,19,12]}] ); } k3dmain.addk3dobject(obj4); varobjba=newk3七夕晚会2021d.k3dobject(); with(objba) { drawmode=“solid”; shademode=“lightsource”; sortmode=“unsorted”; addphi=-0.5; abouty=-90; perslevel=1000; init( [{x:0,y:0,z:-80},{x:-80,y:20,z:0},{x:0,y:0,z:80},{x:80,y:20,z:0}], [], [{color:[227,76,38],vertices:[0,2,1,0]},{color:[227,76,38],vertices:[0,3,2,0]}] ); } k3dmain.addk3dobject(objba); varobjhtml=newk3d.k3dobject(); with(objhtml) { drawmode=“solid”; shademode=“lightsource”; //sortmode=“unsorted”; color=[64,64,64]; doublesided=true; addphi=-0.5; abouty=100; scale=0.75; perslevel=1000; init( [{x:-80,y:40,z:0},{x:-70,y:40,z:0},{x:-70,y:30,z:0},{x:-60,y:30,z:0},{x:-60,y:40,z:0},{x:-50,y:40,z:0},{x:-50,y:10,z:0},{x:-60,y:10,z:0},{x:-60,y:20,z:0},{x:-70,y:20,z:0},{x:-70,y:10,z:0},{x:-80,y:10,z:0},{x:-40,y:40,z:0},{x:-10,y:40,z:0},{x:-10,y:30,z:0},{x:-20,y:30,z:0},{x:-20,y:10,z:0},{x:-30,y:10,z:0},{x:-30,y:30,z:0},{x:-40,y:30,z:0},{x:0,y:40,z:0},{x:10,y:40,z:0},{x:20,y:30,z:0},{x:30,y:40,z:0},{x:40,y:40,z:0},{x:40,y:10,z:0},{x:30,y:10,z:0},{x:30,y:30,z:0},{x:20,y:20,z:0},{x:10,y:30,z:0},{x:10,y:10,z:0},{x:0,y:10,z:0},{x:50,y:40,z:0},{x:60,y:40,z:0},{x:60,y:20,z:0},{x:80,y:20,z:0},{x:80,y:10,z:0},{x:50,y:10,z:0}], [], [{vertices:[0,1,2,3,4,5,6,7,8,9,10,11,0]},{vertices:[12,13,14,15,16,17,18,19,12]},{vertices:[20,21,22,23,24,25,26,27,28,29,30,31,20]},{vertices:[32,33,34,35,36,37,32]}] ); } k3dmain.addk3dobject(objhtml); //addrenderloopcallback varctx=canvas.getcontext(‘2d’); varrotationofft=0; varlen=(canvas.height>canvas.width?canvas.height:canvas.width)*0.7; k3dmain.clearbackground=fal; k3dmain.callback=function() { //manuallyclearbg–aswewanttorendersomeextragoodies ctx.clearrect(0,0,canvas.width,canvas.height); //drawbgeffectbeforek3ddoesits3drendering ctx.save(); ctx.translate(canvas.width/2,canvas.height/2); ctx.rotate(rotationofft); //firstfillpass–outerrays varraycount=24; ctx.fillstyle=“#eee”; ctx.beginpath(); for(vari=0;i<raycount;i++) { //rotatecontext ctx.rotate(twopi/raycount); ctx.moveto(0,0); ctx.lineto(-20,len); ctx.lineto(20,len); } ctx.clopath(); ctx.fill(); //condfillpass–innerrays ctx.fillstyle=“#fff”; ctx.beginpath(); for(vari=0;i<raycount;i++) { //rotatecontext ctx.rotate(twopi/raycount); ctx.moveto(0,0); ctx.lineto(-15,len); ctx.lineto(15,len); } ctx.clopath(); ctx.fill(); ctx.restore(); rotationofft+=0.005; //applyurinteractiontorotation for(vari=0,objs=k3dmain.objects;i<objs.length;i++) { objs[i].ophi+=targetrotationx; } if(targetrotationx>-0.5)targetrotationx-=0.05; elif(targetrotationx<-0.55)targetrotationx+=0.05; if(targetrotationx>-0.55&&targetrotationx<-0.5)targetrotationx=-0.5; }; //startdemoloop k3dmain.paud=true; tinterval(function(){k3dmain.tick()},1000/60); } //niftydrag/toucheventcapturecodeborrowedfrommrdoob/d/file/titlepic/ vartargetrotationx=0; vartargetrotationonmoudownx=0; varmoux=0; varmouxonmoudown=0; vartargetrotationy=0; vartargetrotationonmoudowny=0; varmouy=0; varmouyonmoudown=0; varwindowhalfx=window.innerwidth/2; varwindowhalfy=window.innerheight/2; document.addeventlistener(‘moudown’,ondocumentmoudown,fal); document.addeventlistener(‘touchstart’,ondocumenttouchstart,fal); document.addeventlistener(‘touchmove’,ondocumenttouchmove,fal); functionondocumentmoudown(event){ event.preventdefault(); document.addeventlistener(‘moumove’,ondocumentmoumove,fal); document.addeventlistener(‘mouup’,ondocumentmouup,fal); document.addeventlistener(‘mouout’,ondocumentmouout,fal); mouxonmoudown=event.clientx–windowhalfx; targetrotationonmoudownx=targetrotationx; mouyonmoudown=event.clienty–windowhalfy; targetrotationonmoudowny=targetrotationy; } functionondocumentmoumove(event) { moux=event.clientx–windowhalfx; targetrotationx=targetrotationonmoudownx+(moux–mouxonmoudown)*0.02; mouy=event.clienty–windowhalfy; targetrotationy=targetrotationonmoudowny+(mouy–mouyonmoudown)*0.02; } functionondocumentmouup(event) { document.removeeventlistener(‘moumove’,ondocumentmoumove,fal); document.removeeventlistener(‘mouup’,ondocumentmouup,fal); document.removeeventlistener(‘mouout’,ondocumentmouout,fal); } functionondocumentmouout(event) { document.removeeventlistener(‘moumove’,ondocumentmoumove,fal); document.removeeventlistener(‘mouup’,ondocumentmouup,fal); document.removeeventlistener(‘mouout’,ondocumentmouout,fal); } functionondocumenttouchstart(event) { if(event.touches.length==1) { event.preventdefault(); mouxonmoudown=event.touches[0].pagex–windowhalfx; targetrotationonmoudownx=targetrotationx; mouyonmoudown=event.touches[0].pagey–windowhalfy; targetrotationonmoudowny=targetrotationy; } } functionondocumenttouchmove(event) { if(event.touches.length==1) { event.preventdefault(); moux=event.touches[0].pagex–windowhalfx; targetrotationx=targetrotationonmoudownx+(moux–mouxonmoudown)*0.05; mouy=event.touches[0].pagey–windowhalfy; targetrotationy=targetrotationonmoudowny+(moux–mouyonmoudown)*0.05; } } 以上就是本文的全部内容,希望对大家的学习有所帮助。
本文word下载地址:多视角3D可旋转的HTML5 Logo动画.doc
本文 PDF 下载地址:多视角3D可旋转的HTML5 Logo动画.pdf