Skip to content

Instantly share code, notes, and snippets.

View garystorey's full-sized avatar
:octocat:

Gary Storey garystorey

:octocat:
View GitHub Profile
var testInputType = (function(GOOD, BAD) {
'button checkbox color date datetime datetime-local email month number password radio range reset search submit tel text time url week'.replace(/\S+/g, function(typeName) {
var input = document.createElement('input');
input.setAttribute('type', typeName);
(input.type.toLowerCase() == typeName ? GOOD : BAD).push(typeName);
});
return function(value) {
return 'boolean' == typeof value ? (value ? GOOD : BAD).slice() : GOOD.includes(value.toLowerCase());
};
})([], []);
function fitInto(desiredWidth, desiredHeight, actualWidth, actualHeight) {
var actualSlope = actualHeight / actualWidth;
if (isFinite(actualSlope || '@')) {
if (desiredHeight / desiredWidth > actualSlope) {
desiredHeight = desiredWidth * actualSlope;
}
else {
desiredWidth = desiredHeight / actualSlope;
}
}
function watermarkImage(elemImage, text) {
// Create test image to get proper dimensions of the image.
var testImage = new Image();
testImage.onload = function() {
var h = testImage.height, w = testImage.width, img = new Image();
// Once the image with the SVG of the watermark is loaded...
img.onload = function() {
// Make canvas with image and watermark
var canvas = Object.assign(document.createElement('canvas'), {width: w, height: h});
var ctx = canvas.getContext('2d');
function sortFileNames(arr) {
for (var a, t, b, v, result = [], l = arr.length, i = l; i--; ) {
v = arr[i];
t = typeof v;
result[i] = { b: b = t == 'number' || t == 'string', a: a = [], v: v };
if (b) {
(v + '').replace(/(\d+)|\D+/g, function(m, isDigits) {
a.push(isDigits ? parseInt(m, 10) : m);
});
}
function selectText(e,r,s,d) {
d = document;
if (s = window.getSelection) {
(r = d.createRange()).selectNode(e);
(s = s()).removeAllRanges();
s.addRange(r);
}
else if (d.selection) {
(r = d.body.createTextRange()).moveToElementText(e);
r.select();
img {
filter: greyscale(100%);
mix-blend-mode: multiply;
}
@garystorey
garystorey / deepclone.js
Created December 3, 2015 15:17
Simple Deep cloning of an Object
var clonedWidget = (JSON.parse(JSON.stringify(originalWidget)));
@garystorey
garystorey / dimensions.js
Created July 20, 2015 15:09
browser Dimensions
function report() {
document.getElementsByTagName('output')[0].innerHTML = 'screen.width:'+screen.width+'<br>screen.height:'+screen.height+'<br>window.innerWidth:'+window.innerWidth+'<br>window.innerHeight:'+window.innerHeight+'<br>window.outerWidth:'+window.outerWidth+'<br>window.outerHeight:'+window.outerHeight+'<br>document.documentElement.<br> clientWidth:'+document.documentElement.clientWidth+'<br>document.documentElement.<br> clientHeight:'+document.documentElement.clientHeight+'<br>window.devicePixelRatio:'+window.devicePixelRatio; }
window.addEventListener('load', report, false);
window.addEventListener('resize', report, false);
window.addEventListener('orientationchange', report, false);
window.addEventListener('deviceorientation', report, false);
window.addEventListener('MozOrientation', report, false);
@garystorey
garystorey / osandbrowser.js
Created July 20, 2015 15:05
Detect OS and Browser
var system = navigator.appVersion;
if (navigator.appVersion.indexOf("Mac") != -1 ) {
OS = "Mac";
} else if (navigator.appVersion.indexOf("PowerPC") != -1 ) {
OS = "Mac";
} else if (navigator.appVersion.indexOf("Win") != -1 ) {
OS = "Win";
} else if (navigator.appVersion.indexOf("SunOS") != -1 ) {
OS = "Solaris";
} else {
@garystorey
garystorey / browserclose.js
Created July 20, 2015 15:02
Check browser closes
function checkBrowser() { // triggers on clicking the close button, Alt+F4 , File->Close
if(window.event.clientX < 0 && window.event.clientY < 0) {
window.open("somefile.html", "closewindow",'left=12000,top=1200,width=120,height=50');
}
}