jjzjj

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

mochenxiya 2023-03-28 原文

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

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

<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站一键三连哦!感谢支持!!!

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

  1. 程序员如何提高代码能力? - 2

    前言作为一名程序员,自己的本质工作就是做程序开发,那么程序开发的时候最直接的体现就是代码,检验一个程序员技术水平的一个核心环节就是开发时候的代码能力。众所周知,程序开发的水平提升是一个循序渐进的过程,每一位程序员都是从“菜鸟”变成“大神”的,所以程序员在程序开发过程中的代码能力也是根据平时开发中的业务实践来积累和提升的。提高代码能力核心要素程序员要想提高自身代码能力,尤其是新晋程序员的代码能力有很大的提升空间的时候,需要针对性的去提高自己的代码能力。提高代码能力其实有几个比较关键的点,只要把握住这些方面,就能很好的、快速的提高自己的一部分代码能力。1、多去阅读开源项目,如有机会可以亲自参与开源

  2. ruby-on-rails - 这个 C 和 PHP 程序员如何学习 Ruby 和 Rails? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我来自C、php和bash背景,很容易学习,因为它们都有相同的C结构,我可以将其与我已经知道的联系起来。然后2年前我学了Python并且学得很好,Python对我来说比Ruby更容易学。然后从去年开始,我一直在尝试学习Ruby,然后是Rails,我承认,直到现在我还是学不会,讽刺的是那些打着简单易学的烙印,但是对于我这样一个老练的程序员来说,我只是无法将它

  3. ruby-on-rails - 在 Rails 应用程序的前端获取实时日志 - 2

    在Rails3.x应用程序中,我正在使用net::ssh并向远程pc运行一些命令。我想向用户的浏览器显示实时日志。比如,如果两个命令在net中运行::ssh执行即echo"Hello",echo"Bye"被传递然后"Hello"应该在执行后立即显示在浏览器中。这是代码我在ruby​​onrails应用程序中使用ssh连接和运行命令Net::SSH.start(@servers['local'],@machine_name,:password=>@machine_pwd,:timeout=>30)do|ssh|ssh.open_channeldo|channel|channel.requ

  4. ruby - 如何在转换器插件中访问页面属性(YAML 前端) - 2

    我正在为Jekyll编写一个转换器插件,需要访问一些页眉(YAML前端)属性。只有内容被传递给主要的转换器方法,似乎无法访问上下文。例子:moduleJekyllclassUpcaseConverter关于如何在转换器插件中访问页眉数据有什么想法吗? 最佳答案 基于Jekyll源代码,无法在转换器中检索YAML前端内容。根据您的情况,我看到了两种可行的解决方案。您的文件扩展名可以具有足够的描述性,以提供您本应包含在前言中的信息。看起来Converter插件的设计就是这么基本的。如果修改Jekyll是一个选项,您可以更改Convert

  5. 前端实现文件上传(点击+拖拽) - 2

    一、简介之前在Vue项目中使用过element的上传组件,实现了点击上传+拖拽上传的两种上传功能。然后我就在想是否可以通过原生的html+js来实现文件的点击上传和拖拽上传,说干就干。首先是点击获取上传文件自然没的说,只需要借助input标签即可,但原生的点击上传按钮,实在是过于简陋,所以我的想法是通过一个div,模拟成上传按钮,然后监听其点击事件,通过input.click()去模拟点击真正的上传元素。然后是拖拽获取上传文件,这个稍有难度,我的想法是通过HTML5新增的drag拖放API+dataTransfer来实现文件的拖拽获取,但是由于是html5新增的,所以可能在某些低版本IE浏览器

  6. ruby-on-rails - Rails 还是 Sinatra? PHP程序员入门学习哪个好? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭10年前。我使用PHP的时间太长了,对它感到厌倦了。我也想学习一门新语言。我一直在使用Ruby并且喜欢它。我必须在Rails和Sinatra之间做出选择,那么您会推荐哪一个?Sinatra真的不能用来构建复杂的应用程序,它只能用于简单的应用程序吗?

  7. ruby - 适合 Ruby 程序员的 RPG IV 书籍? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭11年前。我是一名Ruby程序员,由于目前的项目需要学习RPGIV。我想学习原始类型、数据结构、控制流、体系结构等。在线资源和纸质书一样好。我应该从哪本书或在线教程开始?

  8. 教你如何使用vercel服务免费部署前端项目和serverless api - 2

    一、介绍一下vercelvercel是一个站点托管平台,提供CDN加速,同类的平台有Netlify和GithubPages,相比之下,vercel国内的访问速度更快,并且提供Production环境和development环境,对于项目开发非常的有用的,并且支持持续集成,一次push或者一次PR会自动化构建发布,发布在development环境,都会生成不一样的链接可供预览。但是vercel只是针对个人用户免费,teams是收费的首先vercel零配置部署,第二访问速度比github-page好很多,并且构建很快,还是免费使用的,对于部署个人前端项目路、接口服务非常方便vercel类似于git

  9. ruby - SAP 新实现Ruby 对Ruby 程序员有何意义? - 2

    SAP宣布BlueRuby,在ABAP虚拟机中运行的Ruby版本。这似乎为Ruby语言增加了可信度,但是,除了SAP开发人员之外,这是否适用于Ruby社区的其他人?我只是想知道这可能还有什么其他意义。可能会雇用Ruby开发人员从事SAP项目的额外工作机会?Ruby程序员还有其他潜在的好处吗?此外,还有一些我不清楚的地方:除了MRI和JRuby,还有多少种不同的实现,为什么我作为Ruby程序员需要MRI以外的任何实现?我知道如果我想与Java库集成,我可能需要JRuby。除了MRI或JRuby之外,还有什么时候我可能需要研究实现吗?我注意到BlueRuby已编译。这是一个很大的好处吗?这

  10. ruby - 程序员学习 ruby​​ 需要多长时间? - 2

    很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visitthehelpcenter.关闭12年前。开发人员学习ruby​​需要多长时间。并开发像stackoverflow这样的生产网站?一般。如果开发人员有.NET经验但没有ruby​​和MYSQL或PostgreSQL经验。

随机推荐