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"
.
Complementary HSL
copy const a = document.body .appendChild ( document.createElement ( 'div' ) ) , b = document.body .appendChild ( document.createElement ( 'div' ) ) ; let degA = degB = 0 ; const size = { width: '100px' , height: '100px' } ; Object .assign ( a.style , size) ; Object .assign ( b.style , size) ; function loop( ) { degA += 1 ; degB = degA + 180 ; a.style .background = `hsl( ${ degA} deg, 100 %, 50 % ) `; b.style .background = `hsl( ${ degB} deg, 100 %, 50 % ) `; requestAnimationFrame( loop) ; } loop( ) ;
Try it out…
In HSL a hue difference of 180 degrees between two values will create a set of complimentary colors.
Canvas ImageData
copy const canvas = document.body .appendChild ( document.createElement ( 'canvas' ) ) ; const width = 200 ; canvas.width = canvas.height = width; const c = canvas.getContext ( '2d' ) ; const pixels = c.createImageData ( canvas.width , canvas.height ) ; const size = canvas.width * canvas.height ; let index = 0 , x, y; for ( var i = 0 ; i < size; i++ ) { x = i % width; y = Math .floor ( i / width) ; pixels.data [ index++ ] = x; pixels.data [ index++ ] = y; pixels.data [ index++ ] = width - x; pixels.data [ index++ ] = 255 ; } c.putImageData ( pixels, 0 , 0 ) ;
Try it out…
This shows how to set pixel data on an html5 canvas.
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…