]>
git.scottworley.com Git - nt3d/blob - nt3d.js
f22d6763b6024a82e96e2a8c836ab47269c826da
1 /* nt3d - Javascript library for doing some 3D stuff.
2 * Copyright (C) 2012 Scott Worley <ScottWorley@ScottWorley.com>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
14 * You should have received a copy of the GNU Affero General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 triangle: function(a
, b
, c
) {
22 quad: function(a
, b
, c
, d
) {
23 return this.triangle(a
, b
, c
).concat(
24 this.triangle(c
, d
, a
));
31 cross: function(a
, b
) {
32 return [a
[1]*b
[2] - a
[2]*b
[1],
33 a
[2]*b
[0] - a
[0]*b
[2],
34 a
[0]*b
[1] - a
[1]*b
[0]];
36 normal: function(a
, b
, c
) {
37 return this.cross(this.sub(a
, b
), this.sub(b
, c
));
40 // Get params from form
42 for (var i
= 0; i
< this.user_params
.length
; i
++) {
43 params
[i
] = this.form
.elements
["param"+i
].value
;
47 this.points
= this.user_function
.apply(null, params
);
48 if (this.points
.length
% 3 != 0) {
49 alert("Points list length not divisble by 3!");
51 var n
= this.points
.length
/ 3;
54 this.stl
= "solid " + this.user_function
.name
+ "\n";
55 for (var i
= 0; i
< n
; i
++) {
56 var a
= this.points
[i
*3+0];
57 var b
= this.points
[i
*3+1];
58 var c
= this.points
[i
*3+2];
59 var normal
= this.normal(a
, b
, c
);
60 this.stl
+= "facet normal " + normal
[0] + " " + normal
[1] + " " + normal
[2] + "\n" +
62 "vertex " + a
[0] + " " + a
[1] + " " + a
[2] + "\n"+
63 "vertex " + b
[0] + " " + b
[1] + " " + b
[2] + "\n"+
64 "vertex " + c
[0] + " " + c
[1] + " " + c
[2] + "\n"+
68 this.stl
+= "endsolid " + this.user_function
.name
+ "\n";
70 // Remove any previous download links
71 var old_download_link
= document
.getElementById("nt3d_download");
72 if (old_download_link
) {
73 old_download_link
.parentNode
.removeChild(old_download_link
);
76 // Offer result as download
77 var download_link
= document
.createElement("a");
78 download_link
.appendChild(document
.createTextNode("Download!"));
79 download_link
.setAttribute("id", "nt3d_download");
80 download_link
.setAttribute("style", "background-color: blue");
81 download_link
.setAttribute("download", this.user_function
.name
+ ".stl");
82 download_link
.setAttribute("href", "data:application/sla," + encodeURIComponent(this.stl
));
83 this.ui
.appendChild(download_link
);
84 setTimeout(function() { download_link
.setAttribute("style", "-webkit-transition: background-color 0.4s; -moz-transition: background-color 0.4s; -o-transition: background-color 0.4s; -ms-transition: background-color 0.4s; transition: background-color 0.4s; background-color: inherit"); }, 0);
86 framework: function (f
, params
) {
87 this.user_function
= f
;
88 this.user_params
= params
;
91 this.ui
= document
.getElementById("nt3dui");
93 this.ui
= document
.createElement("div");
94 this.ui
.setAttribute("id", "nt3dui");
95 document
.body
.appendChild(this.ui
);
97 this.form
= document
.createElement("form");
98 this.ui
.appendChild(this.form
);
99 var table
= document
.createElement("table");
100 this.form
.appendChild(table
);
101 var tr
= document
.createElement("tr");
102 table
.appendChild(tr
);
103 var th
= document
.createElement("th");
104 th
.appendChild(document
.createTextNode("Variable"));
106 th
= document
.createElement("th");
107 th
.appendChild(document
.createTextNode("Value"));
109 for (var i
= 0; i
< params
.length
; i
++) {
110 tr
= document
.createElement("tr");
111 table
.appendChild(tr
);
112 var td
= document
.createElement("td");
113 td
.appendChild(document
.createTextNode(params
[i
][0]));
115 td
= document
.createElement("td");
116 var input
= document
.createElement("input");
117 input
.setAttribute("name", "param" + i
);
118 input
.setAttribute("value", params
[i
][1]);
119 td
.appendChild(input
);
122 var go
= document
.createElement("input");
123 go
.setAttribute("type", "button");
124 go
.setAttribute("value", "Go!");
125 go
.setAttribute("onclick", "nt3d.go()");
126 this.form
.appendChild(go
);