HTML5如何在网页中实现3D效果

html-css0356

HTML5如何在网页中实现3D效果,第1张

CSS3除了为开发者提供二维变形之外,还将动画从二维平面推动到了三维立体状态,能够实现真正的三维特效。

三维变形和二维变形一样,均使用的是transform属性。想要触发三维变形有两种方式:一种方式是通过语法告知浏览器“请采用三维方式进行变形处理”,另一种方式是直接使用CSS3三维变形的语法。

触发方法1:告知浏览器变形方式

-webkit-transform-style:preserve-3d

Tips:IE不支持三维变形,在移动端,绝大多数的浏览器均为WebKit内核,因此,在此句代码之前需要书写-webkit-的前缀内核。

Tips:不要为body元素设置-webkit- transform-style: preserve 3d,否则会对position:fixed定位的元素造成布局影响。在开发当中,如果当前元素属于body的子级元素,又希望应用三维变形,则在body和当前元素之间多嵌套一层结构,并为这层元素应用三维变形即可。

触发方法2:直接使用CSS3变形语法

<!DOCTYPE html>

<head>

    <meta charset="UTF-8">

    <title>言成科技/title>

    <style>

        .box1 {

            width: 150px

            height: 150px

            border: 2px solid blue

        }

        .box1 div {

            height: 150px

            background: rgba(0, 0, 0, 0.5)

            -webkit-transform: translate3d(30px, 60px, 20px) rotateX(30deg)

            transform: translate3d(30px, 60px, 20px) rotateX(30deg)

        }

    </style>

</head>

<body>

    <div class="box1">

        <div></div>

    </div>

</body>

</html>

具体三维变形的具体属性详见《CSS3-3D相关知识详解—视角以及变形方向》

3D效果制作

需求

制作一个立方体,并进行旋转

代码实例

<!DOCTYPE HTML>

<html>

<head>

 <meta charset="utf-8" />

 <title>言成科技</title>

 <link rel="stylesheet" type="text/css" href="https://css.h5course.cn/reset-1.0.0.css" />

 <style>

  .main-bac { -webkit-perspective:1500 } /*设定透视距离*/

  .main{

   width:200px 

   height:200px 

   margin: 0 auto

   position:relative

   -webkit-transform-style:preserve-3d

   -webkit-transition:-webkit-transform 2s ease 0s/*过渡时间*/

  }

  /*基本样式*/

  .main p{

   position: absolute 

   margin: 0 

   padding: 0 

   width: 200px 

   height: 200px  

   text-align: center 

   line-height: 200px 

   font-size: 26px 

   opacity:0.5

  }

  /*将第一个元素Z轴向前移动100px,形成第一个面(正面)*/

  .main p:nth-of-type(1) {

   background-color:red

   -webkit-transform:translateZ(100px)

  }

  /*将第一个元素Z轴向前移动100px,绕x轴旋转90度形成上面的面*/

  .main p:nth-of-type(2) {

   background-color:orange 

   -webkit-transform:rotateX(90deg) translateZ(100px)

  }

  /*将第一个元素Z轴向前移动100px,绕x轴旋转-90度形成下边的面*/

  .main p:nth-of-type(3) {

   background-color:yellow

   -webkit-transform:rotateX(-90deg) translateZ(100px)

  }

  /*将第一个元素Z轴向前移动100px,绕y轴旋转90度形成右侧的面*/

  .main p:nth-of-type(4) {

   background-color:green

   -webkit-transform:rotateY(90deg) translateZ(100px)

  }

  /*将第一个元素Z轴向前移动100px,绕y轴旋转-90度形成左侧的面*/

  .main p:nth-of-type(5) {

   background-color:#b435bf

   -webkit-transform:rotateY(-90deg) translateZ(100px)

  }

  /*将第一个元素Z轴向前移动100px,绕y轴旋转180度形成后面(背面)*/

  .main p:nth-of-type(6) {

   background-color:blue

   -webkit-transform:rotateY(180deg) translateZ(100px)

  }

  /*鼠标移入时绕Y轴旋转180度,绕Z轴旋转180度*/

  .main:hover {-webkit-transform:rotateY(180deg) rotateZ(180deg) }   

 </style>

</head>

<body>

 <div class="main-bac">

  <div class="main">

   <p>言成科技</p>

   <p>3D立方体</p>

   <p>HTML5学堂</p>

   <p>3D立方体</p>

   <p>码匠</p>

   <p>JavaScript</p>

  </div>              

 </div>

</body>

</html>

代码解析

当鼠标移入的时候,立方体逐渐的发生旋转(非突变),围绕X轴旋转45度的同时,围绕Y轴旋转45度。

