Wrapping / Looping / Infinite Array Traversal

I’ll use JavaScript syntax and provide a JS fiddle POC, but this really applies to almost any language – at least any that use indexed arrays (all that I can think of). To get an index greater than length to “wrap” back from the beginning, use a simple modulus: if ( index >= length ) […]

jQuery (or anything else) basics of dragging

Here’s the most basic implementation of dragging I’m aware of… I’ll present an example usin jQuery, but the same logic can be used in any JS framework, raw JS, or even ActionScript, Java, or any other language or technology that processes similar user events and provided a UI. Here’s the idea: On mousedown: 1. record […]

JavaScript – remove multiple elements from an array

While you can always .filter() an array to remove unwanted elements, here’s a little sugar to remove all elements that evaluate truthily when compared to any argument passed: Array.prototype.remove = function(){ var args = Array.apply(null, arguments); var indices = []; for(var i = 0; i < args.length; i++){ var arg = args[i]; var index = […]

JavaScript – splice array on multiple indices (multisplice)

Managing multiple splices in a loop can get confusing, as the position of each element changes after each splice operation. Here’s a little extension to help manage that: ​Array.prototype.multisplice = function(){ var args = Array.apply(null, arguments); args.sort(function(a, b){ return a – b; }); for(var i = 0; i < args.length; i++){ var index = args[i] […]

JavaScript – separate (split) array

I often have a list from the server that I need to split into 2 separate arrays. For example, all options that are enabled and all that are not. Here’s a little extension to handle that. Takes a single callback as an argument, with map-style params (value, index, array) – if that iterated callback returns […]

ActionScript – using the Factory Pattern for Display Objects (ViewFactory)

Instantiating a Display Object is a fairly heavy lift. Usually this is negligible, but when dealing with particle or fluid effects, it can add up. Here’s one implementation of the Factory pattern used with Display Objects, to minimize the total number created – each Display Object would then have it’s visible properties and behaviors updated. […]

Cache “nudger”

With web technologies generally, and flash specifically, managing cached assets can sometimes feel like walking a tightrope. If you’ve got a simple slideshow and have occasional updates to the image you want immediately reflected to the client, you’ll often see traditional “cache-busting” methods like appending a random number of the current UTC time to the […]

Understanding Easing (Explaining Penner’s equations) – JavaScript and ActionScript

Despite the commonality of the classic easing equations, largely attributed to Penner, there doesn’t seem to be the in-depth examination of “how it works” that a lot of code is subject to nowadays. First, a quick clarification on the most popular easing Classes. The “standard” easing classes (that provide predictable inertia), are Linear, Quad, Cubic, […]

PHP vs JS conditionals and ternary

Just a quick note about how PHP conditional operators differs from ECMA (JS and AS)… The double-pipe ‘OR’ operator, in JS, evaluated to the value of the expression that is not false. In PHP, it evaluates to true if any any expression is true, or false if not (the output will actually be “1” if […]

JS/AS – Cache your RegExp!

In both JavaScript and ActionScript, creating a regular expression – even using literal notation – results in the instantiation of a new RegExp object. If you’re going to use a pattern repeatedly (which is usually the case) – cache your patterns! Just like you would any other object – “literal” doesn’t mean “free”. Simple test […]