Skip to content

Instantly share code, notes, and snippets.

View WebReflection's full-sized avatar
🎯
Focusing

Andrea Giammarchi WebReflection

🎯
Focusing
View GitHub Profile
@WebReflection
WebReflection / ScrollHandler.js
Last active August 29, 2015 13:56
provides basics to handle touch actions on scrollable content
var ScrollHandler = (function(Math, UA){
// (C) 2014 - Andrea Giammarchi
// https://gist.github.com/WebReflection/8924001
var
MINIMUM_SCROLL_DISTANCE = 8,
ONE_EIGHTY_PI = 180 / Math.PI,
atan2 = Math.atan2,
round = Math.round,
sqrt = Math.sqrt,
@WebReflection
WebReflection / scale.fix.js
Last active August 29, 2015 13:55
a better version of github pages file included in all templates since it's currently leaking variables and creating a disturbing function declaration for all browsers.
if (/iP(?:od|hone)/.test(navigator.userAgent)) {
// deletable at least, i.e.
// if (window.gestureStart && dontWantIt) {
// document.removeEventListener("gesturestart", gestureStart);
// delete window.gestureStart;
// }
this.gestureStart = (function(metas, i){
function gestureStart() {
// address .length once per loop
for (i = metas.length; i--;) {
@WebReflection
WebReflection / isFontLoaded.js
Last active December 28, 2015 07:09
well, a weird trick to know if a font is usable inside canvas too
/*! (C) Andrea Giammarchi */
// @link https://gist.github.com/WebReflection/7461879
// @example
// isFontLoaded('FontAwesome', '\uf06b')
var isFontLoaded = function(canvas1, canvas2) {
function reset(canvas, fontName) {
var context = canvas.getContext('2d');
canvas.width = canvas.height = 16;
context.clearRect(0, 0, 16, 16);
context.fillStyle = "rgb(0,0,0)";
@WebReflection
WebReflection / ietouches.md
Last active December 27, 2015 10:49
moved to a repo instead of a gist
@WebReflection
WebReflection / HorizontalScroll.js
Last active December 27, 2015 10:29
the most basic horizontal scroll handler I could think about … no abstraction, some magic happening, works with Android 2.X and all touch devices, might be improved for pointer events too when it comes to touch instead of mouse.
/*! (C) Andrea Giammarchi */
// @link https://gist.github.com/WebReflection/7311642
var HorizontalScroll = (function(UA, Math){
var
// this is horrible, unfortunately needed
bugged = /Android 4\.0/.test(UA),
// AFAIK no proper way to do feature in here detection :-(
preventsAllDefaults = /Android/.test(UA) &&
/Chrom(?:e|ium)/.test(UA),
@WebReflection
WebReflection / ready.js
Created November 3, 2013 21:10
simplifies ready behavior with modern browsers
/*! (C) Andrea Giammarchi */
(function(window, add, dom, load){
function ready(collection) {
return function(e) {
while(collection.length) {
collection.shift().call(window, e);
}
collection.K = true;
};
}
@WebReflection
WebReflection / Delayed.js
Last active March 4, 2020 23:39
whenever you need to execute something after an interaction that might happen many times (panning, scrolling, etc) … this simple mechanism makes simple to execute that maybe expensive operations only once when things are "quiet" and never before or in the middle, if an action start as "start panning" would be is fired and it's calling the clear(…
/*jslint browser: true, indent: 2 */
var Delayed = (function (delay) {
/*! Andrea Giammarchi - Mit Style License */
// https://gist.github.com/WebReflection/7286687
'use strict';
// method shared across all delayed wrappers
function clear() {
@WebReflection
WebReflection / navigator.version.js
Last active December 26, 2015 06:29
navigator.version (major, minor, revision) where exposed and possibl updated to be as cross browser as possible
/** Andrea Giammarchi
* WTFPL - http://www.wtfpl.net/
*/
'version' in navigator || (
navigator.version =
/(?:(?:OS(?: X)?|Android|Windows(?: NT)) |(?:IEMobile|Version|webOS|Nokia\w+)\/)(\d+)[_.](\d+)(?:[_.](\d+(?:\.\d+)?))?/
.test(navigator.userAgent) ?
{
major: RegExp.$1,
minor: RegExp.$2 || '0',
@WebReflection
WebReflection / String.prototype.at.js
Last active April 19, 2018 06:34
After a discussion on symbols VS code points VS strings in es-discuss … here some outcome
!function(dict, from){
/**
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* Version 2, December 2004
*
* Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
*
* Everyone is permitted to copy and distribute verbatim or modified
* copies of this license document, and changing it is allowed as long
@WebReflection
WebReflection / restyle.js
Created October 10, 2013 19:31
A tiny utility to add some dynamic CSS through JavaScript at runtime.
/*jslint browser: true, plusplus: true, indent: 2 */
function restyle(
rules, // an object containing selectors and
// a collection of key/value pairs per each
// selector, i.e.
// restyle({
// 'body > section.main': {
// color: '#EEE',
// 'margin-left': (innerWidth - 200) + 'px'
// }