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

Print a Tabula Recta (codegolf)

  1. const tabulaRecta = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  2.   .replace(/./g,"$&$'$`\n");
  3.  
  4. const pre = document.createElement('pre');
  5. document.body.appendChild(pre);
  6. pre.innerHTML = tabulaRecta;
  7.  
  8. // technique comes from https://codegolf.stackexchange.com/a/87035/63485

This snippet comes from the the codegolf stackexchange. A little while back a challenge for printing a tabula recta popped up along with a very nice answer from user Neil. Take a look at the answer here.

Shuffle an Array

  1. const nums = [1, 10, 20, 30, 50, 88];
  2. nums.sort(() => Math.random() - 0.5);
  3. console.log(nums);

Common way to shuffle the values in an array.

Convert an Image to dataUri

  1. function toDataUri(img, scalar = 1) {
  2.   const canvas = document.createElement('canvas');
  3.   canvas.width = img.width * scalar;
  4.   canvas.height = img.height * scalar;
  5.   canvas.getContext('2d').drawImage(img, 0, 0, canvas.width, canvas.height);
  6.   return canvas.toDataURL('image/png');
  7. }
  8.  
  9. const img = new Image();
  10. img.crossOrigin = 'Anonymous';
  11. img.addEventListener('load', () => {
  12.   const thumb = new Image();
  13.   // use the data URI as the source
  14.   thumb.src = toDataUri(img, .3);
  15.   document.body.appendChild(thumb);
  16. });
  17. img.src = 'https://zevanrosser.com/fotoda/slide-shot-9.jpg';

Load an image and convert it to a data URI.

Make a Debugging Dot

  1. function dot(x, y, radius, color) {
  2.   const el = document.createElement('div');
  3.   const size = `${radius * 2}px`;
  4.   Object.assign(el.style, {
  5.     position: 'absolute', 
  6.     left: `${x}px`,
  7.     top: `${y}px`,
  8.     width: size,
  9.     height: size,
  10.     transform: `translate(${-radius}px, ${-radius}px)`,
  11.     borderRadius: '50%',
  12.     background: color
  13.   });
  14.   el.classList.add('dot');
  15.   document.body.appendChild(el);
  16.   return el;
  17. }
  18.  
  19. dot(100, 100, 10, 'red');
  20. dot(200, 100, 5, 'blue');
  21.  
  22. for (let i = 0; i < 20; i++) {
  23.   dot(20 + i * 12, 200, 5, `hsl(${i * 10}deg, 60%, 50%)`)
  24. }

Draw a dot – good for visualizing things when doing animation and positioning logic.

Draw a Spiral with Resize

  1. const canvas = document.body.appendChild(
  2.   document.createElement('canvas')
  3. );
  4. const c = canvas.getContext('2d');
  5.  
  6. function resize() {
  7.   canvas.width = window.innerWidth;
  8.   canvas.height = window.innerHeight;
  9.   draw();
  10. }
  11.  
  12. function draw() {
  13.   c.clearRect(0, 0, canvas.width, canvas.height);
  14.   c.fillStyle = 'blue';
  15.  
  16.   const iter = 300,
  17.         halfWidth = window.innerWidth / 2,
  18.         halfHeight = window.innerHeight / 2;
  19.   let rad = 0, theta = 0, x, y;
  20.   for (let i = 0; i < iter; i++) {
  21.     x = halfWidth + rad * Math.cos(theta);
  22.     y = halfHeight + rad * Math.sin(theta);
  23.     c.fillRect(x, y, 5, 5);
  24.     rad += Math.min(window.innerWidth, window.innerHeight) * 0.0015;
  25.     theta += .1;
  26.   }
  27. }
  28.  
  29. resize();
  30. window.addEventListener('resize', resize);

Expanding on yesterdays post, this draws a resizable sprial on a canvas.

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