<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>canvas星座</title>
<style>
* {
margin: 0;
padding: 0;
}
#box{
margin:10px 0 0 10px;;
}
input{
outline: none;
font-size:16px;
}
p{
margin-bottom: 10px
}
input[type=date]{
height:36px;
text-indent:10px;
}
input[type=button]{
background:#3A92FF;
color:white;
border: none;
height:40px;
padding:0 10px;
margin-left: -2px;
}
#canvas{
margin-left:10px;
background-color: #000;
display: none;
}
</style>
</head>
<body>
<div id="box">
<p>请选择您的出生日期</p>
<input type="date" id="birth"><input type="button" id="btn" value="展示我的星空图">
</div>
 <canvas id="canvas" width="550" height="550">你的浏览器不支持canvas</canvas>
<script type="text/javascript">
var box=document.getElementById('box');
var birth=document.getElementById('birth');
var btn=document.getElementById('btn');
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var g1,date,timer;
ctx.font = "30px Courier New";
/*星座位置*/
var star={
"白羊座":[
[[0.30,0.78],[0.34,0.66],[0.28,0.48],[0.60,0.26],[0.65,0.20],[0.71,0.23],[0.70,0.32],[0.72,0.36]],
[ [0,1,2,3,4,5],[3,6,7]]
],
"天秤座":[
[[0.16,0.67],[0.34,0.60],[0.60,0.27],[0.75,0.23],[0.84,0.47],[0.63,0.74],[0.51,0.78]],
[[0,1,2,3,4,5,6]]
],
"摩羯座":[
[[0.78,0.21],[0.78,0.34],[0.75,0.45],[0.75,0.70],[0.69,0.78],[0.31,0.66],[0.22,0.49],[0.30,0.53],[0.53,0.54]],
[[0,1,2,3,4,5,6,7,8,1]]
],
"水瓶座":[
[[0.45,0.21],[0.37,0.35],[0.27,0.51],[0.30,0.58],[0.29,0.64],[0.48,0.79],[0.51,0.71],[0.58,0.68],[0.73,0.74],[0.43,0.53],[0.53,0.47]],
[[0,1,2,3,4,5,6,7,8],[2,9,10]]
],
"双鱼座":[
[[0.28,0.43],[0.28,0.53],[0.36,0.73],[0.43,0.78],[0.50,0.70],[0.53,0.62],[0.57,0.58],[0.63,0.43],[0.67,0.39],[0.74,0.39],[0.77,0.34],[0.72,0.30],[0.75,0.22],[0.23,0.50],[0.66,0.33]],
[[0,1,2,3,4,5,6,7,8,9,10,11,12],[0,13,1],[8,14,11]]
],
"金牛座":[
[[0.29,0.21],[0.39,0.36],[0.50,0.51],[0.50,0.57],[0.61,0.63],[0.77,0.71],[0.79,0.79],[0.22,0.43],[0.39,0.57],[0.60,0.71],[0.67,0.76]],
[[0,1,2,3,4,5,6],[7,8,3],[4,9,10]]
],
"双子座":[
[[0.18,0.37],[0.25,0.45],[0.35,0.55],[0.39,0.68],[0.49,0.77],[0.51,0.63],[0.57,0.78],[0.28,0.29],[0.42,0.32],[0.61,0.49],[0.72,0.60],[0.83,0.59],[0.69,0.75],[0.22,0.54],[0.35,0.43],[0.48,0.21]],
[[0,1,2,3,4],[2,5,6],[7,8,9,10,11],[9,12],[13,1,14,8,15]]
],
"巨蟹座":[
[ [0.16,0.39],[0.27,0.36],[0.52,0.49],[0.57,0.65],[0.83,0.78],[0.44,0.21]],
[[0,1,2,3,4],[2,5]]
],
"狮子座":[
[
[0.16,0.75],[0.23,0.67],[0.39,0.77],[0.71,0.53],[0.64,0.39],[0.55,0.37],[0.47,0.27],[0.54,0.24],[0.60,0.27],[0.85,0.56]],
[[0,1,2,3,4,5,6,7,8],[3,9]]
],
"处女座":[
[[0.16,0.59],[0.35,0.63],[0.44,0.70],[0.62,0.51],[0.77,0.46],[0.84,0.37],[0.60,0.42],[0.65,0.26],[0.34,0.75]],
[[0,1,2,3,4,5],[3,6,7],[2,8]]
],
"天蝎座":[
[[0.17,0.50],[0.28,0.63],[0.19,0.70],[0.28,0.78],[0.41,0.77],[0.49,0.72],[0.57,0.55],[0.59,0.44],[0.69,0.31],[0.74,0.21],[0.82,0.29],[0.79,0.44],[0.73,0.50],[0.38,0.47]],
[[0,1,2,3,4,5,6,7,8,9,10,11,12],[1,13],[8,11]]
],
"射手座":[
[[0.22, 0.66],[0.24,0.51],[0.45,0.40],[0.54,0.37],[0.59,0.43],[0.66,0.50],[0.63,0.60],[0.66,0.67],[0.74,0.53],[0.77,0.39],[0.49,0.47],[0.29,0.68],[0.30,0.78],[0.48,0.21],[0.52,0.27],[0.59,0.29]],
[[0,1,2,3,4,5,6,7,8,9],[2,10,11,12],[10,4],[13,14,15,3],[14,3]]
]
};
/*根据出生日期获取星座信息*/
function getStarSign(ts){
var d=new Date(ts),sign=d.getMonth()*100+d.getDate();
switch(true)
{
case sign<20:
return "摩羯座";
case sign<119:
return "水瓶座";
case sign<221:
return "双鱼座";
case sign<320:
return "白羊座";
case sign<421:
return "金牛座";
case sign<522:
return "双子座";
case sign<623:
return "巨蟹座";
case sign<723:
return "狮子座";
case sign<823:
return "处女座";
case sign<923:
return "天秤座";
case sign<1022:
return "天蝎座";
case sign<1122:
return "射手座";
default:
return "摩羯座";
}
}
/*绘制星座*/
function constellation(ctx,ary,w,h){
/*创建线条*/
var points=ary[0], lines=ary[1];
ctx.strokeStyle="#FFF";
var len=lines.length,i,ii,line,point;
ctx.beginPath();
while(len--)
{
i=1;
line=lines[len];
ii=line.length;
point=points[line[0]];
ctx.moveTo(point[0]*w,point[1]*h);
for(;i<ii;i++){
point=points[line[i]];
ctx.lineTo(point[0]*w,point[1]*h);
}
}
ctx.stroke();
/*创建小圆*/
var cur;
i=0;
ii=points.length;
for(;i<ii;i++){
cur=points[i];
new Circle(cur[0]*w,cur[1]*h).draw();//new出实例
}

}
/*创建小圆的构造函数*/
function Circle(x,y,r){
this.x=x;
this.y=y;
this.r=r||Math.round(Math.random()*8+4);
}
Circle.prototype={
draw:function(){
ctx.beginPath();
g1=ctx.createRadialGradient(this.x,this.y,Math.round(Math.random()*1+1),this.x,this.y,Math.round(Math.random()*3+6));
g1.addColorStop(0,'rgba(255,255,255,.9)');
g1.addColorStop(1,'rgba(0,0,0,.1)');
ctx.arc(this.x,this.y,this.r,0,Math.PI*2,true);
ctx.fillStyle =g1;
ctx.closePath();
ctx.fill();
return this;
}};
/*点击‘展示我的星空图’按钮*/
btn.onclick=function(e){
clearInterval(timer);
e.preventDefault();
if (!birth.value){
alert("请选择您的出生日期");
}else{
canvas.style.display="block";
date = new Date(birth.value.replace(/-/g,'/')).getTime();
timer=setInterval(function(){
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.strokeText(getStarSign(date), 50, 50);
constellation(ctx,star[getStarSign(date)],canvas.width,canvas.height);
},500);
}
};
</script>
</body>
</html>

