Mercurial > hg > digilib
comparison webapp/src/main/webapp/jquery/jquery.digilib.measure.js @ 1610:f425f00bf5e3 geom_prototype
changed digilib.geometry to use new and prototypes instead of adding methods to objects.
author | robcast |
---|---|
date | Sun, 12 Mar 2017 15:53:01 +0100 |
parents | 020739227ffd |
children |
comparison
equal
deleted
inserted
replaced
1609:3f3a4f4eecb1 | 1610:f425f00bf5e3 |
---|---|
919 if (vtx == null) { | 919 if (vtx == null) { |
920 vtx = shape.properties.vtx; } | 920 vtx = shape.properties.vtx; } |
921 var coords = shape.geometry.coordinates; | 921 var coords = shape.geometry.coordinates; |
922 var last = getLastVertex(shape, vtx); | 922 var last = getLastVertex(shape, vtx); |
923 // safely assume that screenpos and coords have equal length? | 923 // safely assume that screenpos and coords have equal length? |
924 var dist = fn.getDistance(data, geom.position(coords[last]), geom.position(coords[vtx])); | 924 var dist = fn.getDistance(data, new geom.Position(coords[last]), new geom.Position(coords[vtx])); |
925 return dist.rectified; | 925 return dist.rectified; |
926 }; | 926 }; |
927 | 927 |
928 // calculate distance from current to last vertex (in rectified digilib coords) | 928 // calculate distance from current to last vertex (in rectified digilib coords) |
929 var rectifiedDist = function(data, shape) { | 929 var rectifiedDist = function(data, shape) { |
941 | 941 |
942 // calculate the area of a polygon (rectified digilib coords) | 942 // calculate the area of a polygon (rectified digilib coords) |
943 var rectifiedArea = function(data, shape) { | 943 var rectifiedArea = function(data, shape) { |
944 var ar = fn.getImgAspectRatio(data); | 944 var ar = fn.getImgAspectRatio(data); |
945 var rectifyPoint = function (c) { | 945 var rectifyPoint = function (c) { |
946 return geom.position(ar * c[0], c[1]); | 946 return new geom.Position(ar * c[0], c[1]); |
947 }; | 947 }; |
948 var coords = $.map(shape.geometry.coordinates, rectifyPoint); | 948 var coords = $.map(shape.geometry.coordinates, rectifyPoint); |
949 // formula for ellipse area | 949 // formula for ellipse area |
950 if (shape.geometry.type === 'Ellipse') { | 950 if (shape.geometry.type === 'Ellipse') { |
951 return Math.abs((coords[0].x-coords[1].x) * (coords[0].y-coords[1].y) * Math.PI); | 951 return Math.abs((coords[0].x-coords[1].x) * (coords[0].y-coords[1].y) * Math.PI); |
1213 }; | 1213 }; |
1214 | 1214 |
1215 // initial position of measure bar (bottom left of browser window) | 1215 // initial position of measure bar (bottom left of browser window) |
1216 var setScreenPosition = function(data, $bar) { | 1216 var setScreenPosition = function(data, $bar) { |
1217 if ($bar == null) return; | 1217 if ($bar == null) return; |
1218 var barH = geom.rectangle($bar).height; | 1218 var barH = (new geom.Rectangle($bar)).height; |
1219 var screenH = fn.getFullscreenRect(data).height; | 1219 var screenH = fn.getFullscreenRect(data).height; |
1220 geom.position(10, screenH - barH).adjustDiv($bar); | 1220 new geom.Position(10, screenH - barH).adjustDiv($bar); |
1221 }; | 1221 }; |
1222 | 1222 |
1223 // drag measureBar around | 1223 // drag measureBar around |
1224 var dragMeasureBar = function(event) { | 1224 var dragMeasureBar = function(event) { |
1225 var $div = $(this).parent(); | 1225 var $div = $(this).parent(); |
1367 var p = props.screenpos; | 1367 var p = props.screenpos; |
1368 var vtx = props.vtx; | 1368 var vtx = props.vtx; |
1369 place.call($s); // place the linestring | 1369 place.call($s); // place the linestring |
1370 if (p.length > 2) { // p[2] is the mouse pointer | 1370 if (p.length > 2) { // p[2] is the mouse pointer |
1371 var m1 = p[1].mid(p[2]); | 1371 var m1 = p[1].mid(p[2]); |
1372 var line = geom.line(m1, p[1]); | 1372 var line = new geom.Line(m1, p[1]); |
1373 var m2 = p[0].copy().add(line.vector()); | 1373 var m2 = p[0].copy().add(line.vector()); |
1374 var rad = line.length(); | 1374 var rad = line.length(); |
1375 $c1.attr({cx: m1.x, cy: m1.y, r: rad}); | 1375 $c1.attr({cx: m1.x, cy: m1.y, r: rad}); |
1376 $c2.attr({cx: m2.x, cy: m2.y, r: rad}); | 1376 $c2.attr({cx: m2.x, cy: m2.y, r: rad}); |
1377 } | 1377 } |
1390 var props = shape.properties; | 1390 var props = shape.properties; |
1391 $s.place = function () { | 1391 $s.place = function () { |
1392 var p = props.screenpos; | 1392 var p = props.screenpos; |
1393 var vtx = props.vtx; | 1393 var vtx = props.vtx; |
1394 if (p.length > 2) { // p[2] is the mouse pointer | 1394 if (p.length > 2) { // p[2] is the mouse pointer |
1395 var line1 = geom.line(p[0], p[1]); // base line | 1395 var line1 = new geom.Line(p[0], p[1]); // base line |
1396 var line2 = line1.parallel(p[2]); | 1396 var line2 = line1.parallel(p[2]); |
1397 var p3 = line1.perpendicular().intersection(line2); | 1397 var p3 = line1.perpendicular().intersection(line2); |
1398 var p2 = p3.copy().add(line1.vector()); | 1398 var p2 = p3.copy().add(line1.vector()); |
1399 p[2] = p2.mid(p3); // handle position | 1399 p[2] = p2.mid(p3); // handle position |
1400 shape.geometry.coordinates[2] = trafo.invtransform(p[2]).toArray(); | 1400 shape.geometry.coordinates[2] = trafo.invtransform(p[2]).toArray(); |
1430 $g.append($s).append($c1).append($c2).append($p1).append($p2).append($arc); | 1430 $g.append($s).append($c1).append($c2).append($p1).append($p2).append($arc); |
1431 $g.place = function () { | 1431 $g.place = function () { |
1432 var p = props.screenpos; | 1432 var p = props.screenpos; |
1433 place.call($s); // place the framing rectangle (polygon) | 1433 place.call($s); // place the framing rectangle (polygon) |
1434 if (p.length > 3) { // p[3] is the mouse pointer | 1434 if (p.length > 3) { // p[3] is the mouse pointer |
1435 var side0 = geom.line(p[0], p[1]) // the sides | 1435 var side0 = new geom.Line(p[0], p[1]) // the sides |
1436 var side1 = geom.line(p[1], props.pos[0]); | 1436 var side1 = new geom.Line(p[1], props.pos[0]); |
1437 var side2 = geom.line(props.pos[0], props.pos[1]); | 1437 var side2 = new geom.Line(props.pos[0], props.pos[1]); |
1438 var side3 = geom.line(props.pos[1], p[0]); | 1438 var side3 = new geom.Line(props.pos[1], p[0]); |
1439 var mid0 = side0.mid(); // the midpoints of the sides | 1439 var mid0 = side0.mid(); // the midpoints of the sides |
1440 var mid1 = side1.mid(); | 1440 var mid1 = side1.mid(); |
1441 var mid2 = side2.mid(); | 1441 var mid2 = side2.mid(); |
1442 var mid3 = side3.mid(); | 1442 var mid3 = side3.mid(); |
1443 var axis1 = side0.parallel(mid3); // short axis | 1443 var axis1 = side0.parallel(mid3); // short axis |
1445 var maxDiam = axis1.length()-1; // maximal diameter for small circles | 1445 var maxDiam = axis1.length()-1; // maximal diameter for small circles |
1446 var handle = axis2.perpendicularPoint(p[3]); // drag point projected on long axis | 1446 var handle = axis2.perpendicularPoint(p[3]); // drag point projected on long axis |
1447 if (handle.distance(mid0) > axis2.length()) { // constrain handle | 1447 if (handle.distance(mid0) > axis2.length()) { // constrain handle |
1448 handle.moveTo(mid2); | 1448 handle.moveTo(mid2); |
1449 } else if (handle.distance(mid2) > maxDiam) { | 1449 } else if (handle.distance(mid2) > maxDiam) { |
1450 handle.moveTo(geom.line(mid2, handle).length(maxDiam).point()); | 1450 handle.moveTo(new geom.Line(mid2, handle).length(maxDiam).point()); |
1451 } | 1451 } |
1452 var m1 = handle.mid(mid2); // centers of the small circles | 1452 var m1 = handle.mid(mid2); // centers of the small circles |
1453 var m2 = axis1.mirror(m1); | 1453 var m2 = axis1.mirror(m1); |
1454 var rad1 = m1.distance(mid2); // radius of the small circles | 1454 var rad1 = m1.distance(mid2); // radius of the small circles |
1455 var rd1 = axis1.copy().length(rad1).point(); // point in radius distance from midpoint of long axis | 1455 var rd1 = axis1.copy().length(rad1).point(); // point in radius distance from midpoint of long axis |
1456 var rd2 = axis2.mirror(rd1); | 1456 var rd2 = axis2.mirror(rd1); |
1457 var md1 = rd1.mid(m1); // midpoint of the line connecting this point with the small circle center | 1457 var md1 = rd1.mid(m1); // midpoint of the line connecting this point with the small circle center |
1458 var md2 = axis2.mirror(md1); | 1458 var md2 = axis2.mirror(md1); |
1459 var md3 = axis1.mirror(md1); | 1459 var md3 = axis1.mirror(md1); |
1460 var md4 = axis1.mirror(md2); | 1460 var md4 = axis1.mirror(md2); |
1461 var bi = geom.line(rd1, m1).perpendicular(md1); // construct the perpendicular bisector of the connection line | 1461 var bi = new geom.Line(rd1, m1).perpendicular(md1); // construct the perpendicular bisector of the connection line |
1462 var m3 = axis1.intersection(bi); // find the centers of the big circles | 1462 var m3 = axis1.intersection(bi); // find the centers of the big circles |
1463 var m4 = axis2.mirror(m3); | 1463 var m4 = axis2.mirror(m3); |
1464 var fp1 = geom.line(m3, m1).addEnd(rad1); // the four fitting points | 1464 var fp1 = new geom.Line(m3, m1).addEnd(rad1); // the four fitting points |
1465 var fp2 = geom.line(m3, m2).addEnd(rad1); | 1465 var fp2 = new geom.Line(m3, m2).addEnd(rad1); |
1466 var fp3 = geom.line(m4, m1).addEnd(rad1); | 1466 var fp3 = new geom.Line(m4, m1).addEnd(rad1); |
1467 var fp4 = geom.line(m4, m2).addEnd(rad1); | 1467 var fp4 = new geom.Line(m4, m2).addEnd(rad1); |
1468 var rad2 = m3.distance(fp1); // radius of the big circles | 1468 var rad2 = m3.distance(fp1); // radius of the big circles |
1469 // construct the SVG shapes | 1469 // construct the SVG shapes |
1470 $c1.attr({cx: m1.x, cy: m1.y, r: rad1}); | 1470 $c1.attr({cx: m1.x, cy: m1.y, r: rad1}); |
1471 $c2.attr({cx: m2.x, cy: m2.y, r: rad1}); | 1471 $c2.attr({cx: m2.x, cy: m2.y, r: rad1}); |
1472 $p1.attr({d: // the guidelines | 1472 $p1.attr({d: // the guidelines |
1681 // plugin initialization | 1681 // plugin initialization |
1682 var init = function (data) { | 1682 var init = function (data) { |
1683 console.debug('initialising measure plugin. data:', data); | 1683 console.debug('initialising measure plugin. data:', data); |
1684 var settings = data.settings; | 1684 var settings = data.settings; |
1685 CSS = settings.cssPrefix+'measure-'; | 1685 CSS = settings.cssPrefix+'measure-'; |
1686 FULL_AREA = geom.rectangle(0, 0, 1, 1); | 1686 FULL_AREA = new geom.Rectangle(0, 0, 1, 1); |
1687 // install event handlers | 1687 // install event handlers |
1688 var $data = $(data); | 1688 var $data = $(data); |
1689 $data.on('setup', handleSetup); | 1689 $data.on('setup', handleSetup); |
1690 $data.on('update', handleUpdate); | 1690 $data.on('update', handleUpdate); |
1691 $data.on('createShape', onCreateShape); | 1691 $data.on('createShape', onCreateShape); |