当鼠标移出立方体时,立方体恢复到初始状态。在最开始状态时,并没有采用无限远的视角,设置一定的视角,让刚开始时直视立方体时,不会觉得是一个平面。

3D效果制作-目标效果图

以上资料来源:《HTML5布局之路》

HTML5的诞生给web前端界带来了不小轰动,像什么动画旋转、图片滑块、图片轮播等等这些3D特效,也引发了不少朋友想要学习HTML5的好奇心。最近我一直在做canvas动画效果,发现canvas这个东西做动画不是不可以。相对于flash,它太底层。如果有给力的编辑器或者给力的框架的话,它就能发挥出更大的威力。

于是决定自己写一个简单一点的动画框架,以便能更方便地构建出一些动画效果。

我将分几个章节来讲述我这个小动画框架的实现:

1.通用类的提取:动画对象与帧对象

2.灵与肉的结合:便于拆卸的运动方程

3.进度条的实现:canvas的图片预加载

4.demo测试:通过一个demo测试框架

这一节我们先来说说通用类的提取。

其实上一篇文章我已经用到了这种从flash借鉴来的思路:一个动画对象(类似flash中的元件),一个帧对象(类似flash中的帧)。动画就是在不断在当前帧上绘制每个动画对象来实现的。有了这两个对象,再加上一些运动方法,我们就可以构建出动画来。

首先我们先来看看动画对象Aniele:

/*

*Aniele动画对象

*所有动画对象的始祖

*/

varAniele=function(){

this.img=newImage()

//定义动画对象位置

this.loca={

x:300,

y:300

}

//定义动画对象的大小(可以实现缩放)

this.dw

this.dh

//动画对象的速度属性

this.speed={

x:0,

y:0

}

//设置对象的透明度

this.alpha=1

//设置图像翻转,1为不翻转,-1为翻转

this.scale={

x:1,

y:1

}

//定动画对象的运动方法库

this.motionFncs=[]

}

Aniele.prototype={

//添加运动方法

addMotionFnc:function(name,fnc) {

this.motionFncs[name]=fnc

},

//删除运动方法

deleMotionFnc:function(name){

this.motionFncs[name]=null

},

//遍历运动方法库里的所有运动方法

countMotionFncs:function() {

for(vari=0i

if(this.motionFncs[i]==null)

continue

this.motionFncs[i].call(this)

}

},

//把自己绘制出来的方法,包括功能:水平翻转

draw:function(canvas,ctx){

//存储canvas状态ctx.save()

//实现透明度的改变

ctx.globalAlpha=this.alpha

//实现水平竖直翻转,定义drawImage的两个位置参数dx,dy

vardx=this.loca.x

vardy=this.loca.y

if(this.scale.x!=1||this.scale.y!=1){

if(this.scale.x<0){

console.log(this.img.width)

dx=canvas.width-this.loca.x-this.img.width

ctx.translate(canvas.width,1)

ctx.scale(this.scale.x,1)

}

if(this.scale.y<0){

dy=canvas.height-this.loca.y-this.img.height

ctx.translate(1,canvas.height)

ctx.scale(1,this.scale.y)

}

}

if(this.dw==null)

this.dw=this.img.width

if(this.dh==null)

this.dh=this.img.height

//画出对象

ctx.drawImage(this.img,dx,dy,this.dw,this.dh)

//恢复canvas状态ctx.restore()

}

}

动画对象的主要属性:

this.img=newImage()我们引入一张图片,依附在动画对象上;

this.loca.x等等图片的大小位置透明度等等,便于绘图时调用;

this.motionFncs=[]这个比较关键,我们给动画对象定义一个运动方法库,把动画对象的运动规则都放在这个运动方法库中统一管理(每个动画对象都有自己的运动方法库);

动画对象的主要方法:

addMotionFnc: 为动画对象的运动方法库中添加一个运动方法;

deleMotionFnc:为动画对象的运动方法库中删除一个运动方法;

countMotionFncs:为动画对象遍历运动方法库中的所有运动方法;

draw:把动画对象画在画布上,这里我们会把画布作为参数传到这个方法里面去,便于绘图;

在draw方法里,我封装了一些对图像的简单操作,这些操作在动画中会经常用到:透明,缩放和翻转。

有了这个,我们就好似获得了flash里的一个元件,我们可以通过修改它的属性来随意改变它。

那么帧对象呢?

帧对象肩负着渲染的任务,并且管理所有动画对象:

