2018-02-05 18:39:25 -05:00
|
|
|
function Tool()
|
|
|
|
{
|
2018-02-05 20:14:23 -05:00
|
|
|
this.index = 0;
|
2018-05-07 18:47:09 -04:00
|
|
|
this.settings = { size:{width:300,height:300} }
|
2018-02-06 14:16:01 -05:00
|
|
|
this.layers = [[],[],[]];
|
2018-03-06 17:37:01 -05:00
|
|
|
this.styles = [
|
2018-05-07 19:32:00 -04:00
|
|
|
{ thickness:2,strokeLinecap:"round",strokeLinejoin:"round",color:"#f00",fill:"none",dash:[0,0],mirror_style:0 },
|
|
|
|
{ thickness:2,strokeLinecap:"round",strokeLinejoin:"round",color:"#0f0",fill:"none",dash:[0,0],mirror_style:0 },
|
|
|
|
{ thickness:2,strokeLinecap:"round",strokeLinejoin:"round",color:"#00f",fill:"none",dash:[0,0],mirror_style:0 }
|
2018-03-06 17:37:01 -05:00
|
|
|
];
|
2018-02-05 18:39:25 -05:00
|
|
|
this.verteces = [];
|
2018-05-07 00:24:01 -04:00
|
|
|
this.reqs = { line:2,arc_c:2,arc_r:2,bezier:3,close:0 };
|
2018-02-05 18:39:25 -05:00
|
|
|
|
2018-03-06 17:57:52 -05:00
|
|
|
this.start = function()
|
|
|
|
{
|
|
|
|
this.styles[0].color = dotgrid.theme.active.f_high
|
|
|
|
this.styles[1].color = dotgrid.theme.active.f_med
|
|
|
|
this.styles[2].color = dotgrid.theme.active.f_low
|
|
|
|
}
|
|
|
|
|
2018-02-06 14:16:01 -05:00
|
|
|
this.reset = function()
|
|
|
|
{
|
|
|
|
this.layers = [[],[],[]];
|
|
|
|
this.verteces = [];
|
|
|
|
this.index = 0;
|
2018-05-07 00:51:58 -04:00
|
|
|
dotgrid.set_size({width:300,height:300})
|
2018-02-06 14:16:01 -05:00
|
|
|
}
|
|
|
|
|
2018-03-06 22:08:34 -05:00
|
|
|
this.clear = function()
|
2018-03-06 17:26:18 -05:00
|
|
|
{
|
2018-03-06 22:08:34 -05:00
|
|
|
this.verteces = [];
|
|
|
|
dotgrid.preview();
|
|
|
|
dotgrid.draw();
|
2018-03-06 17:26:18 -05:00
|
|
|
}
|
|
|
|
|
2018-03-06 22:08:34 -05:00
|
|
|
this.undo = function()
|
2018-02-05 20:14:23 -05:00
|
|
|
{
|
2018-03-06 22:08:34 -05:00
|
|
|
this.layers = dotgrid.history.prev();
|
|
|
|
dotgrid.draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.redo = function()
|
|
|
|
{
|
|
|
|
this.layers = dotgrid.history.next();
|
|
|
|
dotgrid.draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
// I/O
|
|
|
|
|
2018-05-07 00:24:01 -04:00
|
|
|
this.export = function(target = {settings:this.settings,layers:this.layers,styles:this.styles})
|
2018-03-06 22:08:34 -05:00
|
|
|
{
|
|
|
|
return JSON.stringify(copy(target), null, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.import = function(layer)
|
|
|
|
{
|
|
|
|
this.layers[this.index] = this.layers[this.index].concat(layer)
|
|
|
|
dotgrid.history.push(this.layers);
|
|
|
|
this.clear();
|
|
|
|
dotgrid.draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.replace = function(dot)
|
|
|
|
{
|
|
|
|
if(!dot.layers || dot.layers.length != 3){ console.log("Incompatible version"); return; }
|
|
|
|
|
2018-05-07 18:47:09 -04:00
|
|
|
if(this.settings && (this.settings.size.width != dot.settings.size.width || this.settings.size.height != dot.settings.size.height)){
|
|
|
|
dotgrid.set_size({width:dot.settings.size.width,height:dot.settings.size.height})
|
2018-05-07 00:51:58 -04:00
|
|
|
}
|
|
|
|
|
2018-03-06 22:08:34 -05:00
|
|
|
this.layers = dot.layers;
|
|
|
|
this.styles = dot.styles;
|
2018-05-07 00:51:58 -04:00
|
|
|
this.settings = dot.settings;
|
|
|
|
|
2018-03-06 22:08:34 -05:00
|
|
|
this.clear();
|
|
|
|
dotgrid.draw();
|
|
|
|
dotgrid.history.push(this.layers);
|
2018-02-05 20:14:23 -05:00
|
|
|
}
|
|
|
|
|
2018-03-06 22:08:34 -05:00
|
|
|
// EDIT
|
|
|
|
|
2018-02-05 18:39:25 -05:00
|
|
|
this.remove_segment = function()
|
|
|
|
{
|
2018-02-12 15:29:16 -05:00
|
|
|
if(this.verteces.length > 0){ this.clear(); return; }
|
|
|
|
|
2018-02-05 20:14:23 -05:00
|
|
|
this.layer().pop();
|
2018-02-05 18:39:25 -05:00
|
|
|
this.clear();
|
|
|
|
dotgrid.draw();
|
|
|
|
}
|
|
|
|
|
2018-02-05 23:19:34 -05:00
|
|
|
this.remove_segments_at = function(pos)
|
|
|
|
{
|
2018-02-05 23:35:25 -05:00
|
|
|
for(segment_id in this.layer()){
|
|
|
|
var segment = this.layer()[segment_id];
|
|
|
|
for(vertex_id in segment.verteces){
|
|
|
|
var vertex = segment.verteces[vertex_id];
|
|
|
|
if(Math.abs(pos.x) == Math.abs(vertex.x) && Math.abs(pos.y) == Math.abs(vertex.y)){
|
|
|
|
segment.verteces.splice(vertex_id,1)
|
|
|
|
}
|
|
|
|
}
|
2018-02-16 20:42:52 -05:00
|
|
|
if(segment.verteces.length < 2){
|
|
|
|
this.layers[this.index].splice(segment_id,1)
|
|
|
|
}
|
2018-02-05 23:35:25 -05:00
|
|
|
}
|
|
|
|
this.clear();
|
|
|
|
dotgrid.draw();
|
2018-02-05 23:19:34 -05:00
|
|
|
}
|
|
|
|
|
2018-02-05 18:39:25 -05:00
|
|
|
this.add_vertex = function(pos)
|
|
|
|
{
|
2018-05-07 19:32:00 -04:00
|
|
|
pos = {x:Math.abs(pos.x),y:Math.abs(pos.y)}
|
2018-02-05 18:39:25 -05:00
|
|
|
this.verteces.push(pos);
|
|
|
|
}
|
|
|
|
|
2018-02-06 13:42:34 -05:00
|
|
|
this.vertex_at = function(pos)
|
|
|
|
{
|
|
|
|
for(segment_id in this.layer()){
|
|
|
|
var segment = this.layer()[segment_id];
|
|
|
|
for(vertex_id in segment.verteces){
|
|
|
|
var vertex = segment.verteces[vertex_id];
|
|
|
|
if(vertex.x == Math.abs(pos.x) && vertex.y == Math.abs(pos.y)){
|
|
|
|
return vertex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-02-05 18:39:25 -05:00
|
|
|
this.cast = function(type)
|
|
|
|
{
|
2018-02-05 20:14:23 -05:00
|
|
|
if(!this.layer()){ this.layers[this.index] = []; }
|
2018-02-05 22:47:25 -05:00
|
|
|
if(!this.can_cast(type)){ console.warn("Cannot cast"); return; }
|
2018-02-05 18:39:25 -05:00
|
|
|
|
2018-02-10 23:00:22 -05:00
|
|
|
var append_target = this.can_append({type:type,verteces:this.verteces.slice()})
|
|
|
|
|
|
|
|
if(append_target){
|
|
|
|
this.layers[this.index][append_target].verteces = this.layers[this.index][append_target].verteces.concat(this.verteces.slice())
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
this.layer().push({type:type,verteces:this.verteces.slice()})
|
|
|
|
}
|
|
|
|
|
2018-02-05 18:39:25 -05:00
|
|
|
this.clear();
|
|
|
|
dotgrid.draw();
|
2018-02-05 22:27:48 -05:00
|
|
|
dotgrid.history.push(this.layers);
|
2018-02-05 18:39:25 -05:00
|
|
|
|
2018-02-05 22:42:14 -05:00
|
|
|
console.log(`Casted ${type} -> ${this.layer().length} elements`);
|
2018-02-05 18:39:25 -05:00
|
|
|
}
|
|
|
|
|
2018-02-10 23:00:22 -05:00
|
|
|
this.can_append = function(content)
|
|
|
|
{
|
|
|
|
for(id in this.layer()){
|
|
|
|
var stroke = this.layer()[id];
|
|
|
|
if(stroke.type != content.type){ continue; }
|
2018-03-06 21:47:18 -05:00
|
|
|
if(!stroke.verteces[stroke.verteces.length-1]){ continue; }
|
2018-02-10 23:00:22 -05:00
|
|
|
if(stroke.verteces[stroke.verteces.length-1].x != content.verteces[0].x){ continue; }
|
|
|
|
if(stroke.verteces[stroke.verteces.length-1].y != content.verteces[0].y){ continue; }
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-05 22:27:48 -05:00
|
|
|
this.can_cast = function(type)
|
2018-02-05 22:47:25 -05:00
|
|
|
{
|
2018-02-06 01:51:09 -05:00
|
|
|
if(!type){ return false; }
|
2018-02-05 22:47:25 -05:00
|
|
|
// Cannot cast close twice
|
|
|
|
if(type == "close"){
|
|
|
|
var prev = this.layer()[this.layer().length-1];
|
2018-02-06 01:51:09 -05:00
|
|
|
if(!prev || prev.type == "close"){
|
2018-02-05 22:47:25 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-02-05 22:42:14 -05:00
|
|
|
return this.verteces.length >= this.reqs[type];
|
2018-02-05 22:27:48 -05:00
|
|
|
}
|
|
|
|
|
2018-04-16 00:36:20 -04:00
|
|
|
this.path = function(layer_id = 0, preview = null)
|
2018-02-05 18:39:25 -05:00
|
|
|
{
|
2018-04-16 00:36:20 -04:00
|
|
|
var layer = preview ? preview : this.layers[layer_id];
|
2018-04-16 00:24:09 -04:00
|
|
|
|
2018-02-05 18:39:25 -05:00
|
|
|
var html = "";
|
2018-02-05 23:19:34 -05:00
|
|
|
for(id in layer){
|
|
|
|
var segment = layer[id];
|
2018-04-15 22:32:54 -04:00
|
|
|
html += segment.type == "close" ? "Z " : this.render(segment,0);
|
2018-04-15 22:48:29 -04:00
|
|
|
|
|
|
|
// Horizontal Mirror
|
2018-04-16 00:24:09 -04:00
|
|
|
if(this.styles[layer_id].mirror_style == 1){
|
2018-04-15 23:13:29 -04:00
|
|
|
html += segment.type == "close" ? "Z " : this.render(segment,0,true,false);
|
|
|
|
}
|
2018-04-15 22:48:29 -04:00
|
|
|
// Vertical Mirror
|
2018-04-16 00:24:09 -04:00
|
|
|
if(this.styles[layer_id].mirror_style == 2){
|
2018-04-15 23:13:29 -04:00
|
|
|
html += segment.type == "close" ? "Z " : this.render(segment,0,false,true);
|
|
|
|
}
|
2018-04-15 22:48:29 -04:00
|
|
|
// Single-fold
|
2018-04-16 00:24:09 -04:00
|
|
|
if(this.styles[layer_id].mirror_style == 3){
|
2018-04-15 23:13:29 -04:00
|
|
|
html += segment.type == "close" ? "Z " : this.render(segment,180,false,false);
|
|
|
|
}
|
|
|
|
// Three-fold
|
2018-04-16 00:24:09 -04:00
|
|
|
if(this.styles[layer_id].mirror_style == 4){
|
2018-04-15 23:13:29 -04:00
|
|
|
html += segment.type == "close" ? "Z " : this.render(segment,120,false,false);
|
|
|
|
html += segment.type == "close" ? "Z " : this.render(segment,240,false,false);
|
|
|
|
}
|
|
|
|
// Four-fold
|
2018-04-16 00:24:09 -04:00
|
|
|
if(this.styles[layer_id].mirror_style == 5){
|
2018-04-15 23:13:29 -04:00
|
|
|
html += segment.type == "close" ? "Z " : this.render(segment,90,false,false);
|
|
|
|
html += segment.type == "close" ? "Z " : this.render(segment,180,false,false);
|
|
|
|
html += segment.type == "close" ? "Z " : this.render(segment,270,false,false);
|
|
|
|
}
|
2018-04-15 22:32:54 -04:00
|
|
|
// Five-folds
|
2018-04-16 00:24:09 -04:00
|
|
|
if(this.styles[layer_id].mirror_style == 6){
|
2018-04-15 23:13:29 -04:00
|
|
|
html += segment.type == "close" ? "Z " : this.render(segment,72,false,false);
|
|
|
|
html += segment.type == "close" ? "Z " : this.render(segment,72*2,false,false);
|
|
|
|
html += segment.type == "close" ? "Z " : this.render(segment,72*3,false,false);
|
|
|
|
html += segment.type == "close" ? "Z " : this.render(segment,72*4,false,false);
|
|
|
|
}
|
|
|
|
// Six-folds
|
2018-04-16 00:24:09 -04:00
|
|
|
if(this.styles[layer_id].mirror_style == 7){
|
2018-04-15 23:13:29 -04:00
|
|
|
html += segment.type == "close" ? "Z " : this.render(segment,60,false,false);
|
|
|
|
html += segment.type == "close" ? "Z " : this.render(segment,60*2,false,false);
|
|
|
|
html += segment.type == "close" ? "Z " : this.render(segment,60*3,false,false);
|
|
|
|
html += segment.type == "close" ? "Z " : this.render(segment,60*4,false,false);
|
|
|
|
html += segment.type == "close" ? "Z " : this.render(segment,60*5,false,false);
|
|
|
|
}
|
2018-02-05 18:39:25 -05:00
|
|
|
}
|
|
|
|
return html
|
|
|
|
}
|
|
|
|
|
2018-02-06 13:42:34 -05:00
|
|
|
this.paths = function()
|
|
|
|
{
|
2018-04-16 00:24:09 -04:00
|
|
|
return [this.path(0),this.path(1),this.path(2)]
|
2018-02-06 13:42:34 -05:00
|
|
|
}
|
|
|
|
|
2018-04-15 22:48:29 -04:00
|
|
|
this.render = function(segment, angle = 0, mirror_x = false, mirror_y = false)
|
2018-02-05 18:39:25 -05:00
|
|
|
{
|
|
|
|
var type = segment.type;
|
|
|
|
var verteces = segment.verteces;
|
2018-02-05 22:27:48 -05:00
|
|
|
var html = ``;
|
|
|
|
var skip = 0;
|
2018-02-05 18:39:25 -05:00
|
|
|
|
|
|
|
for(id in verteces){
|
2018-02-05 22:27:48 -05:00
|
|
|
if(skip > 0){ skip -= 1; continue; }
|
2018-04-15 22:32:54 -04:00
|
|
|
|
2018-04-15 22:48:29 -04:00
|
|
|
var vertex = this.mirror_mod(verteces[id],angle,mirror_x,mirror_y);
|
|
|
|
var next = this.mirror_mod(verteces[parseInt(id)+1],angle,mirror_x,mirror_y)
|
|
|
|
var after_next = this.mirror_mod(verteces[parseInt(id)+2],angle,mirror_x,mirror_y)
|
2018-02-05 22:27:48 -05:00
|
|
|
|
2018-04-15 22:32:54 -04:00
|
|
|
if(id == 0){
|
2018-04-16 00:24:09 -04:00
|
|
|
html += `M${vertex.x},${vertex.y} `;
|
2018-04-15 22:32:54 -04:00
|
|
|
}
|
|
|
|
|
2018-02-05 22:27:48 -05:00
|
|
|
if(type == "line"){
|
|
|
|
html += `L${vertex.x},${vertex.y} `;
|
|
|
|
}
|
|
|
|
else if(type == "arc_c" && next){
|
|
|
|
html += `A${next.x - vertex.x},${next.y - vertex.y} 0 0,1 ${next.x},${next.y} `;
|
|
|
|
}
|
|
|
|
else if(type == "arc_r" && next){
|
2018-02-05 23:02:22 -05:00
|
|
|
html += `A${next.x - vertex.x},${next.y - vertex.y} 0 0,0 ${next.x},${next.y} `;
|
2018-02-05 22:27:48 -05:00
|
|
|
}
|
2018-02-05 22:42:14 -05:00
|
|
|
else if(type == "bezier" && next && after_next){
|
|
|
|
html += `Q${next.x},${next.y} ${after_next.x},${after_next.y} `;
|
2018-02-05 23:02:22 -05:00
|
|
|
skip = 1
|
2018-02-05 22:42:14 -05:00
|
|
|
}
|
2018-02-05 18:39:25 -05:00
|
|
|
}
|
2018-02-05 22:47:25 -05:00
|
|
|
|
2018-02-05 18:39:25 -05:00
|
|
|
return html
|
|
|
|
}
|
|
|
|
|
2018-05-07 21:32:06 -04:00
|
|
|
this.mirror_mod = function(vertex,angle = 0,mirror_x = false,mirror_y = false)
|
2018-04-15 22:32:54 -04:00
|
|
|
{
|
2018-04-15 22:48:29 -04:00
|
|
|
if(!vertex){ return null; }
|
|
|
|
|
|
|
|
if(mirror_x == true){
|
2018-05-07 18:47:09 -04:00
|
|
|
return {x:(dotgrid.tool.settings.size.width - vertex.x),y:vertex.y}
|
2018-04-15 22:48:29 -04:00
|
|
|
}
|
|
|
|
if(mirror_y == true){
|
2018-05-07 18:47:09 -04:00
|
|
|
return {x:vertex.x,y:(dotgrid.tool.settings.size.height - vertex.y)+(dotgrid.height/2)}
|
2018-04-15 22:48:29 -04:00
|
|
|
}
|
2018-05-07 21:32:06 -04:00
|
|
|
if(angle == 0){
|
|
|
|
return vertex;
|
|
|
|
}
|
2018-05-07 18:47:09 -04:00
|
|
|
return rotate_point(vertex.x,vertex.y,dotgrid.tool.settings.size.width/2,dotgrid.tool.settings.size.height/2,angle)
|
2018-04-15 22:32:54 -04:00
|
|
|
}
|
|
|
|
|
2018-02-05 20:02:13 -05:00
|
|
|
this.translate = function(a,b)
|
|
|
|
{
|
2018-02-05 20:14:23 -05:00
|
|
|
for(segment_id in this.layer()){
|
|
|
|
var segment = this.layer()[segment_id];
|
2018-02-05 20:02:13 -05:00
|
|
|
for(vertex_id in segment.verteces){
|
|
|
|
var vertex = segment.verteces[vertex_id];
|
|
|
|
if(vertex.x == Math.abs(a.x) && vertex.y == Math.abs(a.y)){
|
|
|
|
segment.verteces[vertex_id] = {x:Math.abs(b.x),y:Math.abs(b.y)};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-05 22:27:48 -05:00
|
|
|
dotgrid.history.push(this.layers);
|
2018-02-05 23:02:22 -05:00
|
|
|
this.clear();
|
2018-02-05 20:02:13 -05:00
|
|
|
dotgrid.draw();
|
|
|
|
}
|
|
|
|
|
2018-03-21 03:35:28 -04:00
|
|
|
this.translate_multi = function(a,b)
|
|
|
|
{
|
|
|
|
var offset = {x:a.x - b.x,y:a.y - b.y}
|
|
|
|
|
|
|
|
for(segment_id in this.layer()){
|
|
|
|
var segment = this.layer()[segment_id];
|
|
|
|
for(vertex_id in segment.verteces){
|
|
|
|
var vertex = segment.verteces[vertex_id];
|
|
|
|
segment.verteces[vertex_id] = {x:vertex.x+offset.x,y:vertex.y-offset.y};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dotgrid.history.push(this.layers);
|
|
|
|
this.clear();
|
|
|
|
dotgrid.draw();
|
|
|
|
}
|
|
|
|
|
2018-03-06 22:08:34 -05:00
|
|
|
// Style
|
2018-02-06 01:34:45 -05:00
|
|
|
|
2018-03-06 22:08:34 -05:00
|
|
|
this.style = function()
|
2018-02-06 01:34:45 -05:00
|
|
|
{
|
2018-03-06 22:08:34 -05:00
|
|
|
if(!this.styles[this.index]){
|
|
|
|
this.styles[this.index] = [];
|
|
|
|
}
|
|
|
|
return this.styles[this.index];
|
2018-02-06 01:34:45 -05:00
|
|
|
}
|
|
|
|
|
2018-03-06 22:08:34 -05:00
|
|
|
// Layers
|
2018-02-07 15:34:17 -05:00
|
|
|
|
2018-03-06 22:08:34 -05:00
|
|
|
this.layer = function()
|
2018-02-06 01:34:45 -05:00
|
|
|
{
|
2018-03-06 22:08:34 -05:00
|
|
|
if(!this.layers[this.index]){
|
|
|
|
this.layers[this.index] = [];
|
|
|
|
}
|
|
|
|
return this.layers[this.index];
|
2018-02-06 01:34:45 -05:00
|
|
|
}
|
2018-02-06 13:42:34 -05:00
|
|
|
|
2018-02-12 15:29:16 -05:00
|
|
|
this.select_layer = function(id)
|
2018-02-06 13:42:34 -05:00
|
|
|
{
|
2018-02-12 15:29:16 -05:00
|
|
|
this.index = clamp(id,0,2);
|
2018-02-06 13:42:34 -05:00
|
|
|
this.clear();
|
|
|
|
dotgrid.draw();
|
|
|
|
console.log(`layer:${this.index}`)
|
|
|
|
}
|
|
|
|
|
2018-03-06 17:02:43 -05:00
|
|
|
this.select_next_layer = function()
|
2018-02-12 15:29:16 -05:00
|
|
|
{
|
2018-03-06 17:02:43 -05:00
|
|
|
this.index = this.index >= 2 ? 0 : this.index+1
|
|
|
|
this.select_layer(this.index);
|
2018-02-06 13:42:34 -05:00
|
|
|
}
|
2018-02-07 01:44:18 -05:00
|
|
|
|
2018-05-07 18:47:09 -04:00
|
|
|
function rotate_point(pointX, pointY, originX, originY, angle){ angle = angle * Math.PI / 180.0; return { x: (Math.cos(angle) * (pointX-originX) - Math.sin(angle) * (pointY-originY) + originX).toFixed(1), y: (Math.sin(angle) * (pointX-originX) + Math.cos(angle) * (pointY-originY) + originY).toFixed(1) }; }
|
2018-02-07 01:44:18 -05:00
|
|
|
function copy(data){ return data ? JSON.parse(JSON.stringify(data)) : []; }
|
2018-02-12 15:29:16 -05:00
|
|
|
function clamp(v, min, max) { return v < min ? min : v > max ? max : v; }
|
2018-02-05 18:39:25 -05:00
|
|
|
}
|