UNPKG

3.02 kBJavaScriptView Raw
1// https://github.com/HarryStevens/shape2path#readme Version 2.0.5. Copyright 2019 Harry Stevens.
2(function (global, factory) {
3 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
4 typeof define === 'function' && define.amd ? define(['exports'], factory) :
5 (factory((global.shape2path = {})));
6}(this, (function (exports) { 'use strict';
7
8 const NUM_DEFAULT = 0;
9 // The true default is an empty string, but it results in invalid path, so use
10 // "0 0"
11 const POINTS_DEFAULT = "0 0";
12
13 function circle2Path(options) {
14 var cx = options.cx || NUM_DEFAULT,
15 cy = options.cy || NUM_DEFAULT,
16 r = options.r || NUM_DEFAULT;
17
18 return "M" + cx + "," + cy + " m" + (-r) + ",0 a" + r + "," + r + " 0 1,0 " + (r * 2) + ",0 a" + r + "," + r + " 0 1,0 " + (-r * 2) + ",0";
19 }
20
21 function ellipse2path(options){
22 var cx = options.cx || NUM_DEFAULT,
23 cy = options.cy || NUM_DEFAULT,
24 rx = options.rx || NUM_DEFAULT,
25 ry = options.ry || NUM_DEFAULT;
26
27 return "M" + (cx - rx) + "," + cy + " a" + rx + "," + ry + " 0 1,0 " + (rx * 2) + ",0 a" + rx + "," + ry + " 0 1,0 -" + (rx * 2) + ",0";
28 }
29
30 function line2path(options){
31 var x1 = options.x1 || NUM_DEFAULT,
32 x2 = options.x2 || NUM_DEFAULT,
33 y1 = options.y1 || NUM_DEFAULT,
34 y2 = options.y2 || NUM_DEFAULT;
35
36 return "M" + x1 + "," + y1 + " L" + x2 + "," + y2;
37 }
38
39 function polygon2path(options){
40 var points = options.points || POINTS_DEFAULT;
41
42 return points.split(" ").map(function(point, i){ return i == 0 ? "M" + point : "L" + point; }).join(" ") + " Z";
43 }
44
45 function polyline2path(options){
46 var points = options.points || POINTS_DEFAULT;
47
48 return points.split(" ").map(function(point, i){ return i == 0 ? "M" + point : "L" + point; }).join(" ");
49 }
50
51 function rect2Path(options){
52 var x = options.x || NUM_DEFAULT,
53 y = options.y || NUM_DEFAULT,
54 w = (options.width || NUM_DEFAULT) + x,
55 h = (options.height || NUM_DEFAULT) + y;
56
57 if (options.rx || options.ry) {
58 var rx = options.rx ? options.rx : options.ry,
59 ry = options.ry ? options.ry : options.rx;
60
61 return "M" + (x + rx) + "," + y +
62 " H" + (w - rx) +
63 " C" + (w - rx) + "," + y + " " + w + "," + y + " " + w + "," + (y + ry) +
64 " V" + (h - ry) +
65 " C" + w + "," + (h - ry) + " " + w + "," + h + " " + (w - rx) + "," + h +
66 " H" + (x + rx) +
67 " C" + (x + rx) + "," + h + " " + x + "," + h + " " + x + "," + (h - ry) +
68 " V" + (y + ry) +
69 " C" + x + "," + (y + ry) + " " + x + "," + y + " " + (x + rx) + "," + y;
70 } else {
71 return "M" + x + "," + y + " H" + w + " V" + h + " H" + x + " Z";
72 }
73
74 }
75
76 exports.circle = circle2Path;
77 exports.ellipse = ellipse2path;
78 exports.line = line2path;
79 exports.polygon = polygon2path;
80 exports.polyline = polyline2path;
81 exports.rect = rect2Path;
82
83 Object.defineProperty(exports, '__esModule', { value: true });
84
85})));