vue使用canvas繪制圓環(huán)
本文實(shí)例為大家分享了vue使用canvas繪制圓環(huán)的具體代碼,供大家參考,具體內(nèi)容如下
很多時(shí)候,會(huì)有繪制圓環(huán)的要求,比如漸變,圓環(huán)等等
所以現(xiàn)在封裝了一個(gè)方法,可以直接繪制
繪制樣子大概這樣的
// html <div class="medium-graph"> ?? ?<canvas id="medium-graph-canvas" width="292" height="292" /> </div>
// js mounted() { ?? ??? ?var medium_canvas = document.getElementById('medium-graph-canvas') ? ? ?? ?this.drawMain(medium_canvas, 60, 2, '#435377') ?? ?}, ?? ?methods: { ?? ??? ?drawMain(drawing_elem, percent, colornums, bgcolor) { ?? ??? ??? ?/* ?? ??? ??? ??? ?@drawing_elem: 繪制對(duì)象 ?? ??? ??? ??? ?@percent:繪制圓環(huán)百分比, 范圍[0, 100] ?? ??? ??? ??? ?@forecolor: 繪制圓環(huán)的前景色,顏色代碼 ?? ??? ??? ??? ?@bgcolor: 繪制圓環(huán)的背景色,顏色代碼 ?? ??? ??? ??? ?@colornums: 作為參數(shù)傳入,繪制哪個(gè)顏色 ?? ??? ??? ?*/ ?? ??? ??? ?var context = drawing_elem.getContext('2d') ?? ??? ??? ?var center_x = drawing_elem.width / 2 ?? ??? ??? ?var center_y = drawing_elem.height / 2 ?? ??? ??? ?var rad = (Math.PI * 2) / 100 ?? ??? ??? ?var speed = 0 ?? ??? ??? ?// 繪制背景圓圈 ?? ??? ??? ?function backgroundCircle() { ?? ??? ??? ??? ?context.save() ?? ??? ??? ??? ?context.beginPath() ?? ??? ??? ??? ?context.lineWidth = 8 // 設(shè)置線寬 ?? ??? ??? ??? ?var radius = center_x - context.lineWidth ?? ??? ??? ??? ?context.lineCap = 'round' ?? ??? ??? ??? ?context.strokeStyle = bgcolor ?? ??? ??? ??? ?context.arc(center_x, center_y, radius - 14, 0, Math.PI * 2, false) ?? ??? ??? ??? ?context.stroke() ?? ??? ??? ??? ?context.closePath() ?? ??? ??? ??? ?context.restore() ?? ??? ??? ?} ?? ??? ??? ?// 繪制運(yùn)動(dòng)圓環(huán) ?? ??? ??? ?function foregroundCircle(n) { ?? ??? ??? ??? ?context.save() ?? ??? ??? ??? ?// context.strokeStyle = forecolor //決定圓環(huán)顏色 ?? ??? ??? ??? ?context.lineWidth = 22 ?? ??? ??? ??? ?context.lineCap = 'round' ?? ??? ??? ??? ?var radius = center_x - context.lineWidth ?? ??? ??? ??? ?context.beginPath() ?? ??? ??? ??? ?// if (colornums == 1) { ?? ??? ??? ??? ?// ?? ?var g = context.createLinearGradient(0, 0, 180, 0) // 創(chuàng)建漸變對(duì)象 ?漸變開始點(diǎn)和漸變結(jié)束點(diǎn) ?? ??? ??? ??? ?// ?? ?g.addColorStop(0, '#64C58F') // 添加顏色點(diǎn) ?? ??? ??? ??? ?// ?? ?g.addColorStop(1, '#0084FF') // 添加顏色點(diǎn) ?? ??? ??? ??? ?// ?? ?context.strokeStyle = g // 使用漸變對(duì)象作為圓環(huán)的顏色 ?? ??? ??? ??? ?// } ?? ??? ??? ??? ?if (colornums == 2) { ?? ??? ??? ??? ??? ?var g = context.createLinearGradient(0, 0, 180, 0) ?? ??? ??? ??? ??? ?g.addColorStop(0, '#E7954C') ?? ??? ??? ??? ??? ?g.addColorStop(1, '#D36638') ?? ??? ??? ??? ??? ?context.strokeStyle = g ?? ??? ??? ??? ?} ?? ??? ??? ??? ?// if (colornums == 3) { ?? ??? ??? ??? ?// ?? ?var g = context.createLinearGradient(0, 0, 180, 0) ?? ??? ??? ??? ?// ?? ?g.addColorStop(0, '#FF7C78') // ?? ??? ??? ??? ?// ?? ?g.addColorStop(1, '#FD413E') ?? ??? ??? ??? ?// ?? ?context.strokeStyle = g ?? ??? ??? ??? ?// } ?? ??? ??? ??? ?context.arc( ?? ??? ??? ??? ?center_x, ?? ??? ??? ??? ?center_y, ?? ??? ??? ??? ?radius, ?? ??? ??? ??? ?-Math.PI / 4, ?? ??? ??? ??? ?-Math.PI / 4 + n * rad, ?? ??? ??? ??? ?false ?? ??? ??? ??? ?) // 用于繪制圓弧context.arc(x坐標(biāo),y坐標(biāo),半徑,起始角度,終止角度,順時(shí)針/逆時(shí)針) ?? ??? ??? ??? ?context.stroke() ?? ??? ??? ??? ?context.closePath() ?? ??? ??? ??? ?context.restore() ?? ??? ??? ?} ?? ??? ??? ?// 繪制文字 ?? ??? ??? ?function text(n) { ?? ??? ??? ??? ?// context.save() // save和restore可以保證樣式屬性只運(yùn)用于該段canvas元素 ?? ??? ??? ??? ?// context.fillStyle = forecolor ?? ??? ??? ??? ?// var font_size = 40 ?? ??? ??? ??? ?// context.font = font_size + 'px Helvetica' ?? ??? ??? ??? ?// var text_width = context.measureText(n.toFixed(0) + '%').width ?? ??? ??? ??? ?// context.fillText(n.toFixed(0) + '%', center_x - text_width / 2, center_y + font_size / 2) ?? ??? ??? ??? ?// context.restore() ?? ??? ??? ?} ?? ??? ??? ?// 執(zhí)行動(dòng)畫 ?? ??? ??? ?(function drawFrame() { ?? ??? ??? ??? ?if (speed <= percent) { ?? ??? ??? ??? ??? ?window.requestAnimationFrame(drawFrame) ?? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ?return false ?? ??? ??? ??? ?} ?? ??? ??? ??? ?context.clearRect(0, 0, drawing_elem.width, drawing_elem.height) ?? ??? ??? ??? ?// backgroundCircle() ?? ??? ??? ??? ?// text(speed) ?? ??? ??? ??? ?foregroundCircle(speed) ?? ??? ??? ??? ?if (speed >= percent) { ?? ??? ??? ??? ??? ?speed ++ ?? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ?speed += 1 ?? ??? ??? ??? ?} ?? ??? ??? ?})() ?? ??? ?} ?? ?},
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue圖片瀏覽組件v-viewer用法分析【支持旋轉(zhuǎn)、縮放、翻轉(zhuǎn)等操作】
這篇文章主要介紹了Vue圖片瀏覽組件v-viewer用法,結(jié)合實(shí)例形式分析了v-viewer的基本功能與使用方法,包括旋轉(zhuǎn)、縮放、翻轉(zhuǎn)等操作技巧,需要的朋友可以參考下2019-11-11vue項(xiàng)目中使用bpmn-自定義platter的示例代碼
這篇文章主要介紹了vue項(xiàng)目中使用bpmn-自定義platter的實(shí)例代碼,本文通過代碼截圖相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05Vuepress使用vue組件實(shí)現(xiàn)頁面改造
這篇文章主要為大家介紹了Vuepress使用vue組件實(shí)現(xiàn)頁面改造示例過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07Intellij IDEA搭建vue-cli項(xiàng)目的方法步驟
這篇文章主要介紹了Intellij IDEA搭建vue-cli項(xiàng)目的方法步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10vue項(xiàng)目中頁面跳轉(zhuǎn)傳參的方法總結(jié)
在Vue項(xiàng)目中,你可以使用路由(vue-router)來實(shí)現(xiàn)頁面跳轉(zhuǎn)并傳遞參數(shù),這篇文章主要為大家整理了一些常用的方法,感興趣的小伙伴可以學(xué)習(xí)一下2023-11-11關(guān)于this.$refs獲取不到dom的可能原因及解決方法
這篇文章主要介紹了關(guān)于this.$refs獲取不到dom的可能原因及解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11