Skip to content

Instantly share code, notes, and snippets.

@LuRsT
Created September 16, 2012 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LuRsT/3732082 to your computer and use it in GitHub Desktop.
Save LuRsT/3732082 to your computer and use it in GitHub Desktop.
Generate cool background images ( static black ) like twitter bar
<html>
<head>
<!-- Need pnglib http://www.xarg.org/download/pnglib.js -->
<script src="pnglib.js"></script>
<script>
// Change value of textarea to previous executed code
window.onload = function () {
if ( get_cookie("code") ) {
code.value = get_cookie("code");
}
}
function get_cookie ( cookie_name ) {
var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
if ( results )
return ( unescape ( results[2] ) );
else
return null;
}
function delete_cookie ( cookie_name ) {
var cookie_date = new Date ( ); // current date & time
cookie_date.setTime ( cookie_date.getTime() - 1 );
document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}
function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure ) {
var cookie_string = name + "=" + escape ( value );
if ( exp_y ) {
var expires = new Date ( exp_y, exp_m, exp_d );
cookie_string += "; expires=" + expires.toGMTString();
}
if ( path )
cookie_string += "; path=" + escape ( path );
if ( domain )
cookie_string += "; domain=" + escape ( domain );
if ( secure )
cookie_string += "; secure";
document.cookie = cookie_string;
}
function create_image() {
// Gotta put this in textboxes
var width = 200; //document.getElementById("width").value;
var height = 200; //document.getElementById("height").value;
// Create image
var p = new PNGlib(width, height, 256);
var background = p.color(0, 0, 0, 0);
// delete cookie to refresh value
if ( get_cookie("code") ) {
delete_cookie("code");
}
// Border red if javascript error
code.className += " error";
// execute code to draw image
eval(code.value);
// Store code in cookie
set_cookie("code", code.value, 2012, 12, 15 );
// Print image and back button
document.write('<img src="data:image/png;base64,'+p.getBase64()+'">');
document.write('<br /><button onClick="history.back(1);">Back</button>');
return false;
}
</script>
<style>
body * {
font-family: Verdana;
font-size: 13px;
}
#code {
height: 500px;
width: 1000px;
border: 1px solid black;
}
#code.error {
border-color: red;
}
</style>
</head>
<body>
<!-- Later use form to comply with chrome -->
<!-- <form name="myform" action="" onSubmit="return create_image();"> -->
<textarea name="code" id="code">
for (var i = 0; i < 200; i++) {
for (var ii = 0; ii < 200; ii++) {
var colour = (Math.random() * 6) + 33;
p.buffer[p.index(i, ii)] = p.color(colour, colour, colour);
}
}
</textarea>
<br />
<input type="submit" onClick="return create_image();" value="Cria-me uma imagem!"></input>
<!-- </form> -->
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment