Skip to content

Instantly share code, notes, and snippets.

View stekhn's full-sized avatar

Steffen Kühne stekhn

View GitHub Profile
@stekhn
stekhn / getScrollY.js
Last active December 28, 2015 09:49
Function that returns the current scroll position. Useful for "snowfallesk" and parallax scrolling projects.
function getScrollY() {
var currentY = 0;
if (typeof( window.pageYOffset ) == 'number') {
currentY = window.pageYOffset;
} else if (document.body && ( document.body.scrollLeft || document.body.scrollTop )) {
currentY = document.body.scrollTop;
} else if (document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop )) {
currentY = document.documentElement.scrollTop;
}
@stekhn
stekhn / getUrlParam.js
Last active December 28, 2015 09:49
Returns the value for a certain URL parameter. If your URL is http://example.com/index.html?year=2013 the function getUrlParam(year) will return the string "2013".
function getUrlParam(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if (results == null) {
return "";
} else {
@stekhn
stekhn / hideErrors.js
Last active December 29, 2015 01:59
Suppresses alle JavaScript errors. Don't try this at home.
function hideErrors() {
window.onerror = function () {
return true;
}
}
@stekhn
stekhn / playButton.css
Created May 30, 2014 11:48
CSS play button for videos
.playButton {
background-color: #888;
border-radius: 30px;
display: inline-block;
height: 60px;
left: 50%;
position: absolute;
margin-left: -30px;
top: 120px;
width: 60px;
@stekhn
stekhn / takeScreenshot.js
Last active August 29, 2015 14:14
Take a screenshot from a website using PhantomJS
var page = require('webpage').create(),
system = require('system'),
args = system.args,
siteName;
function takeScreenshot() {
if (args.length === 1) {
console.log('Please specify a URL to screenshot.');
@stekhn
stekhn / mongodb.conf
Created October 16, 2015 13:17
Example YAML configuration for a MongoDB 3.0 database on CentOS
processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongod.pid
net:
bindIp: 127.0.0.1
port: 27017
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
@stekhn
stekhn / mapValue.js
Created November 17, 2015 09:21
Map value to range
function mapValue(value, fromMin, toMin, fromMax, toMax) {
return (value - fromMin) / (toMin - fromMin) * (toMax - fromMax) + fromMax;
}
@stekhn
stekhn / gist:16584703877ca138e5a8
Created February 17, 2016 08:44
Generate unique IDs in Excel
=IF(COUNTIFS($B$1:B3;B3)>1;INDEX($A$1:A2;IFERROR(MATCH(B3;$B$1:B2;0);1));MAX($A$1:A2)+1)
@stekhn
stekhn / stdDev.js
Last active July 8, 2016 17:58
Standard deviation in JavaScript
function stdDev(arr) {
var avg = mean(arr);
var squareDiffs = arr.map(function (value) {
var diff = value - avg;
var sqrDiff = diff * diff;
return sqrDiff;
});
@stekhn
stekhn / weightedMean.js
Last active September 25, 2024 03:23
Weighted arithmetic mean (average) in JavaScript
function weightedMean(arrValues, arrWeights) {
var result = arrValues.map(function (value, i) {
var weight = arrWeights[i];
var sum = value * weight;
return [sum, weight];
}).reduce(function (p, c) {