Skip to content

Instantly share code, notes, and snippets.

View fupslot's full-sized avatar
💭
I may be slow to respond.

Eugene Brodsky fupslot

💭
I may be slow to respond.
View GitHub Profile
@fupslot
fupslot / swap_variables.js
Created February 8, 2016 13:08
swap variable values (not temporary variable)
// variables
var a = 10;
var b = 99;
// a^=b, b^=a, a^=b - swap variables using XOR operation,
// details: http://en.wikipedia.org/wiki/XOR_swap_algorithm
a^=b, b^=a, a^=b;
console.log('a', a);
console.log('b', b);
@fupslot
fupslot / createBoundingBox
Last active February 2, 2016 16:03
Draws a bounding box for a given element on the page
/*
Before run the example copy and paste this function into a browser console.
Example:
createBoundingBox($0);
createBoundingBox(document.getElementById('element-id'));
*/
function createBoundingBox(parentEl) {
var body = document.body;
var pRect = parentEl.getBoundingClientRect();
var box = document.createElement('div');
function getInset(rectA, rectB) {
return {
top: (rectB.top - rectB.top),
right: (rectA.left + rectA.width) - (rectB.left + rectB.width),
bottom: (rectA.top + rectA.height) - (rectB.top + rectB.height),
left: rectB.left - rectB.left
};
}
@fupslot
fupslot / gist:5874982
Created June 27, 2013 08:45
JavaScript: jQuery offset relative
(function($){
$.fn.offsetRelative = function(top){
var $this = $(this);
var $parent = $this.offsetParent();
var offset = $this.position();
// add scroll
offset.top += $this.scrollTop()+$parent.scrollTop();
offset.left += $this.scrollLeft()+$parent.scrollLeft();
if(!top) {
var isLeapYear = (new Date(now.getFullYear, 1, 29).getMonth() == 1); // year is leap
// returns total amount of the days that passed since Jan 1 2000
function getDay(aDate) {
if (typeof aDate === "string") {
aDate = new Date(aDate);
}
var zeroYear = 2000;
var totalYears = aDate.getFullYear() - zeroYear;
// calculate all days that passed
// since Jan 1 2000 till current time
<input type="text" class="i18n" data-labels="value#str001 placeholder#str002" />
<button class="i18n" data-labels="innerText#str003">Click Me</button>
<div class="i18n" data-labels="innerText#str004">This string will never be translated</div>
<script>
function i18n_parse (rootElement, labelAttribute, iterator) {
if (arguments.length === 1 && typeof arguments[0] === "function") {
iterator = arguments[0];
rootElement = null;
@fupslot
fupslot / gist:5023373
Created February 24, 2013 10:41
javascript: rgb2hex
function rgbToHex(r, g, b) {
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}
@fupslot
fupslot / unique
Created October 29, 2015 13:44
javascript, unique, uniq, filtering
function unique(in_list) {
var lookup = {};
var values = [];
for (var i = 0; i < in_list.length; i++) {
if (!lookup[in_list[i]]) {
values.push(in_list[i]);
}
lookup[in_list[i]] = true;
}
return values;
@fupslot
fupslot / requestAnimFrame.js
Last active November 17, 2015 11:31 — forked from joelambert/README
Drop in replacements for setTimeout()/setInterval() that makes use of requestAnimationFrame() where possible for better performance
// requestAnimationFrame() shim by Paul Irish
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
window.requestAnimFrame = (function() {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function */ callback, /* DOMElement */ element){
window.setTimeout(callback, 1000 / 60);