From 75177348ffc5f5262ff9ea5bded821de20f1732d Mon Sep 17 00:00:00 2001
From: Lucio Zambon <lucio.zambon@elettra.eu>
Date: Thu, 25 Jul 2024 00:11:56 +0000
Subject: [PATCH] Update panther2d.js

---
 panther2d.js | 98 ++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 72 insertions(+), 26 deletions(-)

diff --git a/panther2d.js b/panther2d.js
index f041eb2..20def5f 100644
--- a/panther2d.js
+++ b/panther2d.js
@@ -28,32 +28,78 @@
 	for (let i=0; i<pa.length; i++) {const p = pa[i].split('='); parameters[p[0]] = p[1];}
 	const machineCaseSensitive = document.location.search.indexOf('machine=')>-1? document.location.search.split('machine=')[1].split('&')[0]: 'elettra';
 	const machine = machineCaseSensitive.toLowerCase();
+	let rulerStart = false;
+	let cx, cy;
 
-
-function getCursorPosition(main, event) {
-    const rect = main.getBoundingClientRect();
-    const x = event.clientX - rect.left;
-    const y = event.clientY - rect.top;
-    console.log(Math.round(x) + "," + Math.round(y) + "," + zoom);
-	$('#stright').val(Math.round((x - panZoomPanther.getPan().x)/zoom * 43.75));
-	$('#sr').val(-Math.round((y - panZoomPanther.getPan().y)/zoom * 43.75));
-}
-function initCursorPosition(main, event) {
-    const rect = main.getBoundingClientRect();
-    const x = event.clientX - rect.left;
-    const y = event.clientY - rect.top;
-    console.log(Math.round(x) + "," + Math.round(y) + ",");
-	main.addEventListener('mousemove', function(e) {
-		getCursorPosition(main, e);
-	});
-}
-if (document.location.search.indexOf('ruler')>-1) {
-	$('#ruler').show();
-	const main = document.getElementById('main');
-	main.addEventListener('mousedown', function(e) {
-		initCursorPosition(main, e);
-	});
-}
+	function setTick(x, y, cx, cy, dist, distPixel, n, pow10=1000) {
+		const tx1 = cx + (x - cx) / dist * pow10 * n;
+		const ty1 = cy + (y - cy) / dist * pow10 * n;
+		const tx2 = tx1 - (y - cy) / distPixel * 10;
+		const ty2 = ty1 + (x - cx) / distPixel * 10;
+		$('#tick'+n).attr('x1', tx1);
+		$('#tick'+n).attr('y1', ty1);
+		$('#tick'+n).attr('x2', tx2);
+		$('#tick'+n).attr('y2', ty2);
+	}	
+	function getCursorPosition(main, event) {
+		const rect = main.getBoundingClientRect();
+		const x = event.clientX - rect.left;
+		const y = event.clientY - rect.top;
+		// console.log(Math.round(x) + "," + Math.round(y) + "," + zoom);
+		// $('#stright').val(Math.round((x - panZoomPanther.getPan().x)/zoom * 43.75));
+		const distPixel = Math.sqrt(Math.pow(x - cx, 2) + Math.pow(y - cy, 2));
+		const dist = distPixel/zoom * 43.75;
+		if (rulerStart) $('#stright').val(Math.round(dist));
+		const alpha = (Math.atan2(y-cy, x-cx) * 180) / Math.PI;
+		$('#sr').val(Math.round((x - panZoomPanther.getPan().x)/zoom * 43.75)+' '+(-Math.round((y - panZoomPanther.getPan().y)/zoom * 43.75)) + ' a: ' + alpha);
+		if (rulerStart) {
+			const pow10 = Math.pow(10, Math.round(Math.log10(dist))-1);
+			$('#rulerLine').attr('x2', x); 
+			$('#rulerLine').attr('y2', y);
+			for (let i=1; i<60; i++) {
+				if (i<dist/1000) setTick(x, y, cx, cy, dist, distPixel, i, pow10); else setTick(x, y, x, y, dist, distPixel, i, pow10);
+			}
+		}
+		// stroke-dasharray: 0,50,50,100 
+	}
+	function initCursorPosition(main, event) {
+		if (rulerStart) { rulerStart = false; return;}
+		const rect = main.getBoundingClientRect();
+		cx = event.clientX - rect.left;
+		cy = event.clientY - rect.top;
+		console.log(Math.round(cx) + "," + Math.round(cy) + ",");
+		if (!rulerStart) {
+			// appendSvg("circle", {id: 'rulerStart', cx: cx, cy: cy, r:8, style:"display: block", fill:"none", stroke:"red","stroke-width":2});
+			if ($('#rulerLine').length==0) {
+				appendSvg("line", {id: 'rulerLine', x1: cx, y1: cy, x2: cx, y2: cy, style:"display: block", fill:"none", stroke:"yellow","stroke-width":2});
+				for (let i=1; i<60; i++) appendSvg("line", {id: 'tick'+i, x1: cx, y1: cy, x2: cx, y2: cy, style:"display: block", fill:"none", stroke:"yellow","stroke-width":2});
+			}
+			else {
+				$('#rulerLine').attr('x1', cx);
+				$('#rulerLine').attr('y1', cy);
+				$('#rulerLine').attr('x2', cx);
+				$('#rulerLine').attr('y2', cy);
+				$('#stright').val('');
+				for (let i=1; i<60; i++) {setTick(cx, cy, cx, cy, 1, 1, i);}
+			}
+			rulerStart = true;
+		}
+		else {
+			// $('#rulerStart').attr('cx', cx);	$('#rulerStart').attr('cy', cy);
+			$('#rulerLine').attr('x1', cx);
+			$('#rulerLine').attr('y1', cy);
+		}
+		main.addEventListener('mousemove', function(e) {
+			getCursorPosition(main, e);
+		});
+	}
+	if (document.location.search.indexOf('ruler')>-1) {
+		$('#ruler').show();
+		const main = document.getElementById('main');
+		main.addEventListener('mousedown', function(e) {
+			initCursorPosition(main, e);
+		});
+	}
 
 	//https://puma-01.elettra.eu/rchan.php?json&valueOnly&src=srv-tango-srf-01.fcs.elettra.trieste.it:20000/f/access_control/safety/Undulator_access_state -> 5 FEL1, 6 FEL2
 	if (machine.indexOf('fermi')>-1) {
@@ -561,7 +607,7 @@ if (document.location.search.indexOf('ruler')>-1) {
 		console.log('initSearch()', sections, facility);
 		for (i=0; i<sections.length; i++) { // if(i>1) break;
 			if (sections[i].bending && sections[i].bending.type) {
-				console.log('names.push()', sections[i].bending.name);
+				// console.log('names.push()', sections[i].bending.name);
 				names.push(sections[i].bending.name.replace('.','_'));
 			}
 			if (typeof sections[i].components == 'object') for (let j=0; j<sections[i].components.length; j++) appendSearch(sections[i].components[j], facility);
-- 
GitLab