This article example shares the specific code of vue canvas drawing timeline for your reference. The specific content is as follows I'm studying canvas drawing timeline recently. Here is the code directly. I hope sharing it can help you. The effect is as follows: The code is as follows, you can copy it to the vue project and preview it directly <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: { /** * Split pixel scale* width: width in ms: number of milliseconds for a small scale pxMs: 10 pixels for a small scale pageShowStartTime: initial time (milliseconds) a large interval duration (seconds) */ 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' // To prevent abnormal display on Apple 2X screens // To prevent abnormal display on Apple 2X screens 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) // Start offset time ms let pxOffset = msOffset / 1000 * pxMs // Starting offset distance px let leftDistance = 0 // distance to the left let leftDistanceTime = 0 // time to the left let beginX = 0 let beginY = 0 for (let i = 0; i < width / (ms * pxMs); i++) { leftDistance = pxOffset + i * (ms * pxMs) // Distance = starting offset distance + number of grids * px/grid leftDistanceTime = pageShowStartTime + msOffset + i * ms // Time = left start time + offset time + number of grids * 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) } } }, /** * Draw lines according to the passed parameters */ 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(); }, /** * The offset of the left start time, returned in ms */ startOffsetTime(timestamp, step) { let remainder = timestamp % step return remainder ? step - remainder : 0 }, /** * Return time */ 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> The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: SQL interview question: Find the sum of time differences (ignore duplicates)
>>: How to quickly modify the root password under CentOS8
This article mainly introduces how to integrate T...
Ubuntu 20.04 does not have root login enabled by ...
Make a note so you can come back and check it lat...
Preface Bootstrap, the most popular front-end dev...
Table of contents 1. Data Type 1.1 Why do we need...
1. Multiple calls to single arrow Once a single a...
I searched on Baidu. . Some people say to use the...
As the title says, otherwise when the page is revi...
1. Components and implemented functions Keepalive...
Virtual machine software: vmware workstation Imag...
Table of contents 1. Props parent component ---&g...
Let's take a look at the situation where Secu...
Version update, the password field in the origina...
Preface One day, I was suddenly asked about MySQL...
Data backup and restoration part 2, as follows Ba...