From 77a58849e80ecf347aa1b9895f8451a484329a2c Mon Sep 17 00:00:00 2001 From: Wouter R Date: Wed, 23 Oct 2013 01:29:50 +0200 Subject: [PATCH] Load sketch path from svg (WIP, metadata is not parsed yet). --- js_src/buttonbehaviors.js | 32 ++++++++++++-- js_src/canvasDrawing.js | 91 +++++++++++++++++++++++++++++++++++---- www/index.html | 4 ++ 3 files changed, 115 insertions(+), 12 deletions(-) diff --git a/js_src/buttonbehaviors.js b/js_src/buttonbehaviors.js index eb7db6b..fc4195e 100644 --- a/js_src/buttonbehaviors.js +++ b/js_src/buttonbehaviors.js @@ -188,6 +188,31 @@ function initButtonBehavior() { //btnStop.on('touchstart mousedown',stopPrint); + var btnLoadSketch = $('#btnLoadSketch'); + btnLoadSketch.mouseup(function(e) { + var sketchId = $('#sketchId').val(); + console.log("loading sketch with id " + sketchId); + + wifiboxURL = "http://192.168.5.1/d3dapi"; + $.ajax({ + url: wifiboxURL + "/sketch/" + sketchId, + dataType: 'json', + type: 'GET', + //timeout: this.timeoutTime, + success: function(response) { + if (response.status == 'error' || response.status == 'fail') { + console.log("loadSketch fail/error: " + response.msg + " -- ", response); + } else { + console.log("loadSketch success: loaded id #" + response.data.id, response); + //console.log("sketch content:\n" + response.data.data); + loadFromSvg(response.data.data); + } + } + }).fail(function() { + console.log("loadSketch failed: ", response); + }); + }); + btnSave.mouseup(function(e) { svg = saveToSvg(); console.log("generated SVG [" + _points.length + " points, " + svg.length + " bytes]:\n" + svg); @@ -200,10 +225,11 @@ function initButtonBehavior() { data: { data: svg }, //timeout: this.timeoutTime, success: function(response) { - if (response.status == 'error' || response.status == 'fail') - console.log("saveSketch fail/error: ", response); - else + if (response.status == 'error' || response.status == 'fail') { + console.log("saveSketch fail/error: " + response.msg + " -- ", response); + } else { console.log("saveSketch success: saved with id #" + response.data.id, response); + } } }).fail(function() { console.log("saveSketch failed: ", response); diff --git a/js_src/canvasDrawing.js b/js_src/canvasDrawing.js index c750947..62e9222 100644 --- a/js_src/canvasDrawing.js +++ b/js_src/canvasDrawing.js @@ -300,6 +300,86 @@ function onCanvasMouseDown(e) { draw(x, y, 0.5); } +function loadFromSvg(svgData) { + var mode = '', x = 0, y = 0; + + console.log("loading " + svgData.length + " bytes of data..."); + + clearDoodle(); + + var p = svgData.indexOf("'): skipSpace(); parse '(\w+):\w*(\w+)'; assign corresp. var if key matches + + renderToImageDataPreview(); + + /* TODO: behaviour for prev/next buttons: + * - call update/status once to init number of saved sketches - 0 means both buttons disabled, otherwise set current to -1, total amount to number and enable left (i.e. clicking that loads last saved sketch) + * - when going back and forth, update current and enable/disable both buttons when borders (i.e. 1 and total amount) are reached + * - when saving, set current to -1 again and update total amount + */ + + return true; +} + function saveToSvg() { var lastX = 0, lastY = 0, lastIsMove; var svg = ''; @@ -332,20 +412,13 @@ function saveToSvg() { svg += '\t\n'; var makeField = function(k,v) { return k + ': ' + v + '; '; } - svg += '\n'; + svg += '\n'; svg += '\n'; return svg; } -function loadFromSvg(svgData) { - //clear out points - //read svg and fill points (and update bounds) - //NOTE: generate the fields comment with some magic value so we can easily detect this while parsing - //update any state necessary to have the canvas/client re-init itself to the new data -} - var prevPoint = {x:-1, y:-1}; function onCanvasMouseMove(e) { // console.log("f:onCanvasMouseMove()"); @@ -527,4 +600,4 @@ function onCanvasTouchEnd(e) { function prevent(e) { e.preventDefault(); -} \ No newline at end of file +} diff --git a/www/index.html b/www/index.html index c33f0bb..6d7337e 100644 --- a/www/index.html +++ b/www/index.html @@ -110,6 +110,10 @@
+
+ + load sketch +