Skip to content

Instantly share code, notes, and snippets.

/**
* This JXA script combines 2 or more images into one using ImageMagick.
*
* Prerequisite:
* 1. Mac Yosemite
* 2. ImageMagick
*
* Steps:
* 1. Launch Automator app
* 2. Create an Automator service.
@krizpoon
krizpoon / rotating.css
Created May 27, 2015 03:32
Infinite rotation by CSS. Useful for loading spinners.
@keyframes rotating
{
from
{
transform: rotate(0deg);
-o-transform: rotate(0deg);
-ms-transform: rotate(0deg);
-moz-transform: rotate(0deg);
-webkit-transform: rotate(0deg);
}
@krizpoon
krizpoon / js-page-navigation.js
Last active August 29, 2015 14:21
iOS-like page navigation with jQuery and CSS
(function($)
{
function HistoryList(home)
{
var arr = [home];
var ptr = 1;
this.push = function(elem) { arr.splice(ptr, arr.length-ptr, elem); ptr = arr.length; return this; }
this.moveTo = function(pos) { ptr = Math.min(Math.max(1, pos), arr.length); return this; }
this.move = function(steps) { return this.moveTo(ptr + steps); }
this.get = function() { return arr[ptr-1]; }
@krizpoon
krizpoon / js-history-list.js
Created May 20, 2015 04:28
A history list object that behaves like browser's history
function HistoryList(home)
{
var arr = [home];
var ptr = 1;
this.push = function(elem) { arr.splice(ptr, arr.length-ptr, elem); ptr = arr.length; return this; }
this.moveTo = function(pos) { ptr = Math.min(Math.max(1, pos), arr.length); return this; }
this.move = function(steps) { return this.moveTo(ptr + steps); }
this.get = function() { return arr[ptr-1]; }
}
@krizpoon
krizpoon / js-fast-button.js
Last active August 29, 2015 14:21
Fast Button using jQuery
/*
* Usage: $(elem).fastButton(opts);
*
* opts.propagates: true|false that touch events should propagate
* opts.moveThreshold: maximum touch move distance after which the action should be considered cancelled, false if there is no threshold
* opts.pressedClass: class name to add when touch started
* opts.insideClass: class name to add when touch is inside
* opts.outsideClass: class name to add when touch is outside
*
* To turn off fast button: $(elem).trigger('fastButtonOff');
@krizpoon
krizpoon / TSFitImage.js
Last active August 29, 2015 14:19
Image Utils
function TSFitImage(img)
{
if (!img) return;
// get the basic metrics
var iw = img.naturalWidth, ih = img.naturalHeight, ww = img.offsetParent.offsetWidth, wh = img.offsetParent.offsetHeight;
if (!iw || !ih || !ww || !wh) return;
// calculate the scale by fitting image to align container's width and height
var sw = ww/iw, sh = wh/ih;
@krizpoon
krizpoon / TSLocalStorageOptions.js
Last active August 29, 2015 14:18
User options with local storage
function PageOptions (pageKey)
{
var values = {};
if (window.localStorage)
{
var json = window.localStorage.getItem(pageKey);
try
{
values = JSON.parse(json);
}
@krizpoon
krizpoon / LocalStorageBinding.js
Last active August 29, 2015 14:15
Local storage and binding to form fields (extending jQuery)
function PageOptions (pageKey)
{
var values = {};
if (window.localStorage)
{
var json = window.localStorage.getItem(pageKey);
try
{
values = JSON.parse(json);
}
@krizpoon
krizpoon / TSDateUtils.js
Last active August 29, 2015 14:08
Javascript Date Utils
Date.prototype.format = function(pattern, utc)
{
if (!pattern) return pattern;
var MonthsShort = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var MonthsLong = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var yyyy = this[utc?'getUTCFullYear':'getFullYear']();
var MM = this[utc?'getUTCMonth' :'getMonth']() + 1;
var dd = this[utc?'getUTCDate' :'getDate']();
@krizpoon
krizpoon / TSImageUtils.js
Last active August 29, 2015 14:07
Javascript Image Utils
function TSCenterImage(img)
{
if (!img || !img.offsetParent) return;
// get the basic metrics
var iw = img.naturalWidth, ih = img.naturalHeight, ww = img.offsetParent.offsetWidth, wh = img.offsetParent.offsetHeight;
if (!iw || !ih || !ww || !wh) return;
// calculate the scale by fitting image to align container's width and height
var sw = ww/iw, sh = wh/ih;