今天又要为大家带来一款超酷的HTML5 Canvas 3D动画特效,是一款可以旋转的HTML5 Logo标志。画面上一共有两块可旋转的区域,第一是可旋转的背景,第二则是可旋转的Logo标志。Logo标志在旋转的时候还有3D的视觉效果,这一切我们都是在canvas上实现的。具体演示和实现过程可以看下文。

你也可以在这里查看在线演示

下面我们来简单分析一下实现这款3D动画的过程及其部分核心代码,主要由HTML代码以及Javascript代码组成。

HTML代码:

<canvas id="canvas" style="background-color:#ddd"></canvas>

非常简单,仅仅是构造了一个canvas容器,接下来的3D动画,包括旋转的特效都将在这里绘制和实现。

Javascript代码:

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);
}

这部分代码是整个动画的初始化代码,其中我们利用了k3d的脚本库,所以你需要在项目中引用它,关于k3d,你可以在最后的源代码中找到它。

<script src="k3d-min.js"></script>

下面是一些鼠标事件,你可以拖动鼠标来让其旋转,具体代码如下:

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;
}
}

好了,一个简单的3D动画实现起来还是有点复杂的,你可以下载源代码来研究学习。源码下载>>

3D HTML5 Logo标志 超炫酷旋转特效的更多相关文章

  1. css3-rotate实现超炫环形旋转特效

    css3-rotate实现超炫环形旋转特效,css3特效,环形旋转,圆形旋转,css3-rotate实现超炫环形旋转特效是一款采用css3 rotate实现的蓝色环形旋转特效代码. http://ww ...

  2. html5+CSS3实现的炫酷超链接特效

    今天为大家介绍一个非常炫酷的超链接特效.在你做一些前端的网页时可以在里面去用上这些前卫时尚的效果. 这些超链接特性,大都是借助伪元素.svg.HTML5动画来实现的.效果都很漂亮,不信看下面. 用你的 ...

  3. 3D超炫酷旋转

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  4. 8个超炫酷仿HTML5动画源码

    1.jQuery万年历插件 带农历老皇历功能 这是一款基于jQuery的日历插件,这款日历插件和之前分享的日历控件有很大差异,它是一本万年历,包含了农历已经老皇历的功能,是一个挑好日子的工具.同时日历 ...

  5. 程序猿必备的10款超炫酷HTML5 Canvas插件

    1.超炫酷HTML5 Canvas 3D旋转地球动画 这是一款基于HTML5 Canvas的3D地球模拟动画,动画以太空作为背景,地球在太空中旋转,同时我们也可以拖拽鼠标来从不同的角度观察地球.另外我 ...

  6. 8个超炫酷的纯CSS3动画及源码分享

    在现代网页中,我们已经越来越习惯使用大量的CSS3元素,而现在的浏览器也基本都支持CSS3,所以很多时候我们不妨思考一下是否可以用纯CSS3制作一些有趣或者实用的网页.本文要分享8个超炫酷的纯CSS3 ...

  7. 优秀教程:使用 CSS3 动画实现的超炫的过渡特效

    Codrops 最近分享了一些很酷的图片切换灵感.有三种不同的用例:小的图像幻灯片,大标题幻灯片以及使用透明背景的产品幻灯片.状态转换使用 CSS 动画完成,我们能够定义从任何方向进来的图片的行为. ...

  8. 8个超炫酷仿苹果应用的HTML5动画

    苹果的产品一直以精美的UI著称,无论是软件应用还是硬件设备.本文主要分享了8个很不错的HTML5动画应用,这些动画正式模仿了苹果的各类应用,有焦点图.钟表.菜单等HTML5应用和jQuery插件,大家 ...

  9. 6个超炫酷的HTML5电子书翻页动画

    相信大家一定遇到过一些电子书网站,我们可以通过像看书一样翻页来浏览电子书的内容.今天我们要分享的HTML5应用跟电子书翻页有关,我们精选出来的6个电子书翻页动画都非常炫酷,而且都提供源码下载,有需要的 ...

随机推荐

  1. web spring 容器

    使用spring的web应用时,不用手动创建spring容器,而是通过配置文件声明式地创建spring容器,因此,在web应用中创建spring容器有如下两种方式: 一.直接在web.xml文件中配置 ...

  2. 国内Docker下载镜像提速方法之一

    众所周知,Docker Hub并没有在国内部署服务器或者使用国内的CDN服务,因此在国内特殊的网络环境下,镜像下载十分耗时.为了克服跨洋网络延迟,能够快速高效地下载Docker镜像,我采用了DaoCl ...

  3. 如何在linux下查看目录的剩余空间大小

    df命令是linux系统以磁盘分区为单位查看文件系统,可以加上参数查看磁盘剩余空间信息,命令格式: df -hl 显示格式为: 文件系统 容量 已用 可用 已用% 挂载点 Filesystem Siz ...

  4. C#学习笔记(19)——使用IComparer(百度文库)

    说明(2017-7-24 19:15:15): 1. 百度文库里的一篇文章,觉得讲的比较好.原文地址:https://wenku.baidu.com/view/b53cd250ad02de80d4d8 ...

  5. C#学习笔记(8)——委托应用(显示,写入时间)

    说明(2017-5-30 09:08:10): 1. 定义一个委托,public delegate void MyDel();无参数,无返回值. 2. 委托作为DoSth的参数,DoSth里面调用委托 ...

  6. splitter 使用

    splitter1.Width = ; splitter1.Height = ; Label l = new Label(); l.Text = "···"; //写入的字,具体指 ...

  7. MySQL的MVCC

    基本概念 Multi-Version Concurrency Control 多版本并发控制,MVCC 是一种并发控制的方法,一般在数据库管理系统中,实现对数据库的并发访问:在编程语言中实现事务内存. ...

  8. mosquitto简单应用

    1. 简述 一款实现了消息推送协议 MQTT v3.1 的开源消息代理软件,提供轻量级的,支持可发布/可订阅的的消息推送模式,使设备对设备之间的短消息通信变得简单,比如现在应用广泛的低功耗传感器,手机 ...

  9. 枚举Enum和常量0之间的恩怨

    1,任何为0的常量表达式都能隐式的转换成枚举Enum. 对于这一点,在程序中没少吃苦头.特别是对于函数重载的情况,往往让人一头雾水. 看看下面的代码(摘自MSDN),你能猜到输出吗? public e ...

  10. idea properties文件unicode码问题

    在git hub上下载了个工程.但是properties文件一直显示不了中文: # \u662F\u5426\u4F7F\u7528\u8FDC\u7A0B\u914D\u7F6E\u6587\u4E ...