亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

vue實現(xiàn)標(biāo)簽云效果的示例

瀏覽:5日期:2022-11-02 17:59:15

閑扯兩句

最近想給自己的博客上加上一個3D標(biāo)簽云的效果,用來表示自己博客文章的分組,網(wǎng)上找到了canvas實現(xiàn)的,還有a元素實現(xiàn)的解析3D標(biāo)簽云,我想讓標(biāo)簽可以選擇和點(diǎn)擊,又不想在標(biāo)簽數(shù)量較多時操作a標(biāo)簽導(dǎo)致性能問題,于是svg就成了一個不錯的選擇。

標(biāo)簽初始化

這里實現(xiàn)的核心主要是參考了前面的那篇解析3D標(biāo)簽云的文章,作者給出了源碼,講解也比較通俗易懂。大體來說,整個代碼分三步:

根據(jù)標(biāo)簽的數(shù)量,算出每個標(biāo)簽在球面上分布的x,y,z坐標(biāo) 根據(jù)標(biāo)簽的坐標(biāo),將標(biāo)簽繪制出來,x,y坐標(biāo)通過標(biāo)簽的位置來表示,z坐標(biāo)通過標(biāo)簽字體的大小和透明度來表示 通過函數(shù)根據(jù)球的旋轉(zhuǎn)角速度不斷計算標(biāo)簽新的x,y坐標(biāo),制造出旋轉(zhuǎn)效果 通過mousemove事件,根據(jù)鼠標(biāo)坐標(biāo)值,改變球旋轉(zhuǎn)的角速度,做出交互效果

貼上代碼:

<div id=’app’ > <svg :width=’width’ :height=’height’ @mousemove=’listener($event)’> <a :href='http://www.aoyou183.cn/bcjs/tag.href' rel='external nofollow' v-for=’tag in tags’><text :x=’tag.x’ :y=’tag.y’ :font-size=’20 * (600/(600-tag.z))’ :fill-opacity=’((400+tag.z)/600)’>{{tag.text}}</text> </a> </svg> </div>

在模板中,借用指令v-for來渲染標(biāo)簽,每個標(biāo)簽上綁定了x,y,font-size(用來表現(xiàn)z軸),fill-opacity(都是與z坐標(biāo)有關(guān)的表達(dá)式,用來表現(xiàn)z軸),及text;

data: { width:700,//svg寬度 height:700,//svg高度 tagsNum:20,//標(biāo)簽數(shù)量 RADIUS:200,//球的半徑 speedX:Math.PI/360,//球一幀繞x軸旋轉(zhuǎn)的角度 speedY:Math.PI/360,//球-幀繞y軸旋轉(zhuǎn)的角度 tags: [] } computed:{ CX(){//球心x坐標(biāo) return this.width/2; }, CY(){//球心y坐標(biāo) return this.height/2; } },

做好了上面的基礎(chǔ),下面我們來初始化標(biāo)簽數(shù)據(jù):

created(){//初始化標(biāo)簽位置 let tags=[]; for(let i = 0; i < this.tagsNum; 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)//計算標(biāo)簽相對于球心的角度 tag.text = i + ’tag’; 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.href = ’https://imgss.github.io’;//給標(biāo)簽添加鏈接 tags.push(tag); } this.tags = tags;//讓vue替我們完成視圖更新 },

到了這里,我們就算了算坐標(biāo),vue完成了視圖更新的工作,這時基本上就可以得到一副靜態(tài)的圖像了:

vue實現(xiàn)標(biāo)簽云效果的示例

下面就是通過改變每一個tag的x,y的值來使球旋轉(zhuǎn)起來;實現(xiàn)方法是rotateX,rotateY函數(shù):

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; } }, 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; } },

這兩個函數(shù)就是根據(jù)標(biāo)簽原來的坐標(biāo)和球旋轉(zhuǎn)的角度算出新的坐標(biāo),最后在mounted鉤子下面,寫一個animate函數(shù),不斷調(diào)用這兩個函數(shù),實現(xiàn)旋轉(zhuǎn)動畫

mounted(){//使球開始旋轉(zhuǎn) setInterval(() => { this.rotateX(this.speedX); this.rotateY(this.speedY); }, 17) },

全部代碼如下:

<script> var app = new Vue({ el: ’#app’, data: {width:700,height:700,tagsNum:20,RADIUS:200,speedX:Math.PI/360,speedY:Math.PI/360,tags: [] }, computed:{CX(){ return this.width/2;},CY(){ return this.height/2;} }, created(){//初始化標(biāo)簽位置let tags=[];for(let i = 0; i < this.tagsNum; 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); tag.text = i + ’tag’; tag.x = this.CX + this.RADIUS * Math.sin(a) * Math.cos(b); tag.y = this.CY + this.RADIUS * Math.sin(a) * Math.sin(b); tag.z = this.RADIUS * Math.cos(a); tag.href = ’https://imgss.github.io’; tags.push(tag);}this.tags = tags; }, mounted(){//使球開始旋轉(zhuǎn)setInterval(() => { this.rotateX(this.speedX); this.rotateY(this.speedY);}, 17) }, methods: {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; } },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; } },listener(event){//響應(yīng)鼠標(biāo)移動 var x = event.clientX - this.CX; var y = event.clientY - this.CY; this.speedX = x*0.0001>0 ? Math.min(this.RADIUS*0.00002, x*0.0001) : Math.max(-this.RADIUS*0.00002, x*0.0001); this.speedY = y*0.0001>0 ? Math.min(this.RADIUS*0.00002, y*0.0001) : Math.max(-this.RADIUS*0.00002, y*0.0001); } } }) </script>

完整demo

vue

no vue

vue實現(xiàn)標(biāo)簽云效果的示例

總結(jié)

vue的數(shù)據(jù)綁定可以減少我們對dom的操作,而將關(guān)注點(diǎn)放在邏輯上面,vue構(gòu)造函數(shù)提供的幾個選項可以幫助我們更好的組織代碼

以上就是vue實現(xiàn)標(biāo)簽云效果的示例的詳細(xì)內(nèi)容,更多關(guān)于vue 標(biāo)簽云的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Vue
相關(guān)文章:
主站蜘蛛池模板: qyule极品视频在线一区 | 日韩爽爽爽视频免费播放 | 国产免费又色又爽又黄在线观看 | 久久精品女人毛片国产 | 色天天综合色天天害人害己 | 加勒比一本大道在线 | 国产精品亚洲一区二区三区正片 | 成人午夜在线观看国产 | 国产精品亚洲va在线观看 | 亚洲视频你懂的 | 亚洲精品久久片久久 | 欧美亚洲一区二区三区在线 | 亚洲美女一级毛片 | 国产精品久久久久三级 | 欧美精品国产一区二区三区 | 日韩第一视频 | 美女黄色网 | 亚洲欧美在线观看 | 国产精品福利在线观看 | 美国人和动物xxx | 在线欧美亚洲 | 国产疯狂露脸对白 | 91pao强力打造免费高清 | 1024免费观看 | 免费国产一级特黄aa大片在线 | 日韩99精品 | 又黄又爽的成人免费网站 | 国产日比视频 | 手机在线观看亚洲国产精品 | 黄色片在线免费播放 | 日韩日韩日韩日韩 | 欧美亚洲日本国产 | 一道本不卡免费视频 | 五月天婷婷视频 | 国产高清免费视频 | 高清影院|精品秒播3 | 国产婷婷色综合成人精品 | 亚洲欧洲一区二区 | 在线精品国内视频秒播 | 三级毛片在线播放 | 亚洲美女视频一区 |