html5 Canvas는 화면 크기(vue)에 어떻게 적응합니까?

캔버스 만들기

       <div  class="twovideo">
           <canvas id="playground" width="560;" height="306"></canvas>
       </div>
.twovideo {
    
    
  width: 100%;
  height: 306px;
  canvas {
    
    
    width: 100%;
    height: 100%;
    display: block;
  }
}

생성된 캔버스는 브라우저 창 크기가 변경될 때 캔버스 크기를 동적으로 변경할 수 없습니다. 하지만 백분율 팝업 창으로 화면마다 다르게 동작하므로 적응이 필요합니다.

vue에서 새 resize.js 파일을 만듭니다.

import {
    
     debounce } from '@/utils'

export default {
    
    
  data() {
    
    
    return {
    
    
      $_sidebarElm: null,
      $_resizeHandler: null
    }
  },
  mounted() {
    
    
    this.initListener()
  },
  activated() {
    
    
    if (!this.$_resizeHandler) {
    
    
      // avoid duplication init
      this.initListener()
    }

    // when keep-alive chart activated, auto resize
    this.resize()
  },
  beforeDestroy() {
    
    
    this.destroyListener()
  },
  deactivated() {
    
    
    this.destroyListener()
  },
  methods: {
    
    
    // use $_ for mixins properties
    // https://vuejs.org/v2/style-guide/index.html#Private-property-names-essential
    $_sidebarResizeHandler(e) {
    
    
      if (e.propertyName === 'width') {
    
    
        this.$_resizeHandler()
      }
    },
    initListener() {
    
    
      this.$_resizeHandler = debounce(() => {
    
    
        this.resize()
      }, 100)
      window.addEventListener('resize', this.$_resizeHandler)

      this.$_sidebarElm = document.getElementsByClassName('sidebar-container')[0]
      this.$_sidebarElm && this.$_sidebarElm.addEventListener('transitionend', this.$_sidebarResizeHandler)
    },
    destroyListener() {
    
    
      window.removeEventListener('resize', this.$_resizeHandler)
      this.$_resizeHandler = null
      this.$_sidebarElm && this.$_sidebarElm.removeEventListener('transitionend', this.$_sidebarResizeHandler)
    },
    resize() {
    
    
      const {
    
     chart } = this
      chart && chart.resize()
    }
  }
}

그 안에 소개된 디바운스 흔들림 방지 기능은 다음과 같습니다. utils/index.js

/**
 * @param {Function} func
 * @param {number} wait
 * @param {boolean} immediate
 * @return {*}
 */
export function debounce(func, wait, immediate) {
    
    
  let timeout, args, context, timestamp, result

  const later = function () {
    
    
    // 据上一次触发时间间隔
    const last = +new Date() - timestamp

    // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
    if (last < wait && last > 0) {
    
    
      timeout = setTimeout(later, wait - last)
    } else {
    
    
      timeout = null
      // 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
      if (!immediate) {
    
    
        result = func.apply(context, args)
        if (!timeout) context = args = null
      }
    }
  }

  return function (...args) {
    
    
    context = this
    timestamp = +new Date()
    const callNow = immediate && !timeout
    // 如果延时不存在,重新设定延时
    if (!timeout) timeout = setTimeout(later, wait)
    if (callNow) {
    
    
      result = func.apply(context, args)
      context = args = null
    }

    return result
  }
}

마지막으로 resize.js를 페이지 믹스에 삽입합니다.

import resize from '../../utils/mixins/resize'

mixins: [resize],//和data同级

캔버스 자체 적응을 달성할 수 있습니다.

추천

출처blog.csdn.net/qq_38594056/article/details/118892775