Showing posts with label functions. Show all posts
Showing posts with label functions. Show all posts

21 September 2012

Loop Javascript (or Actionscript!) array based on index

Looping arrays is particularly useful for things like photo galleries and other iterative processes where you want the "next photo" button on a gallery to continue from the beginning of the album after the user passes the last photo.

In my humble opinion, I believe this functionality should be built into the core of Ecmascript ( For example, something like myArray[-2] should really return the second last element in the array) but alas we have to write it ourselves, so here goes...
Continue Reading >

11 May 2012

Simple Javascript Pie Chart using HTML5 Canvas tag


I had a look around today for a handy pie chart that I could quickly drop into a new web app I'm working on and it seems there's a lot of bloated solutions out there. I like to keep things short and sweet so I found this one online and edited it to make it a bit more user friendly. Enjoy!
Continue Reading >

26 January 2012

Free Javascript for Rotating Website Banner or Fading Slideshow - standalone

As flash becomes less widespread I've had more and more instances where I need to create website banners in Javascript rather than my old flash approach. Today I've decided to share my method with my adoring public (that's you!).

This method is completely standalone and does not require jQuery or any other plugin or library. It has been tested and works in latest versions of all browsers.

View a demo here (right click to get the whole source at once) or view the step-by-step instructions below...

Continue Reading >

05 November 2011

Javascript function for checking a variables type or empty/unset variables - similar to PHP's empty()

I've been building on this one all week and I'm finding it more and more useful for quickly writing variable checks at the top of functions. You can use it to easily check if a var is defined or check if it is of a certain type. It's a work in progress but it's very handy. Check out the commented examples to get an idea of potential uses. Ideas for improvements/additions are welcome!

Continue Reading >

17 July 2011

AS3: Simple, Powerful String Find & Replace all instances function for Flash Actionscript. Similar to PHP's str_replace replacement.

Exactly what it says on the tin!

AS2 version here



Continue Reading >

AS3: Wave of values class function - produces incremental and decremental values

Sometimes it's necessary to have a value increment until it gets to a certain point and then have it decrement again in a wave shape. A simple Math.sin() calculation will work in some cases but its not easy to control the increment using that method. This handy & simple class makes it quick and easy to do just this. There's lots of room for improvements & additions so feel free to post updates :)

Continue Reading >

05 June 2011

AS3 handy weighted random & rounding functions with decimal/floating point precision

Like a lot of the "improvements" Adobe made to Actionscript with AS3, the Math.random() function is now a pain in the ass. It only generates a floating point number between 0 and 1 so 99% of the time, you need a few lines of code to get your random on.
With time saving in mind, I've written this weighted random function which makes beautiful randomness a lot handier to implement. For best results throw this in a class package and include it in every project.
Here it comes...

Continue Reading >

18 April 2011

AS3 function to rotate a 2D or 3D MovieClip or Sprite around its center or change to any registration point

OK here we go - I looked everywhere for this one but only found bits of code from various forums and blogs that were part of classes and packages and as such, full of dependent variables. Anyway, after a bit of advice from the very helpful community at StackOverflow and a bit of tinkering, she's up and running!

The function below will rotate a DisplayObject around any point on a 2D or 3D plane. This is particularly useful for rotating around an object's center point so this is the default behaviour. Enjoy!

Continue Reading >

14 March 2011

Max/MSP Javascript "find object" - lazy dollar function

The second function in my "Making Max JS less crap" series, this function scans all objects in the patch and all subpatches and returns a reference to the object with the specified scripting name: It's basically a lazy man's version of my dollar function which means you dont need to give every patcher a scripting name. The downside is it (probably) runs a bit slower. Enjoy!

Continue Reading >

Max/MSP Javascript: Dollar Function - Returns a reference to a Max Object in a patch/subpatch

I started using Max this month and was simultaneously impressed with its power and simplicity and frustrated by it's cumbersome interface. I quickly checked out it's javascript capabilities and became much more comfortable. JS allows you do simple operations quite quickly that would take a tonne of parsing objects which use up space and lead to a messy patch.

