1.0.4 ā€¢ Published 4 years ago

dropping-canvas v1.0.4

Weekly downloads
1
License
MIT
Repository
-
Last release
4 years ago

DroppingCanvas

npm GitHub file size in bytes

Introduction

šŸ’§ DroppingCanvas is a lightweight (5 KB) physics tool which can help you generate random motive image object simply on canvas. It is inspired by sprinkler, and DroppingCanvas also provide more flexible and modern APIs.

Demo

Demo

Install

npm install dropping-canvas
# or
yarn add dropping-canvas

or just include the compiled file to your page:

<script src="./dropping.js"></script>

Get Started

Just a few code to run:

var canvas = document.getElementById('canvas')
var images = ['./images/1.png', './images/2.png', './images/3.png']
var droppingCanvas = new DroppingCanvas(canvas, images)

droppingCanvas.start()

If you want to modify the motion of objects, you can pass an option as param to the constructor or use method updateOptions. For exmple:

...
var options = {
  dropPerSecond: 15,
  r: 2,
  vY: [200, 400],
  aA: [-0.2, -0.1]
}
// for more options check Option Properties

var droppingCanvas = new DroppingCanvas(canvas, images, options)
...

Now the vertical speed is in the range of 200-400 (random).

For more options, you can see the Demo or check below šŸ‘‡.

Option Properties

PropertyTypeDescriptionDefault
dropPerSecondNumber, StringNumber of objects generate in a second.20
isBoundingSizeBooleanRestrict the object rebound when z(size) is negative.true
xnull, Array, Number(Range of) initial horizontal position. null: object will generate randomly in the range of canvas width.null
ynull, Array, Number(Range of) initial vertical position. null: object will generate over the top of canvas.(cal the maximum radius)null
zArray, Number(Range of) initial size(scale).0.2 , 0.3
aArray, Number(Range of) initial transparency(alpha).a: 1 , 1
rArray, Number(Range of) initial rotation.0 , 2 * Math .PI
vXArray, Number(Range of) horizontal velocity.-60 , 60
vYArray, Number(Range of) vertical velocity.200 , 500
vZArray, Number(Range of) scale velocity.0 , 0
vAArray, Number(Range of) transparency(alpha) velocity.0 , 0
vRArray, Number(Range of) rotation velocity.-1 , 1
aXArray, Number(Range of) horizontal acceleration.0 , 0
aYArray, Number(Range of) vertical acceleration.80 , 200
aZArray, Number(Range of) scale acceleration.-0.3 , 0
aAArray, Number(Range of) transparency(alpha) acceleration.-1 , -0.6
aRArray, Number(Range of) rotation acceleration.0 , 0

Methods

MethodArgumentsDescription
startActivate the animation.
pausePause the animation.
stopisImmediateClean=falseStop the animation. If isImmediateClean is true, objects will fade out immediately, otherwise the animation will stop after objects disappear from canvas.
updateOptionsoptions, isCover=falseUpdate the physics options of object. If isCover is true, it will just update the options you pass, otherwise other options will replace by default options.
updateImagesimagesUpdate the Images of generate object. It accepts Array, String, FileList.
1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago