Demonstrates how to handle the window's focus and blur events.
This code does not depend on any third-party libraries like jQuery.
| // See https://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit, jQuery version with arrays and objects support | |
| function post(path, parameters) { | |
| var form = $('<form></form>'); | |
| form.attr("method", "post"); | |
| form.attr("action", path); | |
| $.each(parameters, function(key, value) { | |
| if ( typeof value == 'object' || typeof value == 'array' ){ | |
| $.each(value, function(subkey, subvalue) { |
| /** | |
| * Original code from https://github.com/mike-schultz/materialette | |
| */ | |
| function rgbToHex(r, g, b) { | |
| return "#" + ((1 << 24) + (parseInt(r) << 16) + (parseInt(g) << 8) + parseInt(b)).toString(16).slice(1); | |
| } | |
| function hexToRgb(hex) { | |
| const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); |
| // from http://en.wikipedia.org/wiki/Luminance_(relative) | |
| // R, G, B are integers between 0 and 255 | |
| function relativeLuminance(R, G, B) { | |
| var Y = (0.2126 * R + 0.7152 * G + 0.0722 * B) / 255; | |
| return Y; | |
| } |
| // from http://www.w3.org/TR/WCAG20/#relativeluminancedef | |
| function relativeLuminanceW3C(R8bit, G8bit, B8bit) { | |
| var RsRGB = R8bit/255; | |
| var GsRGB = G8bit/255; | |
| var BsRGB = B8bit/255; | |
| var R = (RsRGB <= 0.03928) ? RsRGB/12.92 : Math.pow((RsRGB+0.055)/1.055, 2.4); | |
| var G = (GsRGB <= 0.03928) ? GsRGB/12.92 : Math.pow((GsRGB+0.055)/1.055, 2.4); | |
| var B = (BsRGB <= 0.03928) ? BsRGB/12.92 : Math.pow((BsRGB+0.055)/1.055, 2.4); |
Demonstrates how to handle the window's focus and blur events.
This code does not depend on any third-party libraries like jQuery.
| var CubicBezier = function(canvas, opts){ | |
| if(!opts) opts = {}; | |
| this.start = opts.start || new Vec2(100,100); | |
| this.end = opts.end || new Vec2(400, 400); | |
| this.c1 = opts.c1 || new Vec2(100, 300); | |
| this.c2 = opts.c2 || new Vec2(300, 100); | |
| this.curve = new fabric.Path( this.toSVGPath() ); |
| <?php | |
| /** | |
| * Get the average pixel colour from the given file using Image Magick | |
| * | |
| * @param string $filename | |
| * @param bool $as_hex Set to true, the function will return the 6 character HEX value of the colour. | |
| * If false, an array will be returned with r, g, b components. | |
| */ | |
| function get_average_colour($filename, $as_hex_string = true) { |
Debounce a function when you want it to execute only once after a defined interval of time. If the event occurs multiple times within the interval, the interval is reset each time.
Example A user is typing into an input field and you want to execute a function, such as a call to the server, only when the user stops typing for a certain interval, such as 500ms.
| <?php | |
| $data = (object)array( | |
| "html" => "<foo bar=\"baz\"/> &", | |
| "arabic" => "العربية al-ʿarabiyyah, IPA: [æl ʕɑrɑˈbijjɐ], or عربي ʿarabī", | |
| "hebrew" => "עִבְרִית, Ivrit", | |
| "chinese" => "汉语/漢語 Hanyu; 华语/華語 Huáyǔ; 中文 Zhōngwén", | |
| "korean" => "한국어/조선말", | |
| "japanese" => "日本語 Nihongo", | |
| "umlauts" => "äüöãáàß", |
| <?php | |
| function json_response($message = null, $code = 200) | |
| { | |
| // clear the old headers | |
| header_remove(); | |
| // set the actual code | |
| http_response_code($code); | |
| // set the header to make sure cache is forced | |
| header("Cache-Control: no-transform,public,max-age=300,s-maxage=900"); |