涌html编写星空图,canvas实现十二星座星空图

news/2024/12/3 5:04:27/

效果如下:

019dc5a8a85a088f37c3e2ed42d79741.png

代码如下:

canvas星座

* {

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;

}

请选择您的出生日期

你的浏览器不支持canvas

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

point=points[line[i]];

ctx.lineTo(point[0]*w,point[1]*h);

}

}

ctx.stroke();

/*创建小圆*/

var cur;

i=0;

ii=points.length;

for(;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.οnclick=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);

}

};

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


http://www.ppmy.cn/news/433118.html

相关文章

Rasa使用——以星座运势为case

经过两周的摸索和实验&#xff0c;今天终于把星座运势推进到最后的测试阶段。尽管跑通一个demo会觉得rasa非常简单&#xff0c;但完成一个function&#xff0c;从分离不同的intent&#xff0c;对话框架设计&#xff0c;设置rasa pipeline&#xff0c;具体月份的读取&#xff0c…

用python来抓取qq空间好友星座和年龄等信息

############## 绘制星座信息图表 获取性别信息、绘制图表 ######### import json import requests from lxml import etree import pandas as pd import pygal import pickle ################################################################# ################ part 1…

12星座的出生年月日性格_十二星座的日期和性格特点

展开全部 水瓶座(01月e68a84323131333532363134313032313635333133343134633521日─02月19日) 水瓶座的人很有创新精神&#xff0c;思想上很多时候比较大胆&#xff0c;赋予想象所以水瓶座的人大多数比较聪慧&#xff0c;有点刀子嘴豆腐心吧&#xff01; 双鱼座(02月20日─03月…

java测试输入星座匹配_用java编一个程序能通过用户输入的生日判断用户的星座。请大神们给点提示或者思路吧。规定不能自定义方法...

展开全部 既然不允许用其他的方式&#xff0c;那就直接e69da5e887aa3231313335323631343130323136353331333335303465上代码吧....public static void main(String[] args) { int point -1; String[] str {"白羊","金牛","双子","巨蟹&qu…

地耀日12星座

地耀日12星座 好像大概有下列这些&#xff0c;记不清楚了 壁虎星座 麻雀星座 射水鱼星座(斗鱼) 氢原子星座 水分子星座 COMSCI星座 紫水晶星座 土星神星座 马克思星座 爱因斯坦星座 藤本科植物星座 其它的&#xff0c;好像记不清楚了。。…

【题解】 [JOISC2020] 星座 3

solution: 考虑怎样的一对星星会形成星座。 记 l [ i ] l[i] l[i] 和 r [ i ] r[i] r[i] 分别表示从第 i i i 个星星出发&#xff0c;能扩展到的最左和最右节点。 考虑反悔贪心&#xff0c;尽量使得答案更大。换句话说&#xff0c;当前点覆盖的区间一定比之前 [ l [ i ]…

程序员星座论

2019独角兽企业重金招聘Python工程师标准>>> 射手座 特立独行者&#xff0c;射手也。拿到手的模块&#xff0c;他们会用自己的方法去编写代码&#xff0c;可能先前主程交代的思路和注意事项早就被他们网的一干二净了&#xff0c;他们凡事不会和别人商量&#x…

你信星座么?

一 摩羯座 12月22日-1月19日 魔羯给人外表感觉很冷漠&#xff0c;其实内心是火热的&#xff0c;很善良&#xff0c;不容易拒绝别人&#xff0c; 尤其是自己看重的人&#xff0c;受伤了就会想要逃避&#xff0c;多数是吃软不吃硬&#xff01; 脾气很大&#xff0c;但是对事不…