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.
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
Local Mouse Click
copy const RIGHT_BOUND = 200 ; const measureEl = document.createElement ( 'div' ) ; Object .assign ( measureEl.style , { position: 'absolute' , left: 0 , top: 0 , zIndex: 999 } ) ; function localPosition( e, element, w = 1 , h = 1 ) { // normalize desktop and mobile const touches = e.touches ; let x; let y; if ( touches != null && touches.length > 0 ) { x = touches[ 0 ] .clientX ; y = touches[ 0 ] .clientY ; } else { x = e.clientX ; y = e.clientY ; } function location( width) { let left = 0 ; let right = RIGHT_BOUND; let mid; while ( right - left > 0.0001 ) { mid = ( right + left) / 2 ; measureEl.style [ width ? 'width' : 'height' ] = `${ mid} % `; measureEl.style [ width ? 'height' : 'width' ] = '100%' ; element.appendChild ( measureEl) ; const el = document.elementFromPoint ( x, y) ; element.removeChild ( measureEl) ; if ( el === measureEl) { right = mid; } else { if ( right === RIGHT_BOUND) { return null ; } left = mid; } } return mid; } const left = location( 1 ) ; const top = location( 0 ) ; return left != null && top != null ? { // percentage values left, top, // pixel values x: left * w * 0.01 , y: top * h * 0.01 } : null ; } const div = document.body .appendChild ( document.createElement ( 'div' ) ) ; div.innerHTML = 'click me' ; Object .assign ( div.style , { postition: 'absolute' , transform: 'translate(20px, 20px) rotate(45deg) scale(0.8)' , width: '120px' , height: '90px' , color: 'white' , fontFamily: 'sans-serif' , background: 'gray' } ) ; document.addEventListener ( 'touchstart' , onClick) ; document.addEventListener ( 'mousedown' , onClick) ; function onClick( e) { const info = localPosition( e, e.target , 120 , 90 ) ; console.log ( info) ; }
Try it out…
Find the local percentage and pixel values of the mouse/touch location.
I found this one on stackoverflow here by user 4esn0k .
This is an interesting alternative to semi-broken native browser solutions and custom matrix math 😉
elementUnderPoint and elementsUnderPoint
copy document.addEventListener ( 'mousemove' , e => { const el = document.elementFromPoint ( e.clientX , e.clientY ) ; // msElementsFromPoint for old IE versions (which will thankfully be gone soon) const els = document.elementsFromPoint ( e.clientX , e.clientY ) ; if ( el != null ) { console.log ( el.className , els ) el.style .border = '1px solid white' ; } } ) ; // prevent scrolling document.addEventListener ( 'touchmove' , e => e.preventDefault ( ) , { passive: false } ) ; // hardcoded for clarity document.addEventListener ( 'touchmove' , e => { const x = e.touches [ 0 ] .clientX ; const y = e.touches [ 0 ] .clientY ; const el = document.elementFromPoint ( x, y) ; const els = document.elementsFromPoint ( x, y) ; if ( el != null ) { console.log ( el.className , els ) el.style .border = '1px solid white' ; } } ) ; Object .assign ( document.body .style , { background: '#000' , margin: 0 } ) ; // put some boxes on the page for ( let i = 0 ; i < 200 ; i++ ) { const size = 10 + Math .random ( ) * 80 ; const halfSize = size / 2 ; const x = Math .random ( ) * 120 - 10 ; const y = Math .random ( ) * 120 - 10 ; const rot = x * 3 + Math .random ( ) * 90 - 45 ; const col = `hsl( ${ rot} , 50 %, 50 % ) `; document.body .innerHTML += ` < div class= "box box-${i}" style= " transform: rotate(${rot}deg); position: absolute; background: ${col}; width: ${size}px; height: ${size}px; left: ${x}%; top: ${y}%; " ></ div> `; }
Try it out…
Obtain the element or elements underneath the mouse or touch point. Open your dev console to see the results.