Obfuscated Pseudo-typed Globals
copy number: age = 33 ; number: height = 6 ; string: name = 'joe' ; object: person = { name, age, height } ; console.log ( person) ;
Try it out…
WARNING: do not use this
This one is bad enough that I feel the need to put a warning. This is some weird obfuscation… I’ve put labels before global variable definitions so they look like type declarations.
It’s funny, monaco says lots of stuff about this one if you hit the Try it out… button you can see…
Oscillating Canvas Wave
copy const c = document.body .appendChild ( document.createElement ( 'canvas' ) ) .getContext ( '2d' ) ; Object .assign ( document.body .style , { margin: 0 , height: '100%' } ) ; Object .assign ( c.canvas .style , { position: 'absolute' , left: 0 , top: 0 , width: '100%' , height: '100%' } ) ; let t = 0 ; function resize( ) { c.canvas .width = innerWidth * 2 ; c.canvas .height = innerHeight * 2 ; draw( ) ; } window.addEventListener ( 'resize' , resize) ; resize( ) ; function draw( ) { const { canvas: { width, height } } = c; c.fillStyle = 'rgba(155, 155, 155, .4)' ; c.fillRect ( 0 , 0 , width, height) ; c.fillStyle = '#000' ; let x = innerWidth; let y = 0 ; t += 0.05 ; for ( let i = 0 ; i < innerHeight; i++ ) { x += 2 * Math .cos ( t + i * 0.1 * Math .cos ( i / 200 ) ) ; y += 2 ; c.fillRect ( x, y, 100 , 1 ) ; } } function loop( ) { draw( ) ; window.requestAnimationFrame ( loop) ; } loop( ) ;
Try it out…
Speed coded oscillating wave on canvas… Looks better in fullscreen.
CSS Color Picker Gradient
copy const col = document.body .appendChild ( document.createElement ( 'div' ) ) ; Object .assign ( col.style , { position: 'absolute' , left: 0 , top: 0 , width: '100%' , height: '200px' , background: 'linear-gradient(90deg, #ff0000, #ffff00, #00ff00, #00ffff, #0000ff, #ff00ff, #ff0000)' } ) ;
Try it out…
CSS gradient that cycles hue – useful for a colorpicker. This is the main part of the snippet:
linear-gradient(90deg, #ff0000, #ffff00, #00ff00, #00ffff, #0000ff, #ff00ff, #ff0000)
SVG Isometric Box
copy const el = document.body .appendChild ( document.createElement ( 'div' ) ) ; el.innerHTML = ` < svg id= "svg" width= "100%" height= "100%" viewBox= "0 0 550 500" > ${ ( ( ) => { let str = '' let c = 10 ; let num = 200 ; let colStep = 225 / num for ( let i = 0 ; i < num; i++ ) { c += colStep; col = `rgb( ${ c} , ${ c} , ${ c} ) `; str += `< rect fill= "${col}" x= "0" y= "0" width= "200" height= "200" transform= "translate(275, ${250 - i}) scale(1, .5) rotate(45)" /> ` } return str } ) ( ) } </ svg> < style> svg, div, body, html { overflow: visible; height: 100 %; width: 100 %; margin: 0 ; padding: 0 ; background: gray; } </ style> `;
Try it out
Draw a shaded isometric box in SVG.
Speed Coded HTML Templating Thoughts
copy const tmpl = ` main h1 Welcome hr nav button one button two button three hr p this is a test p this is another test textarea hello ul li alpha li beta li zeta ul li organize li things li and stuff hr footer 2022 ` let htm = parse( tmpl) ; document.body .innerHTML += htm console.log ( htm) function isTag( tag) { return !/ Unknown/ .test ( document.createElement ( tag) + '' ) } function parse( input) { let lines = input.split ( /\n/ ) let html = [ ] let closeTags = [ ] let lastIndent = 0 ; for ( let i = 1 ; i < lines.length ; i++ ) { let indent = 0 ; let tag = '' ; let content = '' let line = lines[ i] let mode = 'start' ; for ( let j = 0 ; j < line.length ; j++ ) { const char = line[ j] if ( char == ' ' && mode === 'start' ) { indent++; } else if ( char != ' ' && mode != 'content' ) { mode = 'tag' tag += char } else { mode = 'content' content += char ; } } if ( indent <= lastIndent && closeTags.length > 0 ) { let back = lastIndent while( back >= indent) { html.push ( closeTags.pop ( ) ) back -= 2 } } if ( tag.length > 0 ) { let xtra = '' let origTag = tag; if ( ! isTag( tag) ) { tag = 'div' xtra = ` class= "${origTag}" ` } closeTags.push ( `</ ${ tag} > `) html.push ( `< ${ tag + xtra} > `) if ( content.length > 0 ) html.push ( content) } lastIndent = indent; } return [ ...html , ...closeTags .reverse ( ) ] .join ( '' ) }
Try it out…
Parse a minimalistic html template inspired by the likes of jade, haml, pug etc… This is very speed-coded. I may revisit the same thing with a bit more of an elegant approach in the future.