1.1.2 • Published 4 years ago

canvas-loop-engine v1.1.2

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

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
1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.0-alpha.1

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.1.0-alpha.3

4 years ago

0.1.0-alpha.2

4 years ago

0.1.0-alpha.1

4 years ago

0.1.0-alpha.0

4 years ago