Skip to content

Instantly share code, notes, and snippets.

View monkianer's full-sized avatar
🐵
_

monkianer monkianer

🐵
_
  • .tw
View GitHub Profile
@monkianer
monkianer / RequestAnimationFrame.js
Last active August 29, 2015 14:25 — forked from mrdoob/RequestAnimationFrame.js
Provides requestAnimationFrame in a cross browser way.
/**
* Provides requestAnimationFrame in a cross browser way.
* @author paulirish / http://paulirish.com/
*/
if ( !window.requestAnimationFrame ) {
window.requestAnimationFrame = ( function() {
return window.webkitRequestAnimationFrame ||
/** Get parameter after query(ex:?p=1) sign.
* @param {string} parameter name.
*/
var GetURLParameter = function(sParam){
var oPageURL = window.location.href;
var sPageURL = oPageURL.split('?')[1];
if(sPageURL == null){
return;
}
@monkianer
monkianer / CountDown.js
Created August 3, 2015 08:31
simple count down timer.
var target_date = new Date("Aug 6, 2015").getTime();
// variables for time units
var days, hours, minutes, seconds;
function updateCountdown(){
// get current time stampe
var current_date = new Date().getTime();
// get seconds by using target_date - current_date, divide by million second.
var seconds_left = (target_date - current_date) / 1000;
@monkianer
monkianer / version.js
Last active September 2, 2022 05:49
檢查目前瀏覽器的版本
var versions = function (){
var u = navigator.userAgent, app = navigator.appVersion;
var ua = navigator.userAgent.toLowerCase();
return { //偵測移動端瀏覽器版本信息
trident: u.indexOf('Trident') > -1, //IE 核心
presto: u.indexOf('Presto') > -1, //opera 核心
webKit: u.indexOf('AppleWebKit') > -1, //Apple, google 核心
gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //Firefox 核心
mobile: !!u.match(/AppleWebKit.*Mobile.*/), //行動裝置
@monkianer
monkianer / LineBreaker.js
Last active May 26, 2016 04:57
Add Line Break Every {numbers} Characters
// 10 可以更換成為其他數字(例: .{.20} 即為每20個字元換行)
// 以下例子是以每10個字元換行
string.replace(/(.{10})/g, "$1\n");
@monkianer
monkianer / AddZeroDigitsBefore.js
Last active July 2, 2017 05:18
Add "0" to a number and change it to string
function AddZeroDigitsBefore(d, digits){
var nStr = d + '';
var digits = digits || 0;
while(nStr.length < digits){
nStr = '0' + nStr;
}
return nStr;
}
@monkianer
monkianer / randomColor.js
Created June 10, 2017 10:37
generate colors based on RGB or HSL
function randomRGBColor(){
var r = Math.floor(Math.random()*256);
var g = Math.floor(Math.random()*256);
var b = Math.floor(Math.random()*256);
return "rgb("+ r + "," + g + "," + b +")";
}
function randomHSLColor(){
var h = Math.floor(Math.random()*360);
@monkianer
monkianer / getScrollTop.js
Created July 31, 2017 14:53
Fix the 'ie' is not compatible with window.scrollY
/**
* Fix the 'ie' is not compatible with window.scrollY
* test with ie 9, 10, 11, edge.
**/
function getScrollTop(){
var t = typeof window.scrollY === 'undefined' ? window.pageYOffset : window.scrollY;
return t;
}
@monkianer
monkianer / getCanvasMousePosition.js
Last active August 3, 2017 17:15
Get mouse position on the canvas
function getCanvasMousePosition(e, canvas) {
var isRetina = true;
var offsetX = 0, offsetY = 0, mx, my;
if (canvas.offsetParent !== undefined) {
do {
offsetX += canvas.offsetLeft - canvas.scrollLeft;
offsetY += canvas.offsetTop - canvas.scrollTop;
} while ((canvas = canvas.offsetParent));
}
@monkianer
monkianer / Degree Radians Conversion
Created February 20, 2018 09:25
Degree Radians Conversion
// Converts from degrees to radians.
Math.radians = function(degrees) {
return degrees * Math.PI / 180;
};
// Converts from radians to degrees.
Math.degrees = function(radians) {
return radians * 180 / Math.PI;
};