pointvec/scripts/dotgrid.js

214 lines
5.5 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
{
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;
2016-12-31 10:00:57 -05:00
this.element = null;
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;
2016-12-31 12:18:01 -05:00
var from = null;
var to = null;
var vector_element = null;
2016-12-31 10:00:57 -05:00
this.install = function()
{
2016-12-31 12:18:01 -05:00
// Dotgrid
2016-12-31 10:00:57 -05:00
this.element = document.createElement("div");
this.element.id = "dotgrid";
this.element.style.width = this.width;
this.element.style.height = this.height;
document.body.appendChild(this.element);
2016-12-31 12:18:01 -05:00
// Markers
for (var x = this.grid_x - 1; x >= 0; x--) {
for (var y = this.grid_y - 1; y >= 0; y--) {
var marker = document.createElement("div");
2016-12-31 15:37:13 -05:00
marker.setAttribute("class",(x % this.block_x == 0 && y % this.block_y == 0 ? "marker block" : "marker"));
2016-12-31 16:09:46 -05:00
marker.style.left = parseInt(x * this.grid_width + (this.grid_width/2));
marker.style.top = parseInt(y * this.grid_height + (this.grid_height/2));
2016-12-31 12:18:01 -05:00
this.element.appendChild(marker);
}
}
// Cursors
this.cursor = document.createElement("div");
this.cursor.id = "cursor";
this.element.appendChild(this.cursor);
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
// Vector
vector_element = document.createElementNS("http://www.w3.org/2000/svg", "svg");
vector_element.setAttribute("class","vector");
vector_element.setAttribute("width",this.width+"px");
vector_element.setAttribute("height",this.height+"px");
2016-12-31 16:44:25 -05:00
vector_element.setAttribute("xmlns","http://www.w3.org/2000/svg");
vector_element.setAttribute("baseProfile","full");
vector_element.setAttribute("version","1.1");
2016-12-31 12:18:01 -05:00
vector_element.style.width = this.width;
vector_element.style.height = this.height;
2016-12-31 16:44:25 -05:00
vector_element.style.stroke = this.color;
vector_element.style.strokeWidth = this.thickness;
vector_element.style.fill = "none";
vector_element.style.strokeLinecap = this.linecap;
2016-12-31 12:18:01 -05:00
this.element.appendChild(vector_element);
}
// Cursor
this.mouse_down = function(e)
{
}
this.mouse_move = function(e)
{
var pos = this.position_in_grid(e.clientX,e.clientY);
pos = this.position_on_grid(pos[0],pos[1]);
this.cursor.style.left = -pos[0];
this.cursor.style.top = pos[1];
}
this.mouse_up = function(e)
{
2016-12-31 13:29:31 -05:00
var pos = this.position_in_grid(e.clientX,e.clientY);
pos = this.position_on_grid(pos[0],pos[1]);
if(from === null){ this.set_from(pos); }
else if(to === null){ this.set_to(pos); }
else{ }
2016-12-31 12:18:01 -05:00
}
// Setters
this.set_from = function(pos)
{
from = pos;
2016-12-31 12:37:10 -05:00
cursor_from.style.left = -pos[0];
cursor_from.style.top = pos[1];
2016-12-31 12:18:01 -05:00
}
this.set_to = function(pos)
{
2016-12-31 12:37:10 -05:00
cursor_to.style.left = -pos[0];
cursor_to.style.top = pos[1];
2016-12-31 12:18:01 -05:00
to = pos;
}
// Draw
2016-12-31 12:37:10 -05:00
this.draw_line = function()
{
2016-12-31 13:29:31 -05:00
if(from === null || to === null){ return; }
2016-12-31 12:56:25 -05:00
2016-12-31 12:37:10 -05:00
var s = document.createElementNS('http://www.w3.org/2000/svg', 'line');
s.setAttribute('x1', -from[0]);
s.setAttribute('y1', from[1]);
s.setAttribute('x2', -to[0]);
s.setAttribute('y2', to[1]);
2016-12-31 12:18:01 -05:00
2016-12-31 12:37:10 -05:00
vector_element.appendChild(s);
2016-12-31 12:18:01 -05:00
reset();
}
2016-12-31 13:16:00 -05:00
this.draw_arc = function(orientation)
2016-12-31 12:18:01 -05:00
{
2016-12-31 13:29:31 -05:00
if(from === null || to === null){ return; }
2016-12-31 12:37:10 -05:00
var s = document.createElementNS("http://www.w3.org/2000/svg", "path");
2016-12-31 15:55:35 -05:00
s.setAttribute("d","M"+(-from[0])+","+(from[1])+" A"+(to[0] - from[0])+","+(to[1] - from[1])+" 0 "+orientation+" "+(-to[0])+","+(to[1])+"");
2017-01-04 12:48:08 -05:00
vector_element.appendChild(s);
reset();
}
this.draw_dot = function()
{
var s = document.createElementNS("http://www.w3.org/2000/svg", "circle");
s.setAttribute("cx",-from[0]);
s.setAttribute("cy",from[1]);
s.setAttribute("r","2");
s.setAttribute("fill","black");
vector_element.appendChild(s);
reset();
}
this.draw_circle = function()
{
var s = document.createElementNS("http://www.w3.org/2000/svg", "circle");
s.setAttribute("cx",-from[0]);
s.setAttribute("cy",from[1]);
s.setAttribute("r",(from[0] - to[0]));
2016-12-31 12:37:10 -05:00
vector_element.appendChild(s);
reset();
2016-12-31 12:18:01 -05:00
}
2016-12-31 12:37:10 -05:00
this.reset = function()
{
reset();
}
2016-12-31 12:47:37 -05:00
2016-12-31 12:18:01 -05:00
function reset()
{
from = null;
to = 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;
2016-12-31 12:18:01 -05:00
}
2016-12-31 12:47:37 -05:00
this.erase = function()
{
2016-12-31 16:09:46 -05:00
this.reset();
2016-12-31 15:55:35 -05:00
if(vector_element.lastChild === null){ return; }
2016-12-31 12:47:37 -05:00
vector_element.removeChild(vector_element.lastChild);
}
2016-12-31 15:47:40 -05:00
this.export = function()
{
var w = window.open('about:blank','image from canvas');
w.document.write("<title>Export</title>");
w.document.appendChild(vector_element);
}
2016-12-31 12:18:01 -05:00
// Normalizers
this.position_in_grid = function(x,y)
{
return [(window.innerWidth/2) - (this.width/2) - x,y - 50];
}
this.position_on_grid = function(x,y)
{
x = parseInt(x/this.grid_width) * this.grid_width - (this.grid_width/2);
2016-12-31 15:47:40 -05:00
y = parseInt(y/this.grid_height) * this.grid_height + (this.grid_height/2);
2016-12-31 15:55:35 -05:00
return [parseInt(x),parseInt(y)];
2016-12-31 10:00:57 -05:00
}
}