npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

canvas-loop-engine

v1.1.2

Published

A basic loop engine for canvas animation

Downloads

15

Readme

canvas-loop-engine

CircleCI Status codecov npm npm npm

A basic loop engine for canvas animation

Live example

Install

npm i canvas-loop-engine --save
or
yarn add canvas-loop-engine


Importing

import Engine from "canvas-loop-engine"

const MyLoop = new Engine({ ...options })
MyLoop.start()

Options

$canvas

| Type | Default | required | Description | |:----|:----|:----|:----| | HTMLCanvasElement | null | true | The canvas html element you want to draw on. |

import Engine from "canvas-loop-engine"

const MyLoop = new Engine({
  $canvas: document.querySelector("canvas")
})

contextType

| Type | Default | required | Description | |:----|:----|:----|:----| | string | "2d" | false | The canvas context type giving in the getContext. See getContext documentation |

import Engine from "canvas-loop-engine"

const MyLoop = new Engine({
  $canvas: document.querySelector("canvas"),
  contextType: "webgl"
})

contextAttributes

| Type | Default | required | Description | |:----|:----|:----|:----| | object | null | false | The canvas context attributes giving in the getContext. See getContext documentation |

import Engine from "canvas-loop-engine"

const MyLoop = new Engine({
  $canvas: document.querySelector("canvas"),
  contextType: "webgl",
  contextAttribute: {
    antialias: false,
    depth: false
  }
})

autoStart

| Type | Default | required | Description | |:----|:----|:----|:----| | boolean | true | false | Define if the loop engine should start automaticaly. |

import Engine from "canvas-loop-engine"

const MyLoop = new Engine({
  $canvas: document.querySelector("canvas"),
  autoStart: false //default true
})

clearOnUpdate

| Type | Default | required | Description | |:----|:----|:----|:----| | boolean | true | false | Automaticaly clear the canvas before each draw |

Will trigger just before the onDraw functions

import Engine from "canvas-loop-engine"

const MyLoop = new Engine({
  $canvas: document.querySelector("canvas"),
  autoStart: false //default true
})

data

| Type | Default | required | Description | |:----|:----|:----|:----| | any | {} | false | You can give a data object. it will be return as argument in every props function (onInit, onUpdate, onDraw, onStar, onStop) for a future usage. |

Usually, you can use it to stock your drawing settings (particles etc...)

⚠️ The data object will be deeply clone. It will break every references

import Engine from "canvas-loop-engine"

const MyLoop = new Engine({
  $canvas: document.querySelector("canvas"),
  data: [{
    //...particle data
  }]
})

onInit

| Type | Default | required | Description | |:----|:----|:----|:----| | function | array of function | null | false | function or array of function that will trigger at the loop init |

| name | type | Description | |:----|:----|:----| | $canvas | HTMLCanvasElement | the html convas element | | ctx | HTMLElement | the html convas element | | data | any | the data object you send at the init |

import Engine from "canvas-loop-engine"

const MyLoop = new Engine({
  $canvas: document.querySelector("canvas"),
  onInit: function ({ $canvas, ctx, data }) {
    //do things
  }
})

or

import Engine from "canvas-loop-engine"

const foo = function ({ $canvas, ctx, data }) {
  //do things
}
const bar = function ({ $canvas, ctx, data }) {
  //do things
}

const MyLoop = new Engine({
  $canvas: document.querySelector("canvas"),
  onInit: [foo, bar]
})

onStart

| Type | Default | required | Description | |:----|:----|:----|:----| | function | array of function | null | false | Function or array of function that will trigger when you start the loop |

| name | type | Description | |:----|:----|:----| | $canvas | HTMLCanvasElement | the html convas element | | ctx | HTMLElement | the html convas element | | data | any | the data object you send at the init |

import Engine from "canvas-loop-engine"

const MyLoop = new Engine({
  $canvas: document.querySelector("canvas"),
  onStart: function ({ $canvas, ctx, data }) {
    //do things
  }
})

or

import Engine from "canvas-loop-engine"

const foo = function ({ $canvas, ctx, data }) {
  //do things
}
const bar = function ({ $canvas, ctx, data }) {
  //do things
}

const MyLoop = new Engine({
  $canvas: document.querySelector("canvas"),
  onStart: [foo, bar]
})

onStop

| Type | Default | required | Description | |:----|:----|:----|:----| | function | array of function | null | false | Function or array of function that will trigger when you stop the loop |

| name | type | Description | |:----|:----|:----| | $canvas | HTMLCanvasElement | the html convas element | | ctx | HTMLElement | the html convas element | | data | any | the data object you send at the init |

import Engine from "canvas-loop-engine"

const MyLoop = new Engine({
  $canvas: document.querySelector("canvas"),
  onStop: function ({ $canvas, ctx, data }) {
    //do things
  }
})

or

import Engine from "canvas-loop-engine"

const foo = function ({ $canvas, ctx, data }) {
  //do things
}
const bar = function ({ $canvas, ctx, data }) {
  //do things
}

const MyLoop = new Engine({
  $canvas: document.querySelector("canvas"),
  onStop: [foo, bar]
})

onUpdate

| Type | Default | required | Description | |:----|:----|:----|:----| | function | array of function | null | false | Function or array of function that will trigger on each loop update |

| name | type | Description | |:----|:----|:----| | $canvas | HTMLCanvasElement | the html convas element | | ctx | HTMLElement | the html convas element | | data | any | the data object you send at the init |

import Engine from "canvas-loop-engine"

const MyLoop = new Engine({
  $canvas: document.querySelector("canvas"),
  onUpdate: function ({ $canvas, ctx, data }) {
    //do things
  }
})

or

import Engine from "canvas-loop-engine"

const foo = function ({ $canvas, ctx, data }) {
  //do things
}
const bar = function ({ $canvas, ctx, data }) {
  //do things
}

const MyLoop = new Engine({
  $canvas: document.querySelector("canvas"),
  onUpdate: [foo, bar]
})

onDraw

| Type | Default | required | Description | |:----|:----|:----|:----| | function | array of function | null | false | Function or array of function that will trigger on each draw, usually, it's here you draw on canvas |

| name | type | Description | |:----|:----|:----| | $canvas | HTMLCanvasElement | the html convas element | | ctx | HTMLElement | the html convas element | | data | any | the data object you send at the init |

import Engine from "canvas-loop-engine"

const MyLoop = new Engine({
  $canvas: document.querySelector("canvas"),
  onDraw: function ({ $canvas, ctx, data }) {
    //draw things
  }
})

or

import Engine from "canvas-loop-engine"

const foo = function ({ $canvas, ctx, data }) {
  //draw things
}
const bar = function ({ $canvas, ctx, data }) {
  //draw things
}

const MyLoop = new Engine({
  $canvas: document.querySelector("canvas"),
  onDraw: [foo, bar]
})

Methods

.start()

| Type | Description | |:----|:----| | function | Start the loop, if it was'nt already start |

Will trigger the onStart functions


.stop()

| Type | Description | |:----|:----| | function | Stop the loop, if it was'nt already stop |

Will trigger the onStop functions


.toggle()

| Type | Description | |:----|:----| | function | Toogle the loop (stop or start) |

Will trigger the onStop or onStart functions


.update()

| Type | Description | |:----|:----| | function | trigger the onUpdate functions |


.draw()

| Type | Description | |:----|:----| | function | trigger the onDraw functions |


.isRunning

| Type | Description | |:----|:----| | boolean | the current loop status (if he's running or not) |



TODO

  • [x] Doc
  • [ ] TU
  • [ ] TS