Giter Site home page Giter Site logo

react-geopattern's Introduction

react-geopattern

Package Version codecov

Use GeoPattern from React.

features

  • Written in TypeScript
  • Adds less than 200 bytes at runtime
  • Supports CommonJS or ESM
  • Extensive unit testing
  • Zero-configuration caching of generated patterns

prerequisites

  • Node.js 16+
  • React 18

installation

npm install --save geopattern@1 react-geopattern

usage

This package provides one primary hook, useGeoPattern. The first two arguments are the same as those of GeoPattern.generate, namely an input string and an options object. The third argument is optional and allows you to override the caching behavior by providing your own instance of Map<string, Pattern>.

The most direct way of rendering the returned Pattern object is to call .toDataUrl() on it. Use the result in your CSS via the style prop or another prop that your style framework exposes (e.g. sx in Material-UI).

examples

Simplest usage:

import { useGeoPattern } from 'react-geopattern';

export default function TestComponent() {
  const pattern = useGeoPattern('input-string');

  return <div style={{ 'background-image': pattern.toDataUrl() }}>Test</div>;
}

With custom GeoPattern options:

import { useGeoPattern } from 'react-geopattern';

export default function TestComponent() {
  const pattern = useGeoPattern('input-string', {
    // any options that GeoPattern accepts
    color: '#ff0000'
  });

  return <div style={{ 'background-image': pattern.toDataUrl() }}>Test</div>;
}

With self-managed pattern cache:

import { useMemo } from 'react';
import { useGeoPattern } from 'react-geopattern';

export default function TestComponent() {
  // you can invalidate the cache as needed with useMemo()
  const cache = useMemo(() => new Map(), []);
  const pattern = useGeoPattern('input-string', undefined, cache);

  return <div style={{ 'background-image': pattern.toDataUrl() }}>Test</div>;
}

react-geopattern's People

Contributors

ayan4m1 avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

daviddyess

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.