jakubfiala / atrament

A small JS library for beautiful drawing and handwriting on the HTML Canvas.
http://fiala.space/atrament/demo
MIT License
1.55k stars 116 forks source link

How to retain smoothing effect in SVG when programmatically creating SVG from stroke data #64

Closed jkhaui closed 1 year ago

jkhaui commented 3 years ago

Hey @jakubfiala,

Thanks for the great library - definitely the most realistic canvas drawing package I've come across.

I am working with another developer to export the canvas data created by Atrament to an SVG. We've achieved this by listening to the "stroke recorded" event and using the returned data to re-draw the image onto a specialised canvas provided by https://github.com/gliffy/canvas2svg, which can then be exported as an SVG.

It all works well except that the final SVG does not have the smoothing effect applied. Therefore, it does not look anywhere as good as the Atrament canvas drawing.

We think we could fix this by forking canvas2svg and integrating the smoothing functions of Atrament... Would this work? Do you have any other suggestions as to how we can apply the smoothing effect to the programmatically re-created SVG?

If we can get it working we can also integrate it as an API method of Atrament if this is a feature you'd want. Thanks!

jakubfiala commented 3 years ago

hey there @jkhaui - thank you for reaching out! I'm really glad you like Atrament; the use case you're describing is very interesting indeed. To replicate Atrament's drawing algorithm in SVG, you'd have to either reimplement the draw method yourselves, using the Q path command (equivalent to canvas' quadraticCurveTo) and keep track of all the other stateful variables (thickness, previous position).

In order for Atrament to support this use case out of the box, we'd have to modify the draw method so it calls a sort of "renderer-agnostic" draw function instead of directly calling the canvas methods. We'd have to have such a pattern for the quadraticCurveTo, stroke, beginPath, moveTo and closePath methods, as well as the colour change etc. You could then supply your own "hooks" for these methods, effectively replacing them with functions which just take the arguments and perform SVG operations instead.

In any case, I'd be totally happy for you to take the Atrament algorithm and reimplement it in your code. Let me know how it goes, and perhaps we can indeed make this a supported use case in the future :)

jakubfiala commented 1 year ago

going to close this as it's been a while. Feel free to reopen if needed, and thank you!