Spring physics with CSS variables
How do I make an spring physics with css variables?
Move mouse or tap to get the springy ball going! Currently looks best in Chrome as calc() doesn’t work with all units in Firefox https://bugzilla.mozilla.org/show_bug.cgi?id=956573. What is a spring physics with css variables? How do you make a spring physics with css variables? This script and codes were developed by Val Head on 26 November 2022, Saturday.
Spring physics with CSS variables - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Spring physics with CSS variables</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css"> <style> /* NOTE: The styles were added inline because Prefixfree needs access to your styles and they must be inlined if they are on local disk! */ :root { --mouse-x; --mouse-y; --scale; --radius: 40px; --factor: 1;
}
.ball { width: var(--radius); height: var(--radius); background: #D92659; border-radius: 50%; position:absolute;
/* use calc() and the CSS variables created above to center the ball around the mouse position. */ transform: translate(calc(var(--mouse-x) * 1px - var(--radius)/2),calc(var(--mouse-y) * 1px - var(--radius)/2));
}
.halo { width: var(--radius); height: var(--radius); background: rgb(114, 61, 83); border-radius: 50%; position:absolute; opacity: .15;
/* filter: blur(var(--factor)); */ transform: scale(calc(var(--scale) * var(--factor)));
}
.halo:nth-of-type(1) { --factor: .3;
}
.halo:nth-of-type(2) { --factor: 0.5;
}
.halo:nth-of-type(3) { --factor: .9;
}
body { background: #070611; font-family: Verdana, sans-serif;
}
p { color: whitesmoke; width: 400px; top: 10px; left: 10px; position:absolute;
} </style> <script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
</head>
<body>
<div class="ball"> <div class="halo"></div> <div class="halo"></div> <div class="halo"></div>
</div> <script src='https://cdnjs.cloudflare.com/ajax/libs/hammer.js/2.0.8/hammer.min.js'></script> <script src="js/index.js"></script>
</body>
</html>
Spring physics with CSS variables - Script Codes CSS Codes
:root { --mouse-x; --mouse-y; --scale; --radius: 40px; --factor: 1;
}
.ball { width: var(--radius); height: var(--radius); background: #D92659; border-radius: 50%; position:absolute;
/* use calc() and the CSS variables created above to center the ball around the mouse position. */ transform: translate(calc(var(--mouse-x) * 1px - var(--radius)/2),calc(var(--mouse-y) * 1px - var(--radius)/2));
}
.halo { width: var(--radius); height: var(--radius); background: rgb(114, 61, 83); border-radius: 50%; position:absolute; opacity: .15;
/* filter: blur(var(--factor)); */ transform: scale(calc(var(--scale) * var(--factor)));
}
.halo:nth-of-type(1) { --factor: .3;
}
.halo:nth-of-type(2) { --factor: 0.5;
}
.halo:nth-of-type(3) { --factor: .9;
}
body { background: #070611; font-family: Verdana, sans-serif;
}
p { color: whitesmoke; width: 400px; top: 10px; left: 10px; position:absolute;
}
Spring physics with CSS variables - Script Codes JS Codes
'use strict';
var xpos = 0;
var targetX = 0;
var ypos = 0;
var targetY = 0;
var velX = 0;
var velY = 0;
var docStyle = document.documentElement.style;
var drag = 0.8;
var strength = 0.12;
function springItOn() { var diffX = targetX - xpos; diffX *= strength; velX *= drag; velX += diffX; xpos += velX; var diffY = targetY - ypos; diffY *= strength; velY *= drag; velY += diffY; ypos += velY; //apply the newly calculated positions via CSS custom properties each frame. SO FANCY docStyle.setProperty('--mouse-x', xpos); docStyle.setProperty('--mouse-y', ypos); //set the halo scale property baesd on some arbitrary math for kicks docStyle.setProperty('--scale', (velY + velX) * strength); requestAnimationFrame(springItOn);
}
springItOn();
//update the target position based on where the mouse is
document.addEventListener('mousemove', function (e) { targetX = e.clientX; targetY = e.clientY;
});
Developer | Val Head |
Username | valhead |
Uploaded | November 26, 2022 |
Rating | 4.5 |
Size | 3,591 Kb |
Views | 14,168 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
Neon glow letters | 2,595 Kb |
Net magazine example - Start | 4,830 Kb |
IOS Gmail loader-ish | 2,920 Kb |
Basic CSS animation and transition example | 2,915 Kb |
Geometric Pattern Mask | 2,527 Kb |
Adding Items | 4,008 Kb |
Playing a CSS animation on hover | 2,424 Kb |
Net magazine demo - Step 3 | 5,295 Kb |
Nav animation exercise - START | 3,402 Kb |
Hypno triangle | 2,371 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
Break Out | AzazelN28 | 12,431 Kb |
Mondrian | Chrisarmstrong | 2,787 Kb |
IbrahimJabbari-Effect21 | Ibrahimjabbari | 1,882 Kb |
RRC wrapSwitch | Pshrmn | 2,922 Kb |
A Pen by utcwebdev | Utcwebdev | 2,856 Kb |
Cut and Paste Roll Link | BottomlineInteractive | 2,546 Kb |
Particle Motion trajectories | Sniejadlik | 5,899 Kb |
Vue.js Lazy Loading | Kjbrum | 3,620 Kb |
IE flex collapse bug fix | Rowno | 2,678 Kb |
Virtual vinyl | Davidpanik | 3,474 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!