Skip to content

Instantly share code, notes, and snippets.

@divanvisagie
Created March 15, 2013 12:45
Show Gist options
  • Save divanvisagie/5169652 to your computer and use it in GitHub Desktop.
Save divanvisagie/5169652 to your computer and use it in GitHub Desktop.
Untitled
.lit{
background:#55ee55;
}
.table-restrict {
width:18em;
height:18em;
}
td{
padding:3;
margin:auto;
width:16px;
height:16px;
background:#444;
box-sizing:border-box;
}
.rnd > tbody > tr > td{
border-radius:50%;
}
.table{
width:100%;
height: 100%;
background:black;
border-style: solid;
}
.inline{
display:inline-block;
}
textarea{
width: 20em;
height: 15em;
}
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<link href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css" rel="stylesheet" type="text/css" />
<script src="http://twitter.github.com/bootstrap/assets/js/bootstrap.js"></script>
<meta charset=utf-8 />
<title>M8trix</title>
</head>
<body>
<div class="hero-unit">
<div class="btn-group inline" data-toggle="buttons-checkbox">
<button class="btn" onclick="roundCorners()">Rounded</button>
</div>
<button id="clear" class="btn inline">Clear</button>
<div id="out-type" class="btn-group inline" data-toggle="buttons-radio">
<button id="bin" type="button" class="btn">Binary</button>
<button type="button" class="btn">Bytes</button>
<button type="button" class="btn">Hex</button>
</div>
<div class="table-restrict ">
<table id="matrix" class="table table-bordered " > </table>
</div>
<div class="inline">
<textarea id="output" ></textarea>
</div>
</div>
</body>
</html>
var matrixValues = [
['0','0','0','0','0','0','0','0'],
['0','0','0','0','0','0','0','0'],
['0','0','0','0','0','0','0','0'],
['0','0','0','0','0','0','0','0'],
['0','0','0','0','0','0','0','0'],
['0','0','0','0','0','0','0','0'],
['0','0','0','0','0','0','0','0'],
['0','0','0','0','0','0','0','0']
];
var output_type = 'Binary';
function updateOut(){
console.log('-');
var outstring = '';
for ( var i in matrixValues ){
var r = '';
if ( output_type === 'Binary' ){
r += '\"';
for ( var o in matrixValues[i] ){
r += matrixValues[i][o];
}
r += '\"';
if ( i != 7 ){
r += ',\n';
}
}
else if ( output_type === 'Hex' ){
var binstring = '';
for ( var o in matrixValues[i] ){
binstring += matrixValues[i][o];
}
r+= '0x'+ parseInt( binstring, 2 ).toString(16) ;
if ( i != 7 ){
r += ',\n';
}
}
else if ( output_type === 'Bytes' ){
var binstring = 'B';
for ( var o in matrixValues[i] ){
binstring += matrixValues[i][o];
}
r+= binstring ;
if ( i != 7 ){
r += ',\n';
}
}
outstring += r;
}
console.log( outstring );
document.querySelector( '#output' ).innerHTML = outstring;
}
var body = document.createElement( 'tbody' );
document.querySelector( '#matrix' ).appendChild( body );
for ( var i = 0 ; i < 8; i++ ){
var row = document.createElement( 'tr' );
for ( var x = 0; x < 8;x++ ){
var col = document.createElement( 'td' );
col.innerHTML = 0;
row.appendChild( col );
}
body.appendChild( row );
}
$( '#matrix' ).on( 'click', 'td', function( ev ){
$( ev.target ).toggleClass('lit') ;
if ( ev.target.innerHTML == 1 )
ev.target.innerHTML = 0;
else
ev.target.innerHTML = 1;
var a = ev.delegateTarget.firstChild.children;
/* console.log( Object.getOwnPropertyNames( ev.toElement.parentNode ) ); */
var x = ev.toElement.parentNode.rowIndex;
var y = ev.toElement.cellIndex;
matrixValues[x][y] = ev.target.innerHTML;
updateOut();
});
$( '#clear' ).on( 'click', function( ev ){
var tbody = $( '#matrix' )[0].children[0] ;
console.log( $( '#matrix' )[0].children[0] );
for ( var x = 0; x < 8; x++ ){
console.log( tbody.children[x].firstChild );
for ( var y = 0; y < 8; y++ ){
var el = tbody.children[x].children[y];
matrixValues[x][y] = 0;
$( el ).removeClass( 'lit' );
el.innerHTML = 0;
}
}
updateOut();
});
$( '#bin' ).button( 'toggle' );
$( '#out-type' ).on( 'click', 'button', function( ev ){
output_type = ev.target.innerHTML;
updateOut();
} );
function roundCorners(){
$( '#matrix' ).toggleClass( 'rnd' );
}
{"view":"split-vertical","fontsize":"100","seethrough":"","prefixfree":"1","page":"javascript"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment