I've an obj file placed in a p5.js WEBGL canvas. I'd like to make the 3D model downloadable through a html download attribute link.
I tried:
<a href="SnowstormAloe.obj" download="Snowstorm Aloe .obj">Download OBJ</a>
which of course doesn't work because the html download attribute doesn't include this type of file. Any idea on how to make an .obj downloadable?
Thanks!
If you generate the model in the client, you can create a download link by converting the file content into a Blob
and then create a link using URL.createObjectURL
.
Like this
const objFileContent = `# Viewpoint Datalabs International, Inc. Copyright 1996
mtllib ./vp.mtl
g
v 2.712726 -2.398764 -2.492640
v 2.712726 -1.954302 -2.665440
v -5.975275 -1.954302 -2.665440
v -5.975275 -2.398764 -2.492640
v -6.113514 -1.885536 -2.803680
v 2.712726 -1.885536 -2.803680
v -5.975275 -1.372307 -2.803680
v -5.975275 -1.816770 -2.700000
v 2.712726 -1.816770 -2.700000
v 2.712726 -1.372307 -2.803680
v 4.766168 -2.256987 -2.354400
v 4.766168 -1.372307 -2.665439
v 4.766168 -1.769892 -2.561759
v 4.766168 -1.827445 -2.665439
v 4.766168 -1.884998 -2.527199
v 6.335766 -1.688939 -2.354399
v 6.335766 -1.732171 -2.458079
v 6.335766 -1.775403 -2.319839
v 6.335766 -2.054828 -2.147039
v 6.335766 -1.372307 -2.458079
v 8.078169 -1.372308 -2.043359
v 7.641026 -1.604039 -1.939679
v 7.711631 -1.639892 -2.043359
v 7.505756 -1.675745 -1.905119
v 7.068614 -1.907476 -1.732319
....`
document.querySelector('button').addEventListener('click', () => {
var link = document.createElement('a');
link.download = 'SnowstormAloe.obj';
var blob = new Blob([objFileContent], {
type: 'application/object'
});
link.href = URL.createObjectURL(blob);
document.body.appendChild(link);
link.click();
});
<button>Download .obj file</button>
Working example: https://output.jsbin.com/nugunit.