Implemented offset

This commit is contained in:
Devine Lu Linvega 2017-11-06 13:18:57 +13:00
parent d4be61a250
commit e09894af69
4 changed files with 43 additions and 17 deletions

View File

@ -14,12 +14,19 @@
</head>
<body>
<script>
const {dialog,app} = require('electron').remote;
const fs = require('fs');
try{
const {dialog,app} = require('electron').remote;
const fs = require('fs');
}
catch(err){
console.log("standalone")
}
dotgrid = new Dotgrid(300,300,30,30,5,5, 10,"square","#000000");
dotgrid.install();
var keyboard = new Keyboard();
document.onkeyup = function myFunction(event){ keyboard.listen(event); };
document.addEventListener('mousedown', function(e){ dotgrid.mouse_down(e); }, false);
document.addEventListener('mousemove', function(e){ dotgrid.mouse_move(e); }, false);

View File

@ -10,6 +10,7 @@ function Dotgrid(width,height,grid_x,grid_y,block_x,block_y,thickness = 3,lineca
this.thickness = thickness;
this.linecap = linecap;
this.color = color;
this.offset = new Pos(0,0);
this.element = null;
@ -73,6 +74,7 @@ function Dotgrid(width,height,grid_x,grid_y,block_x,block_y,thickness = 3,lineca
cursor_end.id = "cursor_end";
this.element.appendChild(cursor_end);
this.offset_el = document.createElementNS("http://www.w3.org/2000/svg", "g");
this.mirror_el = document.createElementNS("http://www.w3.org/2000/svg", "g");
this.mirror_path = document.createElementNS("http://www.w3.org/2000/svg", "path");
@ -91,8 +93,9 @@ function Dotgrid(width,height,grid_x,grid_y,block_x,block_y,thickness = 3,lineca
this.svg_el.style.fill = "none";
this.svg_el.style.strokeLinecap = this.linecap;
this.element.appendChild(this.svg_el);
this.svg_el.appendChild(this.path);
this.offset_el.appendChild(this.path)
this.svg_el.appendChild(this.offset_el);
this.svg_el.appendChild(this.mirror_el);
this.mirror_el.appendChild(this.mirror_path);
@ -187,17 +190,23 @@ function Dotgrid(width,height,grid_x,grid_y,block_x,block_y,thickness = 3,lineca
this.mod_move = function(x,y)
{
if(!to && !end){
this.set_from([from[0]+(x*10),from[1]+(y*10)])
if(!to && !end && from){
this.set_from([from[0]-(x),from[1]+(y)])
this.draw();
return;
}
if(!end){
this.set_to([to[0]+(x*10),to[1]+(y*10)])
if(!end && to){
this.set_to([to[0]-(x),to[1]+(y)])
this.draw();
return;
}
this.set_end([end[0]+(x*10),end[1]+(y*10)])
if(end){
this.set_end([end[0]-(x),end[1]+(y)])
this.draw();
return;
}
// Move offset
this.offset = this.offset.add(new Pos(x,y));
this.draw();
}
@ -220,7 +229,9 @@ function Dotgrid(width,height,grid_x,grid_y,block_x,block_y,thickness = 3,lineca
this.svg_el.style.strokeWidth = this.thickness;
this.mirror_path.setAttribute("d",this.mirror ? d : '');
this.mirror_path.setAttribute("transform","translate(300,0),scale(-1,1)")
this.mirror_path.setAttribute("transform","translate("+(300 - (this.offset.x))+","+(this.offset.y)+"),scale(-1,1)")
this.offset_el.setAttribute("transform","translate("+(this.offset.x)+","+(this.offset.y)+")")
this.update_interface();
}
@ -231,7 +242,10 @@ function Dotgrid(width,height,grid_x,grid_y,block_x,block_y,thickness = 3,lineca
if(from === null || to === null){ return; }
var end_point = end ? new Pos(end[0] * -1,end[1]) : null;
this.segments.push(new Path_Line(new Pos(from[0] * -1,from[1]),new Pos(to[0] * -1,to[1]),end_point));
from = new Pos(from[0],from[1])
this.segments.push(new Path_Line(new Pos(from.x * -1,from.y).sub(this.offset),new Pos(to[0] * -1,to[1]).sub(this.offset),end_point.sub(this.offset)));
this.draw();
reset();
@ -242,7 +256,7 @@ function Dotgrid(width,height,grid_x,grid_y,block_x,block_y,thickness = 3,lineca
if(from === null || to === null){ return; }
var end_point = end ? new Pos(end[0] * -1,end[1]) : null;
this.segments.push(new Path_Arc(new Pos(from[0] * -1,from[1]),new Pos(to[0] * -1,to[1]),orientation,end_point));
this.segments.push(new Path_Arc(new Pos(from[0] * -1,from[1]).sub(this.offset),new Pos(to[0] * -1,to[1]).sub(this.offset),orientation,end_point.sub(this.offset)));
this.draw();
reset();
@ -252,7 +266,7 @@ function Dotgrid(width,height,grid_x,grid_y,block_x,block_y,thickness = 3,lineca
{
if(from === null || to === null){ return; }
this.segments.push(new Path_Bezier(new Pos(from[0] * -1,from[1]),new Pos(to[0] * -1,to[1]),new Pos(end[0] * -1,end[1])));
this.segments.push(new Path_Bezier(new Pos(from[0] * -1,from[1]).sub(this.offset),new Pos(to[0] * -1,to[1]).sub(this.offset),new Pos(end[0] * -1,end[1]).sub(this.offset)));
this.draw();
reset();

View File

@ -24,10 +24,10 @@ function Keyboard()
case 8 : dotgrid.erase(); break; // 'Backspace'
case 69 : dotgrid.export(); break; // 'e'
case 38 : dotgrid.mod_move(0,-1); break; // 'up'
case 40 : dotgrid.mod_move(0,1); break; // 'down'
case 37 : dotgrid.mod_move(1,0); break; // 'left'
case 39 : dotgrid.mod_move(-1,0); break; // 'right'
case 38 : dotgrid.mod_move(0,-10); break; // 'up'
case 40 : dotgrid.mod_move(0,10); break; // 'down'
case 37 : dotgrid.mod_move(-10,0); break; // 'left'
case 39 : dotgrid.mod_move(10,0); break; // 'right'
}
dotgrid.draw();
}

View File

@ -13,6 +13,11 @@ function Pos(x,y)
return new Pos(this.x - pos2.x,this.y - pos2.y)
}
this.add = function(pos2)
{
return new Pos(this.x + pos2.x,this.y + pos2.y)
}
this.is_equal = function(pos2)
{
return pos2.x == this.x && pos2.y == this.y;