pointvec/desktop/sources/scripts/dotgrid.js

523 lines
20 KiB
JavaScript
Raw Normal View History

2018-03-06 22:08:34 -05:00
function Dotgrid(width,height,grid_x,grid_y,block_x,block_y)
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();
2018-01-31 04:10:48 -05:00
this.history = new History();
this.guide = new Guide();
this.render = new Render();
this.tool = new Tool();
2018-03-06 19:50:41 -05:00
this.picker = new Picker();
2017-11-06 21:10:09 -05:00
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
2017-11-05 20:10:37 -05:00
this.element = document.createElement("div");
this.element.id = "dotgrid";
2017-11-12 16:47:34 -05:00
2017-11-04 19:59:11 -04:00
this.svg_el = null;
2018-02-06 14:59:16 -05:00
this.layer_1 = document.createElementNS("http://www.w3.org/2000/svg", "path"); this.layer_1.id = "layer_1"; this.layer_1.style.stroke = "black";
this.layer_2 = document.createElementNS("http://www.w3.org/2000/svg", "path"); this.layer_2.id = "layer_2"; this.layer_2.style.stroke = "#999";
this.layer_3 = document.createElementNS("http://www.w3.org/2000/svg", "path"); this.layer_3.id = "layer_3"; this.layer_3.style.stroke = "#ccc";
2018-04-15 23:13:29 -04:00
2018-05-08 17:33:17 -04:00
this.cursor = { pos:{x:0,y:0},translation:null,multi:false,updated:0 }
2018-05-07 19:32:00 -04:00
2016-12-31 10:00:57 -05:00
this.install = function()
2018-01-11 22:22:50 -05:00
{
2018-05-08 05:05:19 -04:00
document.getElementById("app").appendChild(this.element);
document.getElementById("app").appendChild(this.guide.el);
2018-05-07 21:17:41 -04:00
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-05-08 05:15:40 -04:00
this.svg_el.id = "vector"
2018-01-11 22:22:50 -05:00
this.svg_el.setAttribute("class","vector");
2018-05-07 18:47:09 -04:00
this.svg_el.setAttribute("width",this.tool.settings.size.width+"px");
this.svg_el.setAttribute("height",this.tool.settings.size.height+"px");
2017-11-04 19:59:11 -04:00
this.svg_el.setAttribute("xmlns","http://www.w3.org/2000/svg");
this.svg_el.setAttribute("baseProfile","full");
this.svg_el.setAttribute("version","1.1");
2018-03-06 17:26:18 -05:00
2018-05-07 18:47:09 -04:00
this.svg_el.style.width = this.tool.settings.size.width;
this.svg_el.style.height = this.tool.settings.size.height;
2017-11-26 23:35:53 -05:00
this.svg_el.style.fill = "none";
2018-03-06 18:12:00 -05:00
this.svg_el.style.strokeWidth = this.tool.style().thickness;
2017-11-04 19:59:11 -04:00
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");
2018-05-07 18:47:09 -04:00
this.preview_el.setAttribute("width",this.tool.settings.size.width+"px");
this.preview_el.setAttribute("height",this.tool.settings.size.height+"px");
2017-12-02 21:46:27 -05:00
this.preview_el.setAttribute("xmlns","http://www.w3.org/2000/svg");
this.preview_el.setAttribute("baseProfile","full");
this.preview_el.setAttribute("version","1.1");
2018-05-07 18:47:09 -04:00
this.preview_el.style.width = this.tool.settings.size.width;
this.preview_el.style.height = this.tool.settings.size.height;
2018-05-07 01:03:35 -04:00
this.preview_el.style.strokeWidth = this.tool.style().thickness;
this.preview_el.style.strokeLinecap = this.tool.style().strokeLinecap;
this.preview_el.style.fill = this.tool.style().fill;
2017-12-02 21:46:27 -05:00
this.element.appendChild(this.preview_el);
2017-11-05 19:18:57 -05:00
2018-05-07 21:17:41 -04:00
this.svg_el.appendChild(this.layer_3);
this.svg_el.appendChild(this.layer_2);
this.svg_el.appendChild(this.layer_1);
2017-11-04 20:52:05 -04:00
2017-11-06 21:10:09 -05:00
this.theme.start();
2018-03-06 17:57:52 -05:00
this.tool.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-13 15:11:57 -05:00
this.controller.add("default","Edit","Copy",() => { document.execCommand('copy'); },"CmdOrCtrl+C");
2018-02-07 01:44:18 -05:00
this.controller.add("default","Edit","Cut",() => { document.execCommand('cut'); },"CmdOrCtrl+X");
2018-01-13 15:11:57 -05:00
this.controller.add("default","Edit","Paste",() => { document.execCommand('paste'); },"CmdOrCtrl+V");
2018-02-05 22:27:48 -05:00
this.controller.add("default","Edit","Undo",() => { dotgrid.tool.undo(); },"CmdOrCtrl+Z");
this.controller.add("default","Edit","Redo",() => { dotgrid.tool.redo(); },"CmdOrCtrl+Shift+Z");
this.controller.add("default","Edit","Delete",() => { dotgrid.tool.remove_segment(); },"Backspace");
2018-02-05 20:23:25 -05:00
this.controller.add("default","Edit","Deselect",() => { dotgrid.tool.clear(); },"Esc");
2018-01-12 03:46:09 -05:00
this.controller.add("default","Stroke","Line",() => { dotgrid.tool.cast("line"); },"A");
2018-02-05 22:27:48 -05:00
this.controller.add("default","Stroke","Arc",() => { dotgrid.tool.cast("arc_c"); },"S"); // 0,1
this.controller.add("default","Stroke","Arc Rev",() => { dotgrid.tool.cast("arc_r")},"D"); // 0,0
this.controller.add("default","Stroke","Bezier",() => { dotgrid.tool.cast("bezier") },"F");
this.controller.add("default","Stroke","Connect",() => { dotgrid.tool.cast("close") },"Z");
2018-01-12 03:46:09 -05:00
2018-02-06 13:42:34 -05:00
this.controller.add("default","Effect","Linecap",() => { dotgrid.mod_linecap(); },"Q");
this.controller.add("default","Effect","Linejoin",() => { dotgrid.mod_linejoin(); },"W");
this.controller.add("default","Effect","Mirror",() => { dotgrid.mod_mirror(); },"E");
2018-03-07 02:43:46 -05:00
this.controller.add("default","Effect","Fill",() => { dotgrid.mod_fill(); },"R");
2018-03-21 03:10:09 -04:00
this.controller.add("default","Effect","Color",() => { dotgrid.picker.start(); },"G");
2018-01-29 15:48:58 -05:00
this.controller.add("default","Effect","Thicker",() => { dotgrid.mod_thickness(1) },"}");
this.controller.add("default","Effect","Thinner",() => { dotgrid.mod_thickness(-1) },"{");
this.controller.add("default","Effect","Thicker +5",() => { dotgrid.mod_thickness(5,true) },"]");
this.controller.add("default","Effect","Thinner -5",() => { dotgrid.mod_thickness(-5,true) },"[");
2018-02-06 13:42:34 -05:00
2018-05-07 19:32:00 -04:00
this.controller.add("default","Manual","Add Point",() => { dotgrid.tool.add_vertex(dotgrid.cursor.pos); dotgrid.draw() },"Enter");
this.controller.add("default","Manual","Move Up",() => { dotgrid.cursor.pos.y -= 15; dotgrid.draw() },"Up");
this.controller.add("default","Manual","Move Right",() => { dotgrid.cursor.pos.x -= 15; dotgrid.draw() },"Right");
this.controller.add("default","Manual","Move Down",() => { dotgrid.cursor.pos.y += 15; dotgrid.draw() },"Down");
this.controller.add("default","Manual","Move Left",() => { dotgrid.cursor.pos.x += 15; dotgrid.draw() },"Left");
this.controller.add("default","Manual","Remove Point",() => { dotgrid.tool.remove_segments_at(dotgrid.cursor.pos); },"CmdOrCtrl+Backspace");
2018-03-06 15:32:31 -05:00
this.controller.add("default","Layers","Foreground",() => { dotgrid.tool.select_layer(0) },"CmdOrCtrl+1");
this.controller.add("default","Layers","Middleground",() => { dotgrid.tool.select_layer(1) },"CmdOrCtrl+2");
this.controller.add("default","Layers","Background",() => { dotgrid.tool.select_layer(2) },"CmdOrCtrl+3");
2018-02-06 13:42:34 -05:00
2018-01-29 15:48:58 -05:00
this.controller.add("default","View","Tools",() => { dotgrid.interface.toggle(); },"U");
2018-01-12 22:24:18 -05:00
this.controller.add("default","View","Grid",() => { dotgrid.guide.toggle(); },"H");
2018-01-12 03:46:09 -05:00
2018-03-06 19:50:41 -05:00
this.controller.add("default","Mode","Picker",() => { dotgrid.picker.start(); },"CmdOrCtrl+P");
2018-02-18 21:29:07 -05:00
2018-03-06 19:50:41 -05:00
this.controller.add("picker","*","About",() => { require('electron').shell.openExternal('https://github.com/hundredrabbits/Dotgrid'); },"CmdOrCtrl+,");
this.controller.add("picker","*","Fullscreen",() => { app.toggle_fullscreen(); },"CmdOrCtrl+Enter");
this.controller.add("picker","*","Hide",() => { app.toggle_visible(); },"CmdOrCtrl+H");
this.controller.add("picker","*","Inspect",() => { app.inspect(); },"CmdOrCtrl+.");
this.controller.add("picker","*","Documentation",() => { dotgrid.controller.docs(); },"CmdOrCtrl+Esc");
this.controller.add("picker","*","Reset",() => { dotgrid.reset(); dotgrid.theme.reset(); },"CmdOrCtrl+Backspace");
this.controller.add("picker","*","Quit",() => { app.exit(); },"CmdOrCtrl+Q");
this.controller.add_role("picker","Edit","undo");
this.controller.add_role("picker","Edit","redo");
this.controller.add_role("picker","Edit","cut");
this.controller.add_role("picker","Edit","copy");
this.controller.add_role("picker","Edit","paste");
this.controller.add_role("picker","Edit","delete");
this.controller.add_role("picker","Edit","selectall");
this.controller.add("picker","Mode","Stop Picker Mode",() => { dotgrid.picker.stop(); },"Escape");
2018-02-18 21:29:07 -05:00
2018-01-12 03:09:26 -05:00
this.controller.commit();
2017-11-12 15:26:23 -05:00
2018-01-13 15:11:57 -05:00
document.addEventListener('mousedown', function(e){ dotgrid.mouse_down(e); }, false);
document.addEventListener('mousemove', function(e){ dotgrid.mouse_move(e); }, false);
2018-02-06 13:18:43 -05:00
document.addEventListener('contextmenu', function(e){ dotgrid.mouse_alt(e); }, false);
2018-01-13 15:11:57 -05:00
document.addEventListener('mouseup', function(e){ dotgrid.mouse_up(e);}, false);
document.addEventListener('copy', function(e){ dotgrid.copy(e); e.preventDefault(); }, false);
2018-02-07 01:44:18 -05:00
document.addEventListener('cut', function(e){ dotgrid.cut(e); e.preventDefault(); }, false);
2018-01-13 15:11:57 -05:00
document.addEventListener('paste', function(e){ dotgrid.paste(e); e.preventDefault(); }, false);
2018-01-11 22:22:50 -05:00
window.addEventListener('drop', dotgrid.drag);
dotgrid.set_size({width:300,height:300});
2018-01-31 04:10:48 -05:00
this.new();
2016-12-31 12:18:01 -05:00
}
2018-05-07 01:03:35 -04:00
// File
2018-01-12 03:46:09 -05:00
2018-01-12 03:09:26 -05:00
this.new = function()
{
2018-05-07 00:51:58 -04:00
this.set_size({width:300,height:300})
2018-02-05 22:27:48 -05:00
this.history.push(this.tool.layers);
2018-05-07 01:03:35 -04:00
this.clear();
2018-01-12 03:09:26 -05:00
}
this.save = function()
{
this.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());
2018-04-09 00:50:52 -04:00
fs.writeFile(fileName+'.grid', dotgrid.tool.export());
2018-01-12 03:09:26 -05:00
dotgrid.draw()
});
}
this.open = function()
{
2018-04-09 00:50:52 -04:00
var paths = dialog.showOpenDialog({properties: ['openFile'],filters:[{name:"Dotgrid Image",extensions:["dot","grid"]}]});
2018-01-12 03:09:26 -05:00
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; }
2018-02-07 15:34:17 -05:00
dotgrid.tool.replace(JSON.parse(data.toString().trim()));
2018-01-12 03:09:26 -05:00
dotgrid.draw();
});
}
2016-12-31 12:18:01 -05:00
// Cursor
this.mouse_down = function(e)
{
2018-05-08 17:33:17 -04:00
var o = e.target.getAttribute("ar");
if(o){
if(o == "line"){ this.tool.cast("line"); return; }
if(o == "arc_c"){ this.tool.cast("arc_c"); return;}
if(o == "arc_r"){ this.tool.cast("arc_r"); return; }
if(o == "bezier"){ this.tool.cast("bezier"); return; }
if(o == "close"){ this.tool.cast("close"); return; }
if(o == "thickness"){ this.mod_thickness(); return; }
if(o == "linecap"){ this.mod_linecap(); return; }
if(o == "linejoin"){ this.mod_linejoin(); return; }
if(o == "mirror"){ this.mod_mirror(); return; }
if(o == "fill"){ this.mod_fill(); return; }
if(o == "color"){ setTimeout(()=>{ this.picker.start(); }, 100); return; }
if(o == "depth"){ this.tool.select_next_layer(); return; }
e.preventDefault();
}
var pos = this.position_in_grid({x:e.clientX+5,y:e.clientY-5});
pos = this.position_on_grid(pos);
2017-11-13 13:52:53 -05:00
2018-02-05 23:19:34 -05:00
if(e.altKey){ dotgrid.tool.remove_segments_at(pos); return; }
2018-03-21 03:35:28 -04:00
if(dotgrid.tool.vertex_at(pos)){
2018-05-07 19:32:00 -04:00
console.log("Begin translation"); dotgrid.cursor.translation = {from:pos,to:pos};
if(e.shiftKey){ console.log("Begin translation(multi)"); dotgrid.cursor.multi = true; }
2018-03-21 03:35:28 -04:00
}
2016-12-31 12:18:01 -05:00
}
this.mouse_move = function(e)
{
2018-03-06 22:08:34 -05:00
var pos = this.position_in_grid({x:e.clientX+5,y:e.clientY-5}); pos = this.position_on_grid(pos);
2016-12-31 12:18:01 -05:00
2018-05-07 19:32:00 -04:00
this.cursor.pos = pos;
2018-05-08 17:33:17 -04:00
this.cursor.updated = new Date().getTime();
2018-05-07 19:32:00 -04:00
if(dotgrid.cursor.translation && (Math.abs(dotgrid.cursor.translation.from.x) != Math.abs(pos.x) || Math.abs(dotgrid.cursor.translation.from.y) != Math.abs(pos.y))){ dotgrid.cursor.translation.to = pos; }
2017-11-12 22:54:56 -05:00
2018-02-05 23:19:34 -05:00
dotgrid.preview(e.target.getAttribute("ar"));
2018-05-07 19:32:00 -04:00
dotgrid.guide.refresh(pos);
2018-03-06 21:45:41 -05:00
e.preventDefault();
2016-12-31 12:18:01 -05:00
}
this.mouse_up = function(e)
{
2018-05-08 17:33:17 -04:00
if(e.target.getAttribute("ar")){ return } // If clicking on interface
var pos = this.position_in_grid({x:e.clientX+5,y:e.clientY-5});
pos = this.position_on_grid(pos);
2017-11-07 06:04:47 -05:00
2017-11-13 13:52:53 -05:00
if(e.altKey){ return; }
2018-05-07 19:32:00 -04:00
if(pos.x > 0) { dotgrid.cursor.translation = null; return; }
2017-11-13 20:28:06 -05:00
2018-05-07 19:32:00 -04:00
if(dotgrid.cursor.translation && (Math.abs(dotgrid.cursor.translation.from.x) != Math.abs(dotgrid.cursor.translation.to.x) || Math.abs(dotgrid.cursor.translation.from.y) != Math.abs(dotgrid.cursor.translation.to.y))){
if(dotgrid.cursor.multi){
dotgrid.tool.translate_multi(dotgrid.cursor.translation.from,dotgrid.cursor.translation.to);
2018-03-21 03:35:28 -04:00
}
else{
2018-05-07 19:32:00 -04:00
dotgrid.tool.translate(dotgrid.cursor.translation.from,dotgrid.cursor.translation.to);
2018-03-21 03:35:28 -04:00
}
2018-05-07 19:32:00 -04:00
dotgrid.cursor.translation = null;
dotgrid.cursor.multi = null;
2018-02-06 14:16:01 -05:00
this.draw();
2018-03-06 21:45:41 -05:00
e.preventDefault();
2017-11-12 22:54:56 -05:00
return;
}
this.tool.add_vertex({x:pos.x * -1,y:pos.y});
2018-05-07 19:32:00 -04:00
dotgrid.cursor.translation = null;
2017-11-17 01:45:15 -05:00
this.draw();
2018-03-06 21:45:41 -05:00
e.preventDefault();
2017-11-17 01:45:15 -05:00
}
2018-02-06 13:18:43 -05:00
this.mouse_alt = function(e)
{
2018-03-06 22:08:34 -05:00
var pos = this.position_in_grid({x:e.clientX+5,y:e.clientY-5}); pos = this.position_on_grid(pos);
2018-02-06 13:18:43 -05:00
dotgrid.tool.remove_segments_at(pos);
2018-02-16 20:42:52 -05:00
e.preventDefault();
2018-02-06 13:18:43 -05:00
2018-05-07 01:03:35 -04:00
setTimeout(() => { dotgrid.tool.clear(); },150);
2018-04-02 17:48:36 -04:00
}
2018-04-02 18:25:14 -04:00
this.preview_prev = null
2017-12-02 21:46:27 -05:00
this.preview = function(operation)
{
2018-05-08 17:33:17 -04:00
if(this.preview_prev == operation){ return; }
2018-05-06 19:23:02 -04:00
2018-05-08 17:33:17 -04:00
this.preview_el.innerHTML = !operation || operation == "close" || !dotgrid.tool.can_cast(operation) ? `<path d='M0,0'></path>` : `<path d='${dotgrid.tool.path(dotgrid.tool.index,[{type:operation,verteces:dotgrid.tool.verteces}])}'></path>`;
2018-04-02 18:25:14 -04:00
this.preview_prev = operation;
2017-11-04 19:59:11 -04:00
}
2018-02-05 23:25:57 -05:00
// Toggles
2018-01-29 15:48:58 -05:00
this.mod_thickness = function(mod,step = false)
2017-11-04 19:59:11 -04:00
{
2018-03-06 17:26:18 -05:00
if(!mod){ mod = 1; this.tool.style().thickness = this.tool.style().thickness > 30 ? 1 : this.tool.style().thickness }
2017-11-14 16:11:09 -05:00
2018-01-29 15:48:58 -05:00
if(step){
2018-03-06 17:26:18 -05:00
this.tool.style().thickness = parseInt(this.tool.style().thickness/5) * 5;
2018-01-29 15:48:58 -05:00
}
2018-03-06 17:26:18 -05:00
this.tool.style().thickness = Math.max(this.tool.style().thickness+mod,0);
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;
2018-03-06 17:37:01 -05:00
this.tool.style().strokeLinecap = a[this.mod_linecap_index % a.length];
2017-11-04 19:59:11 -04:00
this.draw();
}
2018-02-04 17:45:58 -05:00
this.mod_linejoin_index = 1;
this.mod_linejoin = function(mod)
{
var a = ["miter","round","bevel"];
this.mod_linejoin_index += 1;
2018-03-06 17:37:01 -05:00
this.tool.style().strokeLinejoin = a[this.mod_linejoin_index % a.length];
2018-02-04 17:45:58 -05:00
this.draw();
}
2017-11-10 14:46:11 -05:00
this.mod_mirror = function()
{
2018-04-16 00:24:09 -04:00
this.tool.style().mirror_style += 1;
this.tool.style().mirror_style = this.tool.style().mirror_style > 7 ? 0 : this.tool.style().mirror_style;
2017-11-10 14:46:11 -05:00
this.draw();
}
2018-03-07 02:43:46 -05:00
this.mod_fill = function()
{
this.tool.style().fill = this.tool.style().fill == "none" ? this.tool.style().color : "none";
this.draw();
}
2018-05-07 01:03:35 -04:00
// Basics
2018-05-07 00:51:58 -04:00
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
{
2018-05-07 00:51:58 -04:00
size = { width:clamp(parseInt(size.width/15)*15,100,1000),height:clamp(parseInt(size.height/15)*15,100,1000)}
2018-05-06 19:15:23 -04:00
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
2018-05-07 18:47:09 -04:00
this.tool.settings.size.width = size.width
this.tool.settings.size.height = size.height
2017-11-21 17:58:07 -05:00
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");
2018-05-08 05:15:40 -04:00
this.preview_el.style.width = size.width
this.preview_el.style.height = size.height
2017-12-02 21:57:28 -05:00
this.preview_el.setAttribute("width",size.width+"px");
this.preview_el.setAttribute("height",size.height+"px");
2017-11-21 17:58:07 -05:00
2018-05-07 18:47:09 -04:00
this.element.style.width = this.tool.settings.size.width;
this.element.style.height = this.tool.settings.size.height;
2018-05-07 00:24:01 -04:00
2018-05-07 18:47:09 -04:00
this.grid_width = this.tool.settings.size.width/this.grid_x;
this.grid_height = this.tool.settings.size.height/this.grid_y;
2018-05-07 00:51:58 -04:00
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
{
2018-04-02 17:48:36 -04:00
console.log("draw")
2018-05-07 01:03:35 -04:00
2018-02-06 13:42:34 -05:00
var paths = this.tool.paths();
2018-02-05 23:25:57 -05:00
var d = this.tool.path();
2018-05-07 01:03:35 -04:00
2018-02-06 13:42:34 -05:00
this.layer_1.setAttribute("d",paths[0]);
this.layer_2.setAttribute("d",paths[1]);
this.layer_3.setAttribute("d",paths[2]);
2018-05-07 18:47:09 -04:00
this.svg_el.style.width = this.tool.settings.size.width;
this.svg_el.style.height = this.tool.settings.size.height;
2017-11-04 19:59:11 -04:00
2018-03-06 17:26:18 -05:00
this.layer_1.style.strokeWidth = this.tool.styles[0].thickness;
2018-03-06 17:37:01 -05:00
this.layer_1.style.strokeLinecap = this.tool.styles[0].strokeLinecap;
this.layer_1.style.strokeLinejoin = this.tool.styles[0].strokeLinejoin;
2018-03-06 17:57:52 -05:00
this.layer_1.style.stroke = this.tool.styles[0].color;
2018-03-07 02:43:46 -05:00
this.layer_1.style.fill = this.tool.styles[0].fill;
2018-04-15 23:13:29 -04:00
2018-03-06 17:57:52 -05:00
this.layer_2.style.strokeWidth = this.tool.styles[1].thickness;
this.layer_2.style.strokeLinecap = this.tool.styles[1].strokeLinecap;
2018-03-06 17:37:01 -05:00
this.layer_2.style.strokeLinejoin = this.tool.styles[1].strokeLinejoin;
2018-03-06 17:57:52 -05:00
this.layer_2.style.stroke = this.tool.styles[1].color;
2018-03-07 02:43:46 -05:00
this.layer_2.style.fill = this.tool.styles[1].fill;
2018-04-15 23:13:29 -04:00
2018-03-06 17:57:52 -05:00
this.layer_3.style.strokeWidth = this.tool.styles[2].thickness;
this.layer_3.style.strokeLinecap = this.tool.styles[2].strokeLinecap;
2018-03-06 17:37:01 -05:00
this.layer_3.style.strokeLinejoin = this.tool.styles[2].strokeLinejoin;
2018-03-06 17:57:52 -05:00
this.layer_3.style.stroke = this.tool.styles[2].color;
2018-03-07 02:43:46 -05:00
this.layer_3.style.fill = this.tool.styles[2].fill;
2017-11-05 14:53:59 -05:00
2018-02-05 23:19:34 -05:00
this.preview();
2017-11-09 14:47:06 -05:00
this.render.draw();
2017-11-14 16:11:09 -05:00
this.interface.update();
2018-05-07 18:59:38 -04:00
this.guide.refresh();
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
{
2018-02-06 14:16:01 -05:00
this.tool.clear();
2016-12-31 12:18:01 -05:00
}
2017-11-07 16:40:13 -05:00
this.clear = function()
{
2018-02-04 16:09:46 -05:00
this.history.clear();
2018-02-06 14:16:01 -05:00
this.tool.reset();
2017-11-07 16:40:13 -05:00
this.reset();
this.draw();
}
2018-01-11 22:22:50 -05:00
this.drag = function(e)
{
e.preventDefault();
e.stopPropagation();
var file = e.dataTransfer.files[0];
2018-04-09 00:50:52 -04:00
if(!file.path || file.path.indexOf(".dot") < 0 && file.path.indexOf(".grid") < 0){ console.log("Dotgrid","Not a dot file"); return; }
2018-01-11 22:22:50 -05:00
var reader = new FileReader();
reader.onload = function(e){
2018-02-07 15:34:17 -05:00
dotgrid.tool.replace(JSON.parse(e.target.result.toString().trim()));
2018-01-11 22:22:50 -05:00
dotgrid.draw();
};
reader.readAsText(file);
}
2017-11-21 16:24:25 -05:00
this.copy = function(e)
{
2018-01-13 15:11:57 -05:00
dotgrid.draw();
var svg = dotgrid.svg_el.outerHTML;
2018-03-06 21:57:54 -05:00
e.clipboardData.setData('text/source', dotgrid.tool.export(dotgrid.tool.layer()));
e.clipboardData.setData('text/plain', dotgrid.tool.path());
2018-01-13 15:11:57 -05:00
e.clipboardData.setData('text/html', svg);
e.clipboardData.setData('text/svg+xml', svg);
2018-02-07 01:44:18 -05:00
this.draw();
}
this.cut = function(e)
{
dotgrid.draw();
var svg = dotgrid.svg_el.outerHTML;
e.clipboardData.setData('text/plain', dotgrid.tool.export(dotgrid.tool.layer()));
e.clipboardData.setData('text/html', svg);
e.clipboardData.setData('text/svg+xml', svg);
dotgrid.tool.layers[dotgrid.tool.index] = [];
this.draw();
2017-11-21 16:24:25 -05:00
}
this.paste = function(e)
{
2018-03-06 21:57:54 -05:00
var data = e.clipboardData.getData("text/source");
2018-05-07 21:32:06 -04:00
if(is_json(data)){
data = JSON.parse(data.trim());
dotgrid.tool.import(data);
}
2017-11-21 16:24:25 -05:00
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
{
2018-05-07 18:47:09 -04:00
return {x:(window.innerWidth/2) - (this.tool.settings.size.width/2) - pos.x,y:pos.y - (30+10)}
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
2018-05-07 00:51:58 -04:00
2018-05-08 05:15:40 -04:00
x = clamp(x * -1,0,this.tool.settings.size.width)
y = clamp(y,0,this.tool.settings.size.height)
return {x:x*-1,y:y};
}
2018-05-07 21:32:06 -04:00
2018-05-08 17:33:17 -04:00
function is_json(text){ try{ JSON.parse(text);return true; } catch(error){ return false; }}
2018-05-07 01:03:35 -04:00
function pos_is_equal(a,b){ return a && b && a.x == b.x && a.y == b.y }
2018-05-07 00:51:58 -04:00
function clamp(v, min, max) { return v < min ? min : v > max ? max : v; }
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';
2018-01-13 15:11:57 -05:00
});
2018-03-21 03:10:09 -04:00
String.prototype.capitalize = function()
{
return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase();
}