Export as png #39

This commit is contained in:
Viktor Kovacs 2021-04-11 12:24:41 +02:00
parent 527234d47c
commit e9305ba33c
4 changed files with 75 additions and 44 deletions

View File

@ -378,22 +378,23 @@ OV.Viewer = class
this.scene.add (this.light);
}
GetImageSize ()
{
let originalSize = new THREE.Vector2 ();
this.renderer.getSize (originalSize);
return {
width : parseInt (originalSize.x, 10),
height : parseInt (originalSize.y, 10)
};
}
GetImageAsDataUrl (width, height)
{
let originalSize = null;
if (width && height) {
originalSize = new THREE.Vector2 ();
this.renderer.getSize (originalSize);
this.ResizeRenderer (width, height);
}
let originalSize = this.GetImageSize ();
this.ResizeRenderer (width, height);
this.Render ();
let url = this.renderer.domElement.toDataURL ();
if (originalSize !== null) {
this.ResizeRenderer (
parseInt (originalSize.x, 10),
parseInt (originalSize.y, 10)
);
}
this.ResizeRenderer (originalSize.width, originalSize.height);
return url;
}
};

View File

@ -1,3 +1,9 @@
OV.ExportType =
{
Model : 1,
Image : 2
};
OV.ExportDialog = class
{
constructor (callbacks)
@ -8,34 +14,41 @@ OV.ExportDialog = class
{
name : 'obj',
formats : [
{ name : 'text', format : OV.FileFormat.Text, extension : 'obj' }
{ name : 'text', type: OV.ExportType.Model, format : OV.FileFormat.Text, extension : 'obj' }
]
},
{
name : 'stl',
formats : [
{ name : 'text', format : OV.FileFormat.Text, extension : 'stl' },
{ name : 'binary', format : OV.FileFormat.Binary, extension : 'stl' }
{ name : 'text', type: OV.ExportType.Model, format : OV.FileFormat.Text, extension : 'stl' },
{ name : 'binary', type: OV.ExportType.Model, format : OV.FileFormat.Binary, extension : 'stl' }
]
},
{
name : 'ply',
formats : [
{ name : 'text', format : OV.FileFormat.Text, extension : 'ply' },
{ name : 'binary', format : OV.FileFormat.Binary, extension : 'ply' }
{ name : 'text', type: OV.ExportType.Model, format : OV.FileFormat.Text, extension : 'ply' },
{ name : 'binary', type: OV.ExportType.Model, format : OV.FileFormat.Binary, extension : 'ply' }
]
},
{
name : 'gltf',
formats : [
{ name : 'text', format : OV.FileFormat.Text, extension : 'gltf' },
{ name : 'binary', format : OV.FileFormat.Binary, extension : 'glb' }
{ name : 'text', type: OV.ExportType.Model, format : OV.FileFormat.Text, extension : 'gltf' },
{ name : 'binary', type: OV.ExportType.Model, format : OV.FileFormat.Binary, extension : 'glb' }
]
},
{
name : 'off',
formats : [
{ name : 'text', format : OV.FileFormat.Text, extension : 'off' }
{ name : 'text', type: OV.ExportType.Model, format : OV.FileFormat.Text, extension : 'off' }
]
},
{
name : 'png',
formats : [
{ name : 'current size', type: OV.ExportType.Image, width : null, height : null, extension : 'png' },
{ name : 'fixed size (1920x1080)', type: OV.ExportType.Image, width : 1920, height : 1080, extension : 'png' }
]
}
];
@ -46,7 +59,7 @@ OV.ExportDialog = class
};
}
Show (model)
Show (model, viewer)
{
if (model === null) {
let messageDialog = OV.ShowMessageDialog (
@ -76,7 +89,7 @@ OV.ExportDialog = class
return;
}
mainDialog.Hide ();
obj.ExportFormat (model);
obj.ExportFormat (model, viewer);
}
}
]);
@ -131,30 +144,41 @@ OV.ExportDialog = class
}
}
ExportFormat (model)
ExportFormat (model, viewer)
{
let format = this.formatParameters.selectedFormat;
if (format === null) {
let selectedFormat = this.formatParameters.selectedFormat;
if (selectedFormat === null) {
return;
}
let obj = this;
let progressDialog = new OV.ProgressDialog ();
progressDialog.Show ('Exporting Model');
OV.RunTaskAsync (function () {
let exporter = new OV.Exporter ();
let files = exporter.Export (model, format.format, format.extension);
if (files.length === 0) {
progressDialog.Hide ();
} else if (files.length === 1) {
progressDialog.Hide ();
let file = files[0];
OV.DownloadArrayBufferAsFile (file.GetContent (), file.GetName ());
} else if (files.length > 1) {
progressDialog.Hide ();
obj.ShowExportedFiles (files);
if (selectedFormat.type === OV.ExportType.Model) {
let obj = this;
let progressDialog = new OV.ProgressDialog ();
progressDialog.Show ('Exporting Model');
OV.RunTaskAsync (function () {
let exporter = new OV.Exporter ();
let files = exporter.Export (model, selectedFormat.format, selectedFormat.extension);
if (files.length === 0) {
progressDialog.Hide ();
} else if (files.length === 1) {
progressDialog.Hide ();
let file = files[0];
OV.DownloadArrayBufferAsFile (file.GetContent (), file.GetName ());
} else if (files.length > 1) {
progressDialog.Hide ();
obj.ShowExportedFiles (files);
}
});
} else if (selectedFormat.type === OV.ExportType.Image) {
let url = null;
if (selectedFormat.width === null || selectedFormat.height === null) {
let size = viewer.GetImageSize ();
url = viewer.GetImageAsDataUrl (size.width, size.height);
} else {
url = viewer.GetImageAsDataUrl (selectedFormat.width, selectedFormat.height);
}
});
OV.DownloadUrlAsFile (url, 'model.' + selectedFormat.extension);
}
}
ShowExportedFiles (files)

View File

@ -100,16 +100,22 @@ OV.CopyToClipboard = function (text)
document.body.removeChild (input);
};
OV.DownloadArrayBufferAsFile = function (arrayBuffer, fileName)
OV.DownloadUrlAsFile = function (url, fileName)
{
let link = document.createElement ('a');
link.href = OV.CreateObjectUrl (arrayBuffer);
link.href = url;
link.download = fileName;
document.body.appendChild (link);
link.click ();
document.body.removeChild (link);
};
OV.DownloadArrayBufferAsFile = function (arrayBuffer, fileName)
{
let url = OV.CreateObjectUrl (arrayBuffer);
OV.DownloadUrlAsFile (url, fileName);
};
OV.CreateIconButton = function (iconName, hoverIconName, title, link)
{
let buttonLink = $('<a>');

View File

@ -285,7 +285,7 @@ OV.Website = class
obj.dialog = dialog;
}
});
exportDialog.Show (obj.model);
exportDialog.Show (obj.model, obj.viewer);
});
AddButton (this.toolbar, 'embed', 'Get embedding code', true, function () {
obj.dialog = OV.ShowEmbeddingDialog (importer, obj.importSettings, obj.viewer.GetCamera ());