广告位联系
返回顶部
分享到

CSS极坐标的实现

css 来源:转载 作者:秩名 发布时间:2021-06-05 21:22:40 人浏览
摘要

项目有图表方面的需求,其中有做卫星定位的图形,需要制作极坐标来显示当前北半球或南半球的卫星分布情况。第一时间想到了echarts的极坐标,找到示例,虽然满足了部分需求,但是极坐标是由canvs画的,卫星有自己的编号,所以难以辨析每个点对应的卫星编号。

项目有图表方面的需求,其中有做卫星定位的图形,需要制作极坐标来显示当前北半球或南半球的卫星分布情况。第一时间想到了echarts的极坐标,找到示例,虽然满足了部分需求,但是极坐标是由canvs画的,卫星有自己的编号,所以难以辨析每个点对应的卫星编号。于是就想到了自己去用CSS画极坐标

提示:以下是本篇文章正文内容,下面案例可供参考

一、示例

上面示例,下面echarts示例

polar

二、设计步骤

1.纬度

几个div,设置圆角

2.经度

多条0.5px的边框,通过旋转实现

lines: [
        {
          id: 1,
          transform: "translateX(-50%) rotateZ(0deg) scaleX(0.4)",
          borderStyle: "solid",
          borderColor: "#333",
        },
        {
          id: 2,
          transform: "translateX(-50%) rotateZ(45deg) scaleX(0.4)",
          borderStyle: "dashed",
          borderColor: "#91cc75",
        },
        {
          id: 3,
          transform: "translateX(-50%) rotateZ(90deg) scaleX(0.4)",
          borderStyle: "solid",
          borderColor: "#333",
        },
        {
          id: 4,
          transform: "translateX(-50%) rotateZ(135deg) scaleX(0.4)",
          borderStyle: "dashed",
          borderColor: "#91cc75",
        },
      ],
 

3.卫星(点)

后台的数据只有经度和纬度。纬度很好做,按照90°的比例进行定位。经度用到旋转,注意不是直接在点上旋转,否则只是盒子旋转,需要在点外边套一个div进行旋转,如果需要美化,可以使点反方向旋转该角度达到编号是一个正的效果。

三、代码实现

代码是以vue的组件来写的,satellites就是极坐标的数据接口。

