Skip to content

Instantly share code, notes, and snippets.

View ElliotChong's full-sized avatar
✌️

Elliot Chong ElliotChong

✌️
View GitHub Profile
@ElliotChong
ElliotChong / underscore.mixin.deepExtend.coffee
Last active November 28, 2020 23:55
Copy all of the properties in the source objects over to the destination object, and return the destination object. This method will recursively copy mutual properties which are also objects.
# Create a deep copy of an object. - CoffeeScript conversion of @cederberg's deepClone implementation https://github.com/documentcloud/underscore/pull/595
deepClone = (obj) ->
if !_.isObject(obj) or _.isFunction(obj) then return obj
if _.isDate obj then return new Date do obj.getTime
if _.isRegExp obj then return new RegExp obj.source, obj.toString().replace(/.*\//, "")
isArr = _.isArray obj or _.isArguments obj
func = (memo, value, key) ->
if isArr then memo.push deepClone value
@ElliotChong
ElliotChong / Gruntfile.coffee
Created November 14, 2013 17:20
Example Gruntfile showing concurrent CoffeeScript > JS > minification, SASS > CSS > minification, livereload, and nodemon / nodeinspector tandem.
module.exports = (grunt) ->
config =
browserify:
development:
options:
transform: ["coffeeify"]
debug: true
files:
"./public/scripts/editor.js": ["./frontend/scripts/editor.coffee"]
uglify:
@ElliotChong
ElliotChong / proxy.js
Created September 4, 2012 17:57
JavaScript implementation of a Proxy class.
/**
* JavaScript implementation of a Proxy class.
**/
​function Proxy(p_target)
{
var self = this;
self.target = p_target;
// Access target's properties
self.get = function (p_property)
@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']
@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 / 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 / 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 / outbound-link-tracking.html
Last active December 15, 2015 18:58
Adding Outbound Link Tracking With Google Analytics
@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 / 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();