from Hacker News

Canvas-area – A lightweight HTML Controller for one or more canvas children

by fango on 1/6/18, 5:05 PM with 7 comments

  • by macawfish on 1/6/18, 6:21 PM

    Very cool! This looks really helpful for integrating canvas elements within a page.

    I was thinking of making a similar library for doing smart, reasonably efficient drawing/graphical annotation relative to the positions and dimensions of DOM elements in screen space. Right now, my method has been to create a single canvas with fixed positioning and stretch it across the screen.

    Then, I do the somewhat tedious work of translating between elements' bounding rectangles and positions into this space.

    You can see the technique in action here: https://micahscopes.github.io/tangled-web-components/example...

    The dogs are custom elements and the edges are drawn on a fixed canvas.

    A big part of why it's so tedious to go from DOM coordinates to fixed canvas coordinates is that there's not an efficient way to get notified when a DOM object's position changes relative to the screen (e.g., because you're scrolling or resizing the window) or the body.

    Right now, with MutationObservers, you can get notified when an objects bounding box changes (ResizeObserver), AFAIK there's not an efficient way listen for updates to an objects' screen-space position, so you have to poll getBoundingClientRect... and it's not scalable!

    Check out the top comment on this discussion: https://developers.google.com/web/updates/2016/10/resizeobse...

    The question is "where's PositionObserver" at? Anybody know?

  • by jitl on 1/6/18, 7:03 PM

    No support for touch events I’m afraid. Didn’t pan or zoom on iOS 11 Safari.
  • by brosky117 on 1/6/18, 5:57 PM

    This looks really interesting! What’s the primary use case for this? I read the README and didn’t see any discussion about why this was made.