SVG getScreenCTM
copy const el = document.body .appendChild ( document.createElement `div` ) ; el.innerHTML = ` < svg width= "200" height= "200" viewBox= "0 0 200 200" > < rect class= "rect" transform= "translate(50, 50) scale(1.2) rotate(25)" fill= "purple" x= "0" y= "0" width= "50" height= "50" /> </ svg> `; const box = document.body .appendChild ( document.createElement `div` ) ; Object .assign ( box.style , { position: 'absolute' , left: 0 , top: 0 , width: '50px' , height: '50px' , transformOrigin: '0 0' , outline: '5px solid red' } ) ; const rect = document.querySelector ( '.rect' ) ; const { a, b, c, d, e, f} = rect.getScreenCTM ( ) box.style .transform = ` matrix( ${ [ a, b, c, d, e, f] } ) `;
Try it out…
The transformation matrix of an SVG element can be obtained using getScreenCTM
or getCTM
. The latter of which will be relative to the SVG coordinate space, vs the coordinate space of the page.
Here we take the matrix data from getScreenCTM
and use it on a div to place a border over an SVG rect
node. This is great for layering HTML on top of SVG.
Canvas Particle
copy const canvas = document.createElement ( 'canvas' ) , c = canvas.getContext ( '2d' ) ; canvas.width = 500 ; canvas.height = 500 ; document.body .appendChild ( canvas) ; c.fillStyle = 'black' ; c.fillRect ( 0 , 0 , canvas.width , canvas.height ) ; let a = 0.29 , b = 0.22 ; function f( x, y) { if ( Math .random ( ) < 0.001 ) b = Math .random ( ) ; return Math .cos ( ( x + Math .sin ( x) * 0.01 + Math .cos ( x * a) ) * b) ; } let x = 1 , y = 0 ; setInterval( ( ) => { if ( Math .random ( ) < 0.03 ) { x = 1 ; y = 0 ; } if ( Math .random ( ) < 0.001 ) a = Math .random ( ) ; for ( let i = 0 ; i < 1e3; i++ ) { x = x + f( y) ; y = y + f( x) ; c.save ( ) ; c.translate ( 150 , 250 ) ; c.scale ( 0.5 , 0.5 ) ; c.fillStyle = 'rgba(255, 255, 255, 0.01)' ; c.fillRect ( x, y, 5 , 5 ) ; c.restore ( ) ; } } , 20 ) ;
Try it out…
A single particle moves around and leaves a trail
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…
Easy Hex Color Invert
copy let color = 0xFFFF00; function toHexString( col) { return '#' + ( '000000' + col.toString ( 16 ) ) .substr ( - 6 ) ; } function onClick( ) { // invert the color color ^= 0xFFFFFF; document.body .style .background = toHexString( color) ; } onClick( ) ; document.addEventListener ( 'click' , onClick) ; console.log ( 'try a different initial color' ) ; console.log ( 'click anywhere to invert background...' ) ;
Try it out…
Easily invert a hex color. Expanding on yesterdays post – just one of many reasons you may want to work with colors in their integer form.
Integers to Colors
copy function toHexString( col) { return '#' + ( '000000' + col.toString ( 16 ) ) .substr ( - 6 ) ; } document.body .style .background = toHexString( 0x275ba1) ; console.log ( "#275ba1 as an integer:" , 0x275ba1) ; console.log ( "#ff0000 as an integer:" , 0xff0000) ;
Try it out…
Convert an integer in hex (like 0xff0000
) to a usable hex string "#ff0000"
.