How to draw the timeline with vue+canvas

How to draw the timeline with vue+canvas

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:
  • Vue implements horizontal timeline
  • Vue implements timeline function
  • Vue sample code for implementing two-column horizontal timeline
  • VUE implements timeline playback component
  • Vue.js implements timeline function
  • Vue+swiper realizes timeline effect
  • Vue realizes the logistics timeline effect
  • Vue timeline vue-light-timeline usage instructions
  • Vue implements movable horizontal timeline
  • Vue implements timeline effect

<<:  SQL interview question: Find the sum of time differences (ignore duplicates)

>>:  How to quickly modify the root password under CentOS8

Recommend

Some functions of using tcpdump to capture packets in the Linux command line

tcpdump is a flexible and powerful packet capture...

Basic usage of wget command under Linux

Table of contents Preface 1. Download a single fi...

How to use Nginx to solve front-end cross-domain problems

Preface When developing static pages, such as Vue...

Detailed explanation of the lock structure in MySQL

Mysql supports 3 types of lock structures Table-l...

Summary of common MySQL commands

Set change mysqlroot password Enter the MySQL dat...

Several common redirection connection example codes in html

Copy code The code is as follows: window.location...

React sample code to implement automatic browser refresh

Table of contents What is front-end routing? How ...

Summary of MySQL injection bypass filtering techniques

First, let’s look at the GIF operation: Case 1: S...

Specific use of ES6 array copy and fill methods copyWithin() and fill()

Table of contents Batch copy copyWithin() Fill ar...

How to use CocosCreator for sound processing in game development

Table of contents 1. Basics of audio playback in ...

30 Tips for Writing HTML Code

1. Always close HTML tags In the source code of p...

The process of quickly converting mysql left join to inner join

During the daily optimization process, I found a ...

How to use port 80 in Tomcat under Linux system

Application Scenario In many cases, we install so...

Element table header row height problem solution

Table of contents Preface 1. Cause of the problem...

How to create WeChat games with CocosCreator

Table of contents 1. Download WeChat developer to...