npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-open-seadragon

v1.0.0

Published

Hook for easily using OpenSeadragon with React. Declarative API with full Typescript definitions (including all of OpenSeadragon types).

Downloads

150

Readme

use-open-seadragon

Hook for easily using OpenSeadragon with React. Declarative API with full Typescript definitions (including all of OpenSeadragon types).

npm install use-open-seadragon --save
yarn add use-open-seadragon

Optionally, if you are using the React hooks, you can install React and ReactDOM. OpenSeadragon itself is bundled in the UMD if you want to grab it directly to embed.

Features

  • Fully typed OpenSeadragon
  • Hooks sagely access to the viewer, viewport etc.
  • Wrapper around OSD events for React (and 100% type support for the OSD API)
  • Small: 2kb extra on-top of OSD

Examples

Simple hook usage to display a viewer:

import React from "react";
import { useOpenSeadragon } from "use-open-seadragon";

const tile = {
  type: "image",
  url:
    "https://openseadragon.github.io/example-images/grand-canyon-landscape-overlooking.jpg"
};

function Viewer({ tiles }) {
  const [ref] = useOpenSeadragon(tiles);

  return (
    <div ref={ref} style={{ height: 600, width: 800, position: "relative" }} />
  );
}

Edit use-open-seadragon-simple

Add React component overlays (requires ViewerProvider around root, similar to Redux):

import React, { useState } from "react";
import { useOpenSeadragon, ViewerProvider, Overlay } from "use-open-seadragon";

const tile = {
  type: "image",
  url:
    "https://openseadragon.github.io/example-images/grand-canyon-landscape-overlooking.jpg"
};

function Viewer({ tiles }) {
  const [ref] = useOpenSeadragon(tiles);
  const [count, setCount] = useState(0);

  return (
    <>
      <div ref={ref} style={{ height: 600, width: 800, position: "relative" }}>
        <Overlay x={0.5} y={0.5}>
          <div style={{ background: "#fff" }}>
            <h1>Hello overlay</h1>
            <p>State works too: {count}</p>
          </div>
        </Overlay>
      </div>
      <button onClick={() => setCount(c => c + 1)}>incr counter</button>
    </>
  );
}

export default function App() {
  return (
    <ViewerProvider>
      <Viewer tiles={tile} />
    </ViewerProvider>
  );
}

Edit use-open-seadragon-overlay

Using events

In this example we are subscribing to "update-viewport" and grabbing some values from the viewport and setting them on state. These are then returned as an Overlay which can be rendered inside of the viewer.

import { Overlay, useViewerEvent } from "use-open-seadragon";

function CurrentPosition() {
  const [x, setX] = useState(0);
  const [y, setY] = useState(0);

  useViewerEvent("update-viewport", ev => {
    setX(ev.eventSource.viewport.getBoundsNoRotate().x);
    setY(ev.eventSource.viewport.getBoundsNoRotate().y);
  });

  return (
    <Overlay>
      <div style={{ background: "#fff" }}>
        {x}, {y}
      </div>
    </Overlay>
  );
}

You will get completion for all of the events dispatched.

This library comes with a few pre-canned hooks that will probably grow. But it also comes now with all of the building blocks you would need to create custom hooks around the viewer.

OpenSeadragon without React

If you want OpenSeadragon without react, but with all the types then you can try them out by importing:

import { OpenSeadragon } from 'use-open-seadragon/open-seadragon';

and then using it as you would the openseadragon package. These types should be quite complete, but are still unstable. If they are tested to work well, then they can be published to NPM as @types/openseadragon on definitely typed.