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动画欣赏,每一个都提供全部的源代码,希望对你有所帮 ...
随机推荐
- BZOJ2140_稳定婚姻_KEY
题目传送门 暴力直接对于每个点跑一遍二分图匹配,能拿四十分. 然而我们考虑正解. 对于一对Couple我们建♂->♀的一条边,对于一对曾经有恋情的情侣我们建♀->♂的一条边. 跑Tarja ...
- thinkphp5 开启多语言
一.配置点击打开链接1.开启语言包功能'lang_switch_on' => true,2.支持的语言列表'lang_list' => ['zh-cn','en-us'],二.语言定义(默 ...
- Oracle dba权限下修改用户密码 授予用户权限 解锁用户
1.修改用户密码 alter user scott identified by 123 2.授予用户权限 grant connect,resource to scott 3.解锁用户 alter us ...
- MySQL数据库之单双表查询
单表查询 先创建表 #创建表 create table employee( id int not null unique auto_increment, name varchar(20) not nu ...
- PHPCMS的产品筛选功能
如下图所示功能: 首先,用下面这些代码替换掉phpcms/libs/functions/extention.func.php的内容 <?php /** * extention.func.php ...
- Python基础灬函数(定义,参数)
函数 函数定义 # 定义一个计算绝对值的函数 def cal_abs(x): if x >= 0: return x else: return -x # 调用函数 print('-1的绝对值是: ...
- 算法笔记(c++)--c++中碰到的一些用法
算法笔记(c++)--c++中碰到的一些用法 toupper(xxx)可以变成大写; tolower(xx)小写 isalpha(xxx)判断是不是字母 isalnum(xx)判断是不是数字 abs( ...
- 亚马逊AWS业务副总裁:如何在基础设施上降成本
腾讯科技 林靖东 11月17日编译 亚马逊Amazon Web Services业务的副总裁.著名工程师詹姆斯汉密尔顿(James Hamilton)在AWS re:Invent大会上解释了公司是如何 ...
- win10 redis安装教程
下载解压,没什么好说的,在解压后的目录下有以下这些文件: 在 命令行 启动服务端 redis目录下执行: redis-server.exe redis.windows.conf 如果需要 开机启动:执 ...
- pygame (1) 移动小乌龟
小乌龟图片素材: 第一个简单的小游戏: 小乌龟会不断的移动,并且每当到达窗口的左右边界的时候,还会自动的掉头. 源码: import pygame import sys# 导入sys模块,退出时使用 ...