多视角3D可旋转的HTML5 Logo动画
程序员文章站
2023-11-30 22:48:46
这篇文章主要为大家介绍了一款非常棒的多视角3D可旋转的HTML5 Logo动画,可以无限的旋转,从而可以从多个视角来观察HTML5 Logo,感兴趣的小伙伴们可以参考一下... 16-03-02...
这是一款基于html5和css3的3d动画特效,与之前的3d特效不同的是,这款是完全用html5特性实现的,而并非用多张图片叠加起来。这款3d动画还有一个特点是可以无限的旋转,从而可以从多个视角来观察html5 logo。
html代码
xml/html code复制内容到剪贴板
- <div class="link-overlay">
- </div>
- <canvas id="canvas" style="background-color:#ddd"></canvas>
javascript代码
javascript code复制内容到剪贴板
- // bind to window onload handler
- window.addeventlistener('load', onloadhandler, false);
- /**
- * window onload handler
- */
- function onloadhandler()
- {
- var canvas = document.getelementbyid('canvas');
- canvas.width = window.innerwidth;
- canvas.height = window.innerheight;
- var k3dmain = new k3d.controller(canvas, true);
- // generate 3d objects
- var obj1 = new k3d.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]},{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(obj1);
- var obj2 = new k3d.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);
- var obj3 = new k3d.k3dobject();
- with (obj3)
- {
- 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]},{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);
- var obj4 = new k3d.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);
- var objbase = new k3d.k3dobject();
- with (objbase)
- {
- 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(objbase);
- var objhtml = new k3d.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);
- // add render loop callback
- var ctx = canvas.getcontext('2d');
- var rotationoffset = 0;
- var len = (canvas.height > canvas.width ? canvas.height : canvas.width) * 0.7;
- k3dmain.clearbackground = false;
- k3dmain.callback = function()
- {
- // manually clear bg - as we want to render some extra goodies
- ctx.clearrect(0, 0, canvas.width, canvas.height);
- // draw bg effect before k3d does its 3d rendering
- ctx.save();
- ctx.translate(canvas.width/2, canvas.height/2);
- ctx.rotate(rotationoffset);
- // first fill pass - outer rays
- var raycount = 24;
- ctx.fillstyle = "#eee";
- ctx.beginpath();
- for (var i=0; i<raycount; i++)
- {
- // rotate context
- ctx.rotate(twopi / raycount);
- ctx.moveto(0, 0);
- ctx.lineto(-20, len);
- ctx.lineto(20, len);
- }
- ctx.closepath();
- ctx.fill();
- // second fill pass - inner rays
- ctx.fillstyle = "#fff";
- ctx.beginpath();
- for (var i=0; i<raycount; i++)
- {
- // rotate context
- ctx.rotate(twopi / raycount);
- ctx.moveto(0, 0);
- ctx.lineto(-15, len);
- ctx.lineto(15, len);
- }
- ctx.closepath();
- ctx.fill();
- ctx.restore();
- rotationoffset += 0.005;
- // apply user interaction to rotation
- for (var i=0, objs=k3dmain.objects; i<objs.length; i++)
- {
- objs[i].ophi += targetrotationx;
- }
- if (targetrotationx > -0.5) targetrotationx -= 0.05;
- else if (targetrotationx < -0.55) targetrotationx += 0.05;
- if (targetrotationx > -0.55 && targetrotationx < -0.5) targetrotationx = -0.5;
- };
- // start demo loop
- k3dmain.paused = true;
- setinterval(function(){k3dmain.tick()}, 1000/60);
- }
- // nifty drag/touch event capture code borrowed from mr doob http://mrdoob.com/
- var targetrotationx = 0;
- var targetrotationonmousedownx = 0;
- var mousex = 0;
- var mousexonmousedown = 0;
- var targetrotationy = 0;
- var targetrotationonmousedowny = 0;
- var mousey = 0;
- var mouseyonmousedown = 0;
- var windowhalfx = window.innerwidth / 2;
- var windowhalfy = window.innerheight / 2;
- document.addeventlistener('mousedown', ondocumentmousedown, false);
- document.addeventlistener('touchstart', ondocumenttouchstart, false);
- document.addeventlistener('touchmove', ondocumenttouchmove, false);
- function ondocumentmousedown( event ) {
- event.preventdefault();
- document.addeventlistener('mousemove', ondocumentmousemove, false);
- document.addeventlistener('mouseup', ondocumentmouseup, false);
- document.addeventlistener('mouseout', ondocumentmouseout, false);
- mousexonmousedown = event.clientx - windowhalfx;
- targetrotationonmousedownx = targetrotationx;
- mouseyonmousedown = event.clienty - windowhalfy;
- targetrotationonmousedowny = targetrotationy;
- }
- function ondocumentmousemove( event )
- {
- mousex = event.clientx - windowhalfx;
- targetrotationx = targetrotationonmousedownx + (mousex - mousexonmousedown) * 0.02;
- mousey = event.clienty - windowhalfy;
- targetrotationy = targetrotationonmousedowny + (mousey - mouseyonmousedown) * 0.02;
- }
- function ondocumentmouseup( event )
- {
- document.removeeventlistener('mousemove', ondocumentmousemove, false);
- document.removeeventlistener('mouseup', ondocumentmouseup, false);
- document.removeeventlistener('mouseout', ondocumentmouseout, false);
- }
- function ondocumentmouseout( event )
- {
- document.removeeventlistener('mousemove', ondocumentmousemove, false);
- document.removeeventlistener('mouseup', ondocumentmouseup, false);
- document.removeeventlistener('mouseout', ondocumentmouseout, false);
- }
- function ondocumenttouchstart( event )
- {
- if (event.touches.length == 1)
- {
- event.preventdefault();
- mousexonmousedown = event.touches[0].pagex - windowhalfx;
- targetrotationonmousedownx = targetrotationx;
- mouseyonmousedown = event.touches[0].pagey - windowhalfy;
- targetrotationonmousedowny = targetrotationy;
- }
- }
- function ondocumenttouchmove( event )
- {
- if (event.touches.length == 1)
- {
- event.preventdefault();
- mousex = event.touches[0].pagex - windowhalfx;
- targetrotationx = targetrotationonmousedownx + (mousex - mousexonmousedown) * 0.05;
- mousey = event.touches[0].pagey - windowhalfy;
- targetrotationy = targetrotationonmousedowny + (mousex - mouseyonmousedown) * 0.05;
- }
- }
以上就是本文的全部内容,希望对大家的学习有所帮助。