Coder Social home page Coder Social logo

SVG file is enormous about draw2d HOT 8 OPEN

SteepAtticStairs avatar SteepAtticStairs commented on June 2, 2024
SVG file is enormous

from draw2d.

Comments (8)

drahoslove avatar drahoslove commented on June 2, 2024 1

@llgcode
I agree with your suggestions.
I hadn't really had an optimization for large files in my mind when I was implementing it.

A simple improvement would be to check whether the group attrs changed and reuse the last group if possible, instead of creating a new one every time.

The default values sounds good as well. We could apply values from first calls of SetStrokeColor, SetFillColor, SetLineWidth, SetLineCap etc. to SVG element itself. And later only create a group only if current values differ from those of the SVG tag. - If that is what you meant by the default values.

The css classes would be nice too, but a bit harder to implement.

I looked into the code and I have no idea where the LineTo comes from either.

@SteepAtticStairs
It looks like you are rendering a lot of transparent pieces (with stroke="rgba(0,0,0,0)") - you could detect those and not render them.
The images are very complex. I'm on the edge about whether svg format is more suitable for them compared to png.
Even if we implement some optimization, the result will probably still be larger than the gzipped version.

from draw2d.

SteepAtticStairs avatar SteepAtticStairs commented on June 2, 2024 1

I have pushed a commit to my project:

5f8a6d0

that prevents transparent colors (e.g. rgba() values that end in 0 which are transparent) from rendering, and I saw a drastic size differential, around 95 MB got reduced to around 60 MB.

I am also gzipping the resulting svg, because this does also reduce the size by an incredible amount.

It looks like you are rendering a lot of transparent pieces (with stroke="rgba(0,0,0,0)") - you could detect those and not render them.

@drahoslove FYI.

from draw2d.

llgcode avatar llgcode commented on June 2, 2024

Hi SteepAtticStairs,

I guess you need to reinitialize the path every time you have drawn something, so that the path don't grow on each loop.

Try to do a SVGgc.BeginPath() and PNGgc.BeginPath() at the start of the loop.

You could also implement a render function that take draw2d.GraphicContext as parameter so that you can pass a SVGgc or PNGgc and factorize some code.

Can you please attach your SVG to better understand the issue ?

regards

from draw2d.

SteepAtticStairs avatar SteepAtticStairs commented on June 2, 2024

radar.svg.gz
Here is the radar.svg file. I gzipped it because the original file was 95.8 MB, but the gzip reduced it to 14.3 MB. This seems like it would be a solution, to just gzip the svg, but then the user would have to unzip it and couldn't render the resulting large svg file.

I tried to do SVGgc.BeginPath() on the line right after I declared SVGgc, but didn't decrease the size. The svg file I have attached resulted from code without SVGgc.BeginPath().

I have also modified the code in my original comment to remove PNGgc, because that works fine. I am able to greatly control the size of a png file by setting the size of the canvas. Examples of this are down below.

1024 x 1024 508 KB

2048 x 2048 1.7 MB

4092 x 4092 5.3 MB

None of these above images have been compressed or processed at all, they are the raw output from draw2dimg.SaveToPngFile(out, PNGcanvas). To see the difference between the images, I would download them and zoom in.

from draw2d.

llgcode avatar llgcode commented on June 2, 2024

Thanks,
I've open the svg file. I see a lot of redundant information that could be factorize. For now, the svg generator do not factorize by itself.
I guess one way to factorize is to stroke only when color and lineWidth change. Be careful to not reinitialize the path, I was wrong about the BeginPath in earlier answer, the path is renitialized everytime you draw.
So you can factorize the stroke attribute written into a group. May be you can regroup "draw" that have the same stroke properties.

In our side we could factorize things by using css classes, default values or not using group everytime we draw.

What do you think @drahoslove ?

I see in the SVG path MoveTo and LineTo on the same point, and I couldn't figure out where does this LineTo comes from. It seems that the LineTo is not relevant here. What do you think?

from draw2d.

llgcode avatar llgcode commented on June 2, 2024

Thanks.
Do you want me to implement the optimisation?
Or you want to do it ?

from draw2d.

llgcode avatar llgcode commented on June 2, 2024

I found the redundant LineTo

draw2d/path.go

Line 129 in 80aa0a2

p.LineTo(startX, startY)

from draw2d.

drahoslove avatar drahoslove commented on June 2, 2024

Thanks. Do you want me to implement the optimisation? Or you want to do it ?

Please, feel free to implement it yourself.
I can review your code, once you are done, if you want me to.

I found the redundant LineTo

Good job!

from draw2d.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.