tilk / digitaljs

Teaching-focused digital circuit simulator
BSD 2-Clause "Simplified" License
659 stars 47 forks source link

Add a method to the circuit class, to export circuit SVG as url. #85

Open mateusz-sz opened 2 years ago

yuyichao commented 2 years ago

Note that unless jointjs's internal is changed, simply extracting the svg won't work due to the use of style sheets and foreign objects. (The resulting SVG renders differently compared to what was in the browser window).

Apparently this is something the paid version supports but not the free version. There was a google group post roughly how to fix those. See https://github.com/yuyichao/digitaljs_code/blob/75a49dd290f7c06a97c3b883133a3905b6e0afec/view/imgutils.mjs for what I needed to do along with other tweaks to achieve a good frame rectangle, minimum svg file size etc. A few aspects are still not very generic (size, style, assumptions on property names etc) but other bits should be reusable.

mateusz-sz commented 2 years ago

Hi @yuyichao thanks for your engagement! I am not sure if you're right or not. I've seen this google discussion you're talking about, and I'm aware of the "Rappid" paid JointJS plugin.

I exported SVG via digitaljs_online client. It seems like everything works well for me. I saved the exported SVG in a file and opened it in another browser, and the exported circuit looks good.

Do you think / suggest that this is not going to work if I'd use digitaljs without this client?

Here is the result of the export (above the simulation in the web client; below the exported SVG file): image

From above picture looks like all the styles have been preserved.

yuyichao commented 2 years ago

No, all the paths are preserved not all the styles.

You can see that the content of the input and output boxes are not rendered correctly, the buttons on the subcircuits are shown but shouldn't be there and so is the base selectors for input/output. The input/output are also foreign objects that I don't think works in non-browsers. They show up as black boxes for me in some viewers IIRC. (The font is also different but I'm not an expert on that...)

mateusz-sz commented 2 years ago

Okay I see the problem now. Removing base selectors and subcircuits buttons seems to be pretty easy. But replacing inputs and rendering them in a correct place is going to be more challenging.

Anyway – thank you for your input. I am going to work more on that and fix the mentioned issues.