Archiv der Kategorie ‘javascript‘


ES6 Mode Mouse Event Manager

let ui = {
  ['draw-poly-mousedown'](e) {
    console.log('draw poly mouse down');
  ['draw-poly-mouseup'](e) {
    console.log('draw poly mouse up');
  execMode(e) {
    let fn = ui[`${ui.mode}-${e.type}`];
    if (fn) fn(e);
ui.mode = 'draw-poly';

let e = {};
e.mousedown = 
e.mouseup = 
e.mousemove = (e) => ui.execMode(e);

XOR Color Invert

var color = 0xFFFF00;

function toHex(color) { 
  return '#' + ('000000' + color.toString(16)).substr(-6);

function onClick() {
  color ^= 0xFFFFFF; = toHex(color);


document.addEventListener('click', onClick);

Have a look at the slighty different ES6 version:

This is a great trick and a good reason to learn XOR and binary if you don’t already know about those things. The `toHex` function is also kind of snippet worthy in itself.

Quick Alphabet Array

var alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');

console.log('this is the letter b...', alphabet[1]);

The above will output:

this is the letter b... b
["a", "b", "c", "d", "e"...']

In ES6 you could use `let` instead of `var`.