/*

*Render渲染对象

*管理所有动画对象和渲染

*参数:画布对象,画布上下文*/varRender=function(canvas,ctx) {

//引入画布

this.canvas=canvas

this.ctx=ctx

//创建一个缓冲画布

this.backBuffer=document.('canvas')

this.backBuffer.width=this.canvas.width

this.backBuffer.height=this.canvas.height

this.backBufferctx=this.backBuffer.getContext('2d')

//所有动画对象

this.aniEles=[]

}

Render.prototype={

//初始化画布int

int:function() {

clearInterval(this.sint)

this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height)

this.backBufferctx.clearRect(0,0,this.backBuffer.width,this.backBuffer.height)

},

//设置开始渲染

begin:function() {

this.lastFrame=(newDate()).getTime()

this.sint=setInterval((function(progra){

returnfunction(){progra.render()}

})(this),SECOND)

},

//主渲染方法

render:function() {

//在画布和缓存画布上清除历史帧

this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height)

this.backBufferctx.clearRect(0,0,this.backBuffer.width,this.backBuffer.height)

//保存当前的实时输出帧率this.ftp

this.nowFrame=(newDate()).getTime()

this.ftp=1000/(this.nowFrame-this.lastFrame)

this.lastFrame=this.nowFrame

//调用每个动画对象的运动方法

for(vari=0i

if(this.aniEles[i]==null)

continue

this.aniEles[i].countMotionFncs()

//把对象绘制到后台缓冲画布上

this.aniEles[i].draw(this.backBuffer,this.backBufferctx)

}

//把后台对象绘制到前台

this.ctx.drawImage(this.backBuffer,0,0)

},

//增加动画对象

addAniEle:function(name,aniEle) {

this.aniEles[name]=aniEle

},

//删除动画对象

deleAniEle:function(name) {

this.aniEles[name]=null

}

}

帧对象的主要属性:

this.aniEles=[]用来存储当前画布上所有动画实例的数组;

大家用过canvas载入图片的应该知道,由于图片的异步载入,动画过程中图片会出现闪烁的现象,为了避免这种现象,我采用了双缓冲。

首先后台创建一个画布:

this.backBuffer=document.('canvas')

this.backBuffer.width=this.canvas.width

this.backBuffer.height=this.canvas.height

this.backBufferctx=this.backBuffer.getContext('2d')

我们所有绘制命令都执行在这个后台画布上,最后把后台画布画在前台画布上:

this.ctx.drawImage(this.backBuffer,0,0)

这种先把图绘在后台画布,再把后台画布复制到前台的方法就叫做双缓冲技术。

帧属性的主要方法:

int:用于初始化画布;

begin:开始动画渲染的方法;

render:主渲染的方法;

addAniEle:为当前帧添加动画对象;

deleAniEle:为当前帧删除动画;

我们利用帧对象的流程是:先为当前帧添加动画对象,然后让当前帧开始渲染。

我写了个正方体的模板,你可以按照你想要的把每个面旋转不同角度就行了

<style type="text/css">

html{

font-size:62.5%

}

img{

width:300px

height:300px

}

  #stage{

margin-top:200px

margin-left:auto

margin-right:auto

width:300px

height:300px

perspective:1200px

font-size:5em

font-weight:bold

color:#cc00ff

  }

  .cube{

position:relative

transform:rotateX(-45deg) rotateY(45deg)

transform-style:preserve-3d

transition: all .6s

  }

  .side{

color:blue

text-align:center

width:300px

height:300px

line-height:300px

position:absolute

background:#cc66ff

opacity:0.5

border:1px solid rgba(117,4,24,0.5)

  }

 .front{

transform:translateZ(150px)

 }

 .back{

transform:rotateY(180deg) translateZ(150px)

 }

 .left{

transform:rotateY(-90deg) translateZ(150px)

 }

 .right{

transform:rotateY(90deg) translateZ(150px)

 }

 .top{

transform:rotateX(90deg) translateZ(150px)

 }

 .bottom{

transform:rotatex(-90deg) translateZ(150px)

 }

#stage:hover .cube{

transform:rotateX(-45deg) rotateY(225deg)

transition:transform .6s

}

  </style>

 </head>

 <body>

<div id="stage">

<div class="cube">

<div class="side front"><img src="6.gif" alt="" /></div>

<div class="side back"><img src="2.jpg" alt="" /></div>

<div class="side left"><img src="3.jpg" alt="" /></div>

<div class="side right"><img src="4.jpg" alt="" /></div>

<div class="side top"><img src="5.jpg" alt="" /></div>

<div class="side bottom"><img src="1.jpg" alt="" /></div>

</div>

</div>

  

 </body>