That said, JS in Max is really a slightly ugly cousin of browser based javascript. Calls to objects are long winded and basically everything is a bit head wrecking!

So here we go: the first in my "Making Max JS less crap" series... The infamous dollar function! All you need to do to access a max object is give the object a scripting name in the inspector* and then use: $('scriptingname')

Continue Reading >

13 February 2011

Actionscript 3: Better Random Function & Better Rounding Function

I was amazed at how lame the rounding and random functions are in AS3 so I've written 2 simple functions to add much needed functionality. Enjoy...
Continue Reading >

11 February 2011

Actionscript/Javascript Colour mode conversion functions - convert HSB/HSL/HSV to RGB to web HEX color or to flash matrix

I wrote/found/tweaked these conversion functions today to convert between the most common colour modes. it was very difficult to find a solid source for converting colours online so I thought I'd post them here for the world. They should work in actionscript 2 & 3 and could easily be tweaked to work in php, python or any other language. Here goes...

Continue Reading >

23 August 2010

Javascript: Simple, Drag & Drop element class - multiple draggable elements on one page.

I found a tonne of these drag & drop functions online but they were all either too big & bloated or relied heavily on external libraries. The one I did find that was small and usable triggers chrome bug 7423 because it captures the mousedown event for the page. So I've written my own and posted here for your convenience.
See Demo (Internet Explorer ain't supported)
Continue Reading >

29 June 2010

AS2: Simple, Powerful String Find & Replace all instances function for Flash Actionscript. Similar to PHP's str_replace replacement.

Exactly what it says on the tin!

AS3 version here



Continue Reading >

18 June 2010

Resize a Movie Clip containing a mask by getting dimensions of visible bounds/area and compensating width/height

This was a head wrecker and took me most of the day today. Actionscript does not see Movie Clips as we do and considers invisible content to be part of the MC. As such, when you try to resize an MC that contains masked content which extends beyond what we consider to be the edges of the MovieClip, flash makes a balls of it.

To counteract this I've written and rewritten a double function which resizes your clip based on the visible area rather than what flash considers the bounds. Here it comes...

Continue Reading >

06 May 2010

PHP: Ultimate get/fetch RELATIVE PATH function between 2 folders/directories/files/pages or root dir

I wrote these functions today when everything I found online that attempted the same job was either too bulky, or too restrictive. It took me flippin ages - far longer than it should have! But it has huge functionality and will be a great addition to the arsenal. The main function relpath basically calculates the relative path from anywhere to anywhere on the same domain. Possible uses are listed below:
  1. Get relative path from the current page to the root directory of the local domain (rootpath)...
    echo relpath(); //outputs ../../ or something similar
  2. Get relative path from the current page to any other file/folder on the domain...
    echo relpath("/scripts/javascripts/jsmin/min.js"); //outputs ../../jsmin/min.js
  3. Get relative path from any file/folder on the domain to any other file/folder on the domain...
    echo relpath("/scripts/connect.php","/scripts/javascripts/jsmin"); //outputs ../../scripts.connect.php
Full functions with details and comments below.
Continue Reading >

27 November 2009

PHP: How to Compare Directory Attributes & all files within

I wrote this function today to compare the contents of two folders or directories based on 3 comparison points:

  • Do files exist in both directories?
  • Were they both modified at the same time?
  • Are the contents identical?

This function returns a 3D array containing a list of all differences between the contents of the two directories...

Continue Reading >

24 November 2009

PHP: How to upload ZIP, check if file exists within it & extract to the server

I had to do this today for a job. It took far longer than it should have. The resources online are minimal and php.net does not mention any keywords like file_exists on manual for the function needed to do this. so...

Here is the best way to check if an uploaded zip archive contains a certain file and if so, extract the entire contents to a directory on your server:

Continue Reading >
Cron Design Studio: Dublin based web design & software development