<template>
<div>
<svg style="background-color: black" :width="width" :height="height">
<a class="fontA" v-for="(tag, index) in tags" :key="`tag-${index}`">
<text
:id="tag.id"
:x="tag.x"
:y="tag.y"
:font-size="20 * (600 / (600 - tag.z))"
:fill-opacity="(400 + tag.z) / 600"
@mousemove="listenerMove($event)"
@mouseout="listenerOut($event)"
@click="clickToPage"
>
{{ tag.text }}
</text>
</a>
</svg>
</div>
</template>
<script>
export default {
name: "word-cloud",
//數(shù)據(jù)绵载,寬丈牢,高庭呜,半徑澎羞,半徑一般位寬高的一半。
// props: ["data", "width", "height", "RADIUS"],
data() {
return {
width: 600, //svg寬度
height: 600, //svg高度
tagsNum: 0, //標(biāo)簽數(shù)量
RADIUS: 300, //球的半徑
speedX: Math.PI / 360 / 1.5, //球一幀繞x軸旋轉(zhuǎn)的角度
speedY: Math.PI / 360 / 1.5, //球-幀繞y軸旋轉(zhuǎn)的角度
tags: [],
data: [
"金晨",
"昆凌",
"李冰冰",
"劉詩詩",
"劉雯",
"劉亦菲",
"林心如",
"林志玲",
"李湘",
"李亞男",
"李若彤",
"李沁",
"李嘉欣",
"林依晨",
"劉嘉玲",
"閏妮",
"李宇春",
"李晟",
"羅震環(huán)",
"劉雨欣",
"李波兒",
"黎姿",
"張敏",
"梁小冰",
"黎美嫻",
"李彩樺",
"林允兒",
"米雪",
"李菲兒",
"婁藝瀟",
"李金銘",
"李萌萌",
],
timer: null,
};
},
computed: {
CX() {
//球心x坐標(biāo)
return this.width / 2;
},
CY() {
//球心y坐標(biāo)
return this.height / 2;
},
},
created() {
this.initData();
},
methods: {
// 初始化數(shù)據(jù)
initData() {
//初始化標(biāo)簽位置
let tags = [];
this.tagsNum = this.data.length;
for (let i = 0; i < this.data.length; i++) {
let tag = {};
let k = -1 + (2 * (i + 1) - 1) / this.tagsNum;
let a = Math.acos(k);
let b = a * Math.sqrt(this.tagsNum * Math.PI); //計(jì)算標(biāo)簽相對(duì)于球心的角度
tag.text = this.data[i];
tag.x = this.CX + this.RADIUS * Math.sin(a) * Math.cos(b); //根據(jù)標(biāo)簽角度求出標(biāo)簽的x,y,z坐標(biāo)
tag.y = this.CY + this.RADIUS * Math.sin(a) * Math.sin(b);
tag.z = this.RADIUS * Math.cos(a);
tag.id = i; // 給標(biāo)簽添加id
tags.push(tag);
// console.log(tag);
}
this.tags = tags; //讓vue替我們完成視圖更新
},
// 縱向旋轉(zhuǎn)
rotateX(angleX) {
var cos = Math.cos(angleX);
var sin = Math.sin(angleX);
for (let tag of this.tags) {
var y1 = (tag.y - this.CY) * cos - tag.z * sin + this.CY;
var z1 = tag.z * cos + (tag.y - this.CY) * sin;
tag.y = y1;
tag.z = z1;
}
},
// 橫向旋轉(zhuǎn)
rotateY(angleY) {
var cos = Math.cos(angleY);
var sin = Math.sin(angleY);
for (let tag of this.tags) {
var x1 = (tag.x - this.CX) * cos - tag.z * sin + this.CX;
var z1 = tag.z * cos + (tag.x - this.CX) * sin;
tag.x = x1;
tag.z = z1;
}
},
// 運(yùn)動(dòng)函數(shù)
runTags() {
if (typeof this.timer === "number") {
clearInterval(this.timer);
this.timer = null;
}
let timer = setInterval(() => {
this.rotateX(this.speedX);
this.rotateY(this.speedY);
}, 17);
this.timer = timer;
},
// 監(jiān)聽移入事件
listenerMove(e) {
if (e.target.id) {
clearInterval(this.timer);
}
},
// 監(jiān)聽移出事件
listenerOut(e) {
if (e.target.id) {
this.runTags();
}
},
// 點(diǎn)擊事件
clickToPage() {},
},
mounted() {
this.runTags();
},
};
</script>
<style scoped>
.fontA {
fill: #60cae9;
font-weight: bold;
}
.fontA:hover {
fill: #ffffff;
cursor: pointer;
}
</style>
3D旋轉(zhuǎn)詞云(vue)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門肄程,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人选浑,你說我怎么就攤上這事蓝厌。” “怎么了古徒?”我有些...
- 文/不壞的土叔 我叫張陵拓提,是天一觀的道長。 經(jīng)常有香客問我隧膘,道長代态,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任疹吃,我火速辦了婚禮蹦疑,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘互墓。我一直安慰自己必尼,他們只是感情好蒋搜,可當(dāng)我...
- 文/花漫 我一把揭開白布篡撵。 她就那樣靜靜地躺著判莉,像睡著了一般。 火紅的嫁衣襯著肌膚如雪育谬。 梳的紋絲不亂的頭發(fā)上券盅,一...
- 文/蒼蘭香墨 我猛地睜開眼嚎杨,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼花鹅!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起枫浙,我...
- 序言:老撾萬榮一對(duì)情侶失蹤刨肃,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后箩帚,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體真友,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年紧帕,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了盔然。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
- 正文 年R本政府宣布,位于F島的核電站涩嚣,受9級(jí)特大地震影響崇众,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜航厚,卻給世界環(huán)境...
- 文/蒙蒙 一顷歌、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧幔睬,春花似錦眯漩、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽舱卡。三九已至,卻和暖如春队萤,著一層夾襖步出監(jiān)牢的瞬間轮锥,已是汗流浹背。 一陣腳步聲響...
- 正文 我出身青樓赵辕,卻偏偏與公主長得像既绩,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子还惠,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 最近支撐別的部門做了一個(gè)大屏(Vue項(xiàng)目)熬词,大屏上面有一個(gè)詞云的展示,需要讓詞動(dòng)起來吸重,經(jīng)同事介紹用了一下 TagC...
- 安裝 npm install --save echarts echarts-gl 因?yàn)楸敬武秩拘枰玫絜chart...
- 首先互拾,在ViewController中定義主Layer,這個(gè)CALayer用來存放其他子Layer嚎幸,我們一共需要6...