question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hittest not working after 2.2?

See original GitHub issue

I’ve used ‘hitTest’ code to auto-center on items in collections. And it works with 2.2.0, but nothing later. Could someone point me in the right direction to enable it in newer versions?

Example of working using 2.2.0 -> https://chattanoogahistory.com/marr-field

Example of same page using 2.4.1 (not working / not throwing errors): -> https://chattanoogahistory.com/marr

hitTest code:

var hitTest = function(position) {
            var box;
            var count = viewer.world.getItemCount();
            for (var i = 0; i < count; i++) {
              box = viewer.world.getItemAt(i).getBounds();
              if (position.x > box.x && 
                  position.y > box.y && 
                  position.x < box.x + box.width &&
                  position.y < box.y + box.height) {
                return i;
              }
            }
            return -1;
          };
          var $viewerElement = $('#openseadragon-canvas')
          .on('mousemove', function(event) {
            var pixel = new OpenSeadragon.Point(event.clientX, event.clientY);
            pixel.y -= $viewerElement.position().top;
            var index = hitTest(viewer.viewport.pointFromPixel(pixel));
            $('.output').text(index === -1 ? '' : 'Image ' + index);
          }
             );
          viewer.addHandler('canvas-click', function(event) {
            if (!event.quick) {
              return;
            }
            var index = hitTest(viewer.viewport.pointFromPixel(event.position));
            if (index !== -1) {
              viewer.viewport.fitBounds(viewer.world.getItemAt(index).getBounds());
            }
          }
                           );

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
iangilmancommented, Jul 28, 2021

I still don’t understand what you mean by it not working. If you don’t disable clickToZoom, the hit detection doesn’t happen at all? Or the hit detection happens, but in response it doesn’t zoom correctly? If it’s the latter, that makes sense, because the click to zoom behavior would override any additional navigation you might be adding.

If you want to override the click to zoom behavior dynamically, you can use event.preventDefaultAction = true like so:

viewer.addHandler('canvas-click', function(event) {
  if (!event.quick) {
    return;
  }
  
  var index = hitTest(viewer.viewport.pointFromPixel(event.position));
  if (index !== -1) {
    event.preventDefaultAction = true;
    viewer.viewport.fitBounds(viewer.world.getItemAt(index).getBounds());
  }
});
0reactions
iangilmancommented, Jul 30, 2021

Excellent 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

CustomPainter.hitTest missing size argument #28206 - GitHub
CustomPainter.hitTest is missing the size of the widget, making it impossible to specify custom hit test behaviour when paint uses its size ...
Read more >
Hittest not working properly when view is not the size of the ...
Ok, when using the UIView hitTest It ONLY returns the TestView and never its children. When using the CALayer hitTest it seems like...
Read more >
Why is my hitTest() not working every time? [closed]
I have a code segment in which I use hitTest() a couple of times but at a particular section the hitTest stops working....
Read more >
WPF Diagram 2.2.1.21626 HitTest Exception - MindFusion
We just found the following - it occurred when drag-highliting over a node that contained a 3D control. Is the exception information listed ......
Read more >
Hit test not working as expected - MSDN - Microsoft
The problem is not how to perform the hit test. The problem is that the hit test behavior returns false positives.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found