【前端特效】程序員給你的專屬告白恨胚,快來轉(zhuǎn)發(fā)給你心愛的那個她吧!

【前端特效】程序員給你的專屬告白炎咖,快來轉(zhuǎn)發(fā)給你心愛的那個她吧赃泡!

點擊打開視頻講解更加詳細(xì)

【前端特效】程序員給你的專屬告白,快來轉(zhuǎn)發(fā)給你心愛的那個她吧_Moment.jpg
<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, // 球體轉(zhuǎn)動速率
      radius: 0, // 詞云球體面積大小
      dtr: Math.PI / 180, //鼠標(biāo)滑過球體轉(zhuǎn)動速度
      active: false, // 默認(rèn)加載是否開啟轉(zhuǎn)動
      lasta: 0, // 上下轉(zhuǎn)動
      lastb: 0.5, // 左右轉(zhuǎn)動
      distr: true,
      tspeed: 0, // 鼠標(biāo)移動上去時球體轉(zhuǎn)動
      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();
      //隨機(jī)排序
      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);
        }
        //坐標(biāo)變換
        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;
      }

      //默認(rèn)轉(zhuǎn)動是后是否需要停下
      // 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);
  /* 設(shè)置圖片寬乘盼、高 */
  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>

若對您有幫助升熊,請點擊跳轉(zhuǎn)到B站一鍵三連哦!感謝支持3裾ぁ<兑啊!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末粹胯,一起剝皮案震驚了整個濱河市蓖柔,隨后出現(xiàn)的幾起案子辰企,更是在濱河造成了極大的恐慌阱表,老刑警劉巖椰苟,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異溉跃,居然都是意外死亡懒闷,警方通過查閱死者的電腦和手機(jī)十减,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來愤估,“玉大人帮辟,你說我怎么就攤上這事⊥嫜妫” “怎么了由驹?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長昔园。 經(jīng)常有香客問我蔓榄,道長,這世上最難降的妖魔是什么默刚? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任甥郑,我火速辦了婚禮,結(jié)果婚禮上荤西,老公的妹妹穿的比我還像新娘澜搅。我一直安慰自己,他們只是感情好邪锌,可當(dāng)我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布勉躺。 她就那樣靜靜地躺著,像睡著了一般觅丰。 火紅的嫁衣襯著肌膚如雪饵溅。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天妇萄,我揣著相機(jī)與錄音蜕企,去河邊找鬼。 笑死嚣伐,一個胖子當(dāng)著我的面吹牛糖赔,可吹牛的內(nèi)容都是我干的萍丐。 我是一名探鬼主播轩端,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼逝变!你這毒婦竟也來了基茵?” 一聲冷哼從身側(cè)響起奋构,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎拱层,沒想到半個月后弥臼,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡根灯,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年径缅,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片烙肺。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡纳猪,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出桃笙,到底是詐尸還是另有隱情氏堤,我是刑警寧澤,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布搏明,位于F島的核電站鼠锈,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏星著。R本人自食惡果不足惜购笆,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望虚循。 院中可真熱鬧由桌,春花似錦、人聲如沸邮丰。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽剪廉。三九已至娃循,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間斗蒋,已是汗流浹背捌斧。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留泉沾,地道東北人捞蚂。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像跷究,于是被迫代替她去往敵國和親姓迅。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容