Codice PHP:
<!DOCTYPE html>
<html>
<head>
<title>United Points by badaze</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script language="JavaScript" type="text/javascript">
<!--
var addMode = 1;
var points = 0;
var mouseX = 0;
var mouseY = 0;
var arrPX = new Array();
var arrPY = new Array();
var arrLines = new Array();
var cirDiam = 3;
var selPoint = -1;
function setAddPointStatus(value) {
addMode = value;
selPoint = -1;
redrawPoints();
} // function setAddPointStatus(value)
function doAction() {
if (addMode == 1) {
var a = checkPoint();
if (a == -1) {
addPoint(mouseX,mouseY);
}
showPointsList();
}
if (addMode == 2) {
var a = checkPoint();
if (a != -1) {
removePoint(a);
invalidateCanvas();
showPointsList();
}
}
if (addMode == 3) {
var a = checkPoint();
if (a != selPoint) {
if (a != -1) {
if (selPoint == -1) {
selPoint = a;
drawSelectedPoint(parseInt(arrPX[selPoint]),parseInt(arrPY[selPoint]));
} else {
drawLine(selPoint,a);
redrawPoints();
selPoint = -1;
}
showPointsList();
}
} else {
redrawPoints();
showPointsList();
selPoint = -1;
}
}
if (addMode == 4) {
var a = checkPoint();
if (a != selPoint) {
if (a != -1) {
if (selPoint == -1) {
selPoint = a;
drawSelectedPoint(parseInt(arrPX[selPoint]),parseInt(arrPY[selPoint]));
} else {
removeLineBetweenPoints(selPoint,a);
invalidateCanvas();
selPoint = -1;
}
showPointsList();
}
} else {
redrawPoints();
showPointsList();
selPoint = -1;
}
}
} // function doAction()
function addPoint(x,y) {
drawPoint(x,y);
arrPX[points] = x;
arrPY[points] = y;
points++;
} // function addPoint(x,y)
function showPointsList() {
var list = "";
var str = "";
for (i=0;i<points;i++) {
str = '[' + i + '] = ' + arrPX[i] + ' - ' + arrPY[i];
if (i == checkPoint()) {
str = '<span style="background-color:red;color:white">'+str+'</span>';
}
list = list + str + '<br/>';
}
document.getElementById('pList').innerHTML = list;
document.getElementById('pNum').innerHTML = points;
} // function showPointsList()
function checkPoint() {
var i=0;
for (i=0;i<points;i++) {
if (Math.abs(arrPX[i] - mouseX) <= cirDiam && Math.abs(arrPY[i] - mouseY) <= cirDiam ) {
return i;
}
}
return -1;
} // function checkPoint()
function removePoint(pointToRemove) {
pointToRemove = parseInt(pointToRemove);
var c = document.getElementById( "mon_canvas" );
var ctx = c.getContext("2d");
ctx.lineWidth = 1;
x = parseInt(arrPX[pointToRemove]);
y = parseInt(arrPY[pointToRemove]);
ctx.beginPath(x,y);
ctx.arc(x,y,cirDiam+1,0,Math.PI*2,true);
ctx.strokeStyle = "white";
ctx.fillStyle = "white";
ctx.fill();
ctx.stroke();
// remove lines
removeLine(pointToRemove);
// remove points
for (i=pointToRemove;i<points;i++) {
arrPX[i] = arrPX[i+1];
arrPY[i] = arrPY[i+1];
}
arrPX[points] = -1;
arrPY[points] = -1;
points--;
} // function removePoint()
function clearCanvas() {
var c = document.getElementById( "mon_canvas" );
var ctx = c.getContext("2d");
ctx.clearRect(0, 0, c.height, c.width);
arrPX = [];
arrPY = [];
arrLines = [];
selPoint = -1;
points = 0;
showPointsList();
} // function clearCanvas()
function drawPoint(x,y) {
drawCircle(x,y,"coral","bisque");
} // function drawPoint(x,y)
function drawSelectedPoint(x,y) {
drawCircle(x,y,"blue","blue");
} // function drawSelectedPoint(x,y)
function resetCanvas() {
var c = document.getElementById( "mon_canvas" );
var ctx = c.getContext("2d");
ctx.clearRect(0, 0, c.height, c.width);
redrawPoints();
arrLines = [];
selPoint = -1;
} // function resetCanvas()
function invalidateCanvas() {
var c = document.getElementById( "mon_canvas" );
var ctx = c.getContext("2d");
ctx.clearRect(0, 0, c.height, c.width);
redrawLines();
redrawPoints();
} // function invalidateCanvas()
function removeLine(pointToRemove) {
var i = 0;
var x = arrPX[pointToRemove];
var y = arrPY[pointToRemove];
var len = arrLines.length;
for (i=len-1;i>=0;i--) {
if ((x == arrLines[i].x1 && y == arrLines[i].y1) || (x == arrLines[i].x2 && y == arrLines[i].y2)) {
arrLines.splice(i,1);
}
}
} // function removeLine(pointToRemove)
function removeLineBetweenPoints(fromPoint,toPoint) {
var i = 0;
var x1 = arrPX[fromPoint];
var y1 = arrPY[fromPoint];
var x2 = arrPX[toPoint];
var y2 = arrPY[toPoint];
var len = arrLines.length;
for (i=len-1;i>=0;i--) {
if (((x1 == arrLines[i].x1 && y1 == arrLines[i].y1) && (x2 == arrLines[i].x2 && y2 == arrLines[i].y2)) ||
((x2 == arrLines[i].x1 && y2 == arrLines[i].y1) && (x1 == arrLines[i].x2 && y1 == arrLines[i].y2))) {
arrLines.splice(i,1);
}
}
} // function removeLine(pointToRemove)
function redrawPoints() {
var i = 0;
for (i=0;i<points;i++) {
drawPoint(arrPX[i],arrPY[i]);
}
} // function redrawPoints()
function redrawLines() {
var i=0;
for (i=0;i<arrLines.length;i++) {
drawLineRaw(arrLines[i].x1,arrLines[i].y1,arrLines[i].x2,arrLines[i].y2,"blue");
}
} // function redrawLines()
function drawLine(fromPoint,toPoint) {
drawLineRaw(arrPX[fromPoint],arrPY[fromPoint],arrPX[toPoint],arrPY[toPoint],"blue")
arrLines.push({x1:arrPX[fromPoint],y1:arrPY[fromPoint],x2:arrPX[toPoint],y2:arrPY[toPoint]});
} // function drawLine(fromPoint,toPoint)
function drawLineRaw(fromPointX1,fromPointY1, toPointX2, toPointY2,color) {
var c = document.getElementById( "mon_canvas" );
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.moveTo(fromPointX1,fromPointY1);
ctx.lineTo(toPointX2,toPointY2);
ctx.fillStyle = color;
ctx.strokeStyle = color;
ctx.stroke();
} // function drawLineRaw(fromPointX1,fromPointY1, toPointX2, toPointY2)
function drawCircle(x,y,strokeStyle, fillStyle) {
var c = document.getElementById( "mon_canvas" );
var ctx = c.getContext("2d");
ctx.lineWidth = 1;
ctx.beginPath(x,y);
ctx.arc(x,y,cirDiam,0,Math.PI*2,true);
ctx.strokeStyle = strokeStyle;
ctx.fillStyle = fillStyle;
ctx.fill();
ctx.stroke();
} // function drawCircle(x,y,strokeStyle, fillStyle)
function save() {
var i = 0;
document.getElementById('tosave').value = '';
for (i=0;i<points;i++) {
document.getElementById('tosave').value = document.getElementById('tosave').value + arrPX[i] + ',' + arrPY[i] + '|';
}
res = Math.random();
window.open('save_load.php?action=save&p='+res,"Save","menubar=no, status=no, scrollbars=yes, location=no, width=600, height=400");
} // function save()
function load() {
var i = 0;
document.getElementById('tosave').value = '';
for (i=0;i<points;i++) {
document.getElementById('tosave').value = document.getElementById('tosave').value + arrPX[i] + ',' + arrPY[i] + '|';
}
res = Math.random();
window.open('save_load.php?action=load&p='+res,"Save","menubar=no, status=no, scrollbars=yes, location=no, width=600, height=400");
} // function load()
//-->
</script>
</head>
<body style="font-family:tahoma">
<h1>Unione punti by badaze</h1>
<table summary="">
<tr>
<td>
<div style="border:1px dotted black;width:351;height:351" >
<canvas id="mon_canvas" width="350" height="350" onclick="doAction()">
Spiacente. Il tuo browser non gestisce i canvas
</canvas>
</div>
</td>
<td id="pList" align="top" style="font-size:8pt">
</td>
</tr>
</table>
<hr/>
<input type="button" value="Clear" id="clear" onclick="clearCanvas()"/>
<input type="button" value="Reset" id="reset" onclick="resetCanvas()"/>
Action :
<select onchange="setAddPointStatus(this.value)">
<option value="1">Aggiungere un punto</option>
<option value="2">Rimuovere un punto</option>
<option value="3">Unire due punti</option>
<option value="4">Disunire due punti</option>
</select>
<input type="button" value="Salva" id="save" onclick="save()"/>
<input type="button" value="Carica" id="load" onclick="load()"/>
<hr/>
<table summary="">
<tr>
<td>Mouse X =</td><td id="mposX"></td>
<td> - </td>
<td>Mouse Y =</td><td id="mposY"></td>
<td> - </td>
<td>Points # =</td><td id="pNum"></td>
<td> - </td>
<td>Mouse over point =</td><td id="mouP"></td>
</tr>
</table>
<hr/>
<ul>
<li>Aggiungere un punto : clicca sull'area per aggiungere un punto</li>
<li>Rimuovere un punto : clicca sul punto per cancellarlo</li>
<li>Unire due punti : clicca su un punto (cambia colore), poi clicca su un altro punto</li>
<li>Disunire due punti : clicca su un punto (cambia colore), poi clicca su un altro punto collegato</li>
</ul>
<hr/>
<p>Clear : cancella tutto</p>
<p>Reset : ridisegna tutti i punti</p>
<form method="post" name="form1"/>
<input type="hidden" name="tosave" id="tosave"/>
</form>
<script language="JavaScript" type="text/javascript">
<!--
$("body").mousemove(function(e) {
var c = document.getElementById( "mon_canvas" );
var rect = c.getBoundingClientRect ();
mouseX = parseInt(e.pageX - rect.left);
mouseY = parseInt(e.pageY - rect.top);
document.getElementById('mposX').innerHTML = mouseX;
document.getElementById('mposY').innerHTML = mouseY;
if (checkPoint() != -1) {
document.getElementById('mouP').innerHTML = "yes";
} else {
document.getElementById('mouP').innerHTML = "no";
}
showPointsList();
})
//-->
</script>
</body>
</html>