Skip to content

Instantly share code, notes, and snippets.

@Zer0t3ch
Last active August 2, 2017 20:48
Show Gist options
  • Save Zer0t3ch/3b4bd0d6675bfed6a881 to your computer and use it in GitHub Desktop.
Save Zer0t3ch/3b4bd0d6675bfed6a881 to your computer and use it in GitHub Desktop.
Point/Line Manager for my Voronoi diagram generator
/**=- VERSION 1.2 -=**/
var canvas, c;
var dots = [ ];
var lines = [ ];
var count = 0;
var m = {
mousePos : function(canvas, evt) {
var rect = canvas.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
},
lines : function(val, index) {
dots.forEach(function(v, i) {
if (i > index) {
lines.push([val, v]);
}
});
},
dots : function(x, y) {
console.log("Add at " + x + " x " + y);
dots.push([x, y]);
}
};
var g = {
render : function() {
g.clear();
dots.forEach(g.dots);
dots.forEach(m.lines);
lines.forEach(g.lines);
},
clear : function() {
c.clearRect(0, 0, canvas.width, canvas.height);
//canvas.height = canvas.height; // DEPRACATED / ALTERNATE METHOD
},
lines : function(val, index) {
c.moveTo(val[0][0], val[0][1]);
c.lineTo(val[1][0], val[1][1]);
c.stroke();
},
dots : function(val, index) {
c.moveTo(val[0], val[1]);
c.lineTo(val[0]+1, val[1]);
c.lineTo(val[0]+1, val[1]+1);
c.lineTo(val[0], val[1]+1);
c.stroke();
}
};
function event_onLoad() {
canvas = document.getElementById("can");
c = canvas.getContext("2d");
c.beginPath();
//setInterval("g_render()", 80);
}
function canvas_onClick(e) {
var loc = m.mousePos(canvas, e);
m.dots(loc.x, loc.y);
g.render();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment