Skip to content

Instantly share code, notes, and snippets.

@zuk74
Last active October 16, 2016 13:13
Show Gist options
  • Select an option

  • Save zuk74/3a63d3163aeb2012a24847974b2d35fb to your computer and use it in GitHub Desktop.

Select an option

Save zuk74/3a63d3163aeb2012a24847974b2d35fb to your computer and use it in GitHub Desktop.
モノクロのpbmファイル読み込んでSVGで表示する。画像サイズが大きいとかなり処理が重い。
<!DOCTYPE html>
<html>
<head>
<title>sample</title>
</head>
<body>
<form name="test">
<input type="file" id="selfile"><br>
<textarea name="txt" rows="10" cols="50" readonly></textarea>
</form>
hogehoge<br/>
<div id="svg">
</div>
hogehoge<br/>
<!--
<svg>
<rect x="0" y="0" width="40" height="20" fill="blue"></rect>
</svg>
-->
<script>
var obj1 = document.getElementById("selfile");
obj1.addEventListener("change", function(evt) {
var file = evt.target.files;
var reader = new FileReader();
reader.readAsText(file[0]);
reader.onload = function(ev) {
// document.test.txt.value = reader.result;
var lines = reader.result.split("\n");
var len = lines.length;
// document.test.txt.value = len;
var i=0;
console.log("hoge");
//while (! lines[i].match(/^#/)) { i++; }
while (/^#/.test(lines[i]) ) { i++; }
var type = lines[i];
i++;
console.log("hogehoge");
// while (! lines[i].match(/^#/)) { i++; }
while (/^#/.test(lines[i]) ) { i++; }
var sizes = lines[i].split(/\s/);
i++;
document.test.txt.value = "type=" + type + ", size=" + sizes[0] + ":" + sizes[1] + "\n";
var datas = [];
for (; i<len; i++) {
datas = datas.concat(lines[i].split(/\s/));
// Array.prototype.push.array(datas, lines[i].split(/\s/));
}
console.log("hogehogehoge");
console.log( datas.length );
document.test.txt.value += datas.join(",");
var div_svg = document.getElementById("svg");
var svg = document.createElementNS("http://www.w3.org/2000/svg","svg");
//svg.setAttribute("width","400");
//svg.setAttribute("height","200");
var cnt = 0;
for (var y=0; y<sizes[1]; y++) {
for (var x=0; x<sizes[0]; x++) {
var rect = document.createElementNS("http://www.w3.org/2000/svg", "rect");
rect.setAttribute("x", 0 + x*10);
rect.setAttribute("y", 0 + y*10);
rect.setAttribute("width", "10");
rect.setAttribute("height", "10");
rect.setAttribute("fill", (datas[cnt]==1) ? "rgb(255,255,255)" : "rgb(0,0,0)" );
svg.appendChild(rect);
cnt++;
}
}
div_svg.appendChild(svg);
}
}, false);
// var div_svg = document.getElementById("svg");
// var svg = document.createElementNS("http://www.w3.org/2000/svg","svg");
// svg.setAttribute("width","400");
// svg.setAttribute("height","200");
//
// var rect = document.createElementNS("http://www.w3.org/2000/svg", "rect");
// rect.setAttribute("x", 0);
// rect.setAttribute("y", 0);
// rect.setAttribute("width", "40");
// rect.setAttribute("height", "40");
// rect.setAttribute("fill", "rgb(0,255,255)");
// svg.appendChild(rect);
//
// var rect = document.createElementNS("http://www.w3.org/2000/svg", "rect");
// rect.setAttribute("x", 40);
// rect.setAttribute("y", 40);
// rect.setAttribute("width", "40");
// rect.setAttribute("height", "40");
// rect.setAttribute("fill", "rgb(0,255,0)");
// svg.appendChild(rect);
//
// div_svg.appendChild(svg);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment