Giter Club home page Giter Club logo

bare-minimum-2d's Introduction

A Bare Minimum 2D Plotter

An extremely lightweight React component to declaratively (and elegantly) plot shapes on an inline SVG

NPM MINIFIED GZIPPED

Update: External Plugins ๐Ÿฅณ

You can now use your own shape implementation by passing it as a plugin (see plugin section below for more information). Below are a couple of plugins by @fuddl.

Demo Applications

Responsive Illustrations On-The-Fly Animations Interactive Applications
demo demo demo
source code source code source code

Install

npm install --save bare-minimum-2d

Usage

This is an example of what you can pass to a BareMinimum2d component.

You pass it like so:

import BareMinimum2d from 'bare-minimum-2d'

<div style={{ width: '100%', height: '100vh' }}>
  <BareMinimum2d {...{ data, container }} />
</div>

The component takes the dimensions of its parent and is always centered

Everything you need to know explained in two minutes

A BareMinimum2d component only has two props: container and data. container is a small object with exactly four elements. data is an array containing objects.

Example:

import BareMinimum2d from 'bare-minimum-2d'

const container = {
  color: '#0000FF',
  opacity: 0.2,
  xRange: 300,
  yRange: 500
}

const data = [{
  x: [0],
  y: [0],
  color: "#FFFFFF",
  opacity: 1.0,
  size: 10,
  type: 'points',
  id: 'center'
}]

<div style={{ width: "100%", height: "100vh" }}>
  <BareMinimum2d {...{ data, container }} />
</div>

container.color and container.opacity specifies the canvas color of BareMinimum2d.

The cartesian coordinate system of BareMinimum will follow the diagram below given container.xRange and container.yRange. Position (0, 0) will always be at the center of the rendered component.

                  yRange/2
                     |
                     |
  -xRange/2 -------(0,0)--------- xRange/2
                     |
                     |
                   -yRange/2

Please take a look at more complex example data prop to get the idea. each element of the array data should be a hash-like objectwith a type key which should have a value that is one of the following:

points ellipse lines polygon
plural singular plural singular

Elements of the data array will be stacked based on the order they are declared. The first element will be at the most bottom layer while the last element of the array will be at the top.

All attributes are ALWAYS required, nothing is optional because there are no default values. The id attribute must be unique for each element of the data array.

Create your own

You can add your own shapes as a plugin for example, here's an example plugin written by @fuddl

const Triangle = ({ x, y, transforms, size, color, opacity, id, i }) => {
  const cx = transforms.tx(x)
  const cy = transforms.ty(y)
  const ySize = size * 0.8626
  return (
    <polygon
      {...{
        opacity,
        id: `${id}-${i}`,
        fill: color
      }}
      points={[
        `${cx},${cy - ySize}`,
        `${cx + size},${cy + ySize}`,
        `${cx - size},${cy + ySize}`
      ].join(' ')}
    />
  )
}

const trianglesPlugin = {
  triangle: (element, transforms) => {
    const { size, color, opacity, id } = element
    return element.x.map((x, i) => (
      <Triangle
        {...{
          x,
          y: element.y[i],
          size,
          color,
          opacity,
          id,
          i,
          transforms
        }}
        key={`${id}-${i}`}
      />
    ))
  }
}

And you can use it like so:

const triangle = {
  "x": [-163.72675374383329],
  "y": [-154.33259574213795],
  "opacity": 1,
  "size": 60,
  "color": "#2196F3",
  "type": "triangles",
  "id":"points0"
}

<div style={{ width: "100%", height: "100vh" }}>
  <BareMinimum2d {...{ data: [triangle], container, plugins: [trianglesPlugin] }} />
</div>

END

Contributing

  1. Clone this repository.
  2. Add your changes
  3. You can add a demo or update the demo based on your changes somewhere here
  4. After making your change go run the following command to see if it works as you expect.
npm install && npm run build && rm -rf node_modules && cd example && npm install && npm run start

PRs welcome! Please read the contributing guidelines and the commit style guide!

License

MIT ยฉ Mithi

bare-minimum-2d's People

Contributors

dependabot[bot] avatar fuddl avatar mikong avatar mithi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

bare-minimum-2d's Issues

TODO: Be able to specify and render just one or two quadrants instead of the four

Currently, when we specify the x-range the y-range of the container the default plot would be like so:

                  yRange/2
                     | 
               Q2    |      Q1
  -xRange/2 -------(0,0)--------- xRange/2
               Q3    |      Q4
                     |    
                 -yRange/2

Sometimes we only need one of the following

  • Q1
  • Q2
  • Q3
  • Q4
  • Q1 + Q2
  • Q3 + Q4
  • Q1 + Q4
  • Q2 + Q3
  • Q1 + Q2 + Q3 + Q4

We should be able to specify, so that we don't waste space in the screen that don't have markers.
I'm thinking the API would be something like:

const container = { xRange, yRange, color, opacity, quadrants }

Where quadrant is a string that could contain "Q12", "Q3", "Q1234" etc

We have to change this part

transformX = (x) => x + this.xRange / 2
transformY = (y) => this.yRange / 2 - y

to not be hard coded and be generic

Resolve registry conflict

SOLUTION

$ npm login --registry=https://npm.pkg.github.com --scope=@mithi
$ npm publish --dry-run

from NPM docs

https://docs.npmjs.com/misc/scope

You can associate a scope with a registry at login, e.g.

npm config set @myco:registry http://reg.example.com

Once a scope is associated with a registry, any npm install for a package with that scope will request packages from that registry instead. Any npm publish for a package name that contains the scope will be published to that registry instead.

Problem

v0.1.12 on GitHub registry
https://github.com/mithi/bare-minimum-2d/packages/352301

v0.1.11 on NPM registry
https://www.npmjs.com/package/bare-minimum-2d

But there is actually no difference between the two versions

Notes

npm publish will publish to the github registry because of .npmrc file below
https://github.com/mithi/bare-minimum-2d/blob/master/.npmrc
which has

registry=https://npm.pkg.github.com/mithi

BUT BEFORE PUBLISHING, you need to edit package.json
https://github.com/mithi/bare-minimum-2d/blob/master/package.json
to be

{
  "name": "@mithi/bare-minimum-2d",
  "version": "0.1.12",
}

and to publish to the npm registry, you'd have to remove .npmrc file
and change the name back to { "name": "bare-minimum-2d" }

This is of course a hack which should be resolved.

Please refer to the following in the docs:
https://docs.github.com/en/actions/language-and-framework-guides/publishing-nodejs-packages#publishing-packages-to-npm-and-github-packages

Note: If you need to publish to registries that have different scope prefixes, you'll need to modify the package.json file on the runner to change the scope prefix. For example, if you publish a package to the @mona scope for npm and @octocat scope for GitHub Packages, you can replace the @mona scope with @octocat in the package.json file on the runner after publishing to npm and before publishing to GitHub Packages.

Markers with a text label

Hi,

I'd like to append text labels to each marker:

nearby stars

these should also make sure not to overlap with other text markers.

please let me know if this exceeds the boundaries of bare minimum ๐Ÿ˜….

Would It be possible to define a marker type as a plug-in? such a plug-in could be used to solve #1

TODO: Be able to use and render custom markers (plus, triangle, etc)

Markers

  • Add an element type called "markers"
    If an element is of type markers, you should specify amarkerType like

  • "plus"

  • "plus-circle"

  • "plus-square"

  • "triangle"

  • "mouse-pointer"

  • "arrow-up-circle"

  • "arrow-up"

To use, you can pass it to the data array like so:

const markers1 = {
  x: [0, -50, -50, 50, 50],
  y: [0, -50, 50, 50, -50],
  theta: 0,
  color: 'red',
  opacity: 0.75,
  size: 5,
  type: 'markers',
  markerType: 'arrow-up',
  id: 'marker1'
}

const markers2 = {
  x: [0, 0, 0, 0],
  y: [-20, -10, 10, 20],
  theta: -45,
  color: 'purple',
  opacity: 0.75,
  size: 3,
  type: 'markers',
  markerType: 'plus-circle',
  id: 'markers2'
}

TODO: Use parcel instead of CRA

CRA installs babel jest in the example directory which is unused.
This babel jest causes problems because we need to have babel jest on the root directory,
having two separate versions of babel jest (and related libraries) - one for the root directory and the other to its child directory (in this case is the child directory is the example directory)- is preventing a successful build.

The solution is to use parcel instead of CRA. It will not install babel jest (and related libraries) in the example directory

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.