Skip to content

Instantly share code, notes, and snippets.

View jonathantneal's full-sized avatar

Jonathan Neal jonathantneal

View GitHub Profile
HTMLCanvasElement.prototype.contrast = function (value) {
var
canvas = this,
context = canvas.getContext('2d'),
imageData = context.getImageData(0, 0, canvas.width, canvas.height),
canvasPixelArray = imageData.data,
canvasPixelArrayLength = canvasPixelArray.length,
i = 0;
value = (parseFloat(value) || 0) + 1;
HTMLCanvasElement.prototype.desaturate = function () {
var
canvas = this,
context = canvas.getContext('2d'),
imageData = context.getImageData(0, 0, canvas.width, canvas.height),
canvasPixelArray = imageData.data,
canvasPixelArrayLength = canvasPixelArray.length,
i = 0,
grayscale = 0;
HTMLCanvasElement.prototype.trim = function () {
var
canvas = this,
width = canvas.width,
height = canvas.height,
pixels = width * height,
context = canvas.getContext('2d'),
imageData = context.getImageData(0, 0, width, height),
canvasPixelArray = imageData.data,
top = height,
@jonathantneal
jonathantneal / HTMLCanvasElement.prototype.imageData.js
Created March 17, 2012 19:00
Canvas Prototype: getImageData / putImageData / editImageData
HTMLCanvasElement.prototype.getImageData = function (x, y, width, height) {
return this.getContext('2d').getImageData(parseFloat(x) || 0, parseFloat(y) || 0, width != null ? parseFloat(width) : this.width, height != null ? parseFloat(height) : this.height);
};
HTMLCanvasElement.prototype.putImageData = function (imageData, x, y, width, height) {
var canvas = this;
canvas.width = width != null ? parseFloat(width) : canvas.width;
canvas.height = height != null ? parseFloat(height) : canvas.height;
// Canvas Prototype (because prototyping has always proven to be such a great idea...)
if (HTMLCanvasElement) {
function RGBToHSL(r, g, b) {
var
min = Math.min(r, g, b),
max = Math.max(r, g, b),
diff = max - min,
h = 0, s = 0, l = (min + max) / 2;
@jonathantneal
jonathantneal / RGB2HSL.js
Created March 19, 2012 18:02
RGB to HSL to RGB
function RGBToHSL(r, g, b) {
var
min = Math.min(r, g, b),
max = Math.max(r, g, b),
diff = max - min,
h = 0, s = 0, l = (min + max) / 2;
if (diff != 0) {
s = l < 0.5 ? diff / (max + min) : diff / (2 - max - min);
function boundingWidthFromAngle ( degree , width , height ) {
with ( Math ) {
degree = degree % 360;
var radian = degree * PI / 180;
height = ( ( degree >= 0 && degree < 90 ) || ( degree >= 180 && degree < 270 ) ) ? height : -height;
return ceil( abs( height * sin( radian ) + width * cos( radian ) ) );
}
@jonathantneal
jonathantneal / matchMedia.js
Created March 30, 2012 03:19
Match Media
(function (window, screen, documentElement) {
/* ======================================================================
Setup
====================================================================== */
var hasEventListener = ('addEventListener' in window);
var hasOrientation = ('orientation' in window);
var hasGetComputedStyle = ('getComputedStyle' in window);
var regexpTrim = /^\s+|\s+$/g;
@jonathantneal
jonathantneal / curl.php
Created April 3, 2012 15:47
Curling caniuse
<?php
$referer = 'http://caniuse.com/index.php';
$url = 'http://caniuse.com/jsonp.php';
$user_agent = 'Mozilla/5.0 (compatible; Linux) Curl/1.0';
$file = 'data.json';
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $url);
<meta name="viewport" content="width=device-width">
<script>
document.querySelector && document.querySelector('meta[name="viewport"]').setAttribute(
'content', 'width=device-' + (window.orientation ? 'height' : 'width')
);
</script>