Skip to content

Instantly share code, notes, and snippets.

View mattlundstrom's full-sized avatar

Matt Lundstrom mattlundstrom

View GitHub Profile
@mattlundstrom
mattlundstrom / once_poll.js
Last active October 24, 2020 21:30
JS function that can only be fired once.
// ONCE
function once(fn, context) {
var result;
return function() {
if(fn) {
result = fn.apply(context || this, arguments);
fn = null;
}
@mattlundstrom
mattlundstrom / functionWithDefaultProperties.js
Last active October 24, 2020 21:30
JS Functions with default properties
var func = function( props ){
props = (typeof props !== "object") ? {} : props;
var mx = props.mx || 10;
var my = props.my || 10;
var mz = props.mz || 10;
var time = props.time || 1;
var delay = props.delay || 0;
var ease = props.ease || Linear.easeNone;
@mattlundstrom
mattlundstrom / rectangle circle collision.js
Last active October 24, 2020 21:29
JS is a rectangle colliding with a circle?
function RectCircleColliding(circle,rect){
var distX = Math.abs(circle.x - rect.x-rect.w/2);
var distY = Math.abs(circle.y - rect.y-rect.h/2);
if (distX > (rect.w/2 + circle.r)) { return false; }
if (distY > (rect.h/2 + circle.r)) { return false; }
if (distX <= (rect.w/2)) { return true; }
if (distY <= (rect.h/2)) { return true; }
@mattlundstrom
mattlundstrom / snaptToIncrement.js
Last active October 24, 2020 21:28
JS Snap a number to an increment
/*
val = value to snap
inc = increment to snap to
example:
snapToIncrement(10, 3): 9
snapToIncrement(89, 10): 100
snapToIncrement(35.6, .5): 35.5
*/
@mattlundstrom
mattlundstrom / gist:3824954
Created October 3, 2012 04:13
AS3 Cuepoints
// VIDEO;
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
ns.client = {};
ns.client.onMetaData = nsOnMetaData;
ns.client.onCuePoint = nsOnCuePoint;
ns.addEventListener(NetStatusEvent.NET_STATUS, nsOnNetStatus);
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onAsyncError);
@mattlundstrom
mattlundstrom / gist:3648869
Created September 6, 2012 00:38
AS3 Remove All Children
function removeAllChildren(mc:MovieClip):void
{
while (mc.numChildren)
{
mc.removeChildAt(0);
}
}
@mattlundstrom
mattlundstrom / gist:3107038
Created July 13, 2012 19:59
AS3 Elastic Movement
var vx:Number = 0;
var vy:Number = 0;
var targetX:Number;
var targetY:Number;
var spring:Number = .5;
var friction:Number = .9;
this.addEventListener(Event.ENTER_FRAME, loop);
function loop(e:Event):void
// FROM http://stackoverflow.com/questions/25098021/securityerror-blocked-a-frame-with-origin-from-accessing-a-cross-origin-frame
In your main page:
var frame = document.getElementById('your-frame-id');
frame.contentWindow.postMessage(/*any variable or object here*/, '*');
In your <iframe> (contained in the main page):
window.addEventListener('message', function(event) {
@mattlundstrom
mattlundstrom / gist:3092911
Created July 11, 2012 20:00
AS3 Scale Around Point
function scaleAroundPoint(target:DisplayObject, point:Point, scaleFactor:Number):void
{
var m:Matrix = target.transform.matrix;
m.translate( -point.x, -point.y );
m.scale( scaleFactor, scaleFactor);
m.translate( point.x, point.y );
target.transform.matrix = m;
}
@mattlundstrom
mattlundstrom / fill fit and center one display object to another
Created December 16, 2013 00:10
fill fit and center one display object to another
function centerTo(_object:DisplayObject, _ref:DisplayObject):void{
if (_object != null){
var _wString:String = "width";
var _hString:String = "height";
if (_ref == stage){
_wString = "stageWidth";
_hString = "stageHeight";
trace("stage");
}
var _refWidth:Number = _ref[_wString];