Advertisement
mitchfizz05

ComputerCraft Javascript Image Renderer

Dec 15th, 2013
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     ComputerCraft Image file Javascript Renderer.
  3.      © Copyright 2013. Mitchfizz05.
  4.      
  5.     Version 1.1. Uses correct colors. Thanks theoriginalbit! ;)
  6.    
  7.     -=JAVASCRIPT FILE=-
  8. */
  9.  
  10. // Variables.
  11. var pixelWidth  = 10; // Width of each pixel.
  12. var pixelHeight = 14; // Height of each pixel.
  13. var zoom = 2; // Original sizes will be multiplied by this.
  14. var debug = false; // Recommended you keep false unless developing with this.
  15. var pixelBorders = false; // Debug option. Display borders around pixels. (May offset pixels by a very small amount)
  16.  
  17. // Init.
  18. pixelWidth = pixelWidth * zoom; // Zoom stuff in.
  19. pixelHeight = pixelHeight * zoom;
  20.  
  21. console.log("ComputerCraft Javascript Image Renderer init.");
  22.  
  23. // Running as a file on a harddrive?
  24. if ( document.URL.substring(0, "file://".length) == "file://" ) {
  25.     console.log("WARNING! Running on hard drive! Files can't be read while running on hard drive! Consider uploading to a webserver!");
  26. }
  27.  
  28. var colors = new Array(); // Resolves the characters in the CC image to colors for HTML.
  29. colors['0'] = '#f0f0f0';
  30. colors['1'] = '#f2b233';
  31. colors['2'] = '#e57fd8';
  32. colors['3'] = '#99b2f2';
  33. colors['4'] = '#dede6c';
  34. colors['5'] = '#7fcc19';
  35. colors['6'] = '#f2b2cc';
  36. colors['7'] = '#4c4c4c';
  37. colors['8'] = '#999999';
  38. colors['9'] = '#4c99b2';
  39. colors['a'] = '#b266e5';
  40. colors['b'] = '#253192';
  41. colors['c'] = '#7f664c';
  42. colors['d'] = '#57a64e';
  43. colors['e'] = '#cc4c4c';
  44. colors['f'] = '#191919';
  45.  
  46. // Functions.
  47. function renderPixel(canvas, color) {
  48.     if (debug == true) {
  49.         console.log("Drawing pixel. Canvas: " + canvas.id + ". Color: " + color)
  50.     }
  51.    
  52.     var extraStyles = '';
  53.     if (pixelBorders == true) {
  54.         extraStyles = extraStyles + ' border: solid 1px black; ';
  55.     }
  56.    
  57.     var append = "<div style=\"float: left; width: " + pixelWidth + "px; height: " + pixelHeight + "px; background-color: " + color + ";" + extraStyles + "\">";
  58.     var existing = canvas.innerHTML;
  59.     var newCanvas = existing + append;
  60.     canvas.innerHTML = newCanvas;
  61. }
  62.  
  63. function renderBlankPixel(canvas) {
  64.     if (debug == true) {
  65.         console.log("Drawing blank pixel. Canvas: " + canvas.id)
  66.     }
  67.     var append = "<div style=\"float: left; width: " + pixelWidth + "px; height: " + pixelHeight + "px;\">";
  68.     var existing = canvas.innerHTML;
  69.     var newCanvas = existing + append;
  70.     canvas.innerHTML = newCanvas;
  71. }
  72.  
  73. function nextLine(canvas) {
  74.     if (debug == true) {
  75.         console.log("Going to next line. Canvas: " + canvas.id);
  76.     }
  77.    
  78.     var newCanvas = canvas.innerHTML + "<br clear=\"both\">";
  79.     canvas.innerHTML = newCanvas;
  80. }
  81.  
  82. function renderImageRow(canvas, image) { // Takes a row of an image and renders it.
  83.    
  84.     for ( var i=0; i < image.length; i++ ) { // Loop through each character.
  85.         var pixel = image[i];
  86.         if ( pixel == " " ) { // We must render the correct type of pixel (transparrent and colored).
  87.             renderBlankPixel(canvas);
  88.         } else {
  89.             renderPixel(canvas, colors[pixel]); // Render the actual single pixel.
  90.         }
  91.     }
  92.    
  93. }
  94.  
  95. function renderImageFromFile(canvas, fileStr) { // Takes a file path and outputs the image.
  96.    
  97.     var image = new XMLHttpRequest();
  98.     image.open("GET", fileStr, true);
  99.     image.onreadystatechange = function() {
  100.         if (image.readyState === 4) {  // document is ready to parse.
  101.             if (image.status === 200) {  // file is found
  102.                 allText = image.responseText;
  103.                 lines = image.responseText.split("\n");
  104.                
  105.                 for (var i=0; i < lines.length; i++) {
  106.                     renderImageRow(canvas, lines[i]);
  107.                     nextLine(canvas);
  108.                 }
  109.                
  110.             }
  111.         }
  112.     }
  113.     image.send(null);
  114.    
  115. }
  116.  
  117. function clearCanvas(canvas) {
  118.     canvas.innerHTML = "";
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement