代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Css
】
HTML5 Canvas 3D 立体轮转效果
作者:
彭博
/ 发布于
2012/7/16
/
570
<div><!DOCTYPE html> <html lang="en" > <head> <meta charset="utf-8" /> <title>HTML5 3D 立体轮转效果 | Script Tutorials</title> <link href="css/main.css" rel="stylesheet" type="text/css" /> <script src="js/script.js"></script> </head> <body> <header> <h2><a href='http://www.osctools.net/'>OSCTools</a> HTML5 3D 立体轮转效果</h2> </header> <canvas id="slideshow" width="1280" height="800"></canvas> </body> </html></div> <div></div> <div></div> <div>var canvas, ctx; var aImages = []; var points = []; var triangles = []; var textureWidth, textureHeight; var lev = 3; var angle = 0; // scene vertices var vertices = [ new Point3D(-2,-1,2), new Point3D(2,-1,2), new Point3D(2,1,2), new Point3D(-2,1,2), new Point3D(-2,-1,-2), new Point3D(2,-1,-2), new Point3D(2,1,-2), new Point3D(-2,1,-2) ]; // scene faces (6 faces) var faces = [[0,1,2,3],[1,5,6,2],[5,4,7,6],[4,0,3,7],[0,4,5,1],[3,2,6,7]]; function Point3D(x,y,z) { this.x = x; this.y = y; this.z = z; this.rotateX = function(angle) { var rad, cosa, sina, y, z rad = angle * Math.PI / 180 cosa = Math.cos(rad) sina = Math.sin(rad) y = this.y * cosa - this.z * sina z = this.y * sina + this.z * cosa return new Point3D(this.x, y, z) } this.rotateY = function(angle) { var rad, cosa, sina, x, z rad = angle * Math.PI / 180 cosa = Math.cos(rad) sina = Math.sin(rad) z = this.z * cosa - this.x * sina x = this.z * sina + this.x * cosa return new Point3D(x,this.y, z) } this.rotateZ = function(angle) { var rad, cosa, sina, x, y rad = angle * Math.PI / 180 cosa = Math.cos(rad) sina = Math.sin(rad) x = this.x * cosa - this.y * sina y = this.x * sina + this.y * cosa return new Point3D(x, y, this.z) } this.projection = function(viewWidth, viewHeight, fov, viewDistance) { var factor, x, y factor = fov / (viewDistance + this.z) x = this.x * factor + viewWidth / 2 y = this.y * factor + viewHeight / 2 return new Point3D(x, y, this.z) } } // array of photos var aImgs = [ 'http://static.oschina.net/uploads/space/2012/0709/105428_4aoW_28.jpg', 'http://static.oschina.net/uploads/space/2012/0607/142131_2PqS_28.jpg', 'http://www.oschina.net/img/events/shenzhen-20120526.jpg', 'http://www.oschina.net/img/events/chengdu-20120415.jpg', 'http://www.oschina.net/img/events/shanghai-20120107.jpg', 'http://www.oschina.net/img/events/shenzhen-20111210.jpg', 'http://www.oschina.net/img/events/zhuhai-20111105.jpg', 'http://www.oschina.net/img/events/shenzhen-20110626.jpg' ]; for (var i = 0; i < aImgs.length; i++) { var oImg = new Image(); oImg.src = aImgs[i]; aImages.push(oImg); oImg.onload = function () { textureWidth = oImg.width; textureHeight = oImg.height; } } window.onload = function(){ // creating canvas objects canvas = document.getElementById('slideshow'); ctx = canvas.getContext('2d'); // prepare points for (var i = 0; i <= lev; i++) { for (var j = 0; j <= lev; j++) { var tx = (i * (textureWidth / lev)); var ty = (j * (textureHeight / lev)); points.push({ tx: tx, ty: ty, nx: tx / textureWidth, ny: ty / textureHeight, ox: i, oy: j }); } } // prepare triangles ---- var levT = lev + 1; for (var i = 0; i < lev; i++) { for (var j = 0; j < lev; j++) { triangles.push({ p0: points[j + i * levT], p1: points[j + i * levT + 1], p2: points[j + (i + 1) * levT], up: true }); triangles.push({ p0: points[j + (i + 1) * levT + 1], p1: points[j + (i + 1) * levT], p2: points[j + i * levT + 1], up: false }); } } drawScene(); }; function drawScene() { // clear context ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); // rotate scene var t = new Array(); for (var iv = 0; iv < vertices.length; iv++) { var v = vertices[iv]; var r = v.rotateY(angle); //var r = v.rotateX(angle).rotateY(angle); var prj = r.projection(ctx.canvas.width, ctx.canvas.height, 1000, 3); t.push(prj) } var avg_z = new Array(); for (var i = 0; i < faces.length; i++) { var f = faces[i]; avg_z[i] = {"ind":i, "z":(t[f[0]].z + t[f[1]].z + t[f[2]].z + t[f[3]].z) / 4.0}; } // get around through all faces for (var i = 0; i < faces.length; i++) { var f = faces[avg_z[i].ind]; if (t[f[3]].z+t[f[2]].z+t[f[1]].z+t[f[0]].z > -3) { ctx.save(); // draw surfaces ctx.fillStyle = "rgb(160,180,160)"; ctx.beginPath(); ctx.moveTo(t[f[0]].x,t[f[0]].y); ctx.lineTo(t[f[1]].x,t[f[1]].y); ctx.lineTo(t[f[2]].x,t[f[2]].y); ctx.lineTo(t[f[3]].x,t[f[3]].y); ctx.closePath(); ctx.fill(); // draw stretched images if (i < 4) { var ip = points.length; while (--ip > -1) { var p = points[ip]; var mx = t[f[0]].x + p.ny * (t[f[3]].x - t[f[0]].x); var my = t[f[0]].y + p.ny * (t[f[3]].y - t[f[0]].y); p.px = (mx + p.nx * (t[f[1]].x + p.ny * (t[f[2]].x - t[f[1]].x) - mx)) + p.ox; p.py = (my + p.nx * (t[f[1]].y + p.ny * (t[f[2]].y - t[f[1]].y) - my)) + p.oy; } var n = triangles.length; while (--n > -1) { var tri = triangles[n]; var p0 = tri.p0; var p1 = tri.p1; var p2 = tri.p2; var xc = (p0.px + p1.px + p2.px) / 3; var yc = (p0.py + p1.py + p2.py) / 3; ctx.save(); ctx.beginPath(); ctx.moveTo((1.05 * p0.px - xc * 0.05), (1.05 * p0.py - yc * 0.05)); ctx.lineTo((1.05 * p1.px - xc * 0.05), (1.05 * p1.py - yc * 0.05)); ctx.lineTo((1.05 * p2.px - xc * 0.05), (1.05 * p2.py - yc * 0.05)); ctx.closePath(); ctx.clip(); // transformation var d = p0.tx * (p2.ty - p1.ty) - p1.tx * p2.ty + p2.tx * p1.ty + (p1.tx - p2.tx) * p0.ty; ctx.transform( -(p0.ty * (p2.px - p1.px) - p1.ty * p2.px + p2.ty * p1.px + (p1.ty - p2.ty) * p0.px) / d, // m11 (p1.ty * p2.py + p0.ty * (p1.py - p2.py) - p2.ty * p1.py + (p2.ty - p1.ty) * p0.py) / d, // m12 (p0.tx * (p2.px - p1.px) - p1.tx * p2.px + p2.tx * p1.px + (p1.tx - p2.tx) * p0.px) / d, // m21 -(p1.tx * p2.py + p0.tx * (p1.py - p2.py) - p2.tx * p1.py + (p2.tx - p1.tx) * p0.py) / d, // m22 (p0.tx * (p2.ty * p1.px - p1.ty * p2.px) + p0.ty * (p1.tx * p2.px - p2.tx * p1.px) + (p2.tx * p1.ty - p1.tx * p2.ty) * p0.px) / d, // dx (p0.tx * (p2.ty * p1.py - p1.ty * p2.py) + p0.ty * (p1.tx * p2.py - p2.tx * p1.py) + (p2.tx * p1.ty - p1.tx * p2.ty) * p0.py) / d // dy ); ctx.drawImage(aImages[i], 0, 0); ctx.restore(); } } } } // shift angle and redraw scene angle += 0.3; setTimeout(drawScene, 40); } </div>
试试其它关键字
立体轮转效果
同语言下
.
前端PC-移动端CSS公共样式+HTML
.
手机端页面通用样式
.
H5页面通用头部设置
.
elect默认样式美化代码兼容移动端和pc端
.
按钮效果 css
.
唤醒app
.
放大效果
.
html5+css3实现上拉和下拉刷新
.
html上传图片后,在页面显示上传的图片
.
html5实现点击弹出图片
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
彭博
贡献的其它代码
(
4
)
.
HTML5 实现文件拖放上传
.
HTML5 Canvas 3D 立体轮转效果
.
HTML5 Canvas 时钟
.
HTML5 Canvas 自定义笔刷
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3