Permalink
Browse files

Users can load their own gcode by dropping files into the UI.

  • Loading branch information...
1 parent 88b1359 commit f8cc69b9fbbd709852a91f981d8edcfe300e9ad3 Joe Walnes committed Apr 1, 2012
Showing with 45 additions and 5 deletions.
  1. +6 −3 web/index.html
  2. +39 −2 web/viewer.js
View
@@ -68,10 +68,11 @@
<div class="modal-body">
<h4>Examples</h4>
<ul>
- <li><a href="javascript:openGCode('examples/15mm_cube.gcode')">15mm_cube.gcode</a></li>
- <li><a href="javascript:openGCode('examples/octocat.gcode')">octocat.gcode</a></li>
- <li><a href="javascript:openGCode('examples/part.gcode')">part.gcode</a></li>
+ <li><a href="javascript:openGCodeFromPath('examples/15mm_cube.gcode')">15mm_cube.gcode</a></li>
+ <li><a href="javascript:openGCodeFromPath('examples/octocat.gcode')">octocat.gcode</a></li>
+ <li><a href="javascript:openGCodeFromPath('examples/part.gcode')">part.gcode</a></li>
</ul>
+ <p>To view your own model, drag a gcode file from your desktop and drop it in this window.</p>
</div>
<div class="modal-footer">
<a class="btn" data-dismiss="modal">Cancel</a>
@@ -93,6 +94,8 @@
<p>This viewer shows the operations the machine will take.</p>
<p>Drag the mouse to rotate the model. Hold down 'S' to zoom.</p>
+
+ <p>To view your own model, drag a gcode file from your desktop and drop it in this window.</p>
</div>
<div class="modal-footer">
<a class="btn btn-primary" data-dismiss="modal">OK</a>
View
@@ -210,7 +210,7 @@ function openDialog() {
var scene = null;
var object = null;
-function openGCode(path) {
+function openGCodeFromPath(path) {
$('#openModal').modal('hide');
if (object) {
scene.remove(object);
@@ -219,9 +219,22 @@ function openGCode(path) {
object = createObjectFromGCode(gcode);
scene.add(object);
localStorage.setItem('last-loaded', path);
+ localStorage.removeItem('last-imported');
});
}
+function openGCodeFromText(gcode) {
+ $('#openModal').modal('hide');
+ if (object) {
+ scene.remove(object);
+ }
+ object = createObjectFromGCode(gcode);
+ scene.add(object);
+ localStorage.setItem('last-imported', gcode);
+ localStorage.removeItem('last-loaded');
+}
+
+
$(function() {
if (!Modernizr.webgl) {
@@ -240,7 +253,31 @@ $(function() {
setTimeout(about, 500);
}
+ // Drop files from desktop onto main page to import them.
+ $('body').on('dragover', function(event) {
+ event.stopPropagation();
+ event.preventDefault();
+ event.originalEvent.dataTransfer.dropEffect = 'copy'
+ }).on('drop', function(event) {
+ event.stopPropagation();
+ event.preventDefault();
+ var files = event.originalEvent.dataTransfer.files;
+ if (files.length > 0) {
+ var reader = new FileReader();
+ reader.onload = function() {
+ openGCodeFromText(reader.result);
+ };
+ reader.readAsText(files[0]);
+ }
+ });
+
scene = createScene($('#renderArea'));
- openGCode(localStorage.getItem('last-loaded') || 'examples/octocat.gcode');
+ var lastImported = localStorage.getItem('last-imported');
+ var lastLoaded = localStorage.getItem('last-loaded');
+ if (lastImported) {
+ openGCodeFromText(lastImported);
+ } else {
+ openGCodeFromPath(lastLoaded || 'examples/octocat.gcode');
+ }
});

0 comments on commit f8cc69b

Please sign in to comment.