简单函数实现nwjs桌面端窗口抖动

1,206 阅读1分钟
'use strict'

const gui = window.require('nw.gui')
const win = gui.Window.get()

// 窗口抖动
export function shake () {
  const startX = win.x
  const startY = win.y
  // 抖动轨迹
  const moveArrry = [{ x: -10, y: -7 }, { x: -0, y: -0 }, { x: 7, y: -3 }, { x: -6, y: -3 }, { x: -0, y: -0 }, { x: 6, y: -3 }]
  const routeArray = moveArrry.map(item => ({ x: item.x + startX, y: item.y + startY }))
  let _index = 0
  function animate () {
    if (_index === routeArray.length) return
    setTimeout(() => {
      win.moveTo(routeArray[_index].x, routeArray[_index].y)
      // win.show()
      _index++
      animate()
    }, 60)
  }
  animate()
}