From 3200f0818c24016b185169af7ac19d248e491aac Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Tue, 6 Feb 2018 16:42:14 +1300 Subject: [PATCH] Implemented close --- sources/scripts/dotgrid.js | 8 ++++---- sources/scripts/tool.js | 16 +++++++++++++--- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/sources/scripts/dotgrid.js b/sources/scripts/dotgrid.js index 28f17de..5b77a09 100644 --- a/sources/scripts/dotgrid.js +++ b/sources/scripts/dotgrid.js @@ -236,10 +236,10 @@ function Dotgrid(width,height,grid_x,grid_y,block_x,block_y,thickness = 3,lineca if(!o){ return; } if(o == "line"){ this.tool.cast("line"); } - if(o == "arc_c"){ this.draw_arc("0,1"); } - if(o == "arc_r"){ this.draw_arc("0,0"); } - if(o == "bezier"){ this.draw_bezier(); } - if(o == "close"){ this.draw_close(); } + if(o == "arc_c"){ this.tool.cast("arc_c"); } + if(o == "arc_r"){ this.tool.cast("arc_r"); } + if(o == "bezier"){ this.tool.cast("bezier"); } + if(o == "close"){ this.tool.cast("close"); } if(o == "thickness"){ this.mod_thickness(); } if(o == "linecap"){ this.mod_linecap(); } diff --git a/sources/scripts/tool.js b/sources/scripts/tool.js index dc9d43a..2a0fa6f 100644 --- a/sources/scripts/tool.js +++ b/sources/scripts/tool.js @@ -3,6 +3,7 @@ function Tool() this.index = 0; this.layers = []; this.verteces = []; + this.reqs = {line:2,arc_c:2,arc_r:2,bezier:3,close:0}; this.layer = function() { @@ -34,12 +35,12 @@ function Tool() dotgrid.draw(); dotgrid.history.push(this.layers); - console.log(`Casted ${type}+${this.layer().length}`); + console.log(`Casted ${type} -> ${this.layer().length} elements`); } this.can_cast = function(type) { - return this.verteces.length >= {line:2,arc_c:2,arc_r:2,bezier:3}[type]; + return this.verteces.length >= this.reqs[type]; } this.path = function() @@ -48,6 +49,7 @@ function Tool() for(id in this.layer()){ var segment = this.layer()[id]; + console.log(segment) html += this.render(segment); } return html @@ -60,11 +62,15 @@ function Tool() var html = ``; var skip = 0; + if(type == "close"){ + return `Z `; + } for(id in verteces){ if(skip > 0){ skip -= 1; continue; } - if(id == 0){ html += `M${verteces[0].x},${verteces[0].y} `; continue; } + if(id == 0){ html += `M${verteces[id].x},${verteces[id].y} `; continue; } var vertex = verteces[id]; var next = verteces[parseInt(id)+1] + var after_next = verteces[parseInt(id)+2] if(type == "line"){ html += `L${vertex.x},${vertex.y} `; @@ -77,6 +83,10 @@ function Tool() html += `A${next.x - vertex.x},${next.y - vertex.y} 0 0,0 ${next.x},${next.y} `; skip = 1 } + else if(type == "bezier" && next && after_next){ + html += `Q${next.x},${next.y} ${after_next.x},${after_next.y} `; + skip = 2 + } } return html