How to use the raf.default function in raf

To help you get started, we’ve selected a few raf examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github filidorwiese / spriteling / src / __tests__ / playback.spec.ts View on Github external
import * as imageLoaded from 'image-loaded'
import * as raf from 'raf'
import Spriteling from '../spriteling'

jest.setTimeout(1000)

// Mock imageLoaded
imageLoaded.default = jest.fn((a, b) => {
  a.width = 100
  a.height = 100
  b()
})

// Mock dummy RAF
raf.default = jest.fn()
raf.default.cancel = jest.fn()

// Mock RAF to play x amount of frames
let workingRafTimeout
const workingRaf = (framesToRender: number, delay: number) => {
  let time = 0
  return (a) => {
    if (framesToRender) {
      framesToRender--
      workingRafTimeout = setTimeout(() => {
        a(time += delay)
      }, delay)
    }
  }
}

// Mock element.offsetParent property
github filidorwiese / spriteling / src / __tests__ / playback.spec.ts View on Github external
it('should play script reversed', async (done) => {
      const delay = 10
      const simpleScript = [
        {sprite: 1},
        {sprite: 2},
        {sprite: 3},
        {sprite: 4}
      ]
      let counter = 3

      raf.default.mockImplementation(workingRaf(4, delay))

      await instance.play({
        run: 1,
        delay,
        script: simpleScript,
        reversed: true,
        onFrame: (frameNumber) => {
          expect(frameNumber).toBe(counter)
          counter--
        },
        onStop: () => {
          clearTimeout(workingRafTimeout)
          done()
        }
      })
    })

raf

requestAnimationFrame polyfill for node and the browser

MIT
Latest version published 6 years ago

Package Health Score

74 / 100
Full package analysis