Skip to content

Instantly share code, notes, and snippets.

@okaq
Last active January 4, 2016 07:29
Show Gist options
  • Save okaq/8589492 to your computer and use it in GitHub Desktop.
Save okaq/8589492 to your computer and use it in GitHub Desktop.
// logo bitmap renderer
package main
import (
"encoding/json"
"fmt"
"log"
// "image"
"io"
"io/ioutil"
"net/http"
"os"
"runtime"
)
const (
JSON_PATH = "okaq_logo_01.json"
HTML_PATH = "unaki.html"
)
var (
Html_Data []byte
Json_Data []byte
)
type Unaki struct {
B []byte // input data from JSON
// pipe?
}
func (unaki *Unaki) Read([]byte) (n int, err error) {
return 0, nil
}
func (unaki *Unaki) ReadInto() (n int, err error) {
// read into Unaki.B
return 0, nil
}
func genHandler(w http.ResponseWriter, req *http.Request) {
fmt.Printf("%s,%s,%s\n", req.Method, req.URL, req.Host)
// io.WriteString(w, "ok unaki genHandler")
w.Write(Html_Data)
}
func disHandler(w http.ResponseWriter, req *http.Request) {
fmt.Printf("%s,%s,%s\n", req.Method, req.URL, req.Host)
if req.Method == "POST" {
err := req.ParseMultipartForm(1024)
if err != nil {
fmt.Println(err)
}
fmt.Printf("%s\n", req.Form["data01"])
fmt.Printf("%s\n", req.RequestURI)
h0 := req.Header
for k, v := range h0 {
fmt.Printf("%s:%s\n", k, v)
}
f0 := req.Form
for k, v := range f0 {
fmt.Printf("%s:%s\n", k, v)
}
b0, err := json.Marshal(f0)
if err != nil {
fmt.Printf("json err: %s\n", err)
}
fmt.Println(b0)
Json_Data = b0
Json()
io.WriteString(w, "ok dis post\n")
return
}
io.WriteString(w, "ok unaki disHandler\n")
}
func Servers() {
http.HandleFunc("/gen", genHandler)
http.HandleFunc("/dis", disHandler)
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func Html() {
var err error
Html_Data, err = ioutil.ReadFile(HTML_PATH)
if err != nil {
log.Fatal(err)
}
}
func Json() {
err := ioutil.WriteFile(JSON_PATH, Json_Data, 0644)
if err != nil {
log.Fatal(err)
}
}
func main() {
fmt.Println("ok unaki. bitmap renderer")
n0 := runtime.NumCPU()
fmt.Printf("Number CPUs: %d\n", n0)
runtime.GOMAXPROCS(n0 + 1)
fmt.Printf("GOMAXPROCS: %s\n", os.Getenv("GOMAXPROCS"))
Html()
Servers()
}
// read png, write json datauri
// gen png from html5 canvas2d
// websocket
// read input from chrome
// web input
// bitmap generator
// color bar
// click to save
// message tcp
// net.TCPConn
// port 8808
// paging
// bitmap sequences
// bitmap = [], len = 2^N
// bitseq = {0:bm0,1:bm1,2:bm2}
// wire format
// xhr send ArrayBufferView
// server side byte array (RGBA)
<!DOCTYPE html>
<html lang="en">
<head id="zeta">
<title>okaq - logo bitmap</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=1920,height=1080,initial-scale=1" />
<link rel="shortcut icon" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAIAAAD8GO2jAAAAMklEQVR4nGJ5yXCfgZaAiaamj1owasGoBaMWjFowasGoBaMWjFowasGoBVQEgAAAAP//S1MCC0+bcT4AAAAASUVORK5CYII=" />
<script type="text/javascript">
// ok
console.log("ok unaki!");
// xhr
var xhr = new XMLHttpRequest();
console.log(xhr);
xhr.onload = function(e) {
console.log(e);
console.log(this.responseText);
}
xhr.open("post", "http://localhost:8080/dis");
var fd = new FormData();
console.log(fd);
var b0 = [];
for (var i = 0; i < 8; i++) {
// var b1 = (Math.random() >= 0.5) ? true : false;
var b1 = Math.random();
b0.push(b1);
var s0 = "data0" + i;
console.log(s0);
fd.append(s0, b1);
}
// console.log(b0);
// fd.append("data", b0);
xhr.send(fd);
// async load
(function() {
var async_load = function() {
console.log("window loaded");
g.init();
}
window.addEventListener("load", async_load, false);
})();
// game
var g = {
"init": function() {
console.log("g is on");
x.init();
g.x = 8;
g.y = 8;
g.tot = g.x * g.y;
g.alpha = {};
g.beta = {};
g.beta.c = [];
g.beta.c[0] = c.rc();
g.beta.c[1] = c.rc();
g.beta.state = 0;
g.beta.click_handler = function(e) {
var t0 = g.beta.state;
var t1 = (t0 == 0) ? 1 : 0;
g.beta.state = t1;
render.draw();
g.harvest();
var fd = new FormData();
for (var i = 0; i < g.map.length; i++) {
fd.append(i, g.map[i]);
}
x.req.send(fd);
x.init();
}
lay.init();
lay.beta.can.addEventListener("click", g.beta.click_handler, false);
g.harvest();
render.draw();
},
"harvest": function() {
g.map = [];
for (var i = 0; i < lay.gamma.length; i++) {
g.map.push(lay.gamma[i].map[0]);
}
}
}
// layout
var lay = {
"init": function() {
console.log("lay away");
lay.alpha = document.getElementById("alpha");
lay.beta = new cell({can0:"beta"});
console.log(lay.beta.can);
lay.beta.style(96,64);
// lay.alpha.appendChild(lay.beta.can);
lay.pop();
},
"pop": function() {
lay.gamma = [];
var w1 = (908 / g.x) >>> 0;
var h1 = (908 / g.y) >>> 0;
for (var i = 0; i < g.tot; i++) {
var x0 = i % g.x;
var y0 = (i / g.x) >>> 0;
var x1 = x0 * w1 + 720;
var y1 = y0 * h1 + 32;
var c0 = new cell({w0:w1,h0:h1});
c0.style(x1,y1);
lay.gamma.push(c0);
}
}
}
// cell class
var cell = function(p0) {
this.top = 0;
this.left = 0;
var can1;
// console.log(p0);
if (p0["can0"] != null) {
can1 = document.getElementById(p0["can0"]);
this.w = can1.width;
this.h = can1.height;
this.can = can1;
} else {
can1 = document.createElement("canvas");
this.w = p0["w0"];
this.h = p0["h0"];
this.can = can1;
this.can.width = this.w;
this.can.height = this.h;
}
lay.alpha.appendChild(this.can);
this.con = this.can.getContext("2d");
var b0 = (Math.random() >= 0.5) ? true : false;
this.map = [];
this.map.push(b0);
this.click_handler = function(e) {
var b0 = this.map[0];
this.map[0] = !b0;
render.draw();
}
this.can.addEventListener("click", this.click_handler.bind(this), false);
this.style = function(x0,y0) {
this.can.style.position = "absolute";
this.top = y0;
this.left = x0;
this.can.style.top = this.top + "px";
this.can.style.left = this.left + "px";
}
}
// color class
var color = function() {
this.A = 1.0;
this.R = 255;
this.G = 255;
this.B = 255;
this.css = function() {
// css string
}
}
// color
var c = {
"rc": function(a1) {
var a0 = a1 || Math.random();
var r0 = [c.rb(),c.rb(),c.rb(),a0];
var s0 = r0.join(",");
var s1 = "rgba(" + s0 + ")";
return s1;
},
"rb": function() {
var b0 = (Math.random() * 255) >>> 0;
return b0;
}
}
// xhr
var x = {
"init": function() {
console.log("xhr live");
x.req = new XMLHttpRequest();
x.req.onload = function(e) {
console.log(this.responseText);
}
x.req.open("post", "http://localhost:8080/dis");
}
}
// render
var render = {
"draw": function() {
render.clear();
lay.beta.con.fillStyle = g.beta.c[g.beta.state];
lay.beta.con.fillRect(0,0,lay.beta.w,lay.beta.h);
var c0 = "rgba(0,0,0,1.0)"; // c.rc();
for (var i = 0; i < lay.gamma.length; i++) {
var c1 = c0;
if (!lay.gamma[i].map[0]) {
c1 = "rgba(255,255,255,1.0)";
}
lay.gamma[i].con.fillStyle = c1;
lay.gamma[i].con.fillRect(0,0,lay.gamma[i].w,lay.gamma[i].h);
}
},
"clear": function() {
lay.beta.can.width = lay.beta.can.width;
lay.beta.con.fillStyle = "rgba(255,255,255,1.0)";
lay.beta.con.fillRect(0,0,lay.beta.w,lay.beta.height);
for (var i = 0; i < lay.gamma.length; i++) {
lay.gamma[i].can.width = lay.gamma[i].can.width;
lay.gamma[i].con.fillStyle = "rgba(255,255,255,1.0)";
lay.gamma[i].con.fillRect(0,0,lay.gamma[i].w,lay.gamma[i].height);
}
}
}
</script>
</head>
<body id="alpha">
<canvas id="beta" width="512" height="512"></canvas>
</body>
</html>
{"0":["false"],"1":["false"],"10":["true"],"11":["false"],"12":["true"],"13":["true"],"14":["true"],"15":["true"],"16":["true"],"17":["true"],"18":["false"],"19":["false"],"2":["false"],"20":["true"],"21":["false"],"22":["true"],"23":["true"],"24":["false"],"25":["false"],"26":["true"],"27":["false"],"28":["true"],"29":["true"],"3":["true"],"30":["true"],"31":["false"],"32":["false"],"33":["false"],"34":["false"],"35":["true"],"36":["false"],"37":["false"],"38":["false"],"39":["true"],"4":["true"],"40":["true"],"41":["false"],"42":["false"],"43":["true"],"44":["false"],"45":["false"],"46":["true"],"47":["false"],"48":["false"],"49":["false"],"5":["false"],"50":["true"],"51":["true"],"52":["false"],"53":["false"],"54":["false"],"55":["true"],"56":["false"],"57":["true"],"58":["false"],"59":["false"],"6":["false"],"60":["false"],"61":["false"],"62":["true"],"63":["false"],"7":["true"],"8":["true"],"9":["true"]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment