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.65k stars 514 forks source link

Export image #1243

Open sonichy opened 1 year ago

sonichy commented 1 year ago

Export image to jpg or png.

z3dev commented 1 year ago

@sonichy the focus of JSCAD is 3D printing, and therefore the common interchange formats are supported.

Do you want the CLI (command line tool) to export an image?

sonichy commented 1 year ago

Export image is a basic function of 3D application, it take a preview of a product. I tried js extension to turn canvas to image, but get a black image.

var canvas = document.getElementsByTagName('canvas')[0];
var link = document.createElement('a');
var fn = document.title + '_' + canvas.width + 'X' + canvas.height + '.png';
link.download = fn;
link.href = canvas.toDataURL("image/png");
link.click();

If not support, I had to use system screen capture instead, then use image editor to cut the image.

hrgdavor commented 1 year ago

one step towards a fix would be to set preserveDrawingBuffer to true for webgl context https://stackoverflow.com/questions/26783586/canvas-todataurl-returns-blank-image

after that your snippet would work likely. and then somebody could make a PR to add PNG as one of export options.

sonichy commented 1 year ago

Useless