canvas-starfield
Advanced tools
Comparing version 1.1.3 to 1.1.4
@@ -37,2 +37,4 @@ /* | ||
/* | ||
* Starfield Constructor | ||
* | ||
* config: | ||
@@ -45,5 +47,8 @@ * { | ||
* maxRadius: number, default 2 | ||
* shootingStarInterval: 2000 | ||
* } | ||
*/ | ||
function Starfield(config) { | ||
config = config || {} | ||
this.canvas = typeof config.canvas === 'string' ? | ||
@@ -59,7 +64,7 @@ document.querySelector(config.canvas) : | ||
this.maxStars = config.numStars || 500 | ||
this.maxRadius = config.maxRadius | ||
this.maxRadius = config.maxRadius || 1.5 | ||
this.shootingStarInterval = config.shootingStarInterval || undefined | ||
this.lastShootingStar = this.shootingStarInterval ? Date.now() : undefined | ||
this.shootingStar = undefined; | ||
this.shootingStarInterval = config.shootingStarInterval | ||
this.lastShootingStar = this.shootingStarInterval ? performance.now() : undefined | ||
this.shootingStar | ||
@@ -75,5 +80,5 @@ this.onResize() | ||
y: Math.round(Math.random() * this.canvas.height), | ||
r: 0.5 + (Math.random() * (this.maxRadius || 500)), | ||
r: 0.5 + (Math.random() * this.maxRadius), | ||
l: Math.random(), | ||
bl: 0.1 * (Math.random()*6 + 2), | ||
bl: 0.1 * (Math.random() * 6 + 2), | ||
dl: Math.round(Math.random()) === 1? 0.01: -0.01 | ||
@@ -84,16 +89,18 @@ } | ||
Starfield.prototype.loadStars = function() { | ||
this.stars = [] | ||
this.stars = new Array(this.numStars) | ||
for (var i = 0, l = this.numStars; i < l; i++) | ||
this.stars.push(this.star()) | ||
var i = this.numStars | ||
while (i-- > 0) this.stars[i] = this.star() | ||
} | ||
Starfield.prototype.onResize = function() { | ||
this.canvas.width = Number(this.style.width.replace('px', '')) * window.devicePixelRatio | ||
this.canvas.height = Number(this.style.height.replace('px', '')) * window.devicePixelRatio | ||
var ratio = window.devicePixelRatio || 1 | ||
if (this.canvas.width / window.devicePixelRatio < 500) this.numStars = 100 | ||
this.canvas.width = this.style.width.replace('px', '') | 0 * ratio | ||
this.canvas.height = this.style.height.replace('px', '') | 0 * ratio | ||
if (this.canvas.width / ratio < 500) this.numStars = 100 | ||
else this.numStars = this.maxStars | ||
this.loadStars(); | ||
this.loadStars() | ||
} | ||
@@ -109,4 +116,4 @@ | ||
Starfield.prototype.start = function() { | ||
var tick = function() { | ||
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height) | ||
var tick = function(timeStamp) { | ||
this.ctx.fillStyle = 'black' | ||
@@ -121,3 +128,3 @@ this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height) | ||
star.y += this.vy | ||
star.y += this.vy | ||
star.x += this.vx | ||
@@ -132,20 +139,18 @@ star.l += star.dl | ||
if (this.shootingStar) { | ||
var star = this.shootingStar; | ||
var star = this.shootingStar | ||
this.draw(star) | ||
star.y += 15 | ||
star.x += 15 | ||
star.y += 3 | ||
star.x += 7 | ||
star.l += star.dl | ||
star.r -= .1 | ||
star.r -= 0.06 | ||
if (star.r <= 0) this.shootingStar = undefined | ||
} else if (this.shootingStarInterval) { | ||
var i = this.shootingStarInterval * 1000 | ||
var t = Date.now() | ||
if (t - this.lastShootingStar >= i) { | ||
if (timeStamp - this.lastShootingStar >= this.shootingStarInterval) { | ||
this.shootingStar = this.star() | ||
this.lastShootingStar = Date.now() | ||
this.lastShootingStar = timeStamp | ||
this.shootingStar.r = 3 | ||
@@ -152,0 +157,0 @@ } |
{ | ||
"name": "canvas-starfield", | ||
"version": "1.1.3", | ||
"version": "1.1.4", | ||
"description": "A canvas rendered starfield that twinkles and moves.", | ||
@@ -5,0 +5,0 @@ "main": "canvas-starfield.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
7160
5
118
0