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

@notthatnathan/use-canvas-image

v1.0.8

Published

A React hook for automatically exporting a canvas to img.

Downloads

16

Readme

useCanvasImage

A React hook for automatically exporting a canvas to img.

  • For session recording services like FullStory and LogRocket that are unable to record the contents of a canvas element, this provides a workaround. By default, the img is rendered directly behind the canvas.
    • A positioned parent element should wrap the canvas to ensure the img renders in the correct position.
    • If you have UI rendering above the canvas, you may need to make z-index tweaks to ensure the canvas stays behind the UI. By default, this hook sets the img to z-index: 0 and the canvas to z-index: 1.
  • Can provide a downloadable image of the current state of a canvas. Useful for drawing apps, meme generators, etc.

Note: FullStory adds a semi-transparent striped overlay in place of elements with .fs-exclude. While you can still see the img in session replays, it’s rendered with those stripes over it. We’re working with FullStory to provide a way to completely hide the element.

3rd party canvases

When using a canvas with a WebGL context that you don’t have direct access to manipulate (like three.js):

  • Set the canvas to use preserveDrawingBuffer: true. (This can be passed to THREE.WebGLRenderer if using three.js.)
  • Use the interval property to define how often the img is automatically updated.

Because we’re not able to insert our function directly into the drawing thread when using 3rd party apps that provide no such callback, we have to poll. This requires the drawing buffer to be preserved so we can read it in a later thread. This may result in a small performance hit due to the context switching from swap to copy. Depending on your use case, this may or may not be an issue.


Examples

Basic

Example use with FullStory’s .fs-exclude class.

// in component that renders canvas
const canvasRef = useRef()
const updateImage = useCanvasImage({
  canvas: canvasRef,
  className: 'fs-exclude', // to hide from FullStory
})

useEffect(() => {
  // in this simple example, a change to someVar indicates the canvas has been updated
  updateImage()
}, [someVar])
// JSX
<div style="position: relative">
  <canvas ref={canvasRef} />
</div>
/* html output */
<div style="position: relative">
  <canvas class="fs-exclude" />
  <img />
</div>

3rd party

Example use with @react-three/xr.

useCanvasImage({
  canvas: '[data-engine~="three.js"]',
  className: 'fs-exclude',
  interval: 100,
})
// JSX, using VRCanvas from @react-three/xr
<div style="position: relative">
  <VRCanvas
    // passed to THREE.WebGLRenderer and then to the canvas context
    gl={{ preserveDrawingBuffer: true }}
  />
</div>
/* html output */
<div style="position: relative">
  <canvas class="fs-exclude" />
  <img />
</div>

Arguments

  • canvas (React ref, string selector, or pre-selected HTML canvas element). If string, must be unique in dom and formatted as selector (#id, .class, [attr="val"], etc).
  • imgClassname (string) for styling output image, space separated.
  • canvasClassName (string) for adding .fs-exclude (FullStory) or other class names to the img, space separated.
  • canvasAttributes (object) for adding data-private (LogRocket) or other attributes to the canvas.
  • fileType (string, image/jpeg, image/png, image/webp) what img format to output. default, recommended for session recording: image/jpeg
  • quality (number, 0-1). compression quality. only applies to image/jpeg or image/webp. default: 0.5
  • interval (number or null/false, millisecond). how often to update the image. set to null to not update on an interval and to use the explicit function instead. when set to number, returned function becomes no-op and polling is used. default: null
  • Returns: function to be called when the image should be updated (usually when the canvas is drawn to). no-op when an interval is specified.

Contributing

Please do.