From 32ddc734c0d62eab401d9a000556076188eab8d8 Mon Sep 17 00:00:00 2001 From: Michael Aschauer Date: Fri, 20 Jan 2017 10:49:47 +0100 Subject: [PATCH] refactoring --- index.html | 6 +-- stitchcode/cloud.js | 11 +++- stitchcode/objects.js | 10 ++-- stitchcode/turtleShepherd.js | 98 ++++++++++++++++++++++++------------ 4 files changed, 84 insertions(+), 41 deletions(-) diff --git a/index.html b/index.html index 0ac3ab41..939ad7cc 100644 --- a/index.html +++ b/index.html @@ -26,8 +26,7 @@ - - + @@ -64,10 +63,9 @@ border:1px solid #c0c0c0" >
- diff --git a/stitchcode/cloud.js b/stitchcode/cloud.js index ea4ce180..e0218b64 100644 --- a/stitchcode/cloud.js +++ b/stitchcode/cloud.js @@ -33,9 +33,18 @@ localize*/ modules.cloud = '2015-January-12'; // Global stuff +getBaseURL = function () { + var url = location.href; // entire url including querystring - also: window.location.href; + if (url.lastIndexOf('#') > 0) { + url = url.substring(0, url.lastIndexOf('#')); + } + url = url.substring(0, url.lastIndexOf('/')); + return url + "/"; +}; + var Cloud; var SnapCloud = new Cloud( - tstools.getBaseURL()+ '../cloud' + getBaseURL()+ '../cloud' ); // Cloud ///////////////////////////////////////////////////////////// diff --git a/stitchcode/objects.js b/stitchcode/objects.js index 0587b951..8dd82f68 100644 --- a/stitchcode/objects.js +++ b/stitchcode/objects.js @@ -9,7 +9,7 @@ SpriteMorph.prototype.forward = function (steps) { if (!turtleShepherd.hasSteps()) turtleShepherd.initPosition(oldx, oldy); turtleShepherd.addMoveTo(this.xPosition() , this.yPosition() , this.isDown); - this.reDrawTrails(); + this.reRender(); }; SpriteMorph.prototype.origGotoXY = SpriteMorph.prototype.gotoXY; @@ -24,7 +24,7 @@ SpriteMorph.prototype.gotoXY = function (x, y, justMe) { if (!turtleShepherd.hasSteps()) turtleShepherd.initPosition(oldx, oldy); turtleShepherd.addMoveTo(this.xPosition() , this.yPosition() , this.isDown); - this.reDrawTrails(); + this.reRender(); } }; @@ -32,12 +32,12 @@ SpriteMorph.prototype.origClear = SpriteMorph.prototype.clear; SpriteMorph.prototype.clear = function () { this.origClear(); turtleShepherd.clear(); - this.reDrawTrails(); + this.reRender(); }; -SpriteMorph.prototype.reDrawTrails = function () { +SpriteMorph.prototype.reRender = function () { this.parent.clearPenTrails(); - reDraw(this.parent.penTrails()); + turtleShepherd.reRender(this.parent.penTrails()); }; /* Stage */ diff --git a/stitchcode/turtleShepherd.js b/stitchcode/turtleShepherd.js index 1dcf6ae7..2de72e2b 100644 --- a/stitchcode/turtleShepherd.js +++ b/stitchcode/turtleShepherd.js @@ -1,8 +1,32 @@ +/* + TurtleShepherd + + turltestich's central intelligence agancy + +*/ function TurtleShepherd() { this.clear(); } +TurtleShepherd.prototype.clear = function() { + this.cache = []; + this.minX = 0; + this.minY = 0; + this.maxX = 0; + this.maxY = 0; + this.steps = 0; + this.initX = 0; + this.initY = 0; + this.w = 480; + this.h = 360; + this.scale = 1; +}; + +TurtleShepherd.prototype.hasSteps = function() { + return this.steps > 0; +}; + TurtleShepherd.prototype.addMoveTo= function(x,y,penState) { this.cache.push( { @@ -12,7 +36,7 @@ TurtleShepherd.prototype.addMoveTo= function(x,y,penState) { "penDown":penState, } ); - if (this.count === 0) { + if (this.steps === 0) { this.minX = x; this.minY = y; this.maxX = x; @@ -24,12 +48,14 @@ TurtleShepherd.prototype.addMoveTo= function(x,y,penState) { if (y < this.minY) this.minY = y; if (y > this.maxY) this.maxY = y; } - this.count++; - //if (DEBUG) tstools.debug_msg("add move to" + x + " " + y + " " + penState ); + this.steps++; + if (DEBUG) this.debug_msg("move to " + x + " " + y + " " + penState ); }; -TurtleShepherd.prototype.hasSteps = function() { - return this.count > 0; +TurtleShepherd.prototype.initPosition = function(x,y) { + this.initX = x; + this.initY = y; + if (DEBUG) this.debug_msg("init " + x + " " + y ); }; TurtleShepherd.prototype.addColorChange= function(color) { @@ -41,40 +67,22 @@ TurtleShepherd.prototype.addColorChange= function(color) { ); }; -TurtleShepherd.prototype.clear = function() { - this.cache = []; - this.minX = 0; - this.minY = 0; - this.maxX = 0; - this.maxY = 0; - this.count = 0; - this.initX = 0; - this.initY = 0; - this.w = 480; - this.h = 360; - this.scale = 1; -}; - -TurtleShepherd.prototype.initPosition = function(x,y) { - this.initX = x; - this.initY = y; -}; TurtleShepherd.prototype.setScale = function(s) { this.scale = s; - if (DEBUG) tstools.debug_msg("zoom to scale "+ s ); + if (DEBUG) this.debug_msg("zoom to scale "+ s ); }; TurtleShepherd.prototype.zoomIn = function() { this.scale += 0.1; - if (DEBUG) tstools.debug_msg("zoom to scale "+this.scale ); + if (DEBUG) this.debug_msg("zoom to scale "+this.scale ); }; TurtleShepherd.prototype.zoomOut = function() { if (this.scale > 0.15) this.scale -= 0.1; - if (DEBUG) tstools.debug_msg("zoom to scale "+ this.scale ); + if (DEBUG) this.debug_msg("zoom to scale "+ this.scale ); }; TurtleShepherd.prototype.setDimensions = function(x,y) { @@ -84,11 +92,11 @@ TurtleShepherd.prototype.setDimensions = function(x,y) { TurtleShepherd.prototype.renderGrid = function(size=50) { - return '' + - '' + - '' + + return '\n' + + '\n' + + '\n' + '' + - ''; + '\n'; }; TurtleShepherd.prototype.toSVG = function() { @@ -106,9 +114,11 @@ TurtleShepherd.prototype.toSVG = function() { svgStr += this.renderGrid(); svgStr += ''; + '" width="100%" height="100%" fill="url(#grid)" />\n'; + hasFirst = false; tagOpen = false; + for (var i=0; i < this.cache.length; i++) { if (this.cache[i].cmd == "move") { stitch = this.cache[i]; @@ -169,3 +179,29 @@ TurtleShepherd.prototype.toSVG = function() { svgStr += '\n'; return svgStr; }; + +TurtleShepherd.prototype.reRender = function(cnv) { + //load a svg snippet in the canvas with id = 'svg' + canvas = document.getElementById('svg'); + document.getElementById("code").innerHTML = turtleShepherd.toSVG(); + document.getElementById("svg2").innerHTML = turtleShepherd.toSVG(); + //canvg(document.getElementById('svg'), cmdCache.toSVG()); + + //canvg(cnv, cmdCache.toSVG()); + + //var cnv = caller.parent.penTrails(); + //var ctx = cnv.getContext('2d'); + //ctx.drawSvg(cmdCache.toSVG(), 0, 0, cnv.width, cnv.height); + +} + +TurtleShepherd.prototype.debug_msg = function (st, clear) { + o = ""; + if (!clear) { + o = document.getElementById("debug").innerHTML; + } else { + o = ""; + } + o = st + "
" + o; + document.getElementById("debug").innerHTML = o; +};