Skip to content

Instantly share code, notes, and snippets.

@adammacias
Last active October 14, 2019 19:59
Show Gist options
  • Select an option

  • Save adammacias/5742cd5dcb2f560d2ef4c678c9afa426 to your computer and use it in GitHub Desktop.

Select an option

Save adammacias/5742cd5dcb2f560d2ef4c678c9afa426 to your computer and use it in GitHub Desktop.
window.requestAnimFrame = (function() {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
var c = document.getElementById("canv");
var $ = c.getContext("2d");
var w = c.width = window.innerWidth;
var h = c.height = window.innerHeight;
var lite = (function() {
function lite(x, y) {
this.x = x;
this.y = y;
}
lite.prototype.upd = function() {
this.x -= Math.sin(Math.random()) * 6 - 3;
this.y -= Math.cos(Math.random()) * 6 - 3;
};
return lite;
})();
var strikes = [new lite(w / 2, h / 2)];
$.fillStyle = 'hsla(264, 95%, 25%, 1)';
$.fillRect(0, 0, w, h);
var rnd = function(min, max) {
return Math.random() * (max - min) + min;
}
var draw = function() {
$.globalCompositeOperation = 'source-over';
$.fillRect(0, 0, w, h);
var t1 = "Lightning Strikes".split("").join(String.fromCharCode(0x2004));
$.font = "5em Poiret One";
$.fillStyle = 'hsla(264, 95%, 25%, .9)';
$.measureText(t1);
$.fillText(t1, w / 2 - 450, h / 2);
var t2 = "Lightning Strikes".split("").join(String.fromCharCode(0x2004));
$.fillStyle = 'hsla(195, 95%, 95%, 1)';
$.font = "5em Poiret One";
$.measureText(t2);
$.fillText(t2, w / 2 - 453, h / 2 + 4);
var g =
$.fillStyle = 'hsla(264, 95%, 25%, .2)';
$.lineWidth = rnd(1, 5);
$.lineCap = 'round';
$.shadowBlur = 30;
$.shadowOffsetX = 3;
$.shadowOffsetY = 3;
$.shadowColor = 'hsla(195, 95%, 85%, .8)';
$.strokeStyle = 'hsla(195, 95%, 95%, 1)';
$.globalCompositeOperation = 'lighter';
$.beginPath();
$.moveTo(strikes[0].x, strikes[0].y);
strikes.forEach(function(itm, idx) {
itm.upd();
var idx = idx + (Math.floor(Math.random()) * 6 - 3);
if (0 <= idx && idx < strikes.length) {
var o = strikes[idx];
$.lineTo(o.x, o.y);
}
});
$.stroke();
if (strikes.length > 200) {
strikes.remove(0, 1);
}
};
window.addEventListener("mousemove", function(e) {
if (Math.random() * 100 < 50) {
var x = e.pageX || e.screenX;
var y = e.pageY || e.screenY;
strikes.push(new lite(x, y));
}
});
window.addEventListener("touchmove", function(e) {
if (Math.random() * 100 < 50) {
var x = e.touches[0].pageX || e.touches[0].screenX;
var y = e.touches[0].pageY || e.touches[0].screenY;
e.preventDefault();
strikes.push(new lite(x, y));
}
});
window.addEventListener('resize', function() {
c.width = w = window.innerWidth;
c.height = h = window.innerHeight;
$.fillStyle = 'hsla(264, 95%, 25%, 1)';
draw();
}, false);
function run() {
window.requestAnimFrame(run);
draw();
}
run();
/*!
* Emoji Cursor.js
* - 90's cursors collection
* -- https://github.com/tholman/90s-cursor-effects
* -- https://codepen.io/tholman/full/rxJpdQ
*/
(function emojiCursor() {
var possibleEmoji = ["⚡", "⚡", "⚡", "⚡"]
var width = window.innerWidth;
var height = window.innerHeight;
var cursor = {x: width/2, y: width/2};
var particles = [];
function init() {
bindEvents();
loop();
}
// Bind events that are needed
function bindEvents() {
document.addEventListener('mousemove', onMouseMove);
document.addEventListener('touchmove', onTouchMove);
document.addEventListener('touchstart', onTouchMove);
window.addEventListener('resize', onWindowResize);
}
function onWindowResize(e) {
width = window.innerWidth;
height = window.innerHeight;
}
function onTouchMove(e) {
if( e.touches.length > 0 ) {
for( var i = 0; i < e.touches.length; i++ ) {
addParticle( e.touches[i].clientX, e.touches[i].clientY, possibleEmoji[Math.floor(Math.random()*possibleEmoji.length)]);
}
}
}
function onMouseMove(e) {
cursor.x = e.clientX;
cursor.y = e.clientY;
addParticle( cursor.x, cursor.y, possibleEmoji[Math.floor(Math.random()*possibleEmoji.length)]);
}
function addParticle(x, y, character) {
var particle = new Particle();
particle.init(x, y, character);
particles.push(particle);
}
function updateParticles() {
// Updated
for( var i = 0; i < particles.length; i++ ) {
particles[i].update();
}
// Remove dead particles
for( var i = particles.length -1; i >= 0; i-- ) {
if( particles[i].lifeSpan < 0 ) {
particles[i].die();
particles.splice(i, 1);
}
}
}
function loop() {
requestAnimationFrame(loop);
updateParticles();
}
/**
* Particles
*/
function Particle() {
this.lifeSpan = 120; //ms
this.initialStyles ={
"position": "absolute",
"display": "block",
"pointerEvents": "none",
"z-index": "10000000",
"fontSize": "16px",
"will-change": "transform"
};
// Init, and set properties
this.init = function(x, y, character) {
this.velocity = {
x: (Math.random() < 0.5 ? -1 : 1) * (Math.random() / 2),
y: 1
};
this.position = {x: x - 10, y: y - 20};
this.element = document.createElement('span');
this.element.innerHTML = character;
applyProperties(this.element, this.initialStyles);
this.update();
document.body.appendChild(this.element);
};
this.update = function() {
this.position.x += this.velocity.x;
this.position.y += this.velocity.y;
this.lifeSpan--;
this.element.style.transform = "translate3d(" + this.position.x + "px," + this.position.y + "px,0) scale(" + (this.lifeSpan / 120) + ")";
}
this.die = function() {
this.element.parentNode.removeChild(this.element);
}
}
/**
* Utils
*/
// Applies css `properties` to an element.
function applyProperties( target, properties ) {
for( var key in properties ) {
target.style[ key ] = properties[ key ];
}
}
init();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment