Commit 985de4fe authored by santiaago's avatar santiaago
Browse files

add three handlers to show progress.

parent e15215b1
Loading
Loading
Loading
Loading
+7 −3
Original line number Diff line number Diff line
@@ -2,13 +2,14 @@ package main

import (
	"flag"
	"log"
	"net/http"
	"os"

	"github.com/taironas/route"
	"github.com/taironas/tinygraphs/controllers/checkerboard"
	"github.com/taironas/tinygraphs/controllers/isogrids"
	"github.com/taironas/tinygraphs/controllers/squares"
	"log"
	"net/http"
	"os"
)

var root = flag.String("root", "app", "file system path")
@@ -25,6 +26,9 @@ func main() {
	r.HandleFunc("/squares/[a-zA-Z0-9\\.]+/?", squares.Square)      //cached
	r.HandleFunc("/squares/[0-8]/[a-zA-Z0-9\\.]+/?", squares.Color) // cached

	r.HandleFunc("/isogrids/skeleton/[a-zA-Z0-9]+/?", isogrids.Skeleton)
	r.HandleFunc("/isogrids/diagonals/[a-zA-Z0-9]+/?", isogrids.Diagonals)
	r.HandleFunc("/isogrids/halfdiagonals/[a-zA-Z0-9]+/?", isogrids.HalfDiagonals)
	r.HandleFunc("/isogrids/[a-zA-Z0-9]+/?", isogrids.Isogrids)

	r.AddStaticResource(root)
+74 −18
Original line number Diff line number Diff line
@@ -4,17 +4,16 @@ import (
	"crypto/md5"
	"fmt"

	tgColors "github.com/taironas/tinygraphs/colors"
	"github.com/taironas/tinygraphs/draw"
	"github.com/taironas/tinygraphs/extract"
	// "github.com/taironas/tinygraphs/format"
	"io"
	"log"
	"net/http"

	tgColors "github.com/taironas/tinygraphs/colors"
	"github.com/taironas/tinygraphs/draw"
	"github.com/taironas/tinygraphs/extract"

	"github.com/taironas/tinygraphs/misc"
	"github.com/taironas/tinygraphs/write"
	//	"strings"
)

// Isogrids is the handler for /isogrids/[a-zA-Z0-9]+/?.
@@ -28,16 +27,29 @@ func Isogrids(w http.ResponseWriter, r *http.Request) {
		io.WriteString(h, id)
		key := fmt.Sprintf("%x", h.Sum(nil)[:])

		// e := `"` + key + `"`
		// w.Header().Set("Etag", e)
		// w.Header().Set("Cache-Control", "max-age=2592000") // 30 days
		colorMap := tgColors.MapOfColorPatterns()
		bg, err1 := extract.Background(r)
		if err1 != nil {
			bg = colorMap[0][0]
		}
		fg, err2 := extract.Foreground(r)
		if err2 != nil {
			fg = colorMap[0][1]
		}
		size := extract.Size(r)
		write.ImageSVG(w)
		draw.IsogridsSkeleton(w, key, bg, fg, size)
	}
}

		// if match := r.Header.Get("If-None-Match"); match != "" {
		// 	if strings.Contains(match, e) {
		// 		w.WriteHeader(http.StatusNotModified)
		// 		return
		// 	}
		// }
func Skeleton(w http.ResponseWriter, r *http.Request) {

	if id, err := misc.PermalinkString(r, 3); err != nil {
		log.Printf("error when extracting permalink id: %v", err)
	} else {
		h := md5.New()
		io.WriteString(h, id)
		key := fmt.Sprintf("%x", h.Sum(nil)[:])

		colorMap := tgColors.MapOfColorPatterns()
		bg, err1 := extract.Background(r)
@@ -49,11 +61,55 @@ func Isogrids(w http.ResponseWriter, r *http.Request) {
			fg = colorMap[0][1]
		}
		size := extract.Size(r)
		// if f := extract.Format(r); f == format.SVG {
		write.ImageSVG(w)
		// draw.Isogrids1(w, key, bg, fg, size)
		draw.HalfDiagonals(w, key, bg, fg, size)
		draw.IsogridsSkeleton(w, key, bg, fg, size)
	}
}

		// }
