0.1.1 • Published 3 years ago

orange-svg v0.1.1

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

Orange.SVG

Library for generating SVG as DOM elements or string.

This library has limited functionality (mostly because it was created to be used by another library which doesn't use all SVG elements), but you can extend it in your projects.

You can build this library for static front-end usage via command npm run build (you need to run npm install first). JavaScript file will appear in the dist directory.

Classes

OrangeSVG

Class represents SVG document.

ElementNameTypeDescription
methodconstructorCreates SVG object
propertywnumberWidth
propertyhnumberHeight
propertyx1numberX coordinate of top left corner
propertyy1numberY coordinate of top left corner
methodappendChildOrangeSVGAdds element into SVG document
renderrenderSVGSVGElementRenders SVG in the container (can be defined by element's ID of DOM Element itself)
propertyelementSVGSVGElementSVG Object converted to HTML SVGSVGElement
propertytoStringstringSVG Object converted to HTML string

OrangeSVGAbstractElement

This is parental class for all SVG Elements in this library. All classes below are extended from this class and have its methods.

ElementNameTypeDescription
abstract propertytagstringTag name
propertyargsObjectCustom HTML attributes
methodtoStringstringSVG Element converted to HTML string
methodcreateDOMElementSVGElement*SVG Element converted to HTML string

* - can be any SVG-related class like SVGCircleElement, SVGClipPathElement, SVGComponentTransferFunctionElement, etc.

Elements classes

Classes extending OrangeSVGAbstractElement have different constructors.

ClassConstructor arguments
OrangeSVGCirclecenter_x, center_y, radius, color (optional)
OrangeSVGLinestart_x, start_y, end_x, end_y, color (optional), thickness (optional)
OrangeSVGPathd*, color (optional)
OrangeSVGPolygoncoordinates, color (optional), opacity (optional)
OrangeSVGPolylinecoordinates, color (optional), thickness (optional)
OrangeSVGRecttop_left_x, top_left_y, width, height, color (optional)
OrangeSVGTexttext, x, y, position (optional)

Example

<html>
<script src="./../dist/orange-svg.min.js"></script>
<body>
<div id="svg-container"></div>
<script>
  const svg = new OrangeSVG(1000, 600, -500, -300)
  svg.appendChild(new OrangeSVGCircle(0, 0, 200, 'orange'))
  svg.appendChild(new OrangeSVGText("Click me", 250, 0), {'click': (e,v) => alert('Library: ' + v.name)}, {"name": "Orange.IP"})
  svg.render('svg-container')
</script>
</body>
</html>

Element svg-container will have value:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="-500 -300 1000 600" width="1000" height="600">
    <circle r="200" cx="0" cy="0" fill="orange"></circle>
    <text x="250" y="0" text-anchor="middle">Click me</text>
</svg>

Text element will have function (e,v) => alert('Library: ' + v.name) to be assigned to click event and object {"name": "Orange.IP"} to be passed there as the second argument.