pointvec/sources/scripts/dotgrid.js

714 lines
22 KiB
JavaScript
Raw Normal View History

2016-12-31 16:35:14 -05:00
function Dotgrid(width,height,grid_x,grid_y,block_x,block_y,thickness = 3,linecap = "round", color = "#000000")
2016-12-31 10:00:57 -05:00
{
2018-01-12 03:09:26 -05:00
this.controller = new Controller();
2017-11-06 21:10:09 -05:00
this.theme = new Theme();
2017-11-14 16:11:09 -05:00
this.interface = new Interface();
2017-11-06 21:10:09 -05:00
2016-12-31 10:00:57 -05:00
this.width = width;
this.height = height;
2016-12-31 12:18:01 -05:00
this.grid_x = grid_x;
this.grid_y = grid_y;
2016-12-31 15:37:13 -05:00
this.block_x = block_x;
this.block_y = block_y;
2016-12-31 16:35:14 -05:00
this.thickness = thickness;
this.linecap = linecap;
this.color = color;
2017-11-05 19:18:57 -05:00
this.offset = new Pos(0,0);
2016-12-31 16:35:14 -05:00
2017-11-05 20:10:37 -05:00
// Dotgrid
this.element = document.createElement("div");
this.element.id = "dotgrid";
this.element.style.width = this.width;
this.element.style.height = this.height;
2016-12-31 10:00:57 -05:00
2017-11-12 16:47:34 -05:00
this.wrapper = document.createElement("div");
this.wrapper.id = "wrapper";
2016-12-31 12:18:01 -05:00
this.grid_width = this.width/this.grid_x;
this.grid_height = this.height/this.grid_y;
2016-12-31 12:37:10 -05:00
var cursor = null;
var cursor_from = null;
var cursor_to = null;
2017-11-04 19:59:11 -04:00
var cursor_end = null;
2016-12-31 12:18:01 -05:00
var from = null;
var to = null;
2017-11-04 19:59:11 -04:00
var end = null;
this.svg_el = null;
2017-11-05 14:53:59 -05:00
this.mirror_el = null;
this.mirror = false;
2017-11-05 20:10:37 -05:00
this.fill = false;
2017-11-04 19:59:11 -04:00
2017-11-09 14:47:06 -05:00
this.guide = new Guide();
this.render = new Render();
2017-11-21 16:24:25 -05:00
this.serializer = new Serializer();
2017-11-09 14:47:06 -05:00
2017-11-04 19:59:11 -04:00
this.path = document.createElementNS("http://www.w3.org/2000/svg", "path");
this.segments = [];
2017-11-12 16:47:34 -05:00
this.scale = 1
2016-12-31 12:18:01 -05:00
2016-12-31 10:00:57 -05:00
this.install = function()
2018-01-11 22:22:50 -05:00
{
document.getElementById("app").appendChild(this.wrapper);
2017-11-12 16:47:34 -05:00
this.wrapper.appendChild(this.element);
this.element.appendChild(this.guide.el);
2017-11-12 22:54:56 -05:00
this.element.appendChild(this.guide.widgets);
2017-11-12 16:47:34 -05:00
this.wrapper.appendChild(this.render.el);
2016-12-31 12:18:01 -05:00
// Cursors
this.cursor = document.createElement("div");
this.cursor.id = "cursor";
this.element.appendChild(this.cursor);
2017-11-10 14:46:11 -05:00
this.cursor_coord = document.createElement("div");
this.cursor_coord.id = "cursor_coord";
this.cursor_coord.className = "fl"
this.cursor.appendChild(this.cursor_coord);
2016-12-31 12:37:10 -05:00
cursor_from = document.createElement("div");
cursor_from.id = "cursor_from";
this.element.appendChild(cursor_from);
2016-12-31 12:18:01 -05:00
2016-12-31 12:37:10 -05:00
cursor_to = document.createElement("div");
cursor_to.id = "cursor_to";
this.element.appendChild(cursor_to);
2016-12-31 12:18:01 -05:00
2017-11-04 19:59:11 -04:00
cursor_end = document.createElement("div");
cursor_end.id = "cursor_end";
this.element.appendChild(cursor_end);
2017-11-05 19:18:57 -05:00
this.offset_el = document.createElementNS("http://www.w3.org/2000/svg", "g");
2017-11-05 14:53:59 -05:00
this.mirror_el = document.createElementNS("http://www.w3.org/2000/svg", "g");
this.mirror_path = document.createElementNS("http://www.w3.org/2000/svg", "path");
2016-12-31 12:18:01 -05:00
// Vector
2017-11-04 19:59:11 -04:00
this.svg_el = document.createElementNS("http://www.w3.org/2000/svg", "svg");
2018-01-11 22:22:50 -05:00
this.svg_el.setAttribute("class","vector");
2017-11-04 19:59:11 -04:00
this.svg_el.setAttribute("width",this.width+"px");
this.svg_el.setAttribute("height",this.height+"px");
this.svg_el.setAttribute("xmlns","http://www.w3.org/2000/svg");
this.svg_el.setAttribute("baseProfile","full");
this.svg_el.setAttribute("version","1.1");
this.svg_el.style.width = this.width;
this.svg_el.style.height = this.height;
this.svg_el.style.stroke = this.color;
this.svg_el.style.strokeWidth = this.thickness;
2017-11-26 23:35:53 -05:00
this.svg_el.style.fill = "none";
2017-11-04 19:59:11 -04:00
this.svg_el.style.strokeLinecap = this.linecap;
this.element.appendChild(this.svg_el);
2017-12-02 21:46:27 -05:00
// Preview
this.preview_el = document.createElementNS("http://www.w3.org/2000/svg", "svg");
this.preview_el.id = "preview"
2018-01-11 22:22:50 -05:00
this.preview_el.setAttribute("class","vector");
2017-12-02 21:46:27 -05:00
this.preview_el.setAttribute("width",this.width+"px");
this.preview_el.setAttribute("height",this.height+"px");
this.preview_el.setAttribute("xmlns","http://www.w3.org/2000/svg");
this.preview_el.setAttribute("baseProfile","full");
this.preview_el.setAttribute("version","1.1");
this.preview_el.style.width = this.width;
this.preview_el.style.height = this.height;
this.preview_el.style.stroke = "#72dec2";
this.preview_el.style.strokeWidth = 2;
this.preview_el.style.fill = "none";
this.preview_el.style.strokeLinecap = "round";
this.element.appendChild(this.preview_el);
2017-11-05 19:18:57 -05:00
2017-11-05 23:35:29 -05:00
this.offset_el.appendChild(this.path)
2017-11-05 19:18:57 -05:00
this.svg_el.appendChild(this.offset_el);
2017-11-05 14:53:59 -05:00
this.svg_el.appendChild(this.mirror_el);
this.mirror_el.appendChild(this.mirror_path);
2017-11-04 20:52:05 -04:00
2017-11-06 21:10:09 -05:00
this.theme.start();
2017-11-09 14:47:06 -05:00
this.guide.start();
2017-11-14 16:11:09 -05:00
this.interface.start();
2018-01-12 03:09:26 -05:00
2018-01-12 22:24:18 -05:00
this.controller.add("default","*","About",() => { require('electron').shell.openExternal('https://github.com/hundredrabbits/Dotgrid'); },"CmdOrCtrl+,");
this.controller.add("default","*","Fullscreen",() => { app.toggle_fullscreen(); },"CmdOrCtrl+Enter");
this.controller.add("default","*","Hide",() => { app.toggle_visible(); },"CmdOrCtrl+H");
this.controller.add("default","*","Inspect",() => { app.inspect(); },"CmdOrCtrl+.");
this.controller.add("default","*","Documentation",() => { dotgrid.controller.docs(); },"CmdOrCtrl+Esc");
2018-01-12 15:00:53 -05:00
this.controller.add("default","*","Reset",() => { dotgrid.reset(); dotgrid.theme.reset(); },"CmdOrCtrl+Backspace");
this.controller.add("default","*","Quit",() => { app.exit(); },"CmdOrCtrl+Q");
2018-01-12 03:46:09 -05:00
this.controller.add("default","File","New",() => { dotgrid.new(); },"CmdOrCtrl+N");
this.controller.add("default","File","Open",() => { dotgrid.open(); },"CmdOrCtrl+O");
this.controller.add("default","File","Save",() => { dotgrid.save(); },"CmdOrCtrl+S");
2018-01-12 22:24:18 -05:00
this.controller.add("default","Edit","Vertex",() => { dotgrid.add_point(); },"Enter");
2018-01-12 03:46:09 -05:00
this.controller.add("default","Edit","Undo",() => { dotgrid.undo(); },"CmdOrCtrl+Z");
2018-01-12 22:24:18 -05:00
this.controller.add("default","Edit","Delete",() => { dotgrid.undo(); },"Backspace");
2018-01-12 03:46:09 -05:00
this.controller.add("default","Edit","Move Up",() => { dotgrid.mod_move(new Pos(0,-15)); },"Up");
this.controller.add("default","Edit","Move Down",() => { dotgrid.mod_move(new Pos(0,15)); },"Down");
this.controller.add("default","Edit","Move Left",() => { dotgrid.mod_move(new Pos(-15,0)); },"Left");
this.controller.add("default","Edit","Move Right",() => { dotgrid.mod_move(new Pos(15,0)); },"Right");
2018-01-12 22:24:18 -05:00
this.controller.add("default","Edit","Deselect",() => { dotgrid.reset(); },"Esc");
2018-01-12 03:46:09 -05:00
this.controller.add("default","Stroke","Line",() => { dotgrid.draw_line(); },"A");
this.controller.add("default","Stroke","Arc",() => { dotgrid.draw_arc("0,1"); },"S");
2018-01-12 22:24:18 -05:00
this.controller.add("default","Stroke","Arc Rev",() => { dotgrid.draw_arc("0,0"); },"D");
2018-01-12 03:46:09 -05:00
this.controller.add("default","Stroke","Bezier",() => { dotgrid.draw_bezier(); },"F");
this.controller.add("default","Stroke","Close",() => { dotgrid.draw_close(); },"Z");
2018-01-12 22:24:18 -05:00
this.controller.add("default","Effect","Thicker",() => { dotgrid.mod_thickness(1) },"]");
this.controller.add("default","Effect","Thinner",() => { dotgrid.mod_thickness(-1) },"[");
2018-01-12 03:46:09 -05:00
this.controller.add("default","Effect","Linecap",() => { dotgrid.mod_linecap(); },"/");
this.controller.add("default","Effect","Mirror",() => { dotgrid.mod_mirror(); },"Space");
this.controller.add("default","Effect","Fill",() => { dotgrid.toggle_fill(); },"G");
2018-01-12 22:24:18 -05:00
this.controller.add("default","View","Tools",() => { dotgrid.interface.toggle(); },";");
this.controller.add("default","View","Grid",() => { dotgrid.guide.toggle(); },"H");
this.controller.add("default","View","Expert",() => { dotgrid.interface.toggle_zoom(); },":");
2018-01-12 03:46:09 -05:00
2018-01-12 03:09:26 -05:00
this.controller.commit();
2017-11-12 15:26:23 -05:00
2018-01-11 22:22:50 -05:00
window.addEventListener('drop', dotgrid.drag);
dotgrid.set_size({width:300,height:300});
2017-11-12 15:26:23 -05:00
this.draw();
2016-12-31 12:18:01 -05:00
}
2018-01-12 03:46:09 -05:00
// FILE
2018-01-12 03:09:26 -05:00
this.new = function()
{
dotgrid.segments = [];
dotgrid.draw();
}
this.save = function()
{
if(this.segments.length == 0){ return; }
this.scale = 1
this.draw();
if(dotgrid.fill){ dotgrid.svg_el.style.fill = "black"; dotgrid.render.draw(); }
var svg = dotgrid.svg_el.outerHTML;
dialog.showSaveDialog((fileName) => {
if (fileName === undefined){ return; }
fs.writeFile(fileName+".svg", svg);
fs.writeFile(fileName+'.png', dotgrid.render.buffer());
fs.writeFile(fileName+'.dot', JSON.stringify(dotgrid.serializer.serialize()));
dotgrid.draw()
});
}
this.open = function()
{
var paths = dialog.showOpenDialog({properties: ['openFile'],filters:[{name:"Dotgrid Image",extensions:["dot"]}]});
if(!paths){ console.log("Nothing to load"); return; }
fs.readFile(paths[0], 'utf-8', (err, data) => {
if(err){ alert("An error ocurred reading the file :" + err.message); return; }
dotgrid.serializer.deserialize(JSON.parse(data.toString().trim()));
dotgrid.draw();
});
}
2018-01-12 03:46:09 -05:00
// EDIT
this.undo = function()
{
if(from || to || end){
dotgrid.reset();
}
else{
dotgrid.segments.pop();
}
dotgrid.draw();
}
// STROKE
this.draw_line = function()
{
if(from === null || to === null){ return; }
to = new Pos(to.x * -1, to.y).sub(dotgrid.offset)
from = new Pos(from.x * -1,from.y).sub(dotgrid.offset)
end = end ? new Pos(end.x * -1,end.y).sub(dotgrid.offset) : null;
dotgrid.segments.push(new Path_Line(from,to,end));
dotgrid.reset();
dotgrid.draw();
dotgrid.reset();
}
this.draw_arc = function(orientation = "0,0")
{
if(from === null || to === null){ return; }
to = new Pos(to.x * -1, to.y).sub(dotgrid.offset)
from = new Pos(from.x * -1,from.y).sub(dotgrid.offset)
end = end ? new Pos(end.x * -1,end.y).sub(dotgrid.offset) : null;
dotgrid.segments.push(new Path_Arc(from,to,orientation,end));
dotgrid.reset();
dotgrid.draw();
dotgrid.reset();
}
this.draw_bezier = function()
{
if(from === null || to === null || end === null){ return; }
to = new Pos(to.x * -1, to.y).sub(dotgrid.offset)
from = new Pos(from.x * -1,from.y).sub(dotgrid.offset)
end = new Pos(end.x * -1,end.y).sub(dotgrid.offset)
dotgrid.segments.push(new Path_Bezier(from,to,end));
dotgrid.reset();
dotgrid.draw();
dotgrid.reset();
}
this.draw_close = function()
{
if(dotgrid.segments.length == 0){ return; }
if(dotgrid.segments[dotgrid.segments.length-1].name == "close"){ return; }
dotgrid.segments.push(new Path_Close());
dotgrid.reset();
dotgrid.draw();
dotgrid.reset();
}
2016-12-31 12:18:01 -05:00
// Cursor
2017-11-12 22:54:56 -05:00
this.translation = null;
2016-12-31 12:18:01 -05:00
this.mouse_down = function(e)
{
2017-11-14 06:18:58 -05:00
var o = e.target.getAttribute("ar");
2017-11-12 22:54:56 -05:00
var pos = this.position_in_grid(new Pos(e.clientX,e.clientY));
pos = this.position_on_grid(pos);
2017-11-13 13:52:53 -05:00
if(e.altKey){ dotgrid.delete_at(pos); return; }
if(dotgrid.handle_at(pos)){ dotgrid.translation = {from:pos,to:pos}; return; }
2017-11-12 22:54:56 -05:00
2017-11-04 20:52:05 -04:00
if(!o){ return; }
if(o == "line"){ this.draw_line(); }
if(o == "arc_c"){ this.draw_arc("0,1"); }
2017-11-05 00:58:53 -04:00
if(o == "arc_r"){ this.draw_arc("0,0"); }
if(o == "bezier"){ this.draw_bezier(); }
2017-11-05 14:38:14 -05:00
if(o == "close"){ this.draw_close(); }
2017-11-14 16:11:09 -05:00
if(o == "thickness"){ this.mod_thickness(); }
if(o == "linecap"){ this.mod_linecap(); }
2017-11-10 14:46:11 -05:00
if(o == "mirror"){ this.mod_mirror(); }
2017-11-26 23:35:53 -05:00
if(o == "fill"){ this.toggle_fill(); }
2018-01-12 03:09:26 -05:00
if(o == "export"){ this.save(); }
2016-12-31 12:18:01 -05:00
}
this.mouse_move = function(e)
{
2017-11-07 06:04:47 -05:00
var pos = this.position_in_grid(new Pos(e.clientX,e.clientY));
pos = this.position_on_grid(pos);
2016-12-31 12:18:01 -05:00
2017-11-13 13:52:53 -05:00
if(dotgrid.translation){ dotgrid.translation.to = pos; }
2017-11-12 22:54:56 -05:00
2017-12-02 21:46:27 -05:00
var o = e.target.getAttribute("ar");
dotgrid.preview(o);
2017-11-17 01:45:15 -05:00
dotgrid.move_cursor(pos)
2017-11-12 22:54:56 -05:00
dotgrid.guide.update();
2016-12-31 12:18:01 -05:00
}
this.mouse_up = function(e)
{
2017-11-07 06:04:47 -05:00
var pos = this.position_in_grid(new Pos(e.clientX,e.clientY));
pos = this.position_on_grid(pos);
2017-11-13 13:52:53 -05:00
if(e.altKey){ return; }
2017-11-13 20:28:06 -05:00
if(pos.x>0) { dotgrid.translation = null; return; }
if(dotgrid.translation && !dotgrid.translation.to.is_equal(dotgrid.translation.from) ){
2017-11-12 22:54:56 -05:00
dotgrid.translate(dotgrid.translation);
return;
}
dotgrid.translation = null;
2017-11-05 23:35:29 -05:00
2017-11-17 01:45:15 -05:00
this.add_point(pos)
this.draw();
}
this.move_cursor = function(pos)
{
if(pos.x>0) {
this.cursor.style.visibility = "hidden"
} else {
if(this.cursor.style.visibility == "hidden") {
this.cursor.style.transition = "initial"
}
this.cursor.style.visibility = "visible"
this.cursor.style.left = Math.floor(-(pos.x-this.grid_width));
this.cursor.style.top = Math.floor(pos.y+this.grid_height);
this.cursor_coord.className = -pos.x > this.width/2 ? "fl left" : "fl"
this.cursor_coord.textContent = parseInt(-pos.x/this.grid_width)+","+parseInt(pos.y/this.grid_height);
window.setTimeout(() => dotgrid.cursor.style.transition = "all 50ms", 17 /*one frame*/)
}
}
2018-01-12 15:00:53 -05:00
this.add_point = function(pos = new Pos(0,0))
2017-11-17 01:45:15 -05:00
{
2017-11-12 16:47:34 -05:00
if(from === null){ this.set_from(pos.scale(1/this.scale)); }
else if(to === null){ this.set_to(pos.scale(1/this.scale)); }
else{ this.set_end(pos.scale(1/this.scale)); }
2016-12-31 12:18:01 -05:00
}
2017-11-13 13:52:53 -05:00
this.handle_at = function(pos)
{
for(id in dotgrid.segments){
var segment = dotgrid.segments[id];
if(segment.from && segment.from.is_equal(pos)){ return true; }
if(segment.to && segment.to.is_equal(pos)){ return true; }
if(segment.end && segment.end.is_equal(pos)){ return true; }
}
return false;
}
2017-11-12 22:54:56 -05:00
this.translate = function(t)
{
for(id in dotgrid.segments){
var segment = dotgrid.segments[id];
if(segment.from && segment.from.is_equal(dotgrid.translation.from)){ segment.from = new Pos(-dotgrid.translation.to.x,dotgrid.translation.to.y)}
if(segment.to && segment.to.is_equal(dotgrid.translation.from)){ segment.to = new Pos(-dotgrid.translation.to.x,dotgrid.translation.to.y)}
if(segment.end && segment.end.is_equal(dotgrid.translation.from)){ segment.end = new Pos(-dotgrid.translation.to.x,dotgrid.translation.to.y)}
}
dotgrid.translation = null;
2017-11-13 16:41:16 -05:00
dotgrid.reset();
2017-11-12 22:54:56 -05:00
dotgrid.draw();
}
2017-12-02 21:46:27 -05:00
this.preview = function(operation)
{
if(from && to && operation == "line"){
2017-12-26 17:12:23 -05:00
var d = new Path_Line(from.mirror(),to.mirror(),end ? end.mirror() : null).to_segment();
2017-12-02 21:46:27 -05:00
this.preview_el.innerHTML = "<path d='"+d+"'></path>"
return;
}
2017-12-02 21:47:11 -05:00
else if(from && to && operation == "arc_c"){
2017-12-26 17:12:23 -05:00
var d = new Path_Arc(from.mirror(),to.mirror(),"0,1",end ? end.mirror() : null).to_segment();
2017-12-02 21:46:27 -05:00
this.preview_el.innerHTML = "<path d='"+d+"'></path>"
return;
}
2017-12-02 21:47:11 -05:00
else if(from && to && operation == "arc_r"){
2017-12-26 17:12:23 -05:00
var d = new Path_Arc(from.mirror(),to.mirror(),"0,0",end ? end.mirror() : null).to_segment();
2017-12-02 21:46:27 -05:00
this.preview_el.innerHTML = "<path d='"+d+"'></path>"
return;
}
2017-12-02 21:47:11 -05:00
else if(from && to && operation == "bezier"){
2017-12-26 17:12:23 -05:00
var d = new Path_Bezier(from.mirror(),to.mirror(),end ? end.mirror() : null).to_segment();
2017-12-02 21:46:27 -05:00
this.preview_el.innerHTML = "<path d='"+d+"'></path>"
return;
}
this.preview_el.innerHTML = "";
}
2016-12-31 12:18:01 -05:00
// Setters
this.set_from = function(pos)
{
2017-11-21 17:58:07 -05:00
from = pos.mirror().clamp(0,this.width).mirror();
2016-12-31 12:18:01 -05:00
2017-11-13 14:45:31 -05:00
cursor_from.style.left = Math.floor(-from.x*this.scale + this.grid_width);
cursor_from.style.top = Math.floor(from.y*this.scale + this.grid_height);
2016-12-31 12:18:01 -05:00
}
this.set_to = function(pos)
{
2017-11-21 17:58:07 -05:00
to = pos.mirror().clamp(0,this.width).mirror();
2016-12-31 12:18:01 -05:00
2017-11-13 14:45:31 -05:00
cursor_to.style.left = Math.floor(-to.x*this.scale + this.grid_width);
cursor_to.style.top = Math.floor(to.y*this.scale + this.grid_height);
2016-12-31 12:18:01 -05:00
}
2017-11-04 19:59:11 -04:00
this.set_end = function(pos)
{
2017-11-21 17:58:07 -05:00
end = pos.mirror().clamp(0,this.width).mirror();
2017-11-04 19:59:11 -04:00
2017-11-13 14:45:31 -05:00
cursor_end.style.left = Math.floor(-end.x*this.scale + this.grid_width);
cursor_end.style.top = Math.floor(end.y*this.scale + this.grid_height);
2017-11-04 19:59:11 -04:00
}
2017-11-07 16:40:13 -05:00
this.delete_at = function(pos)
{
var segs = [];
for(id in this.segments){
var s = this.segments[id];
if(s.from && s.from.is_equal(pos)){ continue; }
if(s.to && s.to.is_equal(pos)){ continue; }
if(s.end && s.end.is_equal(pos)){ continue; }
segs.push(s);
}
this.segments = segs;
this.draw();
}
2017-11-04 19:59:11 -04:00
this.mod_thickness = function(mod)
{
2017-11-14 16:11:09 -05:00
if(!mod){ mod = 1; this.thickness = this.thickness > 30 ? 1 : this.thickness }
2017-11-07 01:59:45 -05:00
this.thickness = Math.max(this.thickness+mod,0);
2017-11-10 14:46:11 -05:00
this.cursor_coord.textContent = this.thickness;
2017-11-04 19:59:11 -04:00
this.draw();
}
this.mod_linecap_index = 1;
this.mod_linecap = function(mod)
{
var a = ["butt","square","round"];
this.mod_linecap_index += 1;
this.linecap = a[this.mod_linecap_index % a.length];
this.draw();
}
2017-11-07 06:04:47 -05:00
this.mod_move = function(move)
2017-11-04 19:59:11 -04:00
{
2017-11-05 19:18:57 -05:00
if(!to && !end && from){
2017-11-17 01:45:15 -05:00
var pos = new Pos(from.x-(move.x),from.y+(move.y))
this.set_from(pos)
this.move_cursor(pos)
2017-11-04 20:52:05 -04:00
this.draw();
return;
}
2017-11-05 19:18:57 -05:00
if(!end && to){
2017-11-17 01:45:15 -05:00
var pos = new Pos(to.x-(move.x),to.y+(move.y))
this.set_to(pos)
this.move_cursor(pos)
2017-11-04 20:52:05 -04:00
this.draw();
return;
2017-11-04 19:59:11 -04:00
}
2017-11-05 19:18:57 -05:00
if(end){
2017-11-17 01:45:15 -05:00
var pos = new Pos(end.x-(move.x),end.y+(move.y))
this.set_end(pos)
this.move_cursor(pos)
2017-11-05 19:18:57 -05:00
this.draw();
2017-11-05 23:35:29 -05:00
return;
2017-11-05 19:18:57 -05:00
}
2017-11-04 19:59:11 -04:00
this.draw();
}
2017-11-10 14:46:11 -05:00
this.mirror_index = 0;
this.mod_mirror = function()
{
this.mirror_index += 1;
this.mirror_index = this.mirror_index > 3 ? 0 : this.mirror_index;
this.draw();
}
2017-11-05 20:10:37 -05:00
this.toggle_fill = function()
{
dotgrid.fill = dotgrid.fill ? false : true;
this.draw();
}
2018-01-08 02:53:54 -05:00
this.set_size = function(size = {width:300,height:300},interface = true)
2017-11-21 17:58:07 -05:00
{
var win = require('electron').remote.getCurrentWindow();
2018-01-08 02:53:54 -05:00
win.setSize(size.width+100,size.height+100+(interface ? 10 : 0),true);
2017-11-21 17:58:07 -05:00
this.width = size.width
this.height = size.height
this.element.style.width = size.width+10
this.element.style.height = size.height+10
this.grid_x = size.width/15
this.grid_y = size.height/15
this.svg_el.setAttribute("width",size.width+"px");
this.svg_el.setAttribute("height",size.height+"px");
2017-12-02 21:57:28 -05:00
this.preview_el.style.width = size.width+10
this.preview_el.style.height = size.height+10
this.preview_el.setAttribute("width",size.width+"px");
this.preview_el.setAttribute("height",size.height+"px");
2017-11-21 17:58:07 -05:00
dotgrid.guide.resize(size);
this.interface.update();
this.draw();
}
2017-11-12 16:47:34 -05:00
this.draw = function(exp = false)
2017-11-04 19:59:11 -04:00
{
var d = "";
var prev = "";
for(id in this.segments){
var segment = this.segments[id];
d += segment.to_segment(prev)+" ";
prev = segment;
}
this.path.setAttribute("d",d);
this.svg_el.style.width = this.width;
this.svg_el.style.height = this.height;
this.svg_el.style.stroke = this.color;
this.svg_el.style.strokeLinecap = this.linecap;
2017-11-12 16:47:34 -05:00
this.svg_el.style.strokeWidth = this.thickness*this.scale;
2017-11-26 23:35:53 -05:00
this.svg_el.style.fill = this.fill ? this.theme.active.f_high : "none";
2017-11-04 19:59:11 -04:00
2017-11-10 14:46:11 -05:00
// Draw Mirror
if(this.mirror_index == 1){
this.mirror_path.setAttribute("d",d);
2017-11-12 16:47:34 -05:00
this.mirror_path.setAttribute("transform","translate("+(this.width - (this.offset.x*this.scale))+","+(this.offset.y*this.scale)+"),scale(-1,1)")
2017-11-10 14:46:11 -05:00
}
else if(this.mirror_index == 2){
this.mirror_path.setAttribute("d",d);
2017-11-12 16:47:34 -05:00
this.mirror_path.setAttribute("transform","translate("+((this.offset.x*this.scale))+","+(this.height - (this.offset.y*this.scale))+"),scale(1,-1)")
2017-11-10 14:46:11 -05:00
}
else if(this.mirror_index == 3){
this.mirror_path.setAttribute("d",d);
2017-11-12 16:47:34 -05:00
this.mirror_path.setAttribute("transform","translate("+(this.width -(this.offset.x*this.scale))+","+(this.height - (this.offset.y*this.scale))+"),scale(-1,-1)")
2017-11-10 14:46:11 -05:00
}
else{
this.mirror_path.setAttribute("d",'M0,0');
this.mirror_path.setAttribute("transform","")
}
2017-11-05 19:18:57 -05:00
2017-11-12 16:47:34 -05:00
this.offset_el.setAttribute("transform","translate("+(this.offset.x*this.scale)+","+(this.offset.y*this.scale)+")")
2017-11-05 14:53:59 -05:00
2017-11-09 14:47:06 -05:00
this.render.draw();
2017-11-14 16:11:09 -05:00
this.interface.update();
2017-11-12 22:54:56 -05:00
this.guide.update();
2017-11-04 19:59:11 -04:00
}
2016-12-31 12:18:01 -05:00
// Draw
2018-01-12 03:46:09 -05:00
2016-12-31 12:37:10 -05:00
this.reset = function()
2016-12-31 12:18:01 -05:00
{
from = null;
to = null;
2017-11-04 19:59:11 -04:00
end = null;
2016-12-31 12:37:10 -05:00
cursor_from.style.left = -100;
cursor_from.style.top = -100;
cursor_to.style.left = -100;
cursor_to.style.top = -100;
2017-11-04 19:59:11 -04:00
cursor_end.style.left = -100;
cursor_end.style.top = -100;
2016-12-31 12:18:01 -05:00
}
2017-11-07 16:40:13 -05:00
this.clear = function()
{
this.reset();
this.segments = [];
this.thickness = 10
this.linecap = "square"
this.color = "#000000"
this.draw();
}
2018-01-11 22:22:50 -05:00
this.drag = function(e)
{
e.preventDefault();
e.stopPropagation();
var file = e.dataTransfer.files[0];
if(!file.name || !file.name.indexOf(".dot") < 0){ console.log("Dotgrid","Not a dot file"); return; }
var reader = new FileReader();
reader.onload = function(e){
dotgrid.serializer.deserialize(JSON.parse(e.target.result.toString().trim()));
dotgrid.draw();
};
reader.readAsText(file);
}
2017-11-21 16:24:25 -05:00
this.copy = function(e)
{
if(this.segments.length == 0){ return; }
this.scale = 1
this.width = 300
this.height = 300
2018-01-11 22:22:50 -05:00
this.draw();
2017-11-21 16:24:25 -05:00
2018-01-11 22:22:50 -05:00
var svg = this.svg_el.outerHTML
e.clipboardData.items.add(JSON.stringify(this.serializer.serialize()), "text/plain");
2017-11-21 16:24:25 -05:00
e.clipboardData.items.add(svg, "text/html");
e.clipboardData.items.add(svg, "text/svg+xml");
e.preventDefault();
}
this.paste = function(e)
{
var data = e.clipboardData.getData("text/plain");
try {
data = JSON.parse(data.trim());
if (!data || !data.dotgrid) throw null;
2017-11-21 16:24:25 -05:00
} catch (err) {
// Not a dotgrid JSON.
return;
}
this.serializer.deserialize(data);
this.draw();
}
2016-12-31 12:18:01 -05:00
// Normalizers
2017-11-07 06:04:47 -05:00
this.position_in_grid = function(pos)
2016-12-31 12:18:01 -05:00
{
2017-11-12 16:47:34 -05:00
return new Pos((window.innerWidth/2) - (this.width/2) - pos.x,pos.y - (30+10*(this.scale)))
2016-12-31 12:18:01 -05:00
}
2017-11-12 22:54:56 -05:00
this.position_on_grid = function(pos)
2016-12-31 12:18:01 -05:00
{
2017-11-14 16:57:17 -05:00
pos.y = pos.y - 7.5
2017-11-21 17:58:07 -05:00
pos.x = pos.x + 7.5
2017-11-07 06:04:47 -05:00
x = Math.round(pos.x/this.grid_width)*this.grid_width
2017-11-12 16:47:34 -05:00
y = Math.round(pos.y/this.grid_height)*this.grid_height
2017-11-07 01:55:51 -05:00
off = (x<-this.width || x>0 || y>this.height || y<0)
2017-11-12 22:54:56 -05:00
if(off) {
2017-11-07 01:55:51 -05:00
x = 50
y = -50
}
2017-11-07 06:04:47 -05:00
return new Pos(x,y);
2016-12-31 10:00:57 -05:00
}
2017-11-12 16:47:34 -05:00
2017-11-14 16:11:09 -05:00
// To Clean
this.from = function()
{
return from;
}
this.to = function()
{
return to;
}
this.end = function()
{
return end;
}
2017-11-05 23:35:29 -05:00
}
2017-11-06 21:10:09 -05:00
2017-11-12 16:47:34 -05:00
window.addEventListener('resize', function(e)
{
dotgrid.draw()
}, false);
2018-01-11 22:22:50 -05:00
window.addEventListener('dragover',function(e)
2017-11-06 21:10:09 -05:00
{
e.stopPropagation();
2018-01-11 22:22:50 -05:00
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
});