Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Framerate independent #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions src/bubbleCursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export function bubbleCursor(options) {
let canvas, context

let canvImages = []
let lastTime = 0;

function init(wrapperEl) {
canvas = document.createElement("canvas")
Expand All @@ -31,7 +32,7 @@ export function bubbleCursor(options) {
}

bindEvents()
loop()
requestAnimationFrame(loop);
}

// Bind events that are needed
Expand Down Expand Up @@ -84,12 +85,12 @@ export function bubbleCursor(options) {
particles.push(new Particle(x, y, img))
}

function updateParticles() {
function updateParticles(deltaTime) {
context.clearRect(0, 0, width, height)

// Update
for (let i = 0; i < particles.length; i++) {
particles[i].update(context)
particles[i].update(context, deltaTime);
}

// Remove dead particles
Expand All @@ -100,8 +101,10 @@ export function bubbleCursor(options) {
}
}

function loop() {
updateParticles()
function loop(time) {
const deltaTime = Math.min(100, time - lastTime) / (1000 / 60);
lastTime = time;
updateParticles(deltaTime)
requestAnimationFrame(loop)
}

Expand All @@ -118,13 +121,12 @@ export function bubbleCursor(options) {

this.baseDimension = 4

this.update = function(context) {
this.position.x += this.velocity.x
this.position.y += this.velocity.y
this.velocity.x += ((Math.random() < 0.5 ? -1 : 1) * 2) / 75
this.velocity.y -= Math.random() / 600
this.lifeSpan--

this.update = function(context, deltaTime) {
this.position.x += this.velocity.x * deltaTime
this.position.y += this.velocity.y * deltaTime
this.velocity.x += ((Math.random() < 0.5 ? -1 : 1) * 2) / 75 * deltaTime
this.velocity.y -= Math.random() / 600 * deltaTime
this.lifeSpan -= deltaTime;
const scale =
0.2 + (this.initialLifeSpan - this.lifeSpan) / this.initialLifeSpan

Expand Down
19 changes: 11 additions & 8 deletions src/clockCursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export function clockCursor(options) {
let height = window.innerHeight;
let cursor = { x: width / 2, y: width / 2 };
let canvas, context;
let lastTime = 0;

const dateColor = (options && options.dateColor) || "blue";
const faceColor = (options && options.faceColor) || "black";
Expand Down Expand Up @@ -182,7 +183,7 @@ export function clockCursor(options) {
}

bindEvents();
loop();
requestAnimationFrame(loop);
}

// Bind events that are needed
Expand Down Expand Up @@ -230,14 +231,14 @@ export function clockCursor(options) {
}
}

function updatePositions() {
function updatePositions(deltaTime) {
let widthBuffer = 80;

zy[0] = Math.round((dy[0] += (cursor.y - dy[0]) * del));
zx[0] = Math.round((dx[0] += (cursor.x - dx[0]) * del));
zy[0] = Math.round((dy[0] += (cursor.y - dy[0]) * del * deltaTime));
zx[0] = Math.round((dx[0] += (cursor.x - dx[0]) * del * deltaTime));
for (let i = 1; i < sum; i++) {
zy[i] = Math.round((dy[i] += (zy[i - 1] - dy[i]) * del));
zx[i] = Math.round((dx[i] += (zx[i - 1] - dx[i]) * del));
zy[i] = Math.round((dy[i] += (zy[i - 1] - dy[i]) * del * deltaTime));
zx[i] = Math.round((dx[i] += (zx[i - 1] - dx[i]) * del * deltaTime));
if (dy[i - 1] >= height - 80) dy[i - 1] = height - 80;
if (dx[i - 1] >= width - widthBuffer) dx[i - 1] = width - widthBuffer;
}
Expand Down Expand Up @@ -315,8 +316,10 @@ export function clockCursor(options) {
}
}

function loop() {
updatePositions();
function loop(time) {
const deltaTime = Math.min(100, time - lastTime) / (1000 / 60);
lastTime = time;
updatePositions(deltaTime);
updateParticles();

requestAnimationFrame(loop);
Expand Down
23 changes: 13 additions & 10 deletions src/emojiCursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function emojiCursor(options) {
const particles = []
const canvImages = []
let canvas, context
let lastTime = 0;

function init() {
canvas = document.createElement("canvas")
Expand Down Expand Up @@ -57,7 +58,7 @@ export function emojiCursor(options) {
})

bindEvents()
loop()
requestAnimationFrame(loop);
}

// Bind events that are needed
Expand Down Expand Up @@ -132,12 +133,12 @@ export function emojiCursor(options) {
particles.push(new Particle(x, y, img))
}

function updateParticles() {
function updateParticles(deltaTime) {
context.clearRect(0, 0, width, height)

// Update
for (let i = 0; i < particles.length; i++) {
particles[i].update(context)
particles[i].update(context, deltaTime)
}

// Remove dead particles
Expand All @@ -148,8 +149,10 @@ export function emojiCursor(options) {
}
}

function loop() {
updateParticles()
function loop(time) {
const deltaTime = Math.min(100, time - lastTime) / (1000 / 60);
lastTime = time;
updateParticles(deltaTime)
requestAnimationFrame(loop)
}

Expand All @@ -168,12 +171,12 @@ export function emojiCursor(options) {
this.position = { x: x, y: y }
this.canv = canvasItem

this.update = function(context) {
this.position.x += this.velocity.x
this.position.y += this.velocity.y
this.lifeSpan--
this.update = function(context, deltaTime) {
this.position.x += this.velocity.x * deltaTime
this.position.y += this.velocity.y * deltaTime
this.lifeSpan -= deltaTime;

this.velocity.y += 0.05
this.velocity.y += 0.05 * deltaTime;

const scale = Math.max(this.lifeSpan / this.initialLifeSpan, 0)

Expand Down
23 changes: 13 additions & 10 deletions src/fairyDustCursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export function fairyDustCursor(options) {
const particles = [];
const canvImages = [];
let canvas, context;
let lastTime = 0;

const char = "*";

Expand Down Expand Up @@ -64,7 +65,7 @@ export function fairyDustCursor(options) {
});

bindEvents();
loop();
requestAnimationFrame(loop);
}

// Bind events that are needed
Expand Down Expand Up @@ -133,12 +134,12 @@ export function fairyDustCursor(options) {
particles.push(new Particle(x, y, color));
}

function updateParticles() {
function updateParticles(deltaTime) {
context.clearRect(0, 0, width, height);

// Update
for (let i = 0; i < particles.length; i++) {
particles[i].update(context);
particles[i].update(context, deltaTime);
}

// Remove dead particles
Expand All @@ -149,8 +150,10 @@ export function fairyDustCursor(options) {
}
}

function loop() {
updateParticles();
function loop(time) {
const deltaTime = Math.min(100, time - lastTime) / (1000 / 60);
lastTime = time;
updateParticles(deltaTime);
requestAnimationFrame(loop);
}

Expand All @@ -165,12 +168,12 @@ export function fairyDustCursor(options) {
this.position = { x: x, y: y };
this.canv = canvasItem;

this.update = function (context) {
this.position.x += this.velocity.x;
this.position.y += this.velocity.y;
this.lifeSpan--;
this.update = function (context, deltaTime) {
this.position.x += this.velocity.x * deltaTime;
this.position.y += this.velocity.y * deltaTime;
this.lifeSpan -= deltaTime;

this.velocity.y += 0.02;
this.velocity.y += 0.02 * deltaTime;

const scale = Math.max(this.lifeSpan / this.initialLifeSpan, 0);

Expand Down
19 changes: 11 additions & 8 deletions src/followingDotCursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export function followingDotCursor(options) {
let cursor = { x: width / 2, y: width / 2 };
let dot = new Dot(width / 2, height / 2, 10, 10);
let canvas, context;
let lastTime = 0;

function init() {
canvas = document.createElement("canvas");
Expand All @@ -28,7 +29,7 @@ export function followingDotCursor(options) {
}

bindEvents();
loop();
requestAnimationFrame(loop);
}

// Bind events that are needed
Expand Down Expand Up @@ -61,14 +62,16 @@ export function followingDotCursor(options) {
}
}

function updateDot() {
function updateDot(deltaTime) {
context.clearRect(0, 0, width, height);

dot.moveTowards(cursor.x, cursor.y, context);
dot.moveTowards(cursor.x, cursor.y, context, deltaTime);
}

function loop() {
updateDot();
function loop(time) {
const deltaTime = Math.min(100, time - lastTime) / (1000 / 60);
lastTime = time;
updateDot(deltaTime);
requestAnimationFrame(loop);
}

Expand All @@ -77,9 +80,9 @@ export function followingDotCursor(options) {
this.width = width;
this.lag = lag;

this.moveTowards = function (x, y, context) {
this.position.x += (x - this.position.x) / this.lag;
this.position.y += (y - this.position.y) / this.lag;
this.moveTowards = function (x, y, context, deltaTime) {
this.position.x += (x - this.position.x) / (this.lag / deltaTime);
this.position.y += (y - this.position.y) / (this.lag / deltaTime);

context.fillStyle = "rgba(50, 50, 50, 0.65)";
context.beginPath();
Expand Down
17 changes: 10 additions & 7 deletions src/ghostCursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export function ghostCursor(options) {
let cursor = { x: width / 2, y: width / 2 };
let particles = [];
let canvas, context;
let lastTime = 0;

let baseImage = new Image();
baseImage.src =
Expand All @@ -32,7 +33,7 @@ export function ghostCursor(options) {
}

bindEvents();
loop();
requestAnimationFrame(loop);
}

// Bind events that are needed
Expand Down Expand Up @@ -81,12 +82,12 @@ export function ghostCursor(options) {
particles.push(new Particle(x, y, image));
}

function updateParticles() {
function updateParticles(deltaTime) {
context.clearRect(0, 0, width, height);

// Update
for (let i = 0; i < particles.length; i++) {
particles[i].update(context);
particles[i].update(context, deltaTime);
}

// Remove dead particles
Expand All @@ -97,8 +98,10 @@ export function ghostCursor(options) {
}
}

function loop() {
updateParticles();
function loop(time) {
const deltaTime = Math.min(100, time - lastTime) / (1000 / 60);
lastTime = time;
updateParticles(deltaTime);
requestAnimationFrame(loop);
}

Expand All @@ -114,8 +117,8 @@ export function ghostCursor(options) {

this.image = image;

this.update = function (context) {
this.lifeSpan--;
this.update = function (context, deltaTime) {
this.lifeSpan -= deltaTime;
const opacity = Math.max(this.lifeSpan / this.initialLifeSpan, 0);

context.globalAlpha = opacity;
Expand Down
15 changes: 9 additions & 6 deletions src/rainbowCursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export function rainbowCursor(options) {
let cursor = { x: width / 2, y: width / 2 };
let particles = [];
let canvas, context;
let lastTime = 0;

const totalParticles = options?.length || 20;
const colors = options?.colors || [
Expand Down Expand Up @@ -41,7 +42,7 @@ export function rainbowCursor(options) {
}

bindEvents();
loop();
requestAnimationFrame(loop);
}

// Bind events that are needed
Expand Down Expand Up @@ -85,7 +86,7 @@ export function rainbowCursor(options) {
particles.push(new Particle(x, y, image));
}

function updateParticles() {
function updateParticles(deltaTime) {
context.clearRect(0, 0, width, height);
context.lineJoin = "round";

Expand All @@ -102,8 +103,8 @@ export function rainbowCursor(options) {

particleSets.push({ x: x, y: y });

x += (nextParticle.position.x - particle.position.x) * 0.4;
y += (nextParticle.position.y - particle.position.y) * 0.4;
x += (nextParticle.position.x - particle.position.x) * 0.4 / deltaTime;
y += (nextParticle.position.y - particle.position.y) * 0.4 / deltaTime;
});

colors.forEach((color, index) => {
Expand All @@ -129,8 +130,10 @@ export function rainbowCursor(options) {
});
}

function loop() {
updateParticles();
function loop(time) {
const deltaTime = Math.min(100, time - lastTime) / (1000 / 60);
lastTime = time;
updateParticles(deltaTime);
requestAnimationFrame(loop);
}

Expand Down
Loading