Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save milafrerichs/4024718 to your computer and use it in GitHub Desktop.
Save milafrerichs/4024718 to your computer and use it in GitHub Desktop.
Blog Examples
var EventGarbageCollection = function ( context ) {
var instance = this;
this.collection = [];
this.context = context;
this.context.addEventListener('close', function ( ) {
instance.empty();
});
};
EventGarbageCollection.prototype = {
collect : function ( evt, method ) {
this.collection.push([evt, method]);
},
addEvent : function ( evt, method ) {
Ti.API.addEventListener(evt, method);
this.collect(evt, method);
},
empty : function ( ) {
for ( var i = 0, len = this.collection.length; i < len; i++ ) {
Ti.API.removeEventListener(
this.collection[i][0],
this.collection[i][1]
);
}
this.collection = [];
}
};
var globals = {};
globals.EventGarbageCollection = EventGarbageCollection;
var self = Ti.UI.createWindow(args);
// Grab instance of class.
var egc = new globals.EventGarbageCollection(self);
// Add events through the wrapper.
egc.addEvent('someEvent', function (){});
egc.addEvent('anotherEvent', function (){});
egc.addEvent('tresEvent', function (args){});
// Call as usual...
Ti.API.fireEvent('someEvent');
// More code...
// Maybe open some sub windows or modals...
// ...
// ...
self.close(); // And all of the above will be removed. Worry free.
// ------------------------
// Other stuff you can do.
// ------------------------
// Add event manually.
var func = function (foo) { alert('whoa'); };
Ti.API.addEventListener('someEvent', func);
egc.collect('someEvent', func);
// Remove them all manually.
egc.empty();
var speed = 500,
from = { someProp:0, anotherProp:0 },
to = { someProp:40, anotherProp:100 };
$(from).animate(to, {
duration: speed,
step: function () {
// now we have access to this.someProp and
// this.anotherProp.
},
complete: function () {
// called on complete
}
});
<?php
/**
* Class for parsing svg files for json output.
*
* @author Pete Saia
*/
class PeachSVG
{
/**
* Takes a SVG file and converts it into an array or json object.
*
* PeachSVG::convert($filename);
*
* @param string to/the/file.svg
* @param boolean To output as json or an array.
* @return array | json
*/
public static function convert($filename, $to_json = true)
{
// Make sure the file was readable, if not die.
if ( ! $contents = file_get_contents($filename)) die('Could not open the file.');
// Get array of graphical paths from svg content.
$graphical_paths = self::get_graphical_paths($contents);
// Iterates with every path in the svg file.
$path_iterator = 0;
// The output array.
$output = array();
// Regex to match svg attributes.
$regex = '/\b(id|fill|d|name|stroke|stroke-width)\b=["|\']([^"\']*)["|\']/';
// Loops through paths.
foreach ($graphical_paths as $path)
{
// If matche not found & match doesn't contains a path attr, skip.
if ( ! preg_match_all($regex, $path, $matches) OR ! in_array('d', $matches[1]))
continue;
// Iterates for every attr found.
$attr_iterator = 0;
// Loops through attributes and adds to output array.
foreach ($matches[1] as $match)
{
$output[$path_iterator][$match] = $matches[2][$attr_iterator];
$attr_iterator++;
}
$path_iterator++;
}
// return either json or raw array.
return $to_json ? json_encode($output) : $output;
}
/**
* Extract graphical paths from SVG. These are the paths that are direct
* children of the <svg> tag.
*
* PeachSVG::get_graphical_paths($svg_content);
*
* @param string <svg>.....</svg>
* @return array
*/
protected static function get_graphical_paths ($svg)
{
// Remove all types of line breaks and tabs.
$contents = str_replace(array("\r", "\r\n", "\n", "\t"), '', $svg);
// Get all of the paths.
return explode('<path', $contents);
}
}
?>
var speed = 1500;
$("#myDiv").animate({
// various css properties here.
}, speed);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment