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.
Check if HTML Tag is Valid
copy const isTag = tag => { return !/ Unknown/ .test ( document.createElement ( tag) + '' ) } console.log ( 'section:' , isTag( 'section' ) ) console.log ( 'div:' , isTag( 'div' ) ) console.log ( 'nav:' , isTag( 'nav' ) ) console.log ( 'banana:' , isTag( 'banana' ) )
Try it out
Check if a tagName
is a valid html element.
When casting a dom node to a string, you’ll get a class name like this:
copy document.createElement ( 'div' ) + '' // '[object HTMLDivElement]' // you can cast to a string with `toString` if // you want things to be more readable document.createElement ( 'section' ) .toString ( ) // '[object HTMLElement]' document.createElement ( 'input' ) + '' // '[object HTMLInputElement]'
When you try to create something with an unknown tagName
you’ll end up with:
copy document.createElement ( 'banana' ) + '' // '[object HTMLUnknownElement]'
So, testing for the presence of the string Unknown
is an easy way to check if a tagName
is valid in a given browser. This is the perfect kind of thing to memoize:
copy const tags = { } const isTag = tag => { if ( tags[ tag] != null ) { // already calculated console.log ( 'loking up: ' , tag, tags[ tag] ) ; return tags[ tag] } const result = !/ Unknown/ .test ( document.createElement ( tag) + '' ) tags[ tag] = result return result } console.log ( 'calculator' , isTag( 'calculator' ) ) console.log ( 'calculator' , isTag( 'calculator' ) ) console.log ( 'strong' , isTag( 'strong' ) ) console.log ( 'strong' , isTag( 'strong' ) )
Try it out