jscad / OpenJSCAD.org

JSCAD is an open source set of modular, browser and command line tools for creating parametric 2D and 3D designs with JavaScript code. It provides a quick, precise and reproducible method for generating 3D models, and is especially useful for 3D printing applications.
https://openjscad.xyz/
MIT License
2.61k stars 511 forks source link

Promises in jscad file #396

Open profOnno opened 5 years ago

profOnno commented 5 years ago

Can we have promises in jscad code?

I've added it to my version of @jscad/core. I'm working on a opentype.js wrapper to make 3D text. I would be nice to fetch a font async.

Screenshot: screenshot of openjscad truetype font Screenshot, now in my hacky version, of csg-viewer: screenshot of openjscad truetype font

z3dev commented 5 years ago

@profOnno Wow! This is awesome! Thanks! The support for fonts/text is one of the last missing pieces of JSCAD. This will be very welcomed.

FYI, @lautr3k added support for single-line fonts in the CSG library. See PR #107 I think he may have a few comments and suggestions for this work.

z3dev commented 5 years ago

Promises versus Webworkers... the short answer is that data structures cannot be shared between threads (workers) in browsers. @kaosat-dev has already hit this everal times in the new UI, and there’s not much hope of an answer.

The desktop application does not have restrictions on the use of threads, promises, libraries, etc.

This is just the start of a long thread. 😃

profOnno commented 5 years ago

I'm not really sharing data structures. MDN has a nice page on that here, search for transferable objects. This 'transferable objects' looks like future technology. I'm keeping my data contained in the worker, and post the resulting CSS/CAG (same way as it works now).

Is it planned to have some kind of ttf text in the core? Or do you want to keep the core smaller, and use something like:

include('ttffont') //exposes libobject
const cags = TtfFont.cagFromString('hello world')

To be honest, I don't think it is a 'Promises versus Webworkers', it is only a way to make cag/csg async. The code changed for Promises works in both 'rebuildSolids' and 'rebuildSolidsInWorker' .

function main () {
  return new Promise ( (resolve, reject) => {
     setTimeout(() => resolve(cube({size: 20})), 2000);
  } )
}

I did a quick scan over the 'Hershey Vector Font' of @lautr3k and it's probably a lot lighter. There is no problem of having this living side by side, That is if your work flow uses the include statements to use which ever library you want.

So for the web version, I guess it would be great to have a library directory next to examples directory with an option to include common libraries (Text, connectors, nuts & bolds). But that is more an other topic.

ghost commented 5 years ago

Already done, but rejected by the team, reason? wait for v2... https://github.com/jscad/csg.js/pull/106

profOnno commented 5 years ago

Ai, re-inventing, nice that its already done..

If you use Promises you can however include the font you want, skipping the rest (using the params like params.myFont). At the moment you can't do (or am I missing something):

myFont = include(params.myFont);

The rebuildSolids flow is, to parse the script, replaceIncludes, then hand it over to the Worker. Meaning there is no way to use the font as parameter...? Maybe replaceIncludes can handle the params (when not using Promises)?

z3dev commented 5 years ago

The transferable objects looks promising. We will have to look at the possibility of using those more.

For more advanced designs, especially those that need additional libraries, we feel that V2 is the only solution. Adding bandaids to V1 isn’t going to allow enough flexibility, and maintaining both isn’t possible.

Having said that, we want as many ideas as possible for V2. I think @kaosat-dev has a short (ha ha) list of ideas how how to extend V2, and make the framework more extensible for other uses. Would you like to help?

kaosat-dev commented 5 years ago

Hi @profOnno , nice idea / prototype! a few things to note :

kaosat-dev commented 5 years ago

@profOnno about returning promises from main() I have nothing against it and I see the benefits, however it means your main could return:

quite a bit of possibilities : so would your case return either a single or an array of promises ? (our standard way of dealing with outputs is always to convert data coming from main() to arrays) I think we could add support and detect, resolve the promise. Even better would be to just support 'then-ables' , that would pave the way to supporting anything which can be async-awaited.

kaosat-dev commented 5 years ago

@lautr3k hey , sorry if we came off as too blunt on this ! We did not mean to diminish your insanely good work ! we are just undermanned, and we cannot realistically upgrade V1 and create V2 at the same time ! you csg.js changes have already been injected into V2 so nothing is lost !

z3dev commented 4 years ago

Already done, but rejected by the team, reason? wait for v2... jscad/csg.js#106

Actually, changes were requested, a new pull request was merged, adding support for simplex fonts. These changes are part of V1 and V2.

z3dev commented 3 years ago

@profOnno the ability to use promises within V2 designs is not supported yet. However, there is some hope. See #671

FYI, V2 JSCAD exposes the file system, just like running from the command line. Therefore, fonts and images can be loaded and used as part of V2 projects.

I posted an example at the User Group.

https://openjscad.nodebb.com/topic/188/access-to-the-fake-filesystem-in-web-applications