Skip to content

Instantly share code, notes, and snippets.

View deanhume's full-sized avatar
🎮
Gaming

Dean deanhume

🎮
Gaming
View GitHub Profile
@deanhume
deanhume / intersection-support.js
Created August 7, 2017 15:55
Intersection Observer - Browser Support
// If we don't have support for intersection observer, load the images immediately
if (!('IntersectionObserver' in window)) {
Array.from(images).forEach(image => preloadImage(image));
} else {
// It is supported, load the images
observer = new IntersectionObserver(onIntersection, config);
images.forEach(image => {
observer.observe(image);
});
@deanhume
deanhume / on-intersection.js
Created August 7, 2017 15:54
Intersection Observer - Lazy Load - On Intersection
function onIntersection(entries) {
// Loop through the entries
entries.forEach(entry => {
// Are we in viewport?
if (entry.intersectionRatio > 0) {
// Stop watching and load the image
observer.unobserve(entry.target);
preloadImage(entry.target);
}
@deanhume
deanhume / intersection-observer.js
Last active May 20, 2021 17:55
Intersection Observer - Image lazy load
// Get all of the images that are marked up to lazy load
const images = document.querySelectorAll('.js-lazy-image');
const config = {
// If the image gets within 50px in the Y axis, start the download.
rootMargin: '50px 0px',
threshold: 0.01
};
// The observer for the images on the page
let observer = new IntersectionObserver(onIntersection, config);
@deanhume
deanhume / workbox-ga.js
Created July 27, 2017 12:55
Workbox Google Analytics
importScripts('path/to/offline-google-analytics-import.js');
workbox.googleAnalytics.initialize();
@deanhume
deanhume / workbox-sw.js
Created July 27, 2017 06:31
Workbox Caching Example
importScripts('workbox-sw.prod.v1.1.0.js');
const workboxSW = new self.WorkboxSW();
workboxSW.precache(fileManifest);
// The route for any requests from the googleapis origin
workboxSW.router.registerRoute('https://fonts.googleapis.com/(.*)',
workboxSW.strategies.cacheFirst({
cacheName: 'googleapis',
cacheableResponse: {
@deanhume
deanhume / persistent-storage.js
Last active July 31, 2023 00:37
Persistent Storage API - Progressive Web Apps
if (navigator.storage && navigator.storage.persist) {
navigator.storage.persist().then(granted => {
if (granted) {
alert("Storage will persist and not be cleared");
} else {
alert("Storage won’t persist and may be cleared");
}
});
};
@deanhume
deanhume / text-detection.js
Created May 22, 2017 14:16
Text Detection - Shape Detection API
var image = document.getElementById('image');
// Does the current browser support the text detection API?
if (window.TextDetector) {
let textDetector = new TextDetector();
textDetector.detect(theImage)
.then(detectedTextBlocks => {
// Loop through the results
for (const textBlock of detectedTextBlocks) {
@deanhume
deanhume / face-detection.js
Last active July 24, 2022 00:48
Face Detection - Shape Detection API
var image = document.getElementById('image');
// Does the current browser support the API?
if (window.FaceDetector) {
var faceDetector = new FaceDetector();
faceDetector.detect(image)
.then(faces => {
console.log(‘Faces found:’, faces.length);
@deanhume
deanhume / server-timings-express.js
Created March 30, 2017 08:30
Server Timings - Node Express
const app = express();
const timings = require('server-timings');
app.use(timings.start('routing'));
app.use('/', require('./routes/home.js'));
app.use(timings.end('routing'));
@deanhume
deanhume / web-share.js
Created February 22, 2017 21:34
Web Share API example
var shareButton = document.getElementById('shareThis');
var supported = document.getElementById('support');
// Listen for any clicks
shareButton.addEventListener('click', function (ev) {
// Check if the current browser supports the Web Share API
if (navigator.share !== undefined) {
// Get the canonical URL from the link tag
var shareUrl = document.querySelector('link[rel=canonical]') ? document.querySelector('link[rel=canonical]').href : window.location.href;