Skip to content

Instantly share code, notes, and snippets.

@mattcodez
mattcodez / pixel.js
Created November 26, 2014 06:51
Simple function to write a single pixel to a canvas element
//Copied from http://stackoverflow.com/q/4899799/119909
function pixel(x,y){
d[0] = 255;//r
d[1] = 0;//g
d[2] = 0;//b
d[3] = 255;//a
ctx.putImageData( id, x, y );
}
var ctx = document.getElementById('c').getContext('2d');
var id = ctx.createImageData(1,1);
//Use ES6 modules for your controllers in SailsJS
//requires https://github.com/artificialio/sails-hook-babel
export default {
controllerAction1(req, res) {
//...
},
controllerAction2(req, res) {
//...
@mattcodez
mattcodez / mrdbg-es5.js
Created February 3, 2016 19:13
Mapreduce Debug (ES6 & ES5)
//Transpiled from Babel
"use strict";
function MRDbg() {
for (var _len = arguments.length, txt = Array(_len), _key = 0; _key < _len; _key++) {
txt[_key] = arguments[_key];
}
return function (prev, cur, i, arr) {
if (i === 1) {
@mattcodez
mattcodez / thinArrow.js
Last active March 14, 2016 15:26
Proposal for temporary scope variables
//Proposal for temporary scope variables
let result = something.crazy.long.bar ? something.crazy.long.bar() : something.crazy.long.other();
//with() - Not 'strict' compliant
with (something.crazy.long){
result = bar ? bar() : other();
}
//fat arrow function
@mattcodez
mattcodez / .tmux.conf
Created August 29, 2017 22:31
Saving my current tmux conf
set -g mouse on
#set-option -g mouse on
set -g status-interval 2
set -g status-right "#S #[fg=green,bg=black]#(tmux-mem-cpu-load --colors --interval 2 --powerline-right)#[default]"
set -g status-right-length 60
set -g pane-border-status top
set -g pane-border-format "#{pane_index} #{pane_current_command} #{pane_pid}"
set -g default-terminal "screen-256color"
bind-key P command-prompt -p 'save history to filename:' -I '~/tmux.history' 'capture-pane -S - ; save-buffer %1 ; delete-buffer'
@mattcodez
mattcodez / mypi.cpp
Last active September 11, 2017 02:21
Very simple JS pi generator
// g++ -o mypi -std=c++14 mypi.cpp
#include <iostream>
#include <string>
#include <ctime>
#include <limits>
double myPi(long long loopCount);
typedef std::numeric_limits< double > dbl;
@mattcodez
mattcodez / term.sh
Created September 12, 2017 17:43
Terminal stuff
watch --color git diff --color --stat
watch git status --porcelain
@mattcodez
mattcodez / fib_iterative.js
Created February 23, 2018 13:24
Fibonacci
function fib_i(num) {
console.log(`Series of ${num}`);
let num2 = 0, num1 = 1;
for (let i = 0; i < num; i++) {
console.log(num1);
const next = num2 + num1;
num2 = num1;
num1 = next;
}
}
// Use only vanilla JS
function callWhenReadyToGo(cb) {
var allImagesLoaded = false;
// Look at all P and SPAN for very short text that says "loading"
// we don't want long text as it might be part of a longer paragraph then
var textEls = document.querySelectorAll('SPAN, P');
var loadingEls = [];
for (var i = 0; i < textEls.lenth; i++){
/* GAME OF LIFE
1. Any live cell with fewer than two live neighbours dies.
2. Any live cell with two or three live neighbours lives on to the next generation.
3. Any live cell with more than three live neighbours dies.
4. Any dead cell with exactly three live neighbours becomes a live cell.
*/
const blinkerSeed = {
height: 5,
coords: [[2,1], [2,2], [2,3]]