這是一款基於HTML5和CSS3的3D動畫特效,與之前的3D特效不同的是,這款是完全用HTML5特性實現的,而並非用多張圖片疊加起來。這款3D動畫還有一個特點是可以無限的旋轉,從而可以從多個視角來觀察HTML5 Logo。
線上示範 原始碼下載
HTML程式碼
XML/HTML Code複製內容到剪貼簿
- div class=class=class" >>
-
-
div>
-
canvas id="canvas" style="background-color:#ddd">canvas>
-
JavaScript程式碼
JavaScript Code
複製內容到剪貼簿
-
-
window.addEventListener('load', onloadHandler, false
-
-
/**
- * 視窗載入處理程序
- */
-
函數 onloadHandler()
{ -
-
var canvas = document.getElementById('canvas'
canvas.width = window.innerWidth;
-
canvas.height = window.innerHeight;
-
- var k3dmain = 新 新 新
- 新 3D.Controller>
); -
- //產生 3D 物件
-
var
obj1 = -
new 3D.K3d(3D.K33);
與-
(obj1)
-
{
繪圖模式 =
「實心」-
;
著色模式 =
"光源"-
;
sortmode =
「未排序」-
;
-
addphi = -0.5;
-
約= -90;
-
每等級 = 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}],
-
[],
-
[{顏色:[227,76,38],頂點:[0,1,2,3,0]},{顏色:[235,235,235],頂點:[4,5,67 ,8,9,10,11,4]},{顏色:[235,235,235],頂點:[12,13,14,15,16,17,12]}]
-
);
}
-
k3dmain.addK3DObject(obj1);
-
-
var obj2 = new
-
與 (obj2)
{ -
繪圖模式 = -
「實心」;
著色模式 = -
"光源";
sortmode = -
「未排序」;
addphi = -0.5; -
約= -90; -
每等級 = 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}], -
[], -
[{顏色:[227,76,38],頂點:[0,1,2,3,0]},{顏色:[240,101,41],頂點:[4,5,6 ,7,4]},{顏色:[235,235,235],頂點:[8,9,10,11,8]},{顏色:[235,235,235],頂點:[12,13,14,15,16,17 ,18,19,12]}] -
); -
} -
k3dmain.addK3DObject(obj2); -
-
-
var obj3 = new 3D.K3D.
與-
(obj3)
{
- 繪圖模式 =
「實心」-
;
著色模式 =
"光源"-
;
sortmode =
「未排序」-
;
addphi = -0.5;
-
約= -90;
-
每等級 = 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}],
-
[],
-
[{顏色:[227,76,38],頂點:[0,1,2,3,0]},{顏色:[235,235,235],頂點:[4,5,67 ,8,9,10,11,4]},{顏色:[235,235,235],頂點:[12,13,14,15,16,17,12]}]
-
);
-
}
-
k3dmain.addK3DObject(obj3);
-
-
var obj4 = new 3D.K3D.
-
與 (obj4)
{ -
繪圖模式 = -
「實心」;
著色模式 = -
"光源";
sortmode = -
「未排序」;
addphi = -0.5; -
約= -90; -
每等級 = 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}], -
[], -
[{顏色:[227,76,38],頂點:[0,1,2,3,0]},{顏色:[240,101,41],頂點:[4,5,6 ,7,4]},{顏色:[235,235,235],頂點:[8,9,10,11,8]},{顏色:[235,235,235],頂點:[12,13,14,15,16,17 ,18,19,12]}] -
); -
} -
k3dmain.addK3DObject(obj4); -
-
-
var objBase = new>
與-
(objBase)
{
- 繪圖模式 =
「實心」-
;
著色模式 =
"光源"-
;
sortmode =
「未排序」-
;
addphi = -0.5;
-
約= -90;
-
每等級 = 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}],
-
[],
-
[{顏色:[227,76,38],頂點:[0,2,1,0]},{顏色:[227,76,38],頂點:[0,3,22 ,0]}]
-
);
-
}
-
k3dmain.addK3DObject(objBase);
-
-
var objHtml = new
new-
與
(objHtml) -
{ -
繪圖模式 = 「實心」
; -
著色模式 = "光源"
; -
//sortmode = 「未排序」;
-
顏色 = [64,64,64]; -
雙面= true
; -
addphi = -0.5; -
約 = 100; -
比例= 0.75; -
每等級 = 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}], -
[], -
[{頂點:[0,1,2,3,4,5,6,7,8,9,10,11,0]},{頂點:[12,13,14,15 ,16,17,18,19,12]},{頂點:[20,21,22,23,24,25,26,27,28,29,30,31,20]},{頂點:[32 ,33,34,35,36,37,32]}] -
); -
}
-
k3dmain.addK3DObject(objHtml);
-
-
-
var ctx = canvas.getContext('2d'
-
var rotationOffset = 0;
-
var len = (canvas.height > canvas.width ? vas.height :height > canvas.width ?
k3dmain.clearBackground =
false-
;
k3dmain.callback =
函數-
()
{
-
//手動清除背景 - 因為我們想要渲染額外的好東西 -
ctx.clearRect(0, 0, canvas.width, canvas.height);
-
-
//在 K3D 進行 3D 渲染前繪製背景效果 -
ctx.save();
-
ctx.translate(canvas.width/2, canvas.height/2);
-
ctx.rotate(rotationOffset);
-
-
//第一個填充通道 - 外光線 -
var-
射線計數 = 24;
ctx.fillStyle =
"#eee"-
;
ctx.beginPath();
-
for-
(varO
{
- //旋轉情境
-
ctx.moveTo(0, 0); -
ctx.lineTo(-20, len); -
ctx.lineTo(20, len); -
}
-
ctx.closePath();
-
ctx.fill();
-
-
ctx.fillStyle = "#fff";
-
ctx.beginPath();
-
for (varO
{ -
-
//旋轉情境
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; -
-
-
//將使用者互動套用於旋轉
-
for (var i=0, )
{ -
objs[i].ophi = targetRotationX; -
} -
-
-
if (targetRotationX > -0.5) targetRotationX -= 0.05; >
else-
if if
- if (targetRotationX > -0.55 &&
};
-
- //開始示範循環
-
k3dmain.paused = true
- ;
setInterval(函數
- (){k3dmain.tick()}, 1000/60);
}
-
-
-
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,
document.addEventListener(
'touchstart'-
, onDocumentTouchStart, false
document.addEventListener('touchmove'
, onDocumentTouchMove, -
false
函數-
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;
-
}
-
-
- 函數 onDocumentMouseMove( 事件 )
-
{
-
mouseX = event.clientX - windowHalfX;
-
targetRotationX = targetRotationOnMouseDownX (mouseX - mouseXOnMouseDown) * 0.02;
mouseY = event.clientY - windowHalfY;
-
targetRotationY = targetRotationOnMouseDownY (mouseY - mouseYOnMouseDown) * 0.02;
-
}
-
-
函數 onDocumentMouseUp( event )
-
{
-
document.removeEventListener('mousemove', onDocumentMouseMove, false
- document.removeEventListener('mouseup', onDocumentMouseUp, false;
- document.removeEventListener('mouseout', onDocumentMouseOut, false;
} -
-
-
函數 onDocumentMouseOut( event )
{ -
document.removeEventListener(-
'mousemove', onDocumentMouseMove, false
document.removeEventListener(
- 'mouseup', onDocumentMouseUp, false;
document.removeEventListener(
- 'mouseout', onDocumentMouseOut, false;
}
-
函數-
onDocumentTouchStart( event )
{
-
若-
(event.touches.length == 1)
{
-
event.preventDefault();
-
-
mouseXOnMouseDown = event.touches[0].pageX - windowHalfX;
-
targetRotationOnMouseDownX = targetRotationX;
-
mouseYOnMouseDown = event.touches[0].pageY - windowHalfY;
-
targetRotationOnMouseDownY = targetRotationY;
-
}
-
}
-
函數-
onDocumentTouchMove( 事件 )
{
-
若-
(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;
-
}
-
}
-
以上就是本文的全部內容,希望對大家的學習有所幫助。