ctrl+滚轮控制canvas画布比例

 mounted() {
    this.componentTagList = componentListText
    // 监听滚轮-画布比例大小
    document.querySelector('#home').addEventListener('mousewheel',
      this.handleScroll
      , {
        capture: false,
        passive: false
      });
  },



// 滚轮画布比例调整
    handleScroll(e) {
      //e.wheelDellta:可以用来获取鼠标的滚动方向,对于得到的值,只看正负,往上滚是正值,往下滚是负值。
      //火狐浏览器不支持这个方法,需要会用e.detail来获取滚轮的滚动方向,向上是负值,向下是正值
      let direction = e.deltaY > 0 ? 'down' : 'up';  //deltaY为正则滚轮向下,为负滚轮向上
      //125为用户一次滚动鼠标的wheelDelta的值,125与鼠标滚动一下是几行的不同而不同

      if ((e.wheelDelta && e.ctrlKey) || e.detail) {
        // 关闭默认的滚轮事件
        e.preventDefault();
        // 画布比例设一个最小值,不能为 0
        if (direction == 'down' && e.deltaY >= 125 && e.ctrlKey) {
          if (this.scale <= 5) {
            this.scale = 5;
          } else {
            this.scale -= 5;
            //根据滚轮改变的scale改变画布上控件的大小比例
            _debounce(changeComponentsSizeWithScale(this.scale))
          }
        }
        if (direction == 'up' && e.deltaY <= -125 && e.ctrlKey) {

          if (this.scale >= 200) {
            this.scale = 200
          } else {
            this.scale += 5;
            _debounce(changeComponentsSizeWithScale(this.scale))
          }
        }
      }


    },



// 节流
export function _throttle(fn, wait = 500) {
  let last, now
  return function() {
    now = Date.now()
    if (last && now - last < wait) {
      last = now
    } else {
      last = now
      fn.call(this, ...arguments)
    }
  }
}
 
// 防抖
export function _debounce(fn, wait = 500) {
  let timer
  return function() {
    let context = this
    let args = arguments
    if (timer) clearTimeout(timer)
    timer = setTimeout(() => {
      fn.apply(context, args)
    }, wait)
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值