Skip to content

Instantly share code, notes, and snippets.

@DigiTec
DigiTec / FilterEventTargets.js
Last active August 29, 2015 14:07
This enumerates the window to find types that inherit from EventTarget.
var eventTargetObjects = Object.getOwnPropertyNames(window).filter(function prop(name) {
// This block of code identifies constructors with high confidence using casing, enumerability and prototype property presence
return (name[0] === name[0].toUpperCase() && !this.propertyIsEnumerable(name) && this[name].prototype !== undefined);
}, window).filter(function prop(name) {
// This block of code determines if a constructor is like an EventTarget
var currentPrototype = this[name].prototype;
while (currentPrototype !== null) {
if (currentPrototype === EventTarget.prototype) {
return true;
}
@DigiTec
DigiTec / BitOps.cpp
Created August 25, 2014 06:51
A bunch of bit ops where you need some "magic numbers" now logged in my gists for posterity.
bool PowerOf2(unsigned long value)
{
return (value != 0) && !(value & (value-1));
}
int BitCount(unsigned long value)
{
value = (value & 0x55555555) + ((value & 0xAAAAAAAA) >> 1);
value = (value & 0x33333333) + ((value & 0xCCCCCCCC) >> 2);
value = (value & 0x0F0F0F0F) + ((value & 0xF0F0F0F0) >> 4);
@DigiTec
DigiTec / ChampagneTower.cpp
Last active August 29, 2015 14:05
This uses similar features to Pascals Triangle to solve the champagne tower problem. Since I found two variants of the problem, I've solved both. The first variant gives the row and the column so no decomposition is necessary for the inputs. The second variant assigns cup numbers starting from 1 and assigning from top to bottom left to right as …
double ChampagneTower(double water, double capacity, int row, int col);
double ChampagneTower(double water, double capacity, int cup)
{
// The cup id is from left to right. We want the row and column in the triangular
// array which is basically the triangular root and our remainder.
int triangular_root = ceil((sqrt(8*cup+1)-1)/2);
int column = cup - ((triangular_root*(triangular_root-1))/2);
return ChampagneTower(water, capacity, triangular_root, column);
}
@DigiTec
DigiTec / PascalsMath.cpp
Created August 25, 2014 02:46
Computation for pascal's triangle values by calculating along the row so I don't lose it in the future. This is susceptible to round-off errors because we multiply before a divide when the triangle gets very large.
// Assumes row/col are 1 indexed
int PascalsMath(int row, int col)
{
// Enable mirroring about the central pivot of the row
col = min(col, row - col + 1);
int value = 1;
int numerator = row - 1;
int denominator = 1;
@DigiTec
DigiTec / RangeBasedForOnEnums.cxx
Created December 24, 2013 03:40
So I was thinking about range based for loops and I saw some partial code for one that worked on an enum. This adds more generic behavior to that enum and allows self selection of the start/end of the enumeration and ensures that the range you are using is increasing from first to last. Since this is pretty much a working piece of code and there…
// RangeBasedFor.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
template <typename T, T first = T::First, T last = T::Last>
class Enum
{
static_assert(first <= last, "foo");
public:
@DigiTec
DigiTec / ClickThrough.js
Created September 22, 2013 21:30
In IE 11 every major browser now supports pointer-events, but until IE 11 gains wide adoption there will still be a large base of users who can't benefit from UI's which utilize this feature for "click through" UI's. In the meantime I figured I'd generate a list of click through mechanisms that vary based on properties used, nesting capabilities…
// Utilize visibility. This will cause layout to be invalid and have to be recomputed once the element is made visible again.
function _clickThroughVisNested(evt) {
var localStyle = evt.target.style;
var oldVisibility = localStyle.display;
localStyle.visibility = "hidden";
var target = document.elementFromPoint(evt.pageX, evt.pageY);
// Specific to my implementation. I want to only click through on certain types of elements.
if (target.classList.contains("achievement")) {
@DigiTec
DigiTec / ClassList.js
Last active December 23, 2015 03:39
This is a short polyfill to add classList support to SVG in Internet Explorer. It takes advantage of the duck typing of property and method implementations to be able to simply move the existing property descriptor from HTMLElement up to Element. Note that FIreFox already puts their implementation on Element. And Chrome uses instance properties …
"use strict";
// IE supports duck typed classList so move it from HTMLElement to Element to support SVG.
// FireFox already has classList on Element so make sure not to tweak anything there.
// Chrome supports classList on every element, but as an instance property so no need to move anything there.
(function _initClassListPolyFill() {
if (!Element.prototype.hasOwnProperty("classList") && HTMLElement.prototype.hasOwnProperty("classList")) {
Object.defineProperty(Element.prototype, "classList", Object.getOwnPropertyDescriptor(HTMLElement.prototype, "classList"));
}
@DigiTec
DigiTec / PrivacyPolicy.js
Created March 11, 2013 01:51
Add a privacy policy to your Windows 8 based WWA. Note, all of the samples utilized WinJS and I needed a non WinJS version. I don't claim in any way this is novel, but it took me nearly an hour to pull this together since there were no fully baked samples.
var settingsPane = Windows.UI.ApplicationSettings.SettingsPane.getForCurrentView();
settingsPane.addEventListener("commandsrequested", foo);
function foo(args) {
var cmdPrivacy = new Windows.UI.ApplicationSettings.SettingsCommand("privacy", "Privacy Policy", privacyPolicyInvoked);
args.request.applicationCommands.push(cmdPrivacy);
}
function privacyPolicyInvoked(args) {
switch (args.id) {
@DigiTec
DigiTec / DomMeasureText.js
Last active December 14, 2015 04:19
This Gist uses a cached, hidden div in order to measure height/width of text. Imperfect since I'm still technically inheriting styles through the body that could affect my offsetWidth and offsetHeight but most likely a better approximation than measuring the width of the "M" character which is often a suggestion when dealing with the lack of hei…
(function () {
var _cachedDiv;
function domMeasureText(text, font) {
if (!_cachedDiv) {
_cachedDiv = document.createElement("div");
_cachedDiv.style.visibility = "hidden";
_cachedDiv.style.position = "absolute";
document.body.appendChild(_cachedDiv);
}
@DigiTec
DigiTec / LightDarkColorPicker.html
Last active December 10, 2015 07:58
A simple gist for picking lighter and darker colors based on a current color. Outputs the values in hex, rgb, and rgba for direct use in your CSS. This is inspired by a task I had to pick a darker background color for a UI element so that it would stand out on top of a lighter background. The elements still needed to be themed to their color pro…
<!-- saved from url=(0014)about:internet -->
<!DOCTYPE HTML>
<script>
function Color(r, g, b) {
this.r = r;
this.g = g;
this.b = b;
}
Object.defineProperties(Color, {
colorFromHex: {