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

Divide Rectangle Into Smaller Rectangles

  1. const rand = num => ~~(Math.random() * num)
  2.  
  3. let rectNum = 2 + rand(10)
  4. let rectCount = 0
  5.  
  6. document.body.appendChild(document.createElement('div')).innerText =
  7.   'click anywhere to regenerate'
  8.  
  9. function reset() {
  10.   ;[...document.querySelectorAll('.rect')].forEach(rect => rect.remove())
  11.   rectNum = 2 + rand(10)
  12.   rectCount = 0
  13.   newRect(300, 300, 50, 50)
  14. }
  15. reset()
  16. onpointerup = reset
  17.  
  18. function newRect(w, h, xp, yp) {
  19.   const rect = document.body.appendChild(document.createElement('div'))
  20.  
  21.   rect.classList.add('rect')
  22.   rectCount++
  23.  
  24.   Object.assign(rect.style, {
  25.     position: 'absolute',
  26.     left: `${xp}px`,
  27.     top: `${yp}px`,
  28.     width: `${w}px`,
  29.     height: `${h}px`,
  30.     outline: `1px solid black`,
  31.   })
  32.  
  33.   const props = {
  34.     x: xp,
  35.     y: yp,
  36.     height: h,
  37.     width: w,
  38.     seed: rand(3),
  39.     divide() {
  40.       const div = 2 + rand(5 * Math.random() * Math.random())
  41.       if (rand(2) == rand(2)) {
  42.         const newHeight = this.height / div
  43.  
  44.         newRect(this.width, this.height - newHeight, this.x, this.y)
  45.         newRect(this.width, newHeight, this.x, this.y + this.height - newHeight)
  46.       } else {
  47.         const newWidth = w / div
  48.         newRect(this.width - newWidth, this.height, this.x, this.y)
  49.         newRect(newWidth, this.height, this.x + this.width - newWidth, this.y)
  50.       }
  51.       rect.remove()
  52.     },
  53.   }
  54.   window.requestAnimationFrame(() => {
  55.     if (rectCount < rectNum) {
  56.       props.divide()
  57.     } else {
  58.       console.log('DONE!')
  59.     }
  60.   })
  61. }

This snippet comes to mind from time to time – one easy way to divide a rectangle into smaller rectangles- I actually went back and looked it up as it was an answer to a student question from 2006. The original one was written in ActionScript 2. Have a look:

  1. var wormNum:Number = 123;
  2. var wormCount:Number = 0;
  3. newWorm(400, 400, 0, 0);
  4. this.onEnterFrame = function() {
  5. 	if (wormCount < wormNum) {
  6. 		for (var props:String in this) {
  7. 			if (this[props]._x != undefined) {
  8. 				this[props].divide();
  9. 			}
  10. 		}
  11. 	}
  12. };
  13. function newWorm(w, h, xp, yp) {
  14. 	var currWorm:MovieClip = this.createEmptyMovieClip("box"+wormCount, this.getNextHighestDepth());
  15. 	wormCount++;
  16. 	box(w, h, currWorm, random(0xFFFFFF));
  17. 	currWorm._x = xp;
  18. 	currWorm._y = yp;
  19. 	currWorm.seed = random(3);
  20. 	currWorm.divide = function() {
  21. 		var div = random(4)+(1+Math.random()*1);
  22. 		if (random(2) == random(2)) {
  23. 			// divide vertically
  24. 			var nh:Number = this._height/div;
  25. 			newWorm(this._width, this._height-nh, this._x, this._y);
  26. 			newWorm(this._width, nh, this._x, this._y+this._height-nh);
  27. 		} else {
  28. 			// divide horizonatlly
  29. 			var nw:Number = this._width/div;
  30. 			newWorm(this._width-nw, this._height, this._x, this._y);
  31. 			newWorm(nw, this._height, this._x+this._width-nw, this._y);
  32. 		}
  33. 		this.removeMovieClip();
  34. 	};
  35. }
  36. function box(w:Number, h:Number, mc:MovieClip, col:Number):Void {
  37. 	with (mc) {
  38. 		lineStyle(0, 0, 20);
  39. 		beginFill(col, 10);
  40. 		moveTo(0, 0);
  41. 		lineTo(w, 0);
  42. 		lineTo(w, h);
  43. 		lineTo(0, h);
  44. 		endFill();
  45. 	}
  46. }

Don’t remember why I called them worms instead of rectangles, some AS2 types floating around…

Random Color Strings

  1. R = Math.random
  2. b = document.body
  3. b.style = 'background: black; font-family: sans-serif; text-transform: uppercase; color: white;'
  4.  
  5. setInterval(_ => {
  6.   if (R()<.9) {
  7.     s = ''
  8.     for(i=0;i<R() * 30 + 4;i++) s+=(~~(R() * 0xff)).toString(36)
  9.       .replace(R() < .9 ? /[0-9]/g : '', '')
  10.  
  11.     b.innerHTML += `
  12.      <n style="color:hsl(${R()*360}, 30%, 50%)">${s}</n> `+(R()<.1?'<br>':'');
  13.   }
  14. }, 100)

