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 相关文章推荐
jquery JSON的解析方式
Jul 25 Javascript
基于jquery的仿百度的鼠标移入图片抖动效果
Sep 17 Javascript
读jQuery之十二 删除事件核心方法
Jul 31 Javascript
一款基jquery超炫的动画导航菜单可响应单击事件
Nov 02 Javascript
JavaScript实现邮箱地址自动匹配功能代码
Nov 28 Javascript
javascript判断元素存在和判断元素存在于实时的dom中的方法
Jan 17 Javascript
bootstrap实现动态进度条效果
Mar 08 Javascript
JS基于设计模式中的单例模式(Singleton)实现封装对数据增删改查功能
Feb 06 Javascript
Layui 解决表格异步调用后台分页的问题
Oct 26 Javascript
在vue中使用cookie记住用户上次选择的实例(本次例子中为下拉框)
Sep 11 Javascript
vue3.0 自适应不同分辨率电脑的操作
Feb 06 Vue.js
关于JavaScript轮播图的实现
Nov 20 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写的小东西
2006/12/06 PHP
PHP 事务处理数据实现代码
2010/05/13 PHP
hessian 在PHP中的使用介绍
2010/12/13 PHP
PHP通过iconv将字符串从GBK转换为UTF8字符集
2011/07/18 PHP
PHP弹出对话框技巧详细解读
2015/09/26 PHP
JavaScript静态的动态
2006/09/18 Javascript
让回调函数 showResponse 也带上参数的代码
2007/08/13 Javascript
namespace.js Javascript的命名空间库
2011/10/11 Javascript
解析javascript 实用函数的使用详解
2013/05/10 Javascript
jQuery插件分享之分页插件jqPagination
2014/06/06 Javascript
jquery数组过滤筛选方法grep()简介
2014/06/06 Javascript
ajax级联菜单实现方法实例分析
2016/11/28 Javascript
获取JavaScript异步函数的返回值
2016/12/21 Javascript
JavaScript仿聊天室聊天记录
2016/12/27 Javascript
nodejs中Express与Koa2对比分析
2018/02/06 NodeJs
Vue中在新窗口打开页面及Vue-router的使用
2018/06/13 Javascript
Angular通过指令动态添加组件问题
2018/07/09 Javascript
jquery获取img的src值实例介绍
2019/01/16 jQuery
VUEX-action可以修改state吗
2019/11/19 Javascript
python利用hook技术破解https的实例代码
2013/03/25 Python
python实现bitmap数据结构详解
2014/02/17 Python
python通过shutil实现快速文件复制的方法
2015/03/14 Python
python实现本地图片转存并重命名的示例代码
2018/10/27 Python
浅析Windows 嵌入python解释器的过程
2019/07/26 Python
python 导入数据及作图的实现
2019/12/03 Python
无需压缩软件,用python帮你操作压缩包
2020/08/17 Python
简单的命令查看安装的python版本号
2020/08/28 Python
python 下载m3u8视频的示例代码
2020/11/11 Python
爱情检讨书大全
2014/01/21 职场文书
经典婚礼主持开场白
2014/03/13 职场文书
优秀班主任先进事迹材料
2014/12/16 职场文书
沈阳故宫导游词
2015/01/31 职场文书
退伍军人感言
2015/08/01 职场文书
闭幕词的写作格式与范文!
2019/06/24 职场文书
golang gopm get -g -v 无法获取第三方库的解决方案
2021/05/05 Golang
MySQL 表空间碎片的概念及相关问题解决
2021/05/07 MySQL