Home

Awesome

Starbackjs Starbackjs Demo Night

Click for more examples

Starback.js

<table> <tr> <th>Sponsored by</th> </tr> <tr> <td> <p align="center"> <a href="https://lokal.so/?ref=zuramai"> <img src="https://github.com/zuramai/zuramai/blob/master/sponsors/lokalso.png?raw=true" width="50%"> </a> </p> </td> </tr> </table>

Getting Started

You can use starback.js directly with <script> tag or import syntax.

Example Usage

<canvas id="canvas"></canvas>

<script src="PATH_TO_DIST_FOLDER/starback.js"></script>
<script>
  const canvas = document.getElementById('canvas')
  const starback = new Starback(canvas, {
      type: 'dot',
      quantity: 100,
      direction: 225,
      backgroundColor: ['#0e1118', '#232b3e'],
      randomOpacity: true,
  })
</script>

You can check the more options in Options section

Options

KeyTypeDescriptionRequiredDefault Value
typeStringRequired. Star type to use. The values are 'dot' or 'line'true
widthNumberCanvas width to setno800
heightNumberCanvas height to setno600
backgroundColorString|ArrayColor of the background, use string for solid color, or array of colors for linear gradientno#ccc
quantityNumberNumber of stars to showno100
showFpsBooleanShow FPS on the top left screennofalse
speedNumberStar speedno0.5
starSizeNumber|ArrayUse number for fixed star size. Use [minSize, maxSize] value for random the size within the rangeno[0, 3] for type: dot, 100 for type: line

Additional options for type: 'dot'

KeyTypeDescriptionRequiredDefault Value
directionNumberUse 360 degree to set the directionno225
randomOpacityBoolean|ArrayIf true, random star will get random opacity. Use array [minOpacity, maxOpacity] to limit the random opacitynofalse
starColorStringColor of the stars, you can use rgb or hex like css color.nowhite

Additional options for type: 'line'

KeyTypeDescriptionRequiredDefault Value
directionYNumberThe direction the star to move vertically (1 = to bottom, 0 = to top)no1
directionXNumberThe direction the star to move vertically (1 = to right, 0 = to left)no1
distanceXNumberDistance horizontally the star should reaching atno0.1
frequencyNumberThe frequency of star will be rotatedno10
slopeObjectThe quadraticCurve coordinate that will be applied to stars. Read MDNno{x: 1, y: 1}
starColorString|ArrayColor of the stars, use string for solid color, or array of colors for linear gradientno['#fb00ff', '#00dde0']
spreadNumberThe spread level of the starsno1

Methods

NameParameterDescription
generateStar(amount)amount: numberThe quantity of stars that will be shown
addToFront(callback(ctx))function(ctx: CanvasRenderingContext2D)Add to front of the falling star by given context
addToBehind(callback(ctx))function(ctx: CanvasRenderingContext2D)Add to back of the falling star by given context

Example method usage: adding image to front

let starback = new Starback(canvas, options)

let mountainImage = new Image()
mountainImage.src = 'images/mountain.png'

mountainImage.onload = () => {
  starback.addToFront((ctx) => {
    ctx.drawImage(
      mountain,
      0,
      0,
      mountain.width,
      mountain.height,
      0,
      canvas.height - mountain.height,
      canvas.width,
      mountain.height
    )
  })
}

Contributing

You can contribute to this repository. See CONTRIBUTING.md

Donate

If you have used this library and it's useful for you, please consider to donate:

Ko-fi | Trakteer

License

This library is under MIT license.