Skip to content

Instantly share code, notes, and snippets.

@tvandervossen
tvandervossen / environment.js
Last active April 2, 2024 20:18
Here’s an example of my current web app user agent, device, and/or feature detection approach. I tend to inline this in the page header just before the stylesheet as part of the distribution build. A benefit of this approach is that detection is done early without any external dependencies. It’s also straightforward to modify or extend while you…
env = (function() {
var flags = {}, ua = navigator.userAgent, el = document.createElement('div'), video = document.createElement('video'), audio = document.createElement('audio'), root = document.documentElement, i
function flag(names) {
names = names.split(' ')
for (i = 0; i < names.length; i++)
flags[names[i]] = true
}
function classnames() {
var names = [], name
for(name in flags) if (flags.hasOwnProperty(name))
@tvandervossen
tvandervossen / gist:1231476
Created September 21, 2011 07:33
Mobile Safari viewport sizes on iOS 4.3 and 5
iPad
1024 × 690 In landscape on iOS 4.3
1024 × 672 In landscape on iOS 5
768 × 946 In portrait on iOS 4.3
768 × 928 In portrait on iOS 5
1024 × 660 Always showing bookmarks bar in landscape on iOS 4.3
1024 × 644 Always showing bookmarks bar in landscape on iOS 5
768 × 916 Always showing bookmarks bar in portrait on iOS 4.3
class MyModel < ApplicationRecord
TOKEN_CHARS = 'ybndrfg8ejkmcpqxt1wisza345h769'
before_create :generate_token
# Generates random token leaving out numbers and letters that might be ambiguous and that might
# result in words some people find offensive.
def self.typeable_token(length = 8)
length.times.map do
TOKEN_CHARS[SecureRandom.random_number(TOKEN_CHARS.length)]
TYPES = {
'c' => 'coin',
'w' => 'wall'
}
def map_to_list(map)
list = []
map.strip!.gsub!(/\n\s*/, "\n")
def vector_to_coordinates(distance, angle_in_degrees)
angle_in_radians = angle_in_degrees * Math::PI / 180
x = (distance * Math.cos(angle_in_radians)).round
y = (distance * Math.sin(angle_in_radians)).round
[x, y]
end
require "test/unit"
class TestVectorConversion < Test::Unit::TestCase
loadAPI(1);
host.defineController("Roger Linn Design", "LinnStrument", "1.0", "B7DD06CB-63BA-4902-879E-050B09D3058F");
host.defineMidiPorts(1, 1);
host.addDeviceNameBasedDiscoveryPair(["LinnStrument MIDI"], ["LinnStrument MIDI"]);
function init()
{
host.getMidiInPort(0).setMidiCallback(onMidi);
@tvandervossen
tvandervossen / stimulus_notes.md
Last active May 5, 2018 09:21
Quick guide for people who want to add stimulus.js to a Rails 5.2 app but who haven’t used much Modern JavaScript

How to add stimulus.js to a Rails 5.2 app

This might also work or Rails 5.1, but I haven’t tried that yet.

Add the webpacker gem to your Gemfile:

# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker'

Then run (assuming you’re on macOS and using Homebrew):

// On iPhone and iPod Touch we add a nice dynamic 3D parallax effect by
// updating the perspective origin depending on device tilt.
if (/iPhone|iPod/.test(navigator.platform) && 'ondevicemotion' in window) {
;(function() {
var value,
element = $('div.doors ul').get(0),
values = {x: [0,0,0], y: [0,0,0]}, // Keep the 3 most recent values.
index = 0,
choices = { // Lookup table to normalize based on device orientation.
x: {'0': ['x', -1], '90': ['y', 1], '180': ['x', 1], '-90': ['y', -1]},
@tvandervossen
tvandervossen / stimulus_notes.md
Created April 11, 2018 17:47
Quick guide to add stimulus.js to a Rails 5.2 app if you haven’t used much Modern ™ JavaScript before

Add stimulus.js to a Rails 5.2 app

According to https://github.com/rails/webpacker you should:

Add the webpacker gem to your Gemfile:

# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker'

Then run (assuming you’re on macOS and using Homebrew):

@tvandervossen
tvandervossen / gist:50897e4c0ad40b162b033da1f17b7867
Created March 10, 2018 18:02
Trying to understand the motivation behind the BEM class naming convention
In https://css-tricks.com/bem-101/ the following CSS is given as an exampke:
/* Block component */
.btn {}
/* Element that depends upon the block */
.btn__price {}
/* Modifier that changes the style of the block */
.btn--orange {}