Skip to content

Instantly share code, notes, and snippets.

View nathansearles's full-sized avatar
✌️

Nathan Searles nathansearles

✌️
View GitHub Profile
@nathansearles
nathansearles / scrollTo.js
Created June 8, 2022 16:50 — forked from james2doyle/scrollTo.js
a native scrollTo function in javascript that uses requestAnimationFrame and easing for animation
// easing functions http://goo.gl/5HLl8
Math.easeInOutQuad = function (t, b, c, d) {
t /= d/2;
if (t < 1) {
return c/2*t*t + b
}
t--;
return -c/2 * (t*(t-2) - 1) + b;
};

Demo

Context

useViewportScroll is a great way to create a parallax effect as the page scrolls. In some cases however, we only want to scroll when an element is in the viewport area.

So for example, if we have a "landscape" scene, and want to animate the Sun object only when it's in view, we start with our useViewportScroll implementation:

function Sun(props) {
 const { scrollY, scrollYProgress } = useViewportScroll();
@nathansearles
nathansearles / gist:9d63ea9be2da03e8830212d9207cdcff
Created February 18, 2021 18:28
FFmpeg output mp3 audio for web
ffmpeg -i 'source.mp3' -codec:a libmp3lame -ss 0 -to 16 -af "afade=t=in:st=0:d=3,afade=t=out:st=13:d=3" -map_metadata -1 -b:a 50k 'output.mp3'
@nathansearles
nathansearles / gist:a893f729b100d55362ea1552e3e0d3ad
Last active August 19, 2021 00:43
FFmpeg output mp4 video for web
# Output video
ffmpeg -an -i source.mp4 -vcodec libx264 -pix_fmt yuv420p -profile:v baseline -level 3 output.mp4
# Output video and change width (-vf scale=720:-1)
ffmpeg -an -i source.mp4 -vcodec libx264 -pix_fmt yuv420p -profile:v baseline -level 3 -vf scale=720:-1 output.mp4
#Output first frame as PNG for poster image
ffmpeg -i source.mp4 -ss 00:00:00 -vframes 1 output.png
@nathansearles
nathansearles / Picture.css
Last active October 3, 2020 00:48
Example usage of an Imgix implementation with React using data from headless CMS
img,
picture {
width: 100%;
height: auto;
}
.image {
display: inline-block;
line-height: 0;
position: relative;
@nathansearles
nathansearles / isAutoplaySupported.js
Last active December 13, 2022 11:06
Test if HTML5 video autoplay is supported
// isAutoplaySupported(callback);
// Test if HTML5 video autoplay is supported
isAutoplaySupported = function(callback) {
// Is the callback a function?
if (typeof callback !== 'function') {
console.log('isAutoplaySupported: Callback must be a function!');
return false;
}
// Check if sessionStorage exist for autoplaySupported,
// if so we don't need to check for support again
/*
* Example URL: http://example.com/?city=portland&state=oregon&country=usa
* Example Usage: alert( getQueryString('city') ); will return portland
*/
function getQueryString(obj) {
var string = window.location.search.substring(1);
var strings = string.split('&');
for (var i=0; i < strings.length; i++) {
var set = strings[i].split('=');
if (set[0] == obj) {
@nathansearles
nathansearles / legal-drinking-age.js
Created July 23, 2012 23:43
Get legal drinking age of user
@nathansearles
nathansearles / gist:1112388
Created July 28, 2011 19:50
Example usage of jQuery deferred
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Deferred</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script>
@nathansearles
nathansearles / gist:1112383
Created July 28, 2011 19:48
Return variable from string
var someString = "someArray";
$.someArray =
[
'Item One',
'Item Two',
'Item Three',
'Item Four',
'Item Five',
'Item Six'