Skip to content

Instantly share code, notes, and snippets.

View tennisonchan's full-sized avatar

Tennison Chan tennisonchan

  • Truewind
  • San Francisco
View GitHub Profile
@tennisonchan
tennisonchan / gist:4010395
Created November 4, 2012 05:04 — forked from padolsey/gist:527683
js: detecting ie
// ----------------------------------------------------------
// A short snippet for detecting versions of IE in JavaScript
// without resorting to user-agent sniffing
// ----------------------------------------------------------
// If you're not in IE (or IE version is less than 5) then:
// ie === undefined
// If you're in IE (>=5) then you can determine which version:
// ie === 7; // IE7
// Thus, to detect IE:
// if (ie) {}
@tennisonchan
tennisonchan / gist:1eee36c92b2caab64d48
Created January 22, 2015 23:37
Using phantomjs to capture screen
// http://phantomjs.org/screen-capture.html
// phantomjs rasterize.js https://google.com google.png
var page = require('webpage').create(),
system = require('system'),
address, output, size;
if (system.args.length < 3 || system.args.length > 5) {
console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom]');
console.log(' paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"');
/*
* object.watch polyfill
*
* 2012-04-03
*
* By Eli Grey, http://eligrey.com
* Public Domain.
* NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
*/

2015-01-29 Unofficial Relay FAQ

Compilation of questions and answers about Relay from React.js Conf.

Disclaimer: I work on Relay at Facebook. Relay is a complex system on which we're iterating aggressively. I'll do my best here to provide accurate, useful answers, but the details are subject to change. I may also be wrong. Feedback and additional questions are welcome.

What is Relay?

Relay is a new framework from Facebook that provides data-fetching functionality for React applications. It was announced at React.js Conf (January 2015).

@tennisonchan
tennisonchan / rasterize.js
Created February 10, 2015 20:41
rasterize with userAgents and devicesDimension settings
// http://phantomjs.org/screen-capture.html
// phantomjs rasterize.js https://google.com google.png
var page = require('webpage').create(),
system = require('system'),
address, output, size;
var userAgents = {
'Chrome': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36'
}
@tennisonchan
tennisonchan / fibonacci-binary.js
Created February 10, 2015 20:54
breaking down number into combination of fibonacci number
fib = [1,2,3,5,8,13,21,34,55,89,144];
function dec2fib(num) {
list = [];
for(var i=fib.length;i<0;i--){
if(fib[i] < num){
num -= fib[i];
list.push(i);
}
}
@tennisonchan
tennisonchan / css3-new-features.html
Created February 10, 2015 20:55
template for testing new CSS3 functions
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<style type="text/css">
.css-filter-target {
-webkit-filter: hue-rotate(90deg);
filter: hue-rotate(90deg);
@tennisonchan
tennisonchan / rock_paper_scissors-multi_players_game.js
Last active August 29, 2015 14:15
example for OO js in rock paper scissors
var Player = function(name, match_table){
this.name = name;
this.rounds = [];
this.output_options = [];
this.score = 0;
this.init = function () {
for(var opt in match_table){
this.output_options.push(opt);
}
@tennisonchan
tennisonchan / js-hoist.js
Last active April 25, 2016 13:42
Demo how variable declarations are hoisted. Regardless of where they occur in the function scope, it will be hoisted to the top. That's why all the variables should be declare at the top.
// Regardless of where they occur in the function scope,
// it will be hoisted to the top.
// it looks like this
var foo = 1;
function bar() {
if (!foo) {
var foo = 10;
}
@tennisonchan
tennisonchan / detectPosition.js
Created April 18, 2015 07:57
Detect user scroll position on page
var timeout20 = null;
var timeout50 = null;
onscroll = function(e){
var scrollPosition = window.scrollY / (window.document.body.scrollHeight - window.innerHeight);
if(0.2 < scrollPosition && scrollPosition < 0.5){
clearTimeout(timeout20);
timeout20 = setTimeout(function(){
console.log('20%');
}, 500);