canvas 绘制星座图(好玩)--转载的更多相关文章

  1. 用canvas绘制折线图

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

  2. 使用Canvas绘制背景图

    原文  http://www.imququ.com/post/use-canvas-as-background-image.html 最近iCloud Web的Beta版换了UI,整体风格变得和iOS ...

  3. Android自己定义组件系列【9】——Canvas绘制折线图

    有时候我们在项目中会遇到使用折线图等图形,Android的开源项目中为我们提供了非常多插件,可是非常多时候我们须要依据详细项目自己定义这些图表,这一篇文章我们一起来看看怎样在Android中使用Can ...

  4. Android自定义控件 -Canvas绘制折线图(实现动态报表效果)

    有时候我们在项目中会遇到使用折线图等图形,Android的开源项目中为我们提供了很多插件,但是很多时候我们需要根据具体项目自定义这些图表,这一篇文章我们一起来看看如何在Android中使用Canvas ...

  5. Android自定义组件系列【9】——Canvas绘制折线图

    有时候我们在项目中会遇到使用折线图等图形,Android的开源项目中为我们提供了很多插件,但是很多时候我们需要根据具体项目自定义这些图表,这一篇文章我们一起来看看如何在Android中使用Canvas ...

  6. canvas 绘制八卦图

    绘制要点: 1.getContext('2d'); -->绘图环境,2维空间 2.fillRect(x,y,w,h); -->矩形:实心(黑色背景) 3.strokeRect(x,y,w, ...

  7. 使用canvas绘制扇形图

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

  8. canvas绘制折线图(仿echarts)

    遇到的问题:Retina屏上字体线条模糊问题 解决方案:放大canvas的大小,然后用css压缩回原大小,例如:想要900*400的画布,先将画布设置为 width="1800px" ...

  9. canvas绘制折线图

    效果图: 重难点: 1.画布左上角的顶点的坐标为(0 ,0),右下角的坐标最大,与平常思维相反 2.数据的处理 html代码: <!DOCTYPE html><html lang=& ...

随机推荐

  1. 【PHP】session失效时间

    最近用到php中session时,忽然发现php中的session有点让人头疼啊,要设置一个严格的特定时间内过期的session还真不太容易!后来在网上查询时,发现这个问题还真是有点普遍,网上也有关于 ...

  2. 弱网络模拟测试工具---易测app

    易测功能介绍   易测是一款基于无线客户端研发场景的通用测试工具, 它通过在研发人员的自持机上提供各种辅助能力&标准化的专项测试服务来提升研发质量&效率.   易测app是阿里巴巴做的 ...

  3. 【前端学习笔记】arguments相关

    arguments转数组: (function() { console.log(arguments instanceof Array); // --> false console.log(Obj ...

  4. Android 水波纹点击效果(Ripple Effect)

    上周Android发布了Android M的Preview版本.但想必Android5.0很多炫酷效果,多数开发者还没有使用过,那更不要说广大用户了. 本文介绍的是Android5.0中其中一个炫酷的 ...

  5. varnish启动报错

    错误1.Starting Varnish Cache: Error: Cannot open socket: :80: Address family not supported by protocol ...

  6. BZOJ3875 AHOI2014/JSOI2014骑士游戏(动态规划)

    容易想到设f[i]为杀死i号怪物所消耗的最小体力值,由后继节点更新.然而这显然是有后效性的,正常的dp没法做. 虽然spfa已经死了,但确实还是挺有意思的.只需要用spfa来更新dp值就可以了.dij ...

  7. CF662C Binary Table 枚举 FWT

    题面 洛谷题面 (虽然洛谷最近有点慢) 题解 观察到行列的数据范围相差悬殊,而且行的数量仅有20,完全可以支持枚举,因此我们考虑枚举哪些行会翻转. 对于第i列,我们将它代表的01串提取出来,表示为\( ...

  8. POJ.2251 Dungeon Master (三维BFS)

    POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...

  9. django 自定义认证

    在Django中自定义身份验证 Django 自带的认证系统足够应付大多数情况,但你或许不打算使用现成的认证系统.定制自己的项目的权限系统需要了解哪些一些关键点,即Django中哪些部分是能够扩展或替 ...

  10. php整理

    linux上安装php配套环境有些繁琐 推荐使用一键安装: https://lnmp.org/install.html phalcon框架环境: centos7下php7.0.x安装phalcon框架