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

react-zdog-renderer

v0.0.4

Published

npm install zdog react-zdog-renderer # or yarn add zdog react-zdog-renderer

Downloads

12

Readme

npm install zdog react-zdog-renderer
# or
yarn add zdog react-zdog-renderer

npm npm

react-zdog-renderer is a react renderer for zdog library, Everything that works in vanialla zdog library it should work in react-zdog-renderer, it is binded to specific version of zdog, so even something new added into zdog lib should work out of the box here, unless changes are done API syntax. Try a live demo here.

Note: There is another library as solution to same problem this library trying to solve, react-zdog and I'd recommed checking that out before, just in case you stumbled upon this library before that one (which I doubt). It is light in weight that this library. Only issue is, it is not maintained and I found a bug in it when used react 18 which makes it unusable react 18. though you can still try it, just in case it worked for you.

How it looks like

import React from "react";
import { createRoot } from "react-dom/client";
import { Canvas } from "react-zdog-renderer";

const root = createRoot(document.getElementById("root"));

root.render(
  <Canvas zoom={8} style={{ width: "200px", height: "200px" }}>
    <shape stroke={20} color="lightblue" rotate={{ x: Math.PI }} />
  </Canvas>
);

API

Canvas

it create a root element required for rendering graphics, either svg or canvas, depending on element prop value (defaults to SVG) and initiates the Zdog.Illustration internally and takes all the config options of Illustration as pros. Apart from configs of Illustrations it takes few more props

element

This specifies the type of root element to be used for graphics rendering. Possible values are "svg" and "canvas". Default is "svg"

frameloop

Default is "always". This prop can be used for specifying render mode. Possible values are "always" and "never". If you wanna render the frames on demand you can specify it as "never" and use useInavlidate hook for rendering frames.

background

Sets the background color of scene. Default is "white".

Hooks

useZDog

returns the state of renderer

import {useZDog} from "react-zdog-renderer"

function MyComponent() {
  const {
    Illustration,   // The parent Zdog.Illustration object which is used as scene
    frameloop
  } = useZDog()

  //since we use zustand as state manager, more appropriate way of accessing would be
  const Illustration = useZDog(state => state.Illustration) // this is the only useful state that you can use as of now
}

useAnimate

This hook gives you access to render loop (useful only if you set the frameloop option to "always" on Canvas component)

import {useAnimate} from "react-zdog-renderer"

function MyComponent() {

    const ref = useRef()

    useAnimate(() => {
        if(!ref.current) return

        ref.current.roatate.y += 0.03
    })

    return (
        <anchor ref={ref} >
            {children}
        </anchor>
    )
}

You can also pass the second argument, that is priority of callback you passed.

useInvalidate

It returns the function that can be called to render the frame if you have set the frameloop to "never" (probably will call it "onDemand")

import { useEffect } from "react";
import { useZDog, useInvalidate } from "react-zdog-renderer";

const Rotate = () => {
  const illu = useZDog((state) => state.Illustration);

  const invalidate = useInvalidate();

  useEffect(() => {
    if (illu) {
      illu.onDragMove = () => {
        invalidate();
      };
    }
  }, [illu, invalidate]);

  return <></>;
};

Above Component will render teh frame only when user drags. This can be used as optimization since it will not run render loop unless you need it.