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

How to deploy a simple c/c++ program using docker

1. First, create a hello-world.cpp file The progr...

Linux beginners in virtual machines configure IP and restart the network

For those who are new to virtual machines or have...

Excel export always fails in docker environment

Excel export always fails in the docker environme...

js to implement collision detection

This article example shares the specific code of ...

Detailed explanation of the use of router-view components in Vue

When developing a Vue project, you often need to ...

Introduction to the role of HTML doctype

Document mode has the following two functions: 1. ...

How to Find the Execution Time of a Command or Process in Linux

On Unix-like systems, you may know when a command...

Native js to implement drop-down box selection component

This article example shares the specific code of ...

Detailed steps to modify MySQL stored procedures

Preface In actual development, business requireme...

The whole process of realizing website internationalization using Vite2 and Vue3

Table of contents Preface Install vue-i18n Config...

Complete steps for using Nginx+Tomcat for load balancing under Windows

Preface Today, Prince will talk to you about the ...

How to add indexes to MySQL

Here is a brief introduction to indexes: The purp...