bwiggs / go-nexrad

NEXRAD Data Processing with Go
https://bwiggs.com/projects/go-nexrad/
MIT License
26 stars 12 forks source link

WASM Webapp and initial support for SVG rendering #9

Open SteepAtticStairs opened 2 years ago

SteepAtticStairs commented 2 years ago

WEBAPP I have made a super rudimentary version of this project using wasm, and it was taken almost entirely from go-wasm-pdfcpu.

To build the nexrad.wasm file yourself, run

git clone https://github.com/bwiggs/go-nexrad.git
cd go-nexrad && cd cmd && cd nexrad-render

GOOS=js GOARCH=wasm go build -o nexrad.wasm

I struggled a while on Go 1.18 because I was getting some errors, but then I downgraded to 1.12 and it worked fine. Specifically go version go1.12 darwin/amd64.

Then put the binary under the webapp directory I have made:

git clone https://github.com/SteepAtticStairs/go-nexrad.git
cd go-nexrad && cd webapp

These instructions are just if you want to build it yourself. A live demo of what I (but mostly the author of go-wasm-pdfcpu) have done can be found here: https://steepatticstairs.github.io/go-nexrad/webapp/

I plan to do a lot more cleanup.

VECTOR RENDERING

This is very scuffed code, forgive me - I mainly work with JavaScript, not Go! But this is a basic implementation of vector rendering using the draw2d library. By passing the -v flag with arguments png or svg (png is default), it will generate the relevant file.

However, I think there are three major issues with the code I have written. First, the file size of these svg files are extremely large, hovering around 70 MB. I have tried to reduce the DPI, the canvas size, but nothing has worked, so I would like to try and work out a solution in the future. Any suggestions on combatting this issue would be welcome.

Second issue (fixed for now I think??) Second, the svg image has a slight problem with rendering the pixels closest to the origin. For example, when I generate an svg file from `KLZK20220607_194517_V06` by doing `go run . -f testdata/KLZK20220607_194517_V06 -p ref -v svg`, I get this error right in the middle of the image: Screen Shot 2022-06-07 at 6 07 36 PM As you can see, there is a ring of thick pixels that shouldn't be there. This is **NOT** an issue with PNG rendering.

Third, the variables canvas and gc have been replaced with PNGcanvas PNGgc and SVGcanvas SVGgc, and I will explain that here. I wanted to use an if statement, basically giving these variables their proper arguments if the user passed png or svg. For example, the correct canvas variable for an svg render would be

canvas := draw2dsvg.NewSvg()

while the correct variable for a png render would be

canvas := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))

I wanted to set these dynamically with an if statement, ideally something like this:

if vectorize == "png" {
    canvas := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
    draw.Draw(canvas, canvas.Bounds(), image.Black, image.ZP, draw.Src)
    gc := draw2dimg.NewGraphicContext(canvas)
} else if vectorize == "svg" {
    canvas := draw2dsvg.NewSvg()
    canvas.Width = strconv.Itoa(int(width)) + "px"
    canvas.Height = strconv.Itoa(int(width)) + "px"
    gc := draw2dsvg.NewGraphicContext(canvas)
}

However, the issue with this is that each of these variables using :=, which obviously means that they are declared and set in the same line, where they don't require you to specify a type. This will not work here, because you need to define the variables outside of the if statement. You could do this by writing something like

var canvas *image.RGBA;
var gc *draw2dimg.GraphicContext;

if vectorize == "png" {
    canvas = image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
    draw.Draw(canvas, canvas.Bounds(), image.Black, image.ZP, draw.Src)
    gc = draw2dimg.NewGraphicContext(canvas)
}

but this would mean you would have to change the types of canvas and gc for the case that the user renders an svg.

My solution to this, of course, was to create two separate draw2d objects that the program will draw to simultaneously, but only one will get exported to a file šŸ˜‚. This, of course, is not the best way to do it, because it slows down the program and it looks quite ugly. Again, any suggestions on combatting this issue would be welcome.

Andrew

SteepAtticStairs commented 2 years ago

June 10, 2022 10:30 PM EST

See the original PR comment for more specific details. This comment is meant to provide an easy way to look at my changes, so you don't only have to go digging through my super messy and unnecessarily plentiful commits.

TOTAL COMMITS SO FAR: 15 šŸ¤¦ā€ā™‚ļø

Summary of PR so far:

bwiggs commented 2 years ago

Sorry about this getting closed. I've update the repo to use main as the new default branch.

SteepAtticStairs commented 2 years ago

June 14, 2022 11:15 PM EST

See the original PR comment for more specific details. This comment is meant to provide an easy way to look at my changes, so you don't only have to go digging through my super messy and unnecessarily plentiful commits.

TOTAL COMMITS SO FAR: 34 šŸ¤¦ā€ā™‚ļø

Summary of PR so far (things that have changed / been added since the last summary comment):