Skip to content

Instantly share code, notes, and snippets.

View ElliotChong's full-sized avatar
✌️

Elliot Chong ElliotChong

✌️
View GitHub Profile
@ElliotChong
ElliotChong / benchmark.coffee
Created August 19, 2014 20:00
Super quick and very dirty memory benchmark of Immutable JS.
heapdump = require "heapdump"
Immutable = require "immutable"
pixels = 256 * 256
iterations = 1000
# Immutable
testImmutable = ->
# Simulate a pixel grid
grid = Immutable.Vector()
@ElliotChong
ElliotChong / Highlander.js
Last active August 29, 2015 14:14
When you're the only script that should work on a page.
var HighlanderJS = (function (immortals) {
var highlander = {};
for (var immortal in immortals) {
highlander[immortal] = immortals[immortal];
delete immortals[immortal];
}
immortals.HighlanderJS = function (onlyOne) {
onlyOne.apply(highlander);
};

================

If your language works like this by default..

var x = {a: 5};
var y = {a: 5};
x === y; // => false
@ElliotChong
ElliotChong / addInheritance.js
Created September 4, 2012 18:13
Add inheritance to JavaScript
(function ()
{
if (!Function.prototype.inherit)
{
Function.prototype.inherit = function (p_parent)
{
if (p_parent.constructor == Function)
{
// Normal Inheritance
this.prototype = new p_parent();
@ElliotChong
ElliotChong / facebookInclude.coffee
Created October 2, 2012 23:11
Include Facebook within a webpage via JS - No DOM elements necessary
window.fbAsyncInit = ->
FB.init
appId: '0123456789' # App ID
channelUrl: "//#{ window.location.hostname }/channel.html" # Channel File
status: true # Check login status
cookie: true # Enable cookies to allow the server to access the session
xfbml: true # parse XFBML
frictionlessRequests: true # Enable frictionless Request dialogs
# Application-specific initialization code goes here
@ElliotChong
ElliotChong / outbound-link-tracking.html
Last active December 15, 2015 18:58
Adding Outbound Link Tracking With Google Analytics
@ElliotChong
ElliotChong / pixi-graphics-additions.coffee
Last active December 20, 2015 02:59
Adds drawArc, drawRoundRect, and drawSlice methods to Pixi.js's Graphics object.
toRad = (p_angle) ->
return (p_angle - 90) * Math.PI / 180
for method in ["drawArc", "drawRoundRect", "drawSlice"]
if PIXI.Graphics.prototype[method]? then console.warn "PixiJS already has an #{method} method defined. It's recommended that you update your code to use the official implementation."
PIXI.Graphics.prototype.drawArc = (p_x, p_y, p_radius, p_startAngle, p_endAngle) ->
totalAngle = p_endAngle - p_startAngle
segments = Math.ceil Math.abs(Math.sqrt(1 - Math.pow (1 - Math.min(p_radius / 60, 1)), 2) * totalAngle * p_radius * 0.01)
anglePerSegment = totalAngle / segments
@ElliotChong
ElliotChong / KineticJS-PixelRatio_Override.coffee
Last active December 20, 2015 09:29
KineticJS automatically detects the current device's pixel ratio and renders graphics accordingly so that they will be as crisp as possible on high DPI displays. Sometimes you'd like to opt for a less precise (fuzzier) rendering but higher average framerate, this gist will give you that control.
# Adjust device pixel ratio
setMaximumPixelRatio = (p_maximumRatio=1) ->
canvas = document.createElement('canvas')
context = canvas.getContext('2d')
devicePixelRatio = window.devicePixelRatio || 1
backingStoreRatio = context.webkitBackingStorePixelRatio || context.mozBackingStorePixelRatio || context.msBackingStorePixelRatio || context.oBackingStorePixelRatio || context.backingStorePixelRatio || 1
pixelRatio = devicePixelRatio / backingStoreRatio
for className in ["HitCanvas", "SceneCanvas", "Canvas"]
Kinetic[className].prototype.init = ((p_method) -> (p_config={}) ->
@ElliotChong
ElliotChong / OUTPUT.md
Last active December 29, 2015 14:49
HTML sanitization script that ignores brackets within <script> tags. Tested in V8- Will need the forEach methods swapped out to work in legacy JS runtimes.
✓	 Bar

✓	 A &lt;p&gt;Baz!&lt;/p&gt; B

✓	 &lt;p&gt;Foo&lt;/p&gt; &lt;script&gt; a < b; &lt;/script&gt; &lt;span&gt;Blam!&lt;/span&gt;

✓	 &lt;p&gt;Foo&lt;/p&gt; &lt;script type='text/javascript'&gt; a < b || c > d; &lt;/script&gt; &lt;span&gt;Blam!&lt;/span&gt;

✓ D &lt;p&gt;Foo&lt;/p&gt; &lt;script type='text/javascript'&gt;$('body').append('&lt;' + 'script&gt; a &lt; b; &lt;' + '/script&gt;');&lt;/script&gt; &lt;span&gt;Blam!&lt;/span&gt; F &lt;b&gt;O&lt;/b&gt;
@ElliotChong
ElliotChong / facebookPluginDirectives.coffee
Created October 9, 2012 05:45
Angular directives that will parse Facebook plugins added to the DOM after Facebook has been initialized
module = angular.module 'FacebookPluginDirectives', []
createDirective = (name) ->
module.directive name, ->
restrict: 'C'
link: (scope, element, attributes) ->
FB?.XFBML.parse(element.parent()[0])
createDirective pluginName for pluginName in ['fbActivity', 'fbComments', 'fbFacepile', 'fbLike', 'fbLikeBox', 'fbLiveStream', 'fbLoginButton', 'fbName', 'fbProfilePic', 'fbRecommendations']