Make some random strings and give them a random color… a friend of mine showed a work in progress forked codepen – so I created a golfed version/variation…

Fake RNG

  1. let anchors
  2. let idx
  3. let leng = 10
  4. let size = 200
  5. let px = 0
  6. let py = 0
  7.  
  8. function seed() {
  9.   idx = 0
  10.   anchors = (Date.now() + '').split``
  11.     .reverse()
  12.     .map(v => parseFloat(v) / 10)
  13.     .splice(0, leng)
  14. }
  15.  
  16. let last = 0
  17. let zoom = 1
  18. function rand() {
  19.   if (idx > size * size) seed()
  20.  
  21.   px += zoom
  22.   py += ~~(px / size)
  23.  
  24.   if (px >= size) px = 0
  25.   if (py >= size) py = 0
  26.  
  27.   const point = {
  28.     x: anchors[idx % leng],
  29.     y: anchors[(idx + 1) % leng]
  30.   }
  31.   idx++
  32.  
  33.   let dists = []
  34.   for (let i = 0; i < anchors.length; i += 2) {
  35.     let dx = px - anchors[i] * size
  36.     let dy = py - anchors[i + 1] * size
  37.     dists.push(Math.sqrt(dx * dx + dy * dy))
  38.   }
  39.   dists.sort()
  40.   last += (dists[0] / size - last) / 4
  41.   return last
  42. }
  43.  
  44. seed()
  45.  
  46. let d = document
  47. let b = d.body
  48. with (b.appendChild(
  49.   Object.assign(d.createElement`canvas`, { width: 400, height: 400 })
  50. ).getContext`2d`) {
  51.   fillStyle = 'black'
  52.   fillRect(0, 0, 400, 400)
  53.  
  54.   for (let i = 0; i < 200; i++) {
  55.     for (let j = 0; j < 200; j++) {
  56.       const c = rand() * 255
  57.       fillStyle = `rgb(${c}, ${c}, ${c})`
  58.       fillRect(j * 2, i * 2, 1, 2)
  59.     }
  60.   }
  61. }

Another one for genuary “Create your own pseudo-random number generator and visually check the results.”

Speed Coded Cosine Dither

  1. d = document
  2. b = d.body
  3. wh = 300
  4. hs = wh / 2
  5. S = 1.5
  6. with(
  7.   b.appendChild(Object.assign(
  8.     d.createElement`canvas`, {
  9.       width: wh, height: wh
  10.     })).getContext`2d`) {
  11.  
  12.   canvas.style.transformOrigin = '0 0'
  13.   canvas.style.transform = `scale(${S})`
  14.   canvas.style.imageRendering = 'pixelated'
  15.  
  16.   fillStyle = 'gray'
  17.  
  18.   fillRect(0, 0, wh, wh)
  19.   shadowBlur = 80
  20.   shadowColor = 'black';
  21.   shadowOffsetY = 20
  22.   for (let i = 0; i < 70; i++) {
  23.     save()
  24.     translate(hs, hs)
  25.     rotate(i / 33)
  26.     scale(1 - i / 100, 1)
  27.     translate(-hs, -hs)
  28.     fillStyle = `hsl(${i << 2}, 50%, 50%)`
  29.     beginPath()
  30.     arc(hs, hs, hs * .8, 0, 7)
  31.     fill()
  32.     restore()
  33.     shadowColor = 'transparent'
  34.     shadowBlur = 0
  35.   }
  36.  
  37.   C = Object.assign(d.createElement`canvas`, {
  38.     width: wh, height: wh
  39.   }).getContext('2d')
  40.   C.drawImage(canvas, 0, 0);
  41.  
  42.   im = getImageData(0, 0, wh, wh);
  43.  
  44.   p = im.data
  45.   size = wh * wh * 4
  46.  
  47.   modd = Math.random() * 5550
  48.   for (let i = 0; i < size; i += 4) {
  49.     if (Math.random() < 0.0001) modd = Math.random() * 5550
  50.     M = Math.cos(i % modd) * 255
  51.     p[i] = M < p[i] ? 255 : 0;
  52.     p[i + 1] = M < p[i + 1] ? 255 : 0;
  53.     p[i + 2] = M < p[i + 2] ? 255 : 0;
  54.   }
  55.  
  56.   putImageData(im, 0, 0);
  57.   globalCompositeOperation = 'hard-light'
  58.   drawImage(C.canvas, 0, 0);
  59. }

Some speed-coded canvas stuff with a dither inspired by #genuary2022

valueOf for Shorter Function Calls

  1. r={valueOf:_=>Math.random()}
  2. console.log(+r)
  3. console.log(+r)
  4. console.log(+r)

Use valueOf to shorten function calls. I learned this trick over at stackexchange codegolf here from user cyoce.

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