)
}
}
)
(
}
{
)
)
(
)
(
(
{
}
)
(
)
}
)
)
{
(
(
)
)
}
)
(
}

Semi-golfed Canvas Setup

  1. ((
  2.   width = 200, height = 200, 
  3.   cnv = document.body.appendChild(
  4.     Object.assign(
  5.       document.createElement('canvas'), {
  6.       width, height
  7.     })
  8.   ),
  9.   c = cnv.getContext('2d'), 
  10.   f = c.fillRect.bind(c),
  11.   C = _ => c.fillStyle = _) => {
  12.  
  13.   C`gray`; f(0, 0, width, height);
  14.   C`blue`; f(10, 10, 100, 20);
  15. })();

Quick Random Value

  1. console.log(
  2.   ['x', 'y', 'z'][Math.floor(Math.random() * 3)]
  3. );

This will output either x, y or z. It works by looking up a random index of an inline array. I find this great for quick prototyping – probably not something you want to be using outside throw away code… Hit the “Try it out…” button and open your dev console.

Editible Div With Memory (localStorage)

  1. const el = document.createElement('div')
  2. el.innerHTML = localStorage.memory == null ? 'type here' :
  3.   localStorage.memory
  4. el.contentEditable = true;
  5.  
  6. document.body.appendChild(el);
  7. document.addEventListener('keyup', () => {
  8.   localStorage.memory = el.innerHTML;
  9. });

Create an editable div that remembers what was typed in it.

createElement and Object.assign

  1. const el = document.createElement('div');
  2. Object.assign(el.style, {
  3.   position: 'absolute',
  4.   left: '100px',
  5.   top: '100px',
  6.   width: '30px',
  7.   height: '30px',
  8.   background: 'linear-gradient(black, red)',
  9.   transform: 'rotate(15deg)'
  10. });
  11. document.body.appendChild(el);

Fast and easy way to set many styles on a DOM node. Click/tap the “Try it out” button to play around with the code.

// css // dom // tricks
snippet.zone ~ 2021-24 /// {s/z}