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

svg-polygon-morpher

v1.0.1

Published

Node.js module for morphing SVG Polygons without using SMIL Animation.

Downloads

4

Readme

svg-polygon-morpher

Node.js module for morphing SVG Polygons without using SMIL Animation.

This package was built for use with React

How to use the svg-polygon-morpher package:

1. In the terminal, run the following commands:

A) $ npx-create-react-app test-svg-polygon-morpher

B) $ npm i svg-polygon-morpher

C) $ cd src

D) $ code App.js

2. Replace the contents of App.js with the following:

// Here we are importing React and the React useReducer hook to manage the state of our SVG polygon coordinates.
import React, { useReducer } from 'react';
// Here we are importing morphPolygon
// We will use to morph, or transition, the points of one polygon from one state to another
import morphPolygon from "svg-polygon-morpher";

// Here we are declaring an array containing three objects. Each of these objects contains an x and a y coordinate.
// Together, the three sets of x and y coordinates will be used to define the points of a triangle svg polygon.
let triangle1Coordinates = [
  { x: 10, y: 0 },
  { x: 10, y: 30 },
  { x: 60, y: 40 }
];

// Here we are defining a second array of triangle svg polygon coordinates which differ from the first.
// We will be transitioning the polygons from the initial state provided by the triangle1Coordinates to an updated state provided by the triangle2Coordinates
let triangle2Coordinates = [
  { x: 20, y: 0 },
  { x: 20, y: 40 },
  { x: 50, y: 80 }
];

// the buildPointsString function takes in an arry of coordinates and returns a string value which can be used as a svg polygon points attribute's value

function buildPointsString (coordinatesArray) {
  // if the value of coordinatesArray is not an array, buildPointsString will return an alert informing the user.
  if (!Array.isArray(coordinatesArray)) {
    return alert("buildPointsString receives a coordinatesArray argument whose data type is not an array.");
  }

  // if the coordinatesArray parameter passes the array validation, the .map() Array method is executed on the coordinatesArray.
  // Each iteration of the array mapping will return a string in the following example format: "10,0"
  // the .map() Array method returns an array containing the values returned from inside the .map() method callback, in the case of triangle1Coordinates, this array would look like this:
  // eg: ["10,0", "0,100", "100,100"]

  // The buildPointsString function then uses the .join() array method with a " " space separator string passed in as a .join() argument to insert a space between each joined item in the array.
  // In the case of triangle1Coordinates, the joined array will look like this:
  //eg: "10,0 0,100 100,100" --> This is the correct syntax to use as a svg polygon points attributes value.

  return coordinatesArray.map((coordinates) => {
      return coordinates.x + "," + coordinates.y;
  }).join(" ");
}

function App() {
  // Here we are using React's useReducer hook to manage the state of the value passed into the polygon points attribute;
  const [usePolygonPoints, setPolygonPoints] = useReducer((state, action) => {
    switch (action.type) {
        case "update": return { ...state };
        default: throw new Error("Reducer received unexpected action type");
    }
  }, triangle1Coordinates);

  return (
    <div className="App">

      {/* Below is a button we will use to trigger the svg-polygon-morpher */}
      <button onClick={() => {
          // morphTo is the set of coordinates we will be morphing to
          // if usePolygonPoints state is not equal to triangle1Coordinates then use triangle1Coordinates
          // else use the triangle2Coordinates.
          let morphTo = (usePolygonPoints !== triangle1Coordinates) ? triangle1Coordinates : triangle2Coordinates;

          // morphingInterval is the numeric value of the interval (in milliseconds) with which we would like our morph to occur.
          let morphingInterval = 1;

          // morphPolygonCallback is a callback we pass into morphPolygon.
          // This particular callback will be used to update the state of usePolygonPoints
          let morphPolygonCallback = () => {
            return setPolygonPoints({ type: "update" });
          };

          // morphPolygon takes in four arguments:
          // --- the first is the initial coordinates for our polygon
          // --- the second is the coordinates with which we would like our polygon to morph to
          // --- the third is the interval at which we would like our morph to occur
          // --- the fourth is a callback we pass into the morphPolygon function to update the state of our polygon.
          return morphPolygon(usePolygonPoints, morphTo, morphingInterval, morphPolygonCallback);
      }}> It's morphin' time </button>

      <svg id="svg" viewBox="0 0 100 100">
        <polygon id="polygon" points={buildPointsString(triangle1Coordinates)} />
      </svg>

    </div>
  );
}

export default App;

3) In the terminal, cd into the root directory of test-svg-polygon-morpher && run the following

A) $ npm start