Socket
Socket
Sign inDemoInstall

canvas-loop-engine

Package Overview
Dependencies
5
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    canvas-loop-engine

A basic loop engine for canvas animation


Version published
Weekly downloads
13
increased by44.44%
Maintainers
1
Install size
96.8 kB
Created
Weekly downloads
 

Readme

Source

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

TypeDefaultrequiredDescription
HTMLCanvasElementnulltrueThe canvas html element you want to draw on.
import Engine from "canvas-loop-engine"

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

contextType

TypeDefaultrequiredDescription
string"2d"falseThe 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

TypeDefaultrequiredDescription
objectnullfalseThe 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

TypeDefaultrequiredDescription
booleantruefalseDefine 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

TypeDefaultrequiredDescription
booleantruefalseAutomaticaly 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
TypeDefaultrequiredDescription
any{}falseYou 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

TypeDefaultrequiredDescription
function | array of functionnullfalsefunction or array of function that will trigger at the loop init
nametypeDescription
$canvasHTMLCanvasElementthe html convas element
ctxHTMLElementthe html convas element
dataanythe 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

TypeDefaultrequiredDescription
function | array of functionnullfalseFunction or array of function that will trigger when you start the loop
nametypeDescription
$canvasHTMLCanvasElementthe html convas element
ctxHTMLElementthe html convas element
dataanythe 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

TypeDefaultrequiredDescription
function | array of functionnullfalseFunction or array of function that will trigger when you stop the loop
nametypeDescription
$canvasHTMLCanvasElementthe html convas element
ctxHTMLElementthe html convas element
dataanythe 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

TypeDefaultrequiredDescription
function | array of functionnullfalseFunction or array of function that will trigger on each loop update
nametypeDescription
$canvasHTMLCanvasElementthe html convas element
ctxHTMLElementthe html convas element
dataanythe 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

TypeDefaultrequiredDescription
function | array of functionnullfalseFunction or array of function that will trigger on each draw, usually, it's here you draw on canvas
nametypeDescription
$canvasHTMLCanvasElementthe html convas element
ctxHTMLElementthe html convas element
dataanythe 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()

TypeDescription
functionStart the loop, if it was'nt already start

Will trigger the onStart functions


.stop()

TypeDescription
functionStop the loop, if it was'nt already stop

Will trigger the onStop functions


.toggle()

TypeDescription
functionToogle the loop (stop or start)

Will trigger the onStop or onStart functions


.update()

TypeDescription
functiontrigger the onUpdate functions

.draw()

TypeDescription
functiontrigger the onDraw functions

.isRunning

TypeDescription
booleanthe current loop status (if he's running or not)


TODO

  • Doc
  • TU
  • TS

Keywords

FAQs

Last updated on 18 Jun 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc