fskpf / svg2roughjs

Create sketchy, hand-drawn-like images from SVGs
https://fskpf.github.io/
MIT License
153 stars 13 forks source link

Deterministic randomness #94

Closed marcovo closed 1 year ago

marcovo commented 1 year ago

Is your feature request related to a problem? Please describe. The RoughJS library provides a seed option that allows one to deterministically generate randomization for the image. Unfortunately, this package only uses Math.random() such that generated images are undeterministic. I would love if an option could be added to make it deterministic depending on a seed, just like RoughJS itself provides that option.

Describe the solution you'd like A configurational option seed, if provided Math.random() should never be used but instead a random number sequence dependent on the seed should be used. Or perhaps, even the existing functionality of RoughJS could be reused?

Additional context Here the implementation of RoughJS can be found: https://github.com/rough-stuff/rough/blob/master/src/math.ts

marcovo commented 1 year ago

As a reference, where I am currently using it: https://gitlab.com/marcovo/plattekaart/-/blob/5e1721373b6dd5f362a928eb65ea6b22b4ada722/src/RouteTechniques/ImageGeneratingTechnique.ts#L112 With roughjs config defined in subclasses, e.g.: https://gitlab.com/marcovo/plattekaart/-/blob/5e1721373b6dd5f362a928eb65ea6b22b4ada722/src/RouteTechniques/BolletjePijltje.ts#L25

If you need any help please let me know. I am not an expert in type-/javascript but can always try to contribute. Thanks for the awesome package by the way; works like a charm, almost literally!

fskpf commented 1 year ago

Indeed, this has been missing. You could already remove the randomness by setting randomize = false and providing a seed with the roughConfig option but this would also result in non-random geometries (e.g. each rect has the very same stroking, hachure angle, multi-lining, ...).

I've added a seed property now (v3.1.0):

svg2roughjs.seed = 1234;

As you've suggested, the implementation uses Random from Rough.js since it is actually exported.

Nice to hear that you find the project useful! Let me know if the seed works for you.

marcovo commented 1 year ago

Works great, thanks very much!

https://gitlab.com/marcovo/plattekaart/-/commit/16fe6a85713f8d3c7caa3fc605654ce33a030df8

Indeed I did try to put randomize to false, but the randomness added by this logic is valuable, so having this seed functionality to be able to use the randomize option is great.