<template>
  <div class="polar">
    <div class="polar-main">
      <div class="polar-1">
        <div class="polar-2">
          <div class="polar-3">
            <p
              v-for="item in latitudes"
              :key="item.id"
              class="latitude"
              :style="{
                top: item.location.top,
                transform: item.location.transform,
              }"
            >
              {{ item.value }}
            </p>
            <div class="polar-center">
              <div class="satellites">
                <div v-for="item in satellites" :key="item.name">
                  <p
                    v-for="ele in item.distribution"
                    :key="ele.id"
                    class="satellite-box"
                    :style="{
                      transform: rotate(ele.long),
                    }"
                  >
                    <el-tooltip
                      class="item"
                      effect="dark"
                      :content="`经度:${ele.long} 纬度:${ele.lati}`"
                      placement="top-start"
                    >
                      <span
                        class="satellite"
                        :style="{
                          backgroundColor: ele.color,
                          top: top(ele.lati),
                          transform: rotate(-1 * ele.long),
                        }"
                        >{{ ele.id }}</span
                      >
                    </el-tooltip>
                  </p>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <p
        v-for="item in lines"
        :key="item.id"
        class="line"
        :style="{
          transform: item.transform,
          borderStyle: item.borderStyle,
          borderColor: item.borderColor,
        }"
      ></p>
      <p
        v-for="item in longitudes"
        :key="item.id"
        class="longitude"
        :style="{
          top: item.location.top,
          left: item.location.left,
          transform: item.location.transform,
        }"
      >
        {{ item.value }}
      </p>
    </div>
    <div class="satellite-name"></div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      lines: [
        {
          id: 1,
          transform: "translateX(-50%) rotateZ(0deg) scaleX(0.4)",
          borderStyle: "solid",
          borderColor: "#333",
        },
        {
          id: 2,
          transform: "translateX(-50%) rotateZ(45deg) scaleX(0.4)",
          borderStyle: "dashed",
          borderColor: "#91cc75",
        },
        {
          id: 3,
          transform: "translateX(-50%) rotateZ(90deg) scaleX(0.4)",
          borderStyle: "solid",
          borderColor: "#333",
        },
        {
          id: 4,
          transform: "translateX(-50%) rotateZ(135deg) scaleX(0.4)",
          borderStyle: "dashed",
          borderColor: "#91cc75",
        },
      ],
      longitudes: [
        {
          id: 5,
          value: "90°",
          location: {
            top: "50%",
            left: "100%",
            transform: "translateY(-50%)",
          },
        },
        {
          id: 6,
          value: "180°",
          location: {
            top: "100%",
            left: "50%",

            transform: "translateX(-50%)",
          },
        },
        {
          id: 7,
          value: "270°",
          location: {
            top: "50%",
            left: "0",

            transform: "translateX(-100%) translateY(-50%)",
          },
        },
        {
          id: 8,
          value: "360°",
          location: {
            top: "0",
            left: "50%",
            transform: "translateX(-50%) translateY(-100%)",
          },
        },
      ],
      latitudes: [
        {
          id: 1,
          value: "90°",
          location: {
            top: "50%",
            left: "0",
            transform: "translateY(-50%) translateX(50%)",
          },
        },
        {
          id: 2,
          value: "60°",
          location: {
            top: "0",
            left: "0",
            transform: "translateY(-50%) translateX(50%)",
          },
        },
        {
          id: 3,
          value: "30°",
          location: {
            top: "-50%",
            left: "0",
            transform: "translateY(-50%) translateX(50%)",
          },
        },
      ],
      satellites: [
        {
          name: "Below Mask",
          distribution: [
            {
              id: "10",
              long: 46.397128,
              lati: 56.397128,
              color: "#409eff",
            },
            {
              id: "08",
              long: 76.397128,
              lati: 32.916527,
              color: "#409eff",
            },
          ],
        },
        {
          name: "Unhealthy",
          distribution: [
            {
              id: "25",
              long: 156.397128,
              lati: 62.916527,
              color: "#f56c6c",
            },
            {
              id: "25",
              long: 316.397128,
              lati: 12.916527,
              color: "#f56c6c",
            },
          ],
        },
        {
          name: "Missing",
          distribution: [
            {
              id: "07",
              long: 256.397128,
              lati: 22.916527,
              color: "#118452",
            },
            {
              id: "18",
              long: 56.397128,
              lati: 27.916527,
              color: "#118452",
            },
            {
              id: "12",
              long: 66.397128,
              lati: 27.916527,
              color: "#118452",
            },
            {
              id: "16",
              long: 196.397128,
              lati: 67.916527,
              color: "#118452",
            },
          ],
        },
      ],
    };
  },
  methods: {
    top(lati) {
      return ((90 - lati) / 90) * -90 - 10 + "px";
    },
    rotate(long) {
      let z = (long / 360) * 360;
      return `rotateZ(${z}deg)`;
    },
  },
  //   filters: {},
};
</script>
<style scoped lang='scss'>
$polarPiameter: 180px;
.polar-main {
  width: $polarPiameter;
  height: $polarPiameter;
  position: relative;
  p {
    margin: 0;
  }
  .polar-1 {
    width: $polarPiameter;
    height: $polarPiameter;
    border-style: solid;
    .polar-2 {
      width: $polarPiameter * 2/3;
      height: $polarPiameter * 2/3;
      border-style: dashed;
      .polar-3 {
        width: $polarPiameter/3;
        height: $polarPiameter/3;
        border-style: dashed;
        .polar-center {
          width: 1px;
          height: 1px;
          background-color: #333;
        }
      }
    }
  }
  .line {
    height: $polarPiameter;
    border-right-color: #333;
    border-right-width: 1px;
    border-right-style: solid;
    position: absolute;
    left: 50%;
    cursor: pointer;
  }
  .longitude,
  .latitude {
    height: 14px;
    line-height: 14px;
    font-size: 12px;
    color: #868585;
    position: absolute;
    cursor: pointer;
  }
}
.polar-1,
.polar-2,
.polar-3,
.polar-center {
  border-radius: 50%;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  border-color: #91cc75;
  border-width: 1px;
  box-sizing: border-box;
  cursor: pointer;
}
.polar-1:hover {
  border-width: 2px;
}
.polar-2:hover{
  border-width: 2px;
}
.satellite-box {
  position: absolute;
  width: 1px;
  height: 1px;
  border-radius: 50%;
  background-color: transparent;
  .satellite {
    position: absolute;
    left: -10px;
    width: 20px;
    height: 20px;
    line-height: 20px;
    text-align: center;
    border-radius: 50%;
    font-size: 14px;
    color: #fff;
    cursor: pointer;
    z-index: 999;
    opacity: 0.6;
    transition: 0.6s;
  }
  .satellite:hover {
    background-color: #333 !important;
  }
}
</style>

总结

示例图:

在这里插入图片描述


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 : https://www.jb51.net/web/777899.html
相关文章
  • CSS3浏览器兼容的介绍

    CSS3浏览器兼容的介绍
    一、浏览器兼容 1.1、概要 世界上没有任何一个浏览器是一样的,同样的代码在不一样的浏览器上运行就存在兼容性问题。不同浏览器其内核
  • CSS元素定位之通过元素的标签或者元素的id、cl

    CSS元素定位之通过元素的标签或者元素的id、cl
    大部分人在使用selenium定位元素时,用的是xpath元素定位方式,因为xpath元素定位方式基本能解决定位的需求。xpath元素定位方式更直观,更好
  • 微信小程序纯CSS实现无限弹幕滚动效果

    微信小程序纯CSS实现无限弹幕滚动效果
    啥也不说,先上效果图 实现背景 产品:我们需要一个弹幕滚动效果,类似于微博热搜那种实时评论,但是我们的数据是固定的20条,你让他
  • 使用CSS构建强大且酷炫的粒子动画效果

    使用CSS构建强大且酷炫的粒子动画效果
    粒子动画,顾名思义,就是页面上存在大量的粒子构建而成的动画。传统的粒子动画主要由 Canvas、WebGL 实现。 当然,不使用 HTML + CSS 的主要
  • flex:1的详细介绍

    flex:1的详细介绍
    简单的来说一下在别人问你这个问题的时候怎么来回答它 前端新人,如有错误求大佬指出~求教???? 情景复现 大佬提问:你知道flex: 1;的更深
  • 新的CSS伪类函数 :is 和 :where()示例介绍

    新的CSS伪类函数 :is 和 :where()示例介绍
    在编写 CSS 时,有时可能会使用很长的选择器列表来定位具有相同样式规则的多个元素。例如,如果您想对标题中的 b 标签进行颜色调整,我
  • 纯CSS打字动画的实现方法

    纯CSS打字动画的实现方法
    在一些技术网站中,经常会看到这样一种展示效果:逐个显示一段文本中的字符,模拟出一种打字的效果。通过这种方式可以显著地提升网
  • 实现el-form每行显示两列底部按钮居中效果的教程

    实现el-form每行显示两列底部按钮居中效果的教程
    el-form 每行显示两列,底部按钮居中 问题: 以前的解决办法是:el-col,el-row。但是这里只有一个el-form-item的label数据是已知的,其余项都是循
  • CSS实现六边形的图片效果的代码

    CSS实现六边形的图片效果的代码
    前言 在CodePen上看到一个有意思的效果: 这个效果的难点在于六边形的绘制, 那么接下来我们就一起来看下作者是怎么实现他的吧 实现思路
  • CSS浮动引起的高度塌陷问题的详细介绍

    CSS浮动引起的高度塌陷问题的详细介绍
    正常页面布局 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 style *{ margin:0; padding: 0; } .content{ width:400px; border:1px solid #000; } .box{ width:200px;
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计