Elasticity With Trails
copy let pointX = pointY = 0 ; document.addEventListener ( 'touchmove' , e => e.preventDefault ( ) , { passive: false } ) ; document.addEventListener ( 'mousemove' , e => { pointX = e.clientX ; pointY = e.clientY ; } ) ; document.addEventListener ( 'touchmove' , e => { pointX = e.touches [ 0 ] .clientX ; pointY = e.touches [ 0 ] .clientY ; } ) ; let el = document.body .appendChild ( document.createElement `div` ) ; const size = 20 ; const halfSize = size / 2 ; Object .assign ( el.style , { position: 'absolute' , width: `${ size} px`, height: `${ size} px`, background: 'red' , borderRadius: `${ size} px`, left: 0 , top: 0 } ) ; let x = vx = y = vy = 0 ; const FADE_TIME = 800 ; const plotDot = ( x, y) => { const dot = document.body .appendChild ( el.cloneNode ( ) ) ; const time = dot.style .transform += ' scale(.25)' ; dot.style .transition = `opacity ${ FADE_TIME} ms ease- out`; window.requestAnimationFrame ( ( ) => { dot.style .opacity = 0 ; setTimeout( ( ) => dot.parentNode .removeChild ( dot) , FADE_TIME) ; } ) } let ticks = 0 ; const loop = ( ) => { vx = ( ( pointX - x) * .08 + vx) * .95; vy = ( ( pointY - y) * .08 + vy) * .95; x += vx; y += vy; if ( ticks++ % 2 === 0 && Math .abs ( pointX - x) > 1 && Math .abs ( pointY - y) > 1 ) { plotDot( ) ; } el.style .transform = `translate( ${ x - halfSize} px, ${ y - halfSize} px) `; requestAnimationFrame( loop) ; } loop( ) ; const info = document.body .appendChild ( document.createElement `div` ) ; info.innerHTML = 'move mouse or finger left/right/up/down' ;
Try it out…
This is a variation on yesterdays post. This has elasticity on both axis and draws a trail of dots…
Elasticity
copy let pointX = pointY = 0 ; document.addEventListener ( 'mousemove' , e => { pointX = e.clientX ; pointY = e.clientY ; } ) ; document.addEventListener ( 'touchmove' , e => { pointX = e.touches [ 0 ] .clientX pointY = e.touches [ 0 ] .clientY } ) ; let el = document.body .appendChild ( document.createElement `div` ) ; const size = 20 ; const halfSize = size / 2 ; Object .assign ( el.style , { position: 'absolute' , width: `${ size} px`, height: `${ size} px`, background: 'red' , left: 0 , top: 0 } ) let x = vx = 0 ; const loop = ( ) => { vx = ( ( pointX - x) * .2 + vx) * .79; x += vx; el.style .transform = `translate( ${ x - halfSize} px, 50px) `; requestAnimationFrame( loop) ; } loop( ) ; let info = document.body .appendChild ( document.createElement `div` ) ; info.innerHTML = 'move mouse or finger left/right' ;
Try it out…
Basic interactive elasticity with mouse or touch
Hacky Polish Notation
copy const f = ( o, ...a ) => eval( a.join ( o) ) ; const polish = eq => eval( eq.replace ( /\s+/g , ' ' ) .replace ( /(\))\s([0-9])/g , '$1,$2' ) .replace ( /([0-9]+)[^\)]/g , '$1,' ) .replace ( / \( \s? ( [ \+ \- \* \\/ ] ) / g, 'f(`$1`,' ) ) ; console.log ( polish( '(* 2 2)' ) ) ; console.log ( polish( '(* 2 2 (+ 3 2 1))' ) ) ; console.log ( polish( '(- 10 3)' ) ) ; console.log ( polish( '(/ (+ 10 10 (* 2 2)) 3)' ) ) ;
Try it out…
Hacky way to parse polish notation. This uses regular expressions to transform polish notation into javascript that can be run with eval
. Just a weird/fun idea…
What is the current element? (document.activeElement)
copy console.log ( document.activeElement ) ;
I use document.activeElement
constantly when I do accessibility work (getting things to work with screenreaders like NVDA, JAWS or VoiceOver). document.activeElement
contains the current element that is in focus on a page.
You can easily debug it with a live expression in the chrome console:
Here is a small demo to play with – try clicking or tabbing around the buttons and inputs:
copy const el = document.createElement ( 'el' ) ; el.innerHTML = ` < button class= "one" > one</ button> < button class= "two" > two</ button> < button class= "three" > three</ button> < button class= "four" > four</ button> < hr> < input type= "text" placeholder= "type..." > < hr> < input type= "range" > `; document.body .appendChild ( el) ; let lastActive; setInterval( ( ) => { if ( document.activeElement != lastActive) { lastActive = document.activeElement ; console.log ( lastActive.outerHTML ) ; } // no need to update 60fps } , 100 ) ;
Try it out…
Hijack Focus
copy const el = document.body .appendChild ( document.createElement ( 'div' ) ) ; el.innerHTML = ` < button> one</ button> < button> two</ button> < button> three</ button> `; document.addEventListener ( 'click' , e => { e.target .focus ( ) ; } ) ; const origFocus = HTMLElement.prototype .focus HTMLElement.prototype .focus = function ( ) { console.log ( this .outerHTML , 'focus was called' ) ; origFocus.call ( this ) ; } ; el.firstElementChild .focus ( ) ;
Try it out…
Easy way to debug many calls to focus
.