func Diagonals(w http.ResponseWriter, r *http.Request) {

	if id, err := misc.PermalinkString(r, 3); err != nil {
		log.Printf("error when extracting permalink id: %v", err)
	} else {
		h := md5.New()
		io.WriteString(h, id)
		key := fmt.Sprintf("%x", h.Sum(nil)[:])

		colorMap := tgColors.MapOfColorPatterns()
		bg, err1 := extract.Background(r)
		if err1 != nil {
			bg = colorMap[0][0]
		}
		fg, err2 := extract.Foreground(r)
		if err2 != nil {
			fg = colorMap[0][1]
		}
		size := extract.Size(r)
		write.ImageSVG(w)
		draw.Diagonals(w, key, bg, fg, size)
	}
}

func HalfDiagonals(w http.ResponseWriter, r *http.Request) {

	if id, err := misc.PermalinkString(r, 3); err != nil {
		log.Printf("error when extracting permalink id: %v", err)
	} else {
		h := md5.New()
		io.WriteString(h, id)
		key := fmt.Sprintf("%x", h.Sum(nil)[:])

		colorMap := tgColors.MapOfColorPatterns()
		bg, err1 := extract.Background(r)
		if err1 != nil {
			bg = colorMap[0][0]
		}
		fg, err2 := extract.Foreground(r)
		if err2 != nil {
			fg = colorMap[0][1]
		}
		size := extract.Size(r)
		write.ImageSVG(w)
		draw.HalfDiagonals(w, key, bg, fg, size)
	}
}
+48 −2
Original line number Diff line number Diff line
@@ -8,8 +8,8 @@ import (
	"github.com/ajstarks/svgo"
)

// NormalDiagonals builds an image with 10x10 grids of normal diagonals.
func NormalDiagonals(w http.ResponseWriter, key string, color1, color2 color.RGBA, size int) {
// Diagonals builds an image with 10x10 grids of normal diagonals.
func Diagonals(w http.ResponseWriter, key string, color1, color2 color.RGBA, size int) {
	canvas := svg.New(w)
	size = 400
	canvas.Start(size, size)
@@ -88,3 +88,49 @@ func HalfDiagonals(w http.ResponseWriter, key string, color1, color2 color.RGBA,

	canvas.End()
}

// Isogrids builds an image with 10x10 grids of half diagonals
func IsogridsSkeleton(w http.ResponseWriter, key string, color1, color2 color.RGBA, size int) {
	canvas := svg.New(w)
	size = 400
	canvas.Start(size, size)

	lines := 10
	fringeSize := size / lines

	// vertical lines
	for xL := 0; xL <= lines; xL++ {
		x := xL * fringeSize
		firstY := 0
		lastY := (lines) * fringeSize
		style := fmt.Sprintf("stroke:black;stroke-width:2; %s", fillFromRGBA(color2))
		if (xL % 2) != 0 {
			lastY = lastY - fringeSize/2
			firstY = fringeSize / 2
		}
		canvas.Line(x, firstY, x, lastY, style)
	}

	// y -- > x up right
	for xL := 0; xL <= 2*lines; xL++ {
		x := xL * fringeSize
		style := fmt.Sprintf("stroke:black;stroke-width:2; %s", fillFromRGBA(color2))
		xPrev := 0
		yPrev := (xL) * fringeSize
		if yPrev > 0 {
			canvas.Line(xPrev, yPrev, x*2, 0, style)
		}
	}
	// x --> y down right
	for xL := -2 * lines; xL <= 2*lines; xL++ {
		x := xL * fringeSize * 2
		style := fmt.Sprintf("stroke:black;stroke-width:2; %s", fillFromRGBA(color2))
		xPrev := lines * fringeSize
		yPrev := (lines - xL*2) * fringeSize
		if yPrev > 0 {
			canvas.Line(x, 0, xPrev, yPrev/2, style)
		}
	}

	canvas.End()
}