Skip to content

Instantly share code, notes, and snippets.

@mohnatus
mohnatus / animationFooTemplate.js
Last active March 17, 2019 04:12
animation function with requestAnimationFrame template
let startTime = -1;
let animationDuration = 2000;
function draw(timestamp) {
let progress = 0;
if (startTime < 0) {
startTime = timestamp;
} else {
progress = timestamp - startTime;
@mohnatus
mohnatus / solarSystem.js
Created February 12, 2019 11:37
sun, earth and moon animation on canvas
let sun = new Image();
let moon = new Image();
let earth = new Image();
let canvas = document.getElementById('canvas');
let ctx = canvas.getContext('2d');
let config = {
earth: {
x: 150,
@mohnatus
mohnatus / draw-pending.js
Created February 13, 2019 10:43
do animation with pending
let drawPending = false;
function redraw() {
drawPending = false;
// do drawing
}
function requestRedraw() {
if (!drawPending) {
@mohnatus
mohnatus / hasOwnProperty.js
Created February 13, 2019 16:03
correct for-in loop with hasOwnProperty check
for (var name in map) {
if (map.hasOwnProperty(name)) {
}
}
@mohnatus
mohnatus / matrixReduce.js
Created February 13, 2019 16:39
matrix rows and cols reduce
// свести каждый ряд матрицы к одному значению (например, минимальная высота)
function rowsCombine(rows) {
return rows.map(row => {
return row.reduce((combine, currentCell) => combine + currentCell, 0);
});
}
// свести каждую колонку матрицы к одному значению (например, минимальная ширина)
function colsCombine(rows) {
return rows[0].map((_, i) => {
@mohnatus
mohnatus / module-exports.js
Created February 14, 2019 05:32
node module export
if (typeof module != "undefined" && module.exports)
module.exports = 'hello world';
function holdBeforeFired(funcToFire, holdTime) {
let hold = false,
_this,
_arguments;
return function action() {
if (hold) {
_this = this;
_arguments = arguments;
return;
@mohnatus
mohnatus / bifurcate.js
Created March 10, 2019 12:17
bifurcate array
const bifurcateBy = (arr, fn) =>
arr.reduce((acc, val, i) => (acc[fn(val, i) ? 0 : 1].push(val), acc), [[], []]);
@mohnatus
mohnatus / js-prototype-inheritance.js
Created March 11, 2019 07:47
prototype inheritance in js
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
@mohnatus
mohnatus / custom-error.js
Created March 15, 2019 04:15
create custom error
function CustomError(message) {
this.message = message;
this.stack = (new Error()).stack;
}
CustomError.prototype = Object.create(Error.prototype);
CustomError.prototype.name = "CustomError";