您好,登錄后才能下訂單哦!
本篇內容介紹了“怎么用vue+canvas繪制時間軸”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
最近在研究canvas繪制時間軸,直接上代碼,希望分享能給大家帶來幫助,效果如下:
代碼如下,可以拷貝到vue項目中直接預覽
<template> <div> <canvas id="time_line" width="1200" height="27"></canvas> </div> </template> <script> export default { name: 'pathwaytrack', data() { return { screenWidth: document.body.clientWidth } }, mounted() { let that = this that.carveTimeScale(1200, 1, 10, 0, 10) let canvasColor = '#999999' let initTime = 12000 setInterval(() => { initTime += 1000 that.carveTimeScale(1200, 1, 10, initTime, 10) }, 1000); }, methods: { /** * 分割像素刻度 * width: 寬度 ms:一個小刻度的毫秒數 pxMs:10像素一個小刻度 pageShowStartTime:初始時間(毫秒) 一大段間隔時長(秒) */ carveTimeScale(width, ms, pxMs, pageShowStartTime, intervalTime) { let canvasId = document.getElementById('time_line') let ctx = canvasId.getContext('2d') ctx.clearRect(0, 0, 1200, 60) ctx.fillStyle = '#999999' // 為防止蘋果屏幕2X顯示不正常 // 為防止蘋果屏幕2X顯示不正常 var getPixelRatio = function (context) { var backingStore = context.backingStorePixelRatio || context.webkitBackingStorePixelRatio || context.mozBackingStorePixelRatio || context.msBackingStorePixelRatio || context.oBackingStorePixelRatio || context.backingStorePixelRatio || 1 return (window.devicePixelRatio || 1) / backingStore } let ratio = getPixelRatio(ctx) let msOffset = this.startOffsetTime(pageShowStartTime, ms) // 開始的偏移時間 ms let pxOffset = msOffset / 1000 * pxMs // 開始的偏移距離 px let leftDistance = 0 // 到左側的距離 let leftDistanceTime = 0 // 到左側的時間 let beginX = 0 let beginY = 0 for (let i = 0; i < width / (ms * pxMs); i++) { leftDistance = pxOffset + i * (ms * pxMs) // 距離 = 開始的偏移距離 + 格數 * px/格 leftDistanceTime = pageShowStartTime + msOffset + i * ms // 時間 = 左側開始時間 + 偏移時間 + 格數 * ms beginX = pxOffset + i * (ms * pxMs) let canvasColor let showTime = pageShowStartTime + beginX / pxMs * 1000 if (showTime % (intervalTime * 1000) === 0) { beginY = 0 ctx.font = '12px Arial' ctx.fillText(this.changeTime(showTime, 1), beginX + 10, 22) canvasColor = '#999999' ctx.fillStyle = '#B1B1B1' this.drawLine(leftDistance, beginY, leftDistance, 20, canvasColor, 1) } else if (showTime % intervalTime == 0) { beginY = 0 canvasColor = '#999999' this.drawLine(leftDistance, beginY, leftDistance, 10, canvasColor, 1) } } }, /** * 根據傳入參數畫線 */ drawLine(beginX, beginY, endX, endY, color, width) { let canvasId = document.getElementById('time_line'); let ctx = canvasId.getContext('2d'); ctx.beginPath(); ctx.moveTo(beginX, beginY); ctx.lineTo(endX, endY); ctx.strokeStyle = color; ctx.lineWidth = width; ctx.stroke(); }, /** * 左側開始時間的偏移,返回單位ms */ startOffsetTime(timestamp, step) { let remainder = timestamp % step return remainder ? step - remainder : 0 }, /** * 返回時間 */ changeTime(time, num) { let hour = 0 let minute = 0 let second = 0 second = time / 1000 if (second >= 3600) { minute = (second - (second % 60)) / 60 hour = parseInt((minute / 60).toString()) minute = minute % 60 /* eslint-disable */ hour >= 10 ? hour : hour = '0' + hour minute >= 10 ? minute : minute = '0' + minute second = second % 60 second >= 10 ? second : second = '0' + second /* eslint-enable */ return hour + ':' + minute + ':' + second } if (second < 3600 && second >= 60) { hour = '00' minute = parseInt((second / 60).toString()) /* eslint-disable */ minute >= 10 ? minute : minute = '0' + minute second = second % 60 second >= 10 ? second : second = '0' + second /* eslint-enable */ return hour + ':' + minute + ':' + second } if (second < 60) { hour = '00' minute = '00' second = parseInt(second) /* eslint-disable */ second >= 10 ? second : second = '0' + second /* eslint-enable */ return hour + ':' + minute + ':' + second } } } } </script> <style lang="less" scoped> canvas { background: black; } </style>
“怎么用vue+canvas繪制時間軸”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。