canvas实现十二星座星空图


Posted in Javascript onFebruary 14, 2017

效果如下:

canvas实现十二星座星空图

代码如下:

<!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>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持三水点靠木!

Javascript 相关文章推荐
JavaScript中null与undefined分析
Jul 25 Javascript
jquery全选checkBox功能实现代码(取消全选功能)
Dec 10 Javascript
Jquery修改页面标题title其它JS失效的解决方法
Oct 31 Javascript
js实现获取div坐标的方法
Nov 16 Javascript
分享bootstrap学习笔记心得(组件及其属性)
Jan 11 Javascript
BootStrap实现鼠标悬停下拉列表功能
Feb 17 Javascript
JavaScript 上传文件(psd,压缩包等),图片,视频的实现方法
Jun 19 Javascript
浅析node应用的timing-attack安全漏洞
Feb 28 Javascript
小程序scroll-view安卓机隐藏横向滚动条的实现详解
May 16 Javascript
vue-cli创建的项目中的gitHooks原理解析
Feb 14 Javascript
JavaScript面向对象核心知识与概念归纳整理
May 09 Javascript
JavaScript执行机制详细介绍
Dec 06 Javascript
JavaScript省市级联下拉菜单实例
Feb 14 #Javascript
jqGrid翻页时数据选中丢失问题的解决办法
Feb 13 #Javascript
jQuery实现用户输入自动完成功能
Feb 13 #Javascript
微信小程序 详解页面跳转与返回并回传数据
Feb 13 #Javascript
微信小程序 this和that详解及简单实例
Feb 13 #Javascript
脚本div实现拖放功能(两种)
Feb 13 #Javascript
bootstrap table 数据表格行内修改的实现代码
Feb 13 #Javascript
You might like
PHP 和 HTML
2006/10/09 PHP
PHP关联链接常用代码
2012/11/05 PHP
解析PHP高效率写法(详解原因)
2013/06/20 PHP
PHP strip_tags() 去字符串中的 HTML、XML 以及 PHP 标签的函数
2016/05/22 PHP
yii2学习教程之5种内置行为类详解
2017/08/03 PHP
Javascript 读后台cookie代码
2008/09/15 Javascript
jquery实现在页面加载的时自动为日期插件添加当前日期
2014/08/20 Javascript
简单谈谈javascript中this的隐式绑定
2016/02/22 Javascript
Node.js包管理器Yarn的入门介绍与安装
2016/10/17 Javascript
js print打印网页指定区域内容的简单实例
2016/11/01 Javascript
BootStrapTable 单选及取值的实现方法
2017/01/10 Javascript
详解如何实现一个简单的 vuex
2018/02/10 Javascript
实例讲解JavaScript预编译流程
2019/01/24 Javascript
关于layui时间回显问题的解决方法
2019/09/24 Javascript
微信小程序动态添加和删除组件的现实
2020/02/28 Javascript
利用 JavaScript 实现并发控制的示例代码
2020/12/31 Javascript
Python  连接字符串(join %)
2008/09/06 Python
使用python绘制人人网好友关系图示例
2014/04/01 Python
解析Python中的生成器及其与迭代器的差异
2016/06/20 Python
python pandas获取csv指定行 列的操作方法
2019/07/12 Python
Python实现投影法分割图像示例(二)
2020/01/17 Python
pycharm 多行批量缩进和反向缩进快捷键介绍
2021/01/15 Python
详解修改Anaconda中的Jupyter Notebook默认工作路径的三种方式
2021/01/24 Python
CSS3实现彩色进度条动画的示例
2020/10/29 HTML / CSS
波兰品牌鞋履在线商店:Eastend.pl
2020/01/11 全球购物
linux系统都有哪些运行级别
2012/04/15 面试题
公司会计岗位职责
2014/02/13 职场文书
文明村创建实施方案
2014/03/27 职场文书
护士长竞聘书
2014/03/31 职场文书
意向书范文
2014/03/31 职场文书
公证委托书格式
2014/09/13 职场文书
小学教育见习报告
2014/10/31 职场文书
岗位聘任报告
2015/03/02 职场文书
办公室主任岗位职责范本
2015/03/31 职场文书
超市食品安全承诺书
2015/04/29 职场文书
解决jupyter notebook启动后没有token的坑
2021/04/24 Python