Skip to content

Instantly share code, notes, and snippets.

View benshimmin's full-sized avatar
🇪🇺
Bare ruin'd choirs, where late the sweet birds sang

Ben Shimmin benshimmin

🇪🇺
Bare ruin'd choirs, where late the sweet birds sang
View GitHub Profile
@benshimmin
benshimmin / gist:4088493
Created November 16, 2012 16:03
Scale to fit and centre-align an image with FPDF
<?php
/* Caveat: I'm not a PHP programmer, so this may or may
* not be the most idiomatic code...
*
* FPDF is a free PHP library for creating PDFs:
* http://www.fpdf.org/
*/
require("fpdf.php");
class PDF extends FPDF {
@benshimmin
benshimmin / instructions.md
Created December 14, 2012 13:00
Creating a new Sublime build system to trigger Fabric to run a Grunt task on a Vagrant host to compress Sass files... (How many layers of indirection is that?!)
  • Add the below build file with Tools -> Build System -> New Build System...
  • Make sure you have Tools -> Build System -> Automatic ticked
  • Install this plugin: https://github.com/alexnj/SublimeOnSaveBuild
  • That should be it!
@benshimmin
benshimmin / gist:4335123
Created December 19, 2012 07:51
How to deal with debugging in-browser JavaScript if a third-party has over-written window.onerror to fail silently
// In your browser's developer console, put this:
window.onerror = function(error) { return false; };
@benshimmin
benshimmin / gist:4406368
Last active December 10, 2015 08:18
Fun with Backbone.Collection and _.map
# Backbone.Collection's `where` is very useful for quickly selecting
# parts of a Collection, and it returns an array. But an array of
# Backbone.Models isn't terribly helpful if you want to insert that
# into a template which expects an array of raw objects. This might help
# you a little, though:
collectionToRaw = (collection, key) ->
_.map (collection.where "type" : key), (filtered) ->
filtered.toJSON()
@benshimmin
benshimmin / gist:4443000
Created January 3, 2013 12:03
Stop Chrome popping up those "Confirm form resubmission" prompts when resubmitting POST data
$ /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome -disable-prompt-on-repost &
@benshimmin
benshimmin / raphael-element-bounds.coffee
Last active December 10, 2015 19:29
How to get the bounds of *all* elements on your Raphaël paper
# I'm pretty sure there isn't a built-in way of doing this;
# the below is one (simple) way...
# This assumes `paper` is your Raphael paper
getElementsBounds = ->
bottom = paper.bottom
elements = []
while bottom
elements.push bottom
@benshimmin
benshimmin / todigits.coffee
Created January 14, 2013 05:55
Neatest way of getting the digits out of a string
# It's very common to have a class with a name like "button-23" or
# "container2346112" and to need to extricate just the digits. Of course
# this is simple, but I've seen some developers do this in weird and
# wonderful ways. Here's a simple way:
String::toDigits = -> parseInt @.replace(/\D+/g, ""), 10
# Or in JavaScript:
`String.prototype.toDigits = function() { return parseInt(this.replace(/\D+/, ""), 10); }`
@benshimmin
benshimmin / gist:4580195
Created January 20, 2013 17:46
Convert an SVG polygon into a path (useful for rendering actual SVGs with Raphaël)
# Largely inspired by: http://stackoverflow.com/questions/9690241/rendering-svg-polygons-in-raphael-javascript-library
# You have, for example, this:
# <polygon fill="#00B8C0" stroke="#00FFFF" stroke-miterlimit="10" points="146.5,196.326 137.326,205.5 155.674,205.5 "/>
# You would like this:
# M147,196L137,206L156,206Z
# Assuming `xml` is your <polygon> node...
polygonToPath = (xml) ->
p = xml.getAttribute "points"
@benshimmin
benshimmin / postcode-regex.js
Created January 29, 2013 10:03
Simple (albeit slightly overly permissive) UK postcode regular expression
var regex = /^[A-Z]{1,2}[0-9]{1,2}[A-Z]? ?[0-9][A-Z]{2}$/i;
/**
* NB. This is case-insensitive and doesn't care about the whitespace.
*
* Matches these formats:
*
* AA9A 9AA
* A9A 9AA
* A9 9AA
@benshimmin
benshimmin / runner.js
Created January 30, 2013 10:49
Super simple example of using Jasmine with PhantomJS
[
// Jasmine itself
"lib/jasmine.js",
// a console reporter; this one works fine:
// http://code.google.com/p/phantomjs/source/browse/test/lib/jasmine-console.js
"lib/jasmine-console.js",
// your actual code
"src.js",