Obfuscated Canvas Commands
copy const oCan = ( d = document, b = d.body , canvas = b.appendChild ( document.createElement ( 'canvas' ) ) , c = canvas.getContext ( '2d' ) , props = [ ] , o = { } , docs = { } , cmds = [ ] , L, k, du, j, draw, id ) => { ; ( onresize = ( ) => { canvas.width = innerWidth canvas.height = innerHeight if ( draw) { clearTimeout( id) id = setTimeout( ( ) => cmds.forEach ( v => draw( v) ) , 500 ) } } ) ( ) Object .assign ( b.style , { margin: 0 , height: '100%' } ) // longer way: console.log(Object.getOwnPropertyNames(Object.getPrototypeOf(c))); for ( let i in c) props.push ( i) // make alphabetical since object keys have // no order props.sort ( ) .map ( i => { L = i.match ( /[A-Z]/ ) k = i[ 0 ] if ( L) k += L[ 0 ] du = 0 if ( o[ k] ) { j = 0 while ( o[ k] ) k += i[ ++ j] } o[ k] = ( typeof c[ i] ) [ 0 ] == 'f' ? ( ...args ) => c[ i] .apply ( c, args) : v => ( c[ i] = v) docs[ i] = k } ) console.log ( 'docs:' , JSON.stringify ( docs, null , 2 ) ) return ( draw = ( s, cmd, currFn, args = [ ] , part, fn, num) => { cmd = s.split ( /\s+/ ) cmds.push ( s) c.save ( ) for ( let i = 0 ; i < cmd.length ; i++ ) { part = cmd[ i] fn = o[ part] if ( fn && currFn != fn) { currFn && currFn.apply ( c, args) currFn = fn args = [ ] } else { num = parseFloat( part) args.push ( ! isNaN( num) ? num : part) } } currFn && currFn.apply ( c, args) c.restore ( ) } ) } const c = oCan( ) // `font` & text not suppoted // make str a function so resize works? c( ` fS #ccc fR 0 0 400 ${ innerHeight} fS blue fR 40 0 20 20 gCl difference ro .25 fR 50 0 30 30 gCl source- over fS rgba( 200 , 100 , 9 ) fR 100 100 40 40 `)
Try it out…
I’ve had this idea for a long time, never bothered doing it until now. I wrote it in a semi-golfed style for no reason… Anyway, this lets you write canvas code in a strange obfuscated syntax that looks like this:
copy c( ` fS #ccc fR 0 0 400 ${ innerHeight} fS blue fR 40 0 20 20 gCl difference ro .25 fR 50 0 30 30 gCl source- over fS rgba( 200 , 100 , 9 ) fR 100 100 40 40 `)
This snippet logs out some JSON that shows all the method aliases for the canvas context.
Random Hex Color (semi-golfed)
copy document.body .innerHTML += 'click anywhere...' onclick = ( ) => document.body .style .background = `#${ Math .random ( ) .toString ( 16 ) .substr ( - 6 ) } `
Try it out…
I golfed this snippet slightly for no reason in particular. I recently posted a nice readable way to make random hsl
colors. This snippet generates a random hexidecimal
color.
How it works:
copy Math .random ( ) // random number between 0 and 1 .toString ( 16 ) // convert to hex string (something like "0.2d6bcee4198d4") .substr ( - 6 ) // grab the last 6 characters
Here is a non-golfed version:
copy const instructionsEl = document.createElement ( 'p' ) ; instructionsEl.innerHTML = 'click anywhere...' ; document.body .appendChild ( instructionsEl) ; const randomHexColor = ( ) => `#${ Math .random ( ) .toString ( 16 ) .substr ( - 6 ) } `; document.addEventListener ( 'click' , ( ) => { document.body .style .background = randomHexColor( ) ; } ) ;
Try it out…
Creative Coding Auto-Painting
copy Object .getOwnPropertyNames ( Math ) .map ( i => ( this [ i] = Math [ i] ) ) ; ( ( width = innerWidth * 2 , height = innerHeight * 2 , cnv = document.body .appendChild ( Object .assign ( document.createElement ( 'canvas' ) , { width, height } ) ) , c = cnv.getContext ( '2d' ) , r = ( n = 1 ) => Math .random ( ) * n, NUM = 50 , f = ( ) => ( { ax: r( width) , ay: r( height) , x: 0 , y: 0 , T: r( 9 ) , R: r( innerWidth * 0.8 ) + 40 , t: r( 6 ) , C: round( r( 255 ) ) , m: r( 5 ) + 1 } ) , cs, sn, dx, dy, ns = [ ...Array ( NUM) ] .map ( f) ) => { Object .assign ( cnv.style , { transformOrigin: '0 0' , transform: 'scale(.5)' } ) Object .assign ( document.body .style , { margin: 0 , padding: 0 } ) const clear = ( ) => { c.fillStyle = '#666668' c.fillRect ( 0 , 0 , width, height) c.globalAlpha = 0.5 } onresize = ( ) => { width = cnv.width = innerWidth * 2 height = cnv.height = innerHeight * 2 clear( ) } clear( ) setInterval( ( ) => { for ( i = 0 ; i < 30 ; i++ ) { ns.map ( ( n, i) => { with ( n) { x = ax + R * cos( t) y = ay + R * sin( t) * pow( sin( t * 0.5 ) , m) c.fillStyle = `rgba( ${ C} , ${ C} , ${ C} , .02) ` ; ( cs = cos( T) ) , ( sn = sin( T) ) , ( dx = x - ax) , ( dy = y - ay) c.fillRect ( cs * dx - sn * dy + ax, sn * dx + cs * dy + ay, 50 , 50 ) t += 0.1 R -= 0.01 if ( R < 5 ) ns[ i] = f( ) } } ) } } , 16 ) } ) ( )
Try it out…
Speed coded semi-golfed canvas texture. Best if viewed in fullscreen.
Golfed join
copy const str = [ 'one' , '-' , 'two' ] .join ``; console.log ( str) ; // outputs "one-two"
With tagged templates you can pass a template string to a function without parens.
Print a Tabula Recta (codegolf)
copy const tabulaRecta = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' .replace ( /./g , "$&$'$`\n " ) ; const pre = document.createElement ( 'pre' ) ; document.body .appendChild ( pre) ; pre.innerHTML = tabulaRecta; // technique comes from https://codegolf.stackexchange.com/a/87035/63485
Try it out…
This snippet comes from the
the codegolf stackexchange . A little while back a challenge for printing a tabula recta popped up along with a very nice answer from user Neil . Take a look at the answer here .