canvas绘制黄道十二宫星座

  • 效果图
  • 对照图
  • 准备工作
  • 开始撸代码
  • 白嫖作者的代码

效果图

对照图

准备工作

(以下所有片段代码为手敲,难免会有语法错误,请不要复制,文末会发布全部代码)
先准备一张"宇宙星空图",设为背景

 <style>* {margin:0;padding:0;}body {background: url(./bg.jpg)}</style><body><canvas id=""constellation>你的浏览器不支持canvas,请升级你的浏览器</canvas><body>

开始撸代码

观察对照图,可以看出所有的星座都是“点”与“点”的连线,(绘制小的圆形,达到点的效果)
猜想:直接画几个圆然后连线不就可以了?

 function draw() {var canvas = document.getElementById('constellation');var w = window.innerWidth;var h = window.innerHeight;canvas.width = w;canvas.height = h;if (!canvas.getContext) return;var ctx = canvas.getContext("2d");// 直接绘制圆点ctx.beginPath()ctx.arc(100,100,4,0,Math.PI*2,true)ctx.arc(400,100,4,0,Math.PI*2,true)ctx.arc(100,400,4,0,Math.PI*2,true)ctx.fill(); //填充ctx.beginPath()ctx.arc(500,100,4,0,Math.PI*2,true)ctx.arc(800,100,4,0,Math.PI*2,true)ctx.arc(500,400,4,0,Math.PI*2,true)ctx.stroke();//连线}

效果
在一个beginPath()中,使用填充方法(ctx.fill)会默认调用闭合方法(ctx.closePath),
连线方式能实现“点”与“点”的连线,但是“点”是空心的,并没有达到想要的效果(实心点)


猜想
单独绘制每个点,使用填充方法,
然后共同绘制,使用连线方法

function draw() {var canvas = document.getElementById('constellation');var w = window.innerWidth;var h = window.innerHeight;canvas.width = w;canvas.height = h;if (!canvas.getContext) return;var ctx = canvas.getContext("2d");// 单独绘制圆点ctx.beginPath()ctx.arc(100,100,4,0,Math.PI*2,true)ctx.fill(); //填充ctx.beginPath()ctx.arc(400,100,4,0,Math.PI*2,true)ctx.fill(); //填充ctx.beginPath()ctx.arc(100,400,4,0,Math.PI*2,true)ctx.fill(); //填充// 共同绘制 ctx.beginPath()ctx.arc(500,100,4,0,Math.PI*2,true)ctx.arc(800,100,4,0,Math.PI*2,true)ctx.arc(500,400,4,0,Math.PI*2,true)ctx.stroke();//连线}

效果
可以看到左边的点已经满足,
将“共同绘制的点”坐标,和“单独绘制的点”坐标重合,
不就能模拟出“点”与“点”的连线了吗(坐标重合图)


改进
将“共同绘制的点”,改为“共同绘制的线”

         // 共同绘制ctx.beginPath()// 这里参数,X轴,Y轴,半径,开始弧度,结束弧度,顺时针/逆时针,// 参数太多了吧,而且我只需要线,不需要圆点// ctx.arc(100,100,4,0,Math.PI*2,true)// ctx.arc(400,100,4,0,Math.PI*2,true)// ctx.arc(100,400,4,0,Math.PI*2,true)// 更改为绘制线// 这里需要一个起始点,我使用的方法是1号点的坐标,因为是同一个点,所以对页面无影响// 这么写是为了后面的方法调用更方便ctx.moveTo(100,100);//起始点ctx.lineTo(100,100)ctx.lineTo(400,100)ctx.lineTo(100,400)ctx.stroke();

样式
主体“点线连接”已经实现,那么接下来让它变好看,body添加背景图片,绘制的点、线添加颜色

function draw() {var canvas = document.getElementById('constellation');var w = window.innerWidth;var h = window.innerHeight;canvas.width = w;canvas.height = h;if (!canvas.getContext) return;var ctx = canvas.getContext("2d");// 设置连线颜色,填充颜色ctx.font = "40px sans-serif"//文字字体ctx.strokeStyle = '#bedff8' //连线颜色ctx.fillStyle = '#96d0fc' //坐标点颜色ctx.shadowBlur = 10; //设置坐标点阴影的模糊级别ctx.shadowColor = "#fff" //设置模糊颜色// 单独绘制圆点ctx.beginPath()ctx.arc(100,100,4,0,Math.PI*2,true)ctx.fill(); //填充ctx.beginPath()ctx.arc(400,100,4,0,Math.PI*2,true)ctx.fill(); //填充ctx.beginPath()ctx.arc(100,400,4,0,Math.PI*2,true)ctx.fill(); //填充// 单独绘制线ctx.beginPath()ctx.moveTo(100,100);//起始点ctx.lineTo(100,100)ctx.lineTo(400,100)ctx.lineTo(100,400)ctx.stroke();}

效果

提取公共方法
因为要绘制黄道十二宫,12个图形,并且点的坐标很多,所以很有必要提取公共部分,写成方法调用
这里使用的是变量传参的方式,先绘制一个白羊座看看效果

 const coordinatePoint = {Aries:{name:"Aries",//星座名称offsetX: 100,//X轴偏移量,用于定位offsetY: 0,//y轴偏移量,用于定位point:[//坐标点集合,{x:0,y:220},{x:120,y:250},{x:170,y:280},{x:180,y:300},],} ,}function draw() {var canvas = document.getElementById('constellation');var w = window.innerWidth;var h = window.innerHeight;canvas.width = w;canvas.height = h;if (!canvas.getContext) return;var ctx = canvas.getContext("2d");// 设置连线颜色,填充颜色ctx.font = "40px sans-serif"//文字字体ctx.strokeStyle = '#bedff8' //连线颜色ctx.fillStyle = '#96d0fc' //坐标点颜色ctx.shadowBlur = 10; //设置坐标点阴影的模糊级别ctx.shadowColor = "#fff" //设置模糊颜色Constellation(ctx, coordinatePoint.Aries) //白羊座}draw()// 绘制图形function Constellation(ctx, coordinate) {// 绘制文字ctx.fillText(coordinate.name, coordinate.offsetX, coordinate.offsetY+400);// 绘制点for (let i of coordinate.point) {ctx.beginPath()ctx.arc(coordinate.offsetX + i.x, coordinate.offsetY + i.y, 4, 0, Math.PI * 2, true)ctx.fill(); //填充}// 绘制线ctx.beginPath()// 绘制线必须有起点,这里直接使用的【0】的坐标ctx.moveTo(coordinate.offsetX + coordinate.point[0].x,coordinate.offsetY + coordinate.point[0].Y)// 在下面的循环中,一样绘制了【0】的坐标,由于坐标重合,页面无变化// 在星座图形中,有多个分叉的连线,也是使用的重复坐标for (let i of coordinate.point) {ctx.lineTo(coordinate.offsetX + i.x, coordinate.offsetY + i.y)}ctx.stroke(); //连线}

白羊座效果图

重复的计算坐标
接下来就是其他星座的坐标计算了,我是通过肉眼观察的,计算的大概位置,并不准确
(计算了2个小时,眼睛已经瞎了)

白嫖作者的代码

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>constellation星座</title><style>* {margin: 0;padding: 0;}body {background: url(./bg.jpg);}</style>
</head><body><canvas id="constellation">你的浏览器不支持canvas,请升级你的浏览器</canvas>
</body>
<script type="text/javascript">const coordinatePoint = {Aries:{name:"Aries",offsetX: 100,offsetY: 0,point:[{x:0,y:220},{x:120,y:250},{x:170,y:280},{x:180,y:300},],} ,Taurus:{name:"Taurus",offsetX: 400,offsetY: 0,point:[{x:180,y:300},{x:178,y:285},{x:120,y:235},{x:90,y:205},{x:75,y:195},{x:60,y:185},{x:-10,y:115},{x:60,y:185},{x:75,y:195},{x:90,y:205},{x:95,y:185},{x:85,y:180},{x:85,y:165},{x:70,y:130},{x:30,y:70},],},Gemini:{name:"Gemini",offsetX: 700,offsetY: 0,point:[{x:160,y:300},{x:170,y:270},{x:50,y:230},{x:0,y:210},{x:-5,y:185},{x:15,y:140},{x:30,y:140},{x:55,y:145},{x:150,y:185},{x:190,y:195},{x:210,y:190},{x:190,y:195},{x:170,y:270},],},Cancer:{name:"Cancer",offsetX:1000,offsetY: 0,point:[{x:70,y:270},{x:65,y:210},{x:150,y:240},{x:65,y:210},{x:50,y:190},{x:15,y:150},],},Leo:{name:"Leo",offsetX: 1300,offsetY: 0,point:[{x:80,y:50},{x:60,y:55},{x:55,y:100},{x:70,y:120},{x:100,y:115},{x:130,y:135},{x:40,y:235},{x:10,y:285},{x:10,y:200},{x:70,y:120},],},virgo:{name:"virgo",offsetX: 1600,offsetY: 0,point:[{x:80,y:50},{x:85,y:90},{x:75,y:120},{x:40,y:130},{x:-10,y:120},{x:40,y:130},{x:45,y:190},{x:10,y:220},{x:-20,y:280},{x:10,y:220},{x:45,y:190},{x:100,y:210},{x:80,y:260},{x:60,y:255},{x:45,y:305},{x:60,y:255},{x:80,y:260},{x:100,y:210},{x:80,y:175},{x:75,y:120},],},Libra: {name:"Libra",offsetX: 100,offsetY: 400,point:[{x:30,y:220},{x:60,y:210},{x:70,y:200},{x:90,y:160},{x:130,y:200},{x:120,y:260},{x:80,y:280},{x:78,y:300},{x:80,y:280},{x:120,y:260},{x:90,y:160},],},Scorpio: {name:"Scorpio",offsetX: 400,offsetY: 400,point:[{x:30,y:220},{x:10,y:240},{x:0,y:250},{x:20,y:270},{x:60,y:275},{x:90,y:265},{x:100,y:240},{x:110,y:210},{x:150,y:160},{x:160,y:150},{x:180,y:140},{x:220,y:120},{x:210,y:100},{x:220,y:120},{x:225,y:140},{x:225,y:160},],},Sagittarius: {name:"Sagittarius",offsetX: 700,offsetY: 400,point:[{x:100,y:250},{x:70,y:270},{x:105,y:280},{x:70,y:270},{x:45,y:230},{x:20,y:180},{x:25,y:165},{x:55,y:130},{x:105,y:150},{x:130,y:140},{x:110,y:100},{x:95,y:95},{x:65,y:75},{x:95,y:95},{x:110,y:100},{x:125,y:85},{x:110,y:100},{x:130,y:140},{x:145,y:140},{x:145,y:140},{x:180,y:130},{x:195,y:80},{x:180,y:130},{x:190,y:160},{x:220,y:170},{x:225,y:160},{x:265,y:130},{x:225,y:160},{x:220,y:170},{x:190,y:160},{x:190,y:190},{x:200,y:200},{x:190,y:190},{x:190,y:160},{x:180,y:130},{x:145,y:140},{x:120,y:170},{x:105,y:150},],},Gapricorn:{name:"Gapricorn",offsetX:1000,offsetY: 400,point:[{x:30,y:290},{x:35,y:270},{x:50,y:250},{x:70,y:220},{x:100,y:120},{x:150,y:220},{x:155,y:240},{x:130,y:250},{x:90,y:275},{x:55,y:280},{x:30,y:290},],},Aquarius:{name:"Aquarius",offsetX:1300,offsetY: 400,point:[{x:150,y:280},{x:110,y:250},{x:100,y:240},{x:60,y:245},{x:55,y:280},{x:0,y:200},{x:5,y:185},{x:20,y:170},{x:15,y:140},{x:70,y:160},{x:120,y:150},{x:70,y:160},{x:15,y:140},{x:80,y:90},{x:150,y:30},],},Pisces:{name:"Pisces",offsetX:1600,offsetY: 400,point:[{x:-40,y:300},{x:-20,y:320},{x:0,y:295},{x:60,y:295},{x:100,y:295},{x:150,y:310},{x:110,y:260},{x:80,y:230},{x:65,y:200},{x:40,y:130},{x:30,y:100},{x:15,y:80},{x:20,y:50},{x:40,y:35},{x:70,y:55},{x:75,y:80},{x:60,y:105},{x:30,y:100},],},}function draw() {var canvas = document.getElementById('constellation');var w = window.innerWidth;var h = window.innerHeight;canvas.width = w;canvas.height = h;if (!canvas.getContext) return;var ctx = canvas.getContext("2d");// 设置连线颜色,填充颜色ctx.font = "40px sans-serif"//文字字体ctx.strokeStyle = '#bedff8' //连线颜色ctx.fillStyle = '#96d0fc' //坐标点颜色ctx.shadowBlur = 10; //设置坐标点阴影的模糊级别ctx.shadowColor = "#fff" //设置模糊颜色Constellation(ctx, coordinatePoint.Aries) //白羊座Constellation(ctx, coordinatePoint.Taurus) //金牛座Constellation(ctx, coordinatePoint.Gemini) //双子座Constellation(ctx, coordinatePoint.Cancer) //巨蟹座Constellation(ctx, coordinatePoint.Leo) //狮子座Constellation(ctx, coordinatePoint.virgo) //处女座Constellation(ctx, coordinatePoint.Libra) //天秤座Constellation(ctx, coordinatePoint.Scorpio) //天蝎座Constellation(ctx, coordinatePoint.Sagittarius) //射手座Constellation(ctx, coordinatePoint.Gapricorn) //摩羯座Constellation(ctx, coordinatePoint.Aquarius) //水瓶座Constellation(ctx, coordinatePoint.Pisces) //双鱼座}draw()//绘制图形function Constellation(ctx, coordinate) {// 绘制文字ctx.fillText(coordinate.name, coordinate.offsetX, coordinate.offsetY+400);// 绘制点for (let i of coordinate.point) {ctx.beginPath()ctx.arc(coordinate.offsetX + i.x, coordinate.offsetY + i.y, 4, 0, Math.PI * 2, true)ctx.fill(); //填充}// 绘制线ctx.beginPath()// 绘制线必须有起点,这里直接使用的【0】的坐标ctx.moveTo(coordinate.offsetX + coordinate.point[0].x,coordinate.offsetY + coordinate.point[0].Y)// 在下面的循环中,一样绘制了【0】的坐标,由于坐标重合,页面无变化// 在星座图形中,有多个分叉的连线,也是使用的重复坐标for (let i of coordinate.point) {ctx.lineTo(coordinate.offsetX + i.x, coordinate.offsetY + i.y)}ctx.stroke(); //连线}
</script></html>

刚接触1天的canvas,然后制作的demo,希望各位大佬提出改进的建议。

canvas绘制星座(黄道十二宫)相关推荐

  1. 我的星座图 php,canvas 绘制星座图(好玩)--转载

    canvas星座 * { margin: 0; padding: 0; } #box{ margin:10px 0 0 10px;; } input{ outline: none; font-size ...

  2. canvas绘制的文字如何换行

    <html><head><title>canvas绘制的文字如何换行</title><style type="text/css" ...

  3. HTML5 canvas绘制雪花飘落

    Canvas是HTML5新增的组件,它就像一块幕布,可以用JavaScript在上面绘制各种图表.动画等.没有Canvas的年代,绘图只能借助Flash插件实现,页面不得不用JavaScript和Fl ...

  4. canvas绘制时钟

    听了慕课网Sliav Zhou 的课程canvas绘制时钟,记录下来的代码,每句做了注解便于以后学习,原先每次边听别的课边敲码,错误百出,明明原封不动复制的代码,就会出错,还找不到原因,今天可能运气好 ...

  5. canvas绘制圆形

    canvas绘制圆形的思路: 1.创建路径 XXX.beginpath(); 2.创建圆形的路径: 3.关闭路径:XXX.closepath(); 路径不关闭也能绘制出图形 4.设定绘制样式. 创建圆 ...

  6. java canvas 画图片_[Java教程][HTML5] Canvas绘制简单图片

    [Java教程][HTML5] Canvas绘制简单图片 0 2016-05-13 13:00:04 获取Image对象,new出来 定义Image对象的src属性,参数:图片路径 定义Image对象 ...

  7. html5 canvas绘制圆形进度实例

    2019独角兽企业重金招聘Python工程师标准>>> html5 canvas绘制圆形进度实例 <canvas id="test" width=200 h ...

  8. 【Android 应用开发】Canvas 绘制文字 ( 文字尺寸测量 | 基线绘制 )

    文章目录 I . 文字尺寸测量 II . 基线绘制 I . 文字尺寸测量 1 . 精准绘制需求 : Canvas 绘制文字时 , 有时需要精准的控制文字的绘制 , 如绘制到指定的区域 , 居中 , 或 ...

  9. 小猿圈html5教程之canvas绘制线段方法

    HTML5现在是时下较火的编程语言之一,但是对于怎么学习很多朋友都是不了解的,不知道从何处下手,针对以上内容小猿圈web前端讲师每天会分享一个web前端知识,希望对你的前端学习有一定的帮助,今天分享的 ...

最新文章

  1. 使用深度学习阅读和分类扫描文档
  2. Redis中列表list数据类型(增加(在左侧、右侧或指定元素前后插入数据)、获取(获取表内指定范围的元素)、更新(获取指定索引位置的元素值)、删除(删除指定元素、count))
  3. 语言 读ini文件_让C语言的调试更加高大上
  4. HTTP协议之post multipart/form-data数据类型实例
  5. 关于android的几个小知识点
  6. JAVA程序设计计时器代码_Java中的定时器Timer使用示例代码详解
  7. 《ASP.NET Core 真机拆解》 送书活动结果公布
  8. python twisted和flask_Python高效开发实战——Django、Tornado、Flask、Twisted(第2版)
  9. ZooKeeper官方文档学习笔记03-程序员指南
  10. 刚刚,贺建奎回应一切:如果是我孩子,我会第一个去试验
  11. php strtofloat,Delphi6函数大全(3)
  12. 友基-绘影G10数位屏到底是个什么东西呢要不要拆了看看
  13. flutter配置高德地图定位
  14. 回给collapsar的信
  15. 语义分割评价指标代码:混淆矩阵计算详解
  16. 初学者如何吃透一个Java项目
  17. 银河麒麟系统设置变更
  18. app2sd 与 A2SD+
  19. 【路径规划-TSP问题】基于粒子群结合蚁群算法求解旅行商问题附matlab代码
  20. 【CloudXNS教您几招】如何让多ip域名配置游刃有余?(1)

热门文章

  1. 固态硬盘各种受损,数据恢复一个对策,你值得拥有!
  2. jenkins配置中执行 ant 命令时,提示找不到ant 命令
  3. c#的Console.WriteLine中花括号怎么用
  4. linux上如何创建超链接,GridView中的超链接
  5. WebApp开发----数字角标
  6. u3d011 秘密行动_学习记录
  7. 爱奇艺 linux版本好用,腾讯视频Linux版客户端,提供rpm和deb软件包下载
  8. 最新骗局:利用支付宝快捷支付 套取手机验证码转账
  9. 编程之美中的NIM游戏及异或性质应用
  10. Java求两集合中元素交集的四种方法对比总结