Skip to content

Instantly share code, notes, and snippets.

@veganben
veganben / otsbjs.md
Created July 22, 2012 08:31 — forked from agnoster/otsbjs.md
Open Tech School Berlin JS

In the spirit of Railsgirls Berlin, we want to start a programming education group for JavaScript. We need your help to get all the coaching done.

If you are interested in coaching JavaScript, fork this gist and add yourself or leave your contact data in a comment:

@veganben
veganben / Math.correctInverseTan
Created December 10, 2012 11:29
When you want to calculate an angle given the opposite and adjacent sides, there are multiple possible values. This gist calculates the one you really wanted all along.
Math.correctInverseTan = function(x,y){
var θ = Math.atan(y / x);
if(x < 0){
θ += Math.PI;
} else {
if(y < 0){
θ += 2*Math.PI;
}
}
return θ;
@veganben
veganben / gist:6355552
Created August 27, 2013 16:05
You don't always need to know which way up a device is being held. You just need to be able to access the values for height and width. This seems to work:
var devicePixelRatio = window.devicePixelRatio || 1,
ww = devicePixelRatio * (window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth),
wh = devicePixelRatio * (window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight);
// width, height
var landscape = [Math.max(ww, wh), Math.min(ww, wh)];
var portrait = [Math.min(ww, wh), Math.max(ww ,wh)];
@veganben
veganben / Periodic Table of the Elements in JSON format
Last active August 15, 2019 18:37
I couldn't find a periodic table of the elements in JSON format, so I made one. For unknown atomic weights, a value of NaN has been assigned.
elements = {
HYDROGEN: {
atomic_number: 1,
abbreviation: "H",
atomic_weight: 1.008
},
HELIUM: {
atomic_number: 2,
abbreviation: "He",
atomic_weight: 4.003
@veganben
veganben / contrastColour
Created December 3, 2014 12:33
Get a contrasting colour. Put a black label on a light background, a white label on a light one. For example.
/**
* @param hex {String} hex colour with or without hash
* @returns {String} black if the original colour was light, white if it was dark. With hash if it had it in the first place.
*/
getContrastMonochrome = function (hex) {
var colour = hex,
sum,
hasHas = false;
hex = hex.charAt(0) === "#" ? hasHash = hex.substring(1) : hex;
@veganben
veganben / contrastColour
Last active August 29, 2015 14:10
Get a contrasting colour. Put a black label on a light background, a white label on a light one. For example.
/**
* @param hex {String} hex colour with or without hash
* @returns {String} black if the original colour was light, white if it was dark. With hash if it had it in the first place.
*/
getContrastMonochrome = function (hex) {
var colour = hex,
sum,
hasHash = false;
hex = hex.charAt(0) === "#" ? hasHash = hex.substring(1) : hex;
@veganben
veganben / Swiss Number Format
Created February 27, 2015 08:45
Swiss Number Format
var swissNumberFormattter = function(s) {
var numDec,
decimals,
returnVal;
if(s < 10000) {
returnVal = s;
} else {
if(Math.floor(s) === s) {
numDec = 0;
} else {
String.prototype.latinify = function() {
var map = {
ä: "ae",
ö: "oe",
ü: "ue",
Ä: "Ae",
Ö: "Oe",
Ü: "Ue"
};
var replacedString = "";
@veganben
veganben / primefactors.html
Created June 11, 2019 15:54
In a throw-away comment on The Infinite Monkey Cage, Prof Brian Cox said that the year 2021 has prime factors 43 and 47. This got me thinking …
<!doctype html>
<html>
<head>
<title>Prof Brian Cox's throw away line that 2021 has prime factors 43 and 47 got me thinking... </title>
<style>
html {
background-color: #deeded;
}
h1, h2, p, input, label {
color: #234567;