forked from sketchfab/experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
220 lines (202 loc) · 8.12 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Sketchfab Viewer Configurator</title>
<link rel="stylesheet" href="../styles/sketchfab.css" media="screen">
<link rel="stylesheet" type="text/css" href="style/app.css">
<script type="text/javascript" src="https://static.sketchfab.com/api/sketchfab-viewer-1.1.0.js"></script>
<script>
(function (i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r;
i[r] = i[r] || function () {
(i[r].q = i[r].q || []).push(arguments)
}, i[r].l = 1 * new Date();
a = s.createElement(o),
m = s.getElementsByTagName(o)[0];
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m)
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
ga('create', 'UA-22680456-16', 'auto');
ga('send', 'pageview');
</script>
</head>
<body>
<header class="header">
<h1 class="header--logo">
<a href="../">
<img src="../assets/sketchfab-logo.svg" alt="Sketchfab" width="120" class="header--image">
<span>Labs Experiments</span>
</a>
</h1>
</header>
<div class="app">
<div class="panel">
<div class="viewer">
<iframe src="" id="api-frame" allowfullscreen mozallowfullscreen="true" webkitallowfullscreen="true" width="640" height="480"></iframe>
<form class="options"></form>
</div>
<div class="sidebar">
<p>This is an example of a 3D product configurator made with the Sketchfab Viewer API in JavaScript.</p>
<ul>
<li>
<a href="https://github.com/sketchfab/experiments/tree/master/configurator">Source code</a>
</li>
<li>
<a href="https://sketchfab.com/developers/viewer">API documentation</a>
</li>
</ul>
</div>
</div>
</div>
<script>
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var iframe = document.getElementById('api-frame');
var url = getParameterByName('urlid');
var prefix = getParameterByName('prefix');
var DEFAULT_URLID = 'c632823b6c204797bd9b95dbd9f53a06';
var DEFAULT_PREFIX = 'seat ';
var CONFIG = {
urlid: url !== '' ? url : DEFAULT_URLID,
prefix: prefix !== '' ? prefix : DEFAULT_PREFIX
};
var Configurator = {
api: null,
config: null,
options: [],
/**
* Initialize viewer
*/
init: function (config, iframe) {
this.config = config;
var client = new Sketchfab(iframe);
client.init(config.urlid, {
ui_infos: 0,
ui_controls: 0,
graph_optimizer: 0,
success: function onSuccess(api) {
api.start();
api.addEventListener('viewerready', function () {
this.api = api;
this.initializeOptions(function () {
console.log('Found the following options:', this.options);
this.selectOption(0);
UI.init(this.config, this.options);
}.bind(this));
}.bind(this));
}.bind(this),
error: function onError() {
console.log('Viewer error');
}
});
},
/**
* Initialize options from scene
*/
initializeOptions: function initializeOptions(callback) {
this.api.getNodeMap(function (err, nodes) {
if (err) {
console.error(err);
return;
}
var node;
var isOptionObject = false;
var keys = Object.keys(nodes);
for (var i = 0; i < keys.length; i++) {
node = nodes[keys[i]];
isOptionObject = node.name &&
node.name.indexOf(this.config.prefix) !== -1 &&
(node.type === 'Geometry' || node.type === 'Group');
if (isOptionObject) {
this.options.push({
id: node.instanceID,
name: node.name,
selected: false
});
}
}
callback();
}.bind(this));
},
/**
* Select option to show
*/
selectOption: function selectOption(index) {
var options = this.options;
for (var i = 0, l = options.length; i < l; i++) {
if (i === index) {
options[i].selected = true;
this.api.show(options[i].id);
} else {
options[i].selected = false;
this.api.hide(options[i].id);
}
}
}
}
var UI = {
config: null,
options: null,
init: function init(config, options) {
this.config = config;
this.options = options;
this.el = document.querySelector('.options');
this.render();
this.el.addEventListener('change', function (e) {
e.preventDefault();
var index = parseInt(this.el.elements['color'].value, 10);
this.select(index);
}.bind(this));
},
select: function (index) {
Configurator.selectOption(parseInt(index, 10));
this.render();
},
render: function () {
if (this.config.urlid === DEFAULT_URLID) {
this.renderRadio();
} else {
this.renderSelect();
}
},
/**
* Render options as multiple `<input type="radio">`
*/
renderRadio: function render() {
var html = this.options.map(function (option, i) {
var checkedState = option.selected ? 'checked="checked"' : '';
var className = option.name.replace(this.config.prefix, '');
return [
'<label class="options__option">',
'<input type="radio" name="color" value="' + i + '" ' + checkedState + '>',
'<span class="' + className + '">' + option.name + '</span>',
'</label>'
].join('');
}.bind(this)).join('');
this.el.innerHTML = html;
},
/**
* Render option as `<select>`
*/
renderSelect: function () {
var html = this.options.map(function (option, i) {
var checkedState = option.selected ? 'selected="selected"' : '';
return [
'<option value="' + i + '" ' + checkedState + '>',
option.name,
'</option>',
].join('');
}).join('');
this.el.innerHTML = '<select name="color">' + html + '</select>';
}
}
Configurator.init(CONFIG, iframe);
</script>
</body>
</html>