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

@shjeon0730/svg-gen-utils

v3.0.2

Published

a group of utilities to help svg paths, text using absolute position

Downloads

33

Readme

SVG-gen-utils

Overview

This utilility helps to create an SVG image.


DEMO demo - I know they are not very pretty, but I believe you will make awesome result using this library

supported functionalities

  • path
  • text(centered text)
  • mask
  • render

path

  • arc
  • line

example code:

import svgUtils from '@shjeon0730/svg-gen-utils';
...
function makeAPathPrimitive(color) {
  const pathUtil = svgUtils.path;
  const offsetX = -30;
  const offsetY = -30;

  const arrToPt = arr => ({ x: arr[0] + offsetX, y: arr[1] + offsetY });
  const startPoint = arrToPt([0, 0]);
  const linePtsL2R = [[4, 0], [14, 5], [24, -5], [34, 0]].map(arrToPt);
  const posArcsT2B = [[34, 10], [34, 20], [34, 30], [34, 40]].map(arrToPt);
  const bigArcR2L = [[0, 50]].map(arrToPt);
  const negArcsB2T = posArcsT2B.map(pt => ({ ...pt, x: offsetX })).reverse();

  const instance = pathUtil.default(startPoint);
  const builder = instance.element;
  const elements = [
    ...linePtsL2R.map(pt => builder.line(pt)),
    ...posArcsT2B.map(pt => builder.arc(pt, 7, 0, 1)),
    ...bigArcR2L.map(pt => builder.arc(pt, 20, 1, 1)),
    ...negArcsB2T.map(pt => builder.arc(pt, 5, 0, 0))
  ];
  return instance.render(elements, 'red', color, '1');
}

function primitivePathSample() {
  return svgUtils.renderer.svgRender(React.createElement, -50, -50, 100, 100, {}, [makeAPathPrimitive('yellow')]);
}

pathsWithAbsPts

this api is for lazy users. With this api, you can continue your previous action just with next point. even though you want to change one of the feature of your previous action, you don't have to define everything once more.


function makeAPath(color) {
  const pathUtil = svgUtils.path;

  const { pathsFromAbsPts, PathTools } = pathUtil;
  const offsetX = -30;
  const offsetY = -30;
  const arrToPt = arr => ({ x: arr[0] + offsetX, y: arr[1] + offsetY });
  const startPoint = arrToPt([0, 0]);
  const linePtsL2R = [[4, 0], [14, 5], [24, -5], [34, 0]].map(arrToPt);
  const posArcsT2B = [[34, 10], [34, 20], [34, 30], [34, 40]].map(arrToPt);
  const bigArcR2L = [[0, 50]].map(arrToPt);
  const negArcsB2T = posArcsT2B.map(pt => ({ ...pt, x: offsetX })).reverse();

  return pathsFromAbsPts([
    startPoint,
    PathTools.LINE.init(),
    ...linePtsL2R,
    PathTools.ARC.init(7, 0, 1), //full setting. radius:7, bigArc:0, positiveArc:1
    ...posArcsT2B,
    PathTools.ARC.rad(20),//chang radius to 20
    PathTools.ARC.bigArc(),//use big arc.
    ...bigArcR2L,
    PathTools.ARC.rad(5),//change radius to 5
    PathTools.ARC.negativeArc(),//use negative arc
    ...negArcsB2T
  ], 'black', color, 1);
}
function pathSample() {

  return svgUtils.renderer.svgRender(React.createElement, -50, -50, 100, 100, {}, [makeAPath('green')]);
}

Since it is saving the previous option, you don't have to setup arc option all again like above example. But you can setup the tool setting all again by using init function if you want to.


mask


function maskSample() {

  const circle = (color) => {
    return svgUtils.renderer.createElementInfo('circle', {
      r: 50,
      cx: 0,
      cy: 0,
      style: { fill: color, strokeWidth: 0.1, stroke: 'black' },
      ...svgUtils.mask.useMask('sample-mask')
    });
  }
  const rect = color => { //positive mask(white)
    return svgUtils.renderer.createElementInfo('rect', {
      width: 90, height: 90, x: -45, y: -45,
      style: { fill: color, strokeWidth: 0.1, stroke: 'black' }
    })
  }
  const path = makeAPath('black');//negative mask(black)
  ///, 
  const mask = svgUtils.mask.createMaskElement([rect('white'), path], 'sample-mask');

  const maskGroup = svgUtils.etc.group([circle('blue'), mask], 'mask-group-sample');

  return svgUtils.renderer.svgRender(React.createElement, -50, -50, 100, 100, {}, [maskGroup]);
}

making mask is not hard even if you make mask element using the primitive functionality. But I added this for a convenience.


text

  • center text
  • center link
  • multiple lines
  • color/fontSize settings
  • additional properties

example code:

import svgUtil from '@shjeon0730/svg-gen-utils';
...
const txtUtil = svgUtil.text;

const centerTexts = {
    defaultOptions: svgUtil.text.createDefaultOption(5,1 ,'red'),
    items:["hey", "I am a", "chart"]
}
const txt1 = txtUtil.drawCenterText(centerTexts.items, {x: offsetX, y: offsetY}, centerTexts.defaultOptions)

const linkTexts = [
    createLinkObj("go to google", 9, null, 'blue', 'http://www.google.com', 
        {
            style:{textDecoration:'none'},
            target: "_blank"
        }),
    createTextObj('normalText', 7, 1, 'red')
];
const txt2 = txtUtil.drawCenterText(centerTexts, {x: offsetX, y: offsetY});

const defaultTexts = ['text1','text2'];

renderer

This library doesn't render HTML element directly since I did not use any specific framework (ex> React, Vue.js, or Angular). So once you have results from path or text utils, you need to render using your own render function that your framework supports.

But I made renderer utils for you to render easily.

// if your framework is React
import svgUtil from '@shjeon0730/svg-gen-utils';
...
const renderer = svgUtil.renderer;
const svgInfo = renderer.renderSvgInfo(left, top, viewBoxWidth, viewBoxHeight, {
        id, 
        'aria-labelledby':[titleId, descId].join(' ')
    }, [ 
        renderer.createElementInfo('title',{id:titleId},'svg shoechart'),
        renderer.createElementInfo('desc',{id:descId}, 'svg shoehorse'),
        renderer.createElementInfo('g', {}, [elementInfo1, elementInfo2]),
        createElementInfo('g', {}, [textInfo1, textInfo2])
    ]);
renderer.render(React.createElement, svgInfo);

Make sure your render function has 3 arguments like this:

function myRenderFunction(tagName, attributes, children=[]){
    return React.CreateElement(tagName, attributes, children);
}

If your framework looks different, you need to create one like above and pass the function reference to render function.

renderer.render(myRenderFunction, svgInfo);