canvas - 炫酷的3D星空
1.国际惯例,先上效果

(⊙o⊙)… 效果图看上去效果并不很炫酷啊,直接戳 这里 看效果吧!
2代码部分
html:
<canvas id="canvas" width="1920" height="1080"></canvas>
css:
*{margin:;padding:;}
/*没啥必须的css*/
js:(这个博主也是够了,那么多的js代码,一点注释都没有,差评!)公子莫慌,由于代码比较多,注释部分就不在这写啦,直接移步 效果展示 查看效果及代码注释。
var Starry = function (canvasId, starCont) {
this.renderLoop = null;
this.framerate = 42;
this.starfield = null;
this.rings_y = 0;
this.altitude = 0;
this.preset = {
radius : 600,
fov : 260,
cruise : 0.01,
angle : 0.006,
vert : -0.001,
rings : 0.2
};
this.state = {
width : document.documentElement.clientWidth,
height : document.documentElement.clientHeight,
altitude : 0,
settings : this.preset,
framerate : 50
};
var S = this
if (!canvasId) return false
S.canvas = document.getElementById(canvasId);
S.canvas.width = document.documentElement.clientWidth;
S.canvas.height = document.documentElement.clientHeight;
S.ctx = S.canvas.getContext('2d');
this.starfield = this.generateStarfield();
}
Starry.prototype = {
reset: function(){
clearTimeout(this.renderLoop)
this.state.width = this.canvas.width = document.documentElement.clientWidth;
this.state.height = this.canvas.height = document.documentElement.clientHeight;
this.init()
},
generateStarfield : function() {
var radius = this.state.settings.radius;
var star_count = Math.min( 1000, Math.round( this.canvas.offsetWidth / 2 ) );
var stars = new Array( star_count );
for (var i = 0; i < stars.length; i++) {
stars[i] = {
x: ( Math.random() * ( radius * 2 ) ) - ( radius ),
y: ( Math.random() * ( radius * 2 ) ) - ( radius ),
z: ( Math.random() * ( radius * 2 ) ) - ( radius )
}
};
return stars;
},
move : function ( stars ) {
var angle = this.state.settings.angle;
var vert = this.state.settings.vert;
var fov = this.state.settings.fov;
var cruise = this.state.settings.cruise;
var radius = this.state.settings.radius;
var cosRX = Math.cos(angle);
var sinRX = Math.sin(angle);
var cosRY = Math.cos(vert);
var sinRY = Math.sin(vert);
return stars.map( function(star){
var tempx = star.x;
var tempy = star.y;
var tempz = star.z + fov;
var x = ( tempx * cosRX ) + ( tempz * sinRX );
var y = ( tempy * cosRY ) + ( tempz * sinRY );
// Depth based on X
// var z = ( tempx * -sinRX ) + ( tempz * cosRX );
// Depth based on Y
var z = ( tempy * -sinRY ) + ( tempz * cosRY ) - cruise;
var limit = radius;
// x
if ( x < -limit ) x = limit;
else if ( x > limit ) x = -limit;
// y
if ( y < -limit ) y = limit;
else if ( y > limit ) y = - limit;
// z
if ( z < -limit ) z = limit;
else if ( z > limit ) z = -limit;
return { x:x, y:y, z: z - fov };
});
},
run: function () {
var S = this;
var width = this.state.width
var height = this.state.height
var fov = this.state.settings.fov
var rings = this.state.settings.rings
S.ctx.clearRect(0, 0, width, height);
S.starfield.forEach(function(item, index){
var scale = ( fov / 2 ) / ( fov + item.z );
var x2d = ( item.x * scale ) + ( width / 2 );
var y2d = ( item.y * scale ) + ( height / 2 );
var opacity = Math.min( Math.max( Math.abs( scale ), 0.1 ), 1 );
S.ctx.beginPath();
S.ctx.arc(x2d, y2d, Math.min( Math.abs( scale ), 3 ) , 0, 360);
S.ctx.fillStyle = 'rgba(167,180,224,'+opacity+')';
S.ctx.fill();
})
var ring_radius = height * 1.6;
var ring_center = { x: width / 4, y: height * 2 };
S.ctx.beginPath();
S.ctx.arc( ring_center.x, ring_center.y + this.rings_y, ring_radius, 0, 360);
S.ctx.lineWidth = 1;
S.ctx.strokeStyle = 'rgba(167,180,224,0.15)';
S.ctx.stroke();
S.ctx.beginPath();
S.ctx.arc( ring_center.x + 60, ring_center.y + 60 + ( this.rings_y * 0.8 ), ring_radius, 0, 360);
S.ctx.lineWidth = 1;
S.ctx.strokeStyle = 'rgba(167,180,224,0.05)';
S.ctx.stroke();
this.rings_y += rings;
if( this.rings_y > ( width / 2 ) ) {
this.rings_y = -( width/ 2 )
};
S.starfield = S.move( S.starfield )
var _this = this;
this.renderLoop = setTimeout(function (){_this.run()}, _this.state.framerate)
},
init: function () {
this.run();
},
pause: function () {
var _this = this;
clearTimeout(_this.renderLoop)
}
}
var starbg = new Starry ('canvas', 100);
window.onload = function () {
starbg.init();
}
window.onresize = function () {
starbg.reset();
}
(未完)
canvas - 炫酷的3D星空的更多相关文章
- 纯CSS炫酷的3D旋转
<html><head><meta charset="utf-8"><title>纯CSS炫酷的3D旋转</title> ...
- 初级开发者也能码出专业炫酷的3D地图吗?
好看的3D地图搭建出来,一定是要能为开发者所用与业务系统开发中才能真正地体现价值.基因于此,CityBuilder建立了与ThingJS的通道——直转ThingJS代码,支持将配置完成的3D地图一键转 ...
- canvas炫酷时钟
canvas炫酷时钟 实现的功能 主要用到canvas的一些基础api 直接看效果 html: <canvas id="myCanvas" width="500&q ...
- 基于jQuery和CSS3炫酷图片3D旋转幻灯片特效
在线预览 源码下载 iPresenter是一款效果非常炫酷的jQuery和CSS3 3D旋转幻灯片特效插件.你可以使用它来制作产品展示.图片画廊或者各种幻灯片和轮播图特效.这款幻灯片插件的特点有: ...
- HTML5 canvas炫酷棱镜效果的幻灯片特效
这是一款效果很炫酷华丽的HTML5 canvas带棱镜效果的幻灯片特效. 这个特效在每个幻灯片的前面放置一个图形.并将图形制作为三棱镜效果.它底下的幻灯片图片会被"折射"到棱镜上面 ...
- WebGIS简单实现一个区域炫酷的3D立体地图效果
1.别人的效果 作为一个GIS专业的,做一个高大上的GIS系统一直是我的梦想,虽然至今为止还没有做出一个理想中的系统,但是偶尔看看别人做的,学习下别人的技术还是很有必要的.眼睛是最容易误导我们的,有时 ...
- 【pano2vr】网页Flash中简单实现炫酷的3D模型制作
花了两天时间学习如何能够高效的实现3D模型效果,毕竟是从0开始学习,感觉pano2vr这款软件挺容易上手,并且可以很容易实现简单的热点交互,可以根据交互需求设置皮肤,故将这一款软件推荐给大家: 1.简 ...
- 24、Cocos2dx 3.0游戏开发找小三之网格动作:高炫酷的3D动作
重开发人员的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/37596763 网格动作类似于动作特效,能够实现翻转. ...
- 8个经典炫酷的HTML5 Canvas动画欣赏
HTML5非常强大,尤其是Canvas技术的应用,让HTML5几乎可以完成所有Flash能完成的效果.本文精选了8个经典炫酷的HTML5 Canvas动画欣赏,每一个都提供全部的源代码,希望对你有所帮 ...
随机推荐
- CentOS 6.8 curl支持的NSS修改为OpenSSL的方法
在CentOS 6.8的系统里面的curl支持的https是nss版本的,而不是openssl,所以在php使用curl访问https的时候会报Unable to load client key -8 ...
- ELKStack入门篇(五)之实用架构解析
(1)用户通过nginx或haproxy访问ELK日志统计平台,IP地址为keepalived的vip地址. (2)nginx将请求转发到kibana (3)kibana到elasticsearch获 ...
- dsp6657的helloworld例程测试-第二篇-CFG文件
1. 上一篇疑问,int StackTest()这个函数是怎么运行的,后来在.cfg文件找到了答案,.cfg包含丰富的信息,对于用惯C语言的,确实不太习惯 var Memory = xdc.useMo ...
- 【MYSQL用户创建报错】ERROR 1396 (HY000): Operation CREATE USER failed for 'user1'@'%'
原文参考自:http://blog.csdn.net/u011575570/article/details/51438841 1.创建用户的时候报错ERROR 1396 (HY000): Operat ...
- DevOps是一种文化,不是角色!
一.DevOps是一种文化,不是角色! 软件无处不在.在如今的世界里,每个主流公司/组织都和软件开发息息相关,并且公司需要向软件一样运作.更快且更敏捷,同时保证安全性和可靠性,这样的要求前所未有的强烈 ...
- ln in Linux
默认情况(硬连接) ln 目标 连接名称 ll -i 显示文件的inode信息,即文件节点信息 ➜ test1 ll -i 1.txt 27987655 -rw-r--r-- 1 myuser ...
- 「Leetcode」976. Largest Perimeter Triangle(C++)
分析 好久不刷题真的思维僵化,要考虑到这样一个结论:如果递增的三个数\(x_i,x_{i+1},x_{i+2}\)不符合题意,那么最大的两边之差一定大于等于第一条边,那么任何比第一条边小的都不能成立. ...
- selenium webdriver API详解(三)
本系列主要讲解webdriver常用的API使用方法(注意:使用前请确认环境是否安装成功,浏览器驱动是否与谷歌浏览器版本对应) 一:获取页面元素的文本内容:text 例:获取我的博客名字文本内容 代码 ...
- Vue学习计划基础笔记(三)-class与style绑定,条件渲染和列表渲染
Class与style绑定.条件渲染和列表渲染 目标: 熟练使用class与style绑定的多种方式 熟悉v-if与v-for的用法,以及v-if和v-for一起使用的注意事项 class与style ...
- Spring 定时任务Scheduled 开发详细图文
Spring 定时任务Scheduled 开发 文章目录 一.前言 1.1 定时任务 1.2 开发环境 1.3 技术实现 二.创建包含WEB.xml 的Maven 项目 2.1 创建多模块项目task ...