Skip to content

Instantly share code, notes, and snippets.

View morulus's full-sized avatar
💭
Hey, I have publish new package press-any-key on npmjs

Vladimir Kalmykov morulus

💭
Hey, I have publish new package press-any-key on npmjs
View GitHub Profile
@morulus
morulus / getvectorangle
Created May 15, 2015 08:17
Функция определения угла вектора по 2-м координатам
/* Returns CSS rotateZ compatible angle for verctor */
var getVectorAngle = function(x0,x,y0,y) {
if (y0===y) {
if (x>x0) angle = 0;
else if (x<x0) angle = 180;
else if (x===x0) angle = 0;
} else if (x0===x) {
if (y>y0) angle = 90;
else if (y0>y) angle = -90;
else angle = 0;
@morulus
morulus / pixalize.js
Created May 15, 2015 08:32
Преобразование процентной велечины в пиксельную
/*
Converts percents (or any value in %, px, int) to pixels.
pixelize("50%", 1000); // 500
*/
function pixelize(value, quantity) {
if ("string" == typeof value) {
if (value.substr(-1)==='%') {
return ((quantity/100)* (value.substring(0, value.length-1)));
} else {
return parseInt(value.split('px').join(''));
@morulus
morulus / svgPathToCubicBezierPoints.js
Last active August 7, 2020 12:36
SVG path to cubic Bezier converter (alpha)
function svgPathToCubicBezierPoints(path, percentRequired) {
var path=path.replace(/([\n])/g, ' ').replace(/[\t]+/g, ''),si=0,cubic=[],
task=0,
M_EREG = /M[\s\d\.\-\,]+[^\sSCLM]+/i,
c_EREG = /c[\s\d\.]+,[\d\.]+\-[\d\.]+,[\d\.]+[^\sSCLM]+/i,
s_EREG = /s[\s\d\.\-\,][^\sSCLM]+/i,
Parts = [],p=null,shift=0,cubic=[],vals,tonullx=0,tonully=0,i=0,locksblocks=[],addsx=0,addsy=0,
minx=9999999,miny=9999999,maxx=0,maxy=0;
// Search for M
@morulus
morulus / morulus-customsb.css
Last active August 29, 2015 14:21
Custom ScrollBar
*.-morulus-customsb {
position:relative;
width:100%;height:100%;
overflow:hidden;
}
*.-morulus-customsb > figure:first-child {
margin: 0px !important;
position: absolute;
right: 4px !important;
@morulus
morulus / usage.md
Last active August 29, 2015 14:21
Watch property

Example

var test = {
	foo: 123
}

watch(test, 'foo', function(val) {
	console.log('Prop changed to ', val);	
});
@morulus
morulus / rafpolyfill.js
Created May 25, 2015 07:55
requestAnimationFrame polyfill
(function() {
var vendors = ['ms', 'moz', 'webkit', 'o'],customRequestAnimationFrame=window.requestAnimationFrame,customCancelAnimationFrame=window.cancelAnimationFrame;
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
customRequestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
customCancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame']
|| window[vendors[x]+'CancelRequestAnimationFrame'];
};
window.requestAnimationFrame = function() {
customRequestAnimationFrame.apply(window, arguments);
};
@morulus
morulus / invisible-scrollbar.js
Created May 29, 2015 16:33
Polyfill solution for making scrollbar invisible (without disabling scroll) for body element
/*
Special solution to hide vertical body scroll in FF, without disabling scroll.
Usage:
<body></body>
<script>
firefoxHideScrollbar();
</script>
*/
;(function(purpose) {
// Event listner polyfill
@morulus
morulus / mixin.js
Created June 13, 2015 06:45
Simple mixin
var mixin = (function() {
var mixinup = function(a,b) {
for(var i in b) {
if (b.hasOwnProperty(i)) {
a[i]=b[i];
}
}
return a;
@morulus
morulus / eventListnerPolyfill.js
Last active August 29, 2015 14:23
eventListner Polyfill
// Remove event listner polyfill
varremoveEventListner = function(el, type, handler) {
if ( el.addEventListener ) {
el.removeEventListener(type, handler, false);
} else if ( elem.attachEvent ) {
el.detachEvent("on" + type, handler);
} else {
el["on"+type] = null;
};
},
@morulus
morulus / ratioThSum.js
Last active August 29, 2015 14:23
The ratio of numbers to their sum. Calc each value by the ratio.
/*
Функция принимает массив чисел и возвращает отношение каждого к их сумме
Returns array of ratios
*/
var ratioThSum = function(numbers) {
var sum=0,i,r=[];
for (i=0;i<numbers.length;++i) sum+=numbers[i];
for (i=0;i<numbers.length;++i) r.push(numbers[i]/sum);
return r;
}