Giter Club home page Giter Club logo

ticker's Introduction

ticker

A module for running animation and game loops with browserify.

You've probably heard of requestAnimationFrame: a helpful method for running animations at higher frame rates than setInterval. It works really well for rendering animations to the screen, adjusting the speed to fit your screen refresh rate and battery life, etc.

Unfortunately it's not predictable - it tends to fluctuate quite a bit, and leaves you with results either far too fast or too slow depending on the device. You could use setInterval, but that can be unreliable too. Keeping track of delta time is a good solution, but it too can behave differently depending on the frame rate.

So ticker handles running your update loop at a more consistent rate - either speeding it up or slowing it down in response to performance, this way.

Installation

npm install ticker

Usage

ticker = require('ticker')(element, framerate, skips)

Creates a new ticker instance.

  • element should either be window or the canvas element you're drawing to.
  • framerate is the number of frames per second you'd like to tick, and defaults to 60.
  • skips is the maximum frames you'd like to skip per render. Defaults to 1. Set to 0 to disable entirely.

ticker.on('tick', callback)

Emitted for every frame of logic you should to run.

ticker.on('draw', callback)

Emitted for every draw call you should run.

var ticker = require('ticker')
  , canvas = document.createElement('canvas')
  , ctx = canvas.getContext('2d')
  , x = 0
  , y = 0

ticker(window, 60).on('tick', function() {
  x += Math.round(Math.random()*2-1)*10
  y += Math.round(Math.random()*2-1)*10
}).on('draw', function() {
  ctx.fillStyle = 'black'
  ctx.fillRect(0, 0, canvas.width, canvas.height)
  ctx.fillStyle = 'white'
  ctx.fillRect(x, y, 10, 10)
})

ticker's People

Contributors

hughsk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.