首页 > 编程语言 >【前端特效】程序员给你的专属告白,快来转发给你心爱的那个她吧!

【前端特效】程序员给你的专属告白,快来转发给你心爱的那个她吧!

时间:2022-09-04 09:46:19浏览次数:103  
标签:告白 转发给 color cloudContent 程序员 tagAttrList let Math name

【前端特效】程序员给你的专属告白,快来转发给你心爱的那个她吧!

点击打开视频讲解更加详细

在这里插入图片描述

<template>
  <div class="content">
    <img src="../assets/live.gif" alt="" />
    <section class="cloud-bed">
      <div class="cloud-box">
        <span
          v-for="(item, index) in dataList"
          :key="index"
          @click="getDataInfo(item)"
          :style="{ color: item.color, background: item.bgColor }"
        >
          {{ item.name }}
        </span>
      </div>
    </section>
  </div>
</template>
 
<script>
export default {
  name: "word-cloud",
  data() {
    return {
      timer: 50, // 球体转动速率
      radius: 0, // 词云球体面积大小
      dtr: Math.PI / 180, //鼠标滑过球体转动速度
      active: false, // 默认加载是否开启转动
      lasta: 0, // 上下转动
      lastb: 0.5, // 左右转动
      distr: true,
      tspeed: 0, // 鼠标移动上去时球体转动
      mouseX: 0,
      mouseY: 0,
      tagAttrList: [],
      tagContent: null,
      cloudContent: null,
      sinA: "",
      cosA: "",
      sinB: "",
      cosB: "",
      sinC: "",
      cosC: "",
      dataList: [
        {
          name: "金晨",
          value: "1",
          bgColor: "rgb(57, 193, 207,0.12)",
          color: "#39c1cf",
        },
        {
          name: "昆凌",
          value: "8",
          bgColor: "rgb(66, 105, 245,0.12)",
          color: "#4269f5",
        },
        {
          name: "刘亦菲",
          value: "9",
          bgColor: "rgb(184, 107, 215,0.12)",
          color: "#b86bd7",
        },
        {
          name: "林志玲",
          value: "3",
          bgColor: "rgb(243, 84, 83,0.12)",
          color: "#f35453",
        },
        {
          name: "林心如",
          value: "6",
          bgColor: "rgb(250, 116, 20,0.12)",
          color: "#FA7414",
        },
        {
          name: "李沁",
          value: "10",
          bgColor: "rgb(255, 171, 30,0.12)",
          color: "#FFAB1E",
        },
        {
          name: "林依晨",
          value: "2",
          bgColor: "rgb(136, 104, 217,0.12)",
          color: "#8868D9",
        },
        {
          name: "李宇春",
          value: "5",
          bgColor: "rgb(42, 184, 230,0.12)",
          color: "#2AB8E6",
        },
        {
          name: "贾静雯",
          value: "7",
          bgColor: "rgb(117, 133, 162,0.12)",
          color: "#7585A2",
        },
      ],
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.radius = document.querySelector(".cloud-box").offsetWidth / 3;
      this.initWordCloud();
    });
  },
  beforeDestroy() {
    clearInterval(this.timer);
  },
  methods: {
    // 获取点击文本信息
    getDataInfo(item) {
      console.log(item, "item");
    },
    initWordCloud() {
      this.cloudContent = document.querySelector(".cloud-box");
      this.tagContent = this.cloudContent.getElementsByTagName("span");
      for (let i = 0; i < this.tagContent.length; i++) {
        let tagObj = {};
        tagObj.offsetWidth = this.tagContent[i].offsetWidth;
        tagObj.offsetHeight = this.tagContent[i].offsetHeight;
        this.tagAttrList.push(tagObj);
      }
      this.sineCosine(0, 0, 0);
      this.positionAll();
      this.cloudContent.onmouseover = () => {
        this.active = true;
      };
      this.cloudContent.onmouseout = () => {
        this.active = false;
      };
      this.cloudContent.onmousemove = (ev) => {
        let oEvent = window.event || ev;
        this.mouseX =
          oEvent.clientX -
          (this.cloudContent.offsetLeft + this.cloudContent.offsetWidth / 2);
        this.mouseY =
          oEvent.clientY -
          (this.cloudContent.offsetTop + this.cloudContent.offsetHeight / 2);
        this.mouseX /= 5;
        this.mouseY /= 5;
      };
      setInterval(this.update, this.timer);
    },
    positionAll() {
      let phi = 0;
      let theta = 0;
      let max = this.tagAttrList.length;
      let aTmp = [];
      let oFragment = document.createDocumentFragment();
      //随机排序
      for (let i = 0; i < this.tagContent.length; i++) {
        aTmp.push(this.tagContent[i]);
      }
      aTmp.sort(() => {
        return Math.random() < 0.5 ? 1 : -1;
      });
      for (let i = 0; i < aTmp.length; i++) {
        oFragment.appendChild(aTmp[i]);
      }
      this.cloudContent.appendChild(oFragment);
      for (let i = 1; i < max + 1; i++) {
        if (this.distr) {
          phi = Math.acos(-1 + (2 * i - 1) / max);
          theta = Math.sqrt(max * Math.PI) * phi;
        } else {
          phi = Math.random() * Math.PI;
          theta = Math.random() * (2 * Math.PI);
        }
        //坐标变换
        this.tagAttrList[i - 1].cx =
          this.radius * Math.cos(theta) * Math.sin(phi);
        this.tagAttrList[i - 1].cy =
          this.radius * Math.sin(theta) * Math.sin(phi);
        this.tagAttrList[i - 1].cz = this.radius * Math.cos(phi);
        this.tagContent[i - 1].style.left =
          this.tagAttrList[i - 1].cx +
          this.cloudContent.offsetWidth / 2 -
          this.tagAttrList[i - 1].offsetWidth / 2 +
          "px";
        this.tagContent[i - 1].style.top =
          this.tagAttrList[i - 1].cy +
          this.cloudContent.offsetHeight / 2 -
          this.tagAttrList[i - 1].offsetHeight / 2 +
          "px";
      }
    },
    update() {
      let angleBasicA;
      let angleBasicB;

      if (this.active) {
        angleBasicA =
          (-Math.min(Math.max(-this.mouseY, -200), 200) / this.radius) *
          this.tspeed;
        angleBasicB =
          (Math.min(Math.max(-this.mouseX, -200), 200) / this.radius) *
          this.tspeed;
      } else {
        angleBasicA = this.lasta * 0.98;
        angleBasicB = this.lastb * 0.98;
      }

      //默认转动是后是否需要停下
      // lasta=a;
      // lastb=b;

      // if(Math.abs(a)<=0.01 && Math.abs(b)<=0.01)
      // {
      // return;
      // }
      this.sineCosine(angleBasicA, angleBasicB, 0);
      for (let j = 0; j < this.tagAttrList.length; j++) {
        let rx1 = this.tagAttrList[j].cx;
        let ry1 =
          this.tagAttrList[j].cy * this.cosA +
          this.tagAttrList[j].cz * -this.sinA;
        let rz1 =
          this.tagAttrList[j].cy * this.sinA +
          this.tagAttrList[j].cz * this.cosA;

        let rx2 = rx1 * this.cosB + rz1 * this.sinB;
        let ry2 = ry1;
        let rz2 = rx1 * -this.sinB + rz1 * this.cosB;

        let rx3 = rx2 * this.cosC + ry2 * -this.sinC;
        let ry3 = rx2 * this.sinC + ry2 * this.cosC;
        let rz3 = rz2;
        this.tagAttrList[j].cx = rx3;
        this.tagAttrList[j].cy = ry3;
        this.tagAttrList[j].cz = rz3;

        let per = 350 / (350 + rz3);

        this.tagAttrList[j].x = rx3 * per - 2;
        this.tagAttrList[j].y = ry3 * per;
        this.tagAttrList[j].scale = per;
        this.tagAttrList[j].alpha = per;

        this.tagAttrList[j].alpha =
          (this.tagAttrList[j].alpha - 0.6) * (10 / 6);
      }
      this.doPosition();
      this.depthSort();
    },
    doPosition() {
      let len = this.cloudContent.offsetWidth / 2;
      let height = this.cloudContent.offsetHeight / 2;
      for (let i = 0; i < this.tagAttrList.length; i++) {
        this.tagContent[i].style.left =
          this.tagAttrList[i].cx +
          len -
          this.tagAttrList[i].offsetWidth / 2 +
          "px";
        this.tagContent[i].style.top =
          this.tagAttrList[i].cy +
          height -
          this.tagAttrList[i].offsetHeight / 2 +
          "px";
        // this.tagContent[i].style.fontSize = Math.ceil(12 * this.tagAttrList[i].scale/2) + 8 + 'px';
        this.tagContent[i].style.fontSize =
          Math.ceil((12 * this.tagAttrList[i].scale) / 2) + 2 + "px";
        this.tagContent[i].style.filter =
          "alpha(opacity=" + 100 * this.tagAttrList[i].alpha + ")";
        this.tagContent[i].style.opacity = this.tagAttrList[i].alpha;
      }
    },
    depthSort() {
      let aTmp = [];
      for (let i = 0; i < this.tagContent.length; i++) {
        aTmp.push(this.tagContent[i]);
      }
      aTmp.sort((item1, item2) => item2.cz - item1.cz);
      for (let i = 0; i < aTmp.length; i++) {
        aTmp[i].style.zIndex = i;
      }
    },
    sineCosine(a, b, c) {
      this.sinA = Math.sin(a * this.dtr);
      this.cosA = Math.cos(a * this.dtr);
      this.sinB = Math.sin(b * this.dtr);
      this.cosB = Math.cos(b * this.dtr);
      this.sinC = Math.sin(c * this.dtr);
      this.cosC = Math.cos(c * this.dtr);
    },
  },
};
</script>
 
 
<style scoped>
.content {
  width: 100%;
  height: calc(100vh - 0px);
  background-image: url(../assets/liveimg.jpeg);
  /* 设置图片宽、高 */
  background-size: 100% 100%;
  /*按比例缩放*/
  background-repeat: no-repeat;
  display: flex;
  justify-content: space-around;
}
.cloud-bed {
  width: 300px;
  height: 200px;
}
.cloud-box {
  position: relative;
  margin: 20px auto 0px;
  width: 100%;
  height: 100%;
  background: #00000000;
}
.cloud-box span {
  position: absolute;
  padding: 3px;
  top: 0px;
  font-weight: bold;
  text-decoration: none;
  left: 0px;
  background-image: linear-gradient(to bottom, red, #fff);
  background-clip: text;
  color: transparent;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  text-align: center;

  display: flex;
  align-items: center;
  justify-content: center;

  /* line-height: 50px;
      overflow:hidden;
      white-space: nowrap;
      text-overflow: ellipsis; */
}
</style>

若对您有帮助,请点击跳转到B站一键三连哦!感谢支持!!!

标签:告白,转发给,color,cloudContent,程序员,tagAttrList,let,Math,name
From: https://www.cnblogs.com/mochenxiya/p/16654286.html

相关文章

  • A DRY KISS — 程序员的逗留
    ADRYKISS—程序员的逗留无论是新手还是经验丰富的程序员——如果你是一名程序员,你就会听到仅仅几个月的支持者保证“总是使用DRYKISS”。对于我们这些凡人来说,DRY......
  • 程序员的钱真的好挣么?
    昨天看了统计,去年中国程序员规模已经达到600万,已经是一个庞大的消费群体了,他们的消费能力是毋庸置疑的。多少创业者在打他们的主意,从早期的CSDN,到如今的掘金,都靠吃程序员员......
  • 程序员的摸鱼加速器!
    ​ 最近趁摸鱼时间体验了一款神器,堪称后端前端们的摸鱼加速器,测试们的寿命催化剂。那就是:Apifox。中国自主研发的集文档、接口调试、Mock、接口自动化测试一体的协作平......
  • 每个程序员都需要知道的操作系统基础知识
    每个程序员都需要知道的操作系统基础知识实际上什么是操作系统?操作系统或操作系统可以理解为您可以在设备上获得的最低控制层。操作系统管理您计算机的内存、进程、软件......
  • 程序员如何实现财富自由?
    有国外的码农在论坛里提问:“为了过上更好的生活我两年前移民到法国后,开始从事前端开发,我的月薪从当时的1.5万涨到现在的1.9万。这样的工资还算可以,但我觉得不够多。......
  • 程序员如何减少开发中的 Bug?
    周会上同事抛出了一个问题,程序员如何减少开发中的Bug?很有意思的一个话题,本篇文章我们来进行探讨与总结。一、概述爱因斯坦曾经说过:「如果给我一个小时解答......
  • 后端程序员生产力工具合集
    后端程序员除了写代码,也难免要写设计文档,画各种图。因此掌握各种生产力工具,是很有必要的,可以达到事半功倍的效果。下面结合楼主亲身体验,推荐一些生产力工具,欢迎探讨和补充......
  • Java入门-程序员的世界“Hello,World!”
    HelloWorld!java大小写敏感,尽量使用英文,用英文符号使用cmd输出建文件夹存放代码,新建Java文件,编写代码如下,编译javacjava,生成class文件,运行class文件:publicclassHell......
  • 疯狂Java程序员的基本修养 pdf
    高清扫描版下载链接:https://pan.baidu.com/s/1ImHUTRBIVmvENnb7rEoLoA点击这里获取提取码《疯狂Java程序员的基本修养》知识主要分为四个部分,第一部分主要介绍Java程序的......
  • 写给所有程序员的对象的一封信
    ​ 因为本人有一枚可爱的老婆,她经常有很多奇怪的问题(我承认其实是我老想跟她分享),但是有些问题需要有一定的理论支撑,所以我就打算在这里一并告诉她。就是一些关于编程的前......