Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <p>Use this form to create a csv of weights for drawing an image using the Bigtable Key Visualizer. Upload the file to a public GCS bucket once generated. You can find more inforamtion in this blog post</p>
<form>
  <label for="imgInp">Upload image:</label>
  <input type='file' id="imgInp" />
  <br>
  
  <label for="hours">Hours to draw:</label>
  <input type="number" min="0" max="240" value="24" id="hours">
  </br>
  <input id="download" value="download" type="button">
  <input id="downloadAll" value="download all" type="button">
</form>
<canvas id="canvas" width="1" height="1"></canvas>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>



<span id="output"></span>
              
            
!

CSS

              
                #output {
    white-space: pre;
  font-family: Lucida Console, Courier, monospace;
}



              
            
!

JS

              
                let output;
let image;
let imgBase = "google";

// Commented number refers to the amount of tablets needed for that density of a portrait image. (default max is 1000 tablets and ~1GB per tablet)
const COMMON_HOURS = [
  1, // 7
  4, // 30
  8, // 56
  12, // 85
  24, // 170
  24 * 2, // 340
  24 * 3, // 512
  24 * 4, // 682
  24 * 5, // 850
  24 * 6, // 1024
];

function draw(hours) {
  let canvas = document.getElementById("canvas");
  let ctx = canvas.getContext("2d");

  let pixelsW = 4 * hours;
  let pixelsH = Math.round((image.height * pixelsW) / image.width);
  console.log(pixelsH);
  ctx.canvas.width = pixelsW;
  ctx.canvas.height = pixelsH;
  ctx.drawImage(image, 0, 0, pixelsW, pixelsH);

  let imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
  let data = imageData.data;

  for (var i = 0; i < data.length; i += 4) {
    var avg = (data[i] + data[i + 1] + data[i + 2]) / 3;
    data[i] = avg; // red
    data[i + 1] = avg; // green
    data[i + 2] = avg; // blue
  }
  let contrast = 0;
  contrast = contrast / 100 + 1; //convert to decimal & shift range: [0..2]
  var intercept = 128 * (1 - contrast);
  for (var i = 0; i < data.length; i += 4) {
    //r,g,b,a
    data[i] = data[i] * contrast + intercept;
    data[i + 1] = data[i + 1] * contrast + intercept;
    data[i + 2] = data[i + 2] * contrast + intercept;
  }

  output = "";
  ctx.putImageData(imageData, 0, 0);
  let outputData = data.filter((x, i) => i % 4 === 0);
  for (let i = 0; i < pixelsH; i++) {
    for (let j = 0; j < pixelsW; j++) {
      let val = outputData[i * pixelsW + j];
      val = 1 - (255 - val) / 255;
      output += val.toFixed(2) + ",";
    }
    output += "\n";
  }

  var downloadBtn = document.getElementById("download");
  downloadBtn.addEventListener("click", download);
  var downloadAllBtn = document.getElementById("downloadAll");
  downloadAllBtn.addEventListener("click", downloadAll);
}

function download() {
  let a = document.createElement("a");
  let file = new Blob([output], { type: "text/json" });
  let fileName = `${imgBase}_${$("#hours").val()}h.txt`;
  a.href = URL.createObjectURL(file);
  a.download = fileName;
  a.click();
}

function downloadAll() {
  COMMON_HOURS.forEach((hour) => {
    // console.log("hr" + hour);
    // draw(hour);
    $("#hours").val(hour).change();
    download();
  });
  $("#hours").val(24).change();
}

// Load Google Image to start script
drawUrl("https://i.imgur.com/OXS68pa.png");

// Read image data from and draw that on the canvas.
function drawUrl(url) {
  var img = new Image();
  img.crossOrigin = "Anonymous";
  img.src = url;
  img.onload = function () {
    image = this;
    draw(24);
  };
}

// Get uploaded img url and draw using it.
function drawUploadedImage(input) {
  if (input.files && input.files[0]) {
    var reader = new FileReader();

    reader.onload = function (e) {
      let url = e.target.result;

      drawUrl(url);
    };

    reader.readAsDataURL(input.files[0]);
  }
}

$("#imgInp").change(function () {
  imgBase = $(this).val().replace("C:\\fakepath\\", "").split(".")[0];
  drawUploadedImage(this);
});

$("#hours").change(function () {
  draw($(this).val());
});

              
            
!
999px

Console