UNPKG

snapsvg

Version:
93 lines (92 loc) 3.19 kB
// Copyright (c) 2014 Adobe Systems Incorporated. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. Snap.plugin(function (Snap, Element, Paper, glob, Fragment) { var box = Snap._.box, is = Snap.is, firstLetter = /^[^a-z]*([tbmlrc])/i, toString = function () { return "T" + this.dx + "," + this.dy; }; /*\ * Element.getAlign [ method ] ** * Returns shift needed to align the element relatively to given element. * If no elements specified, parent `<svg>` container will be used. - el (object) @optional alignment element - way (string) one of six values: `"top"`, `"middle"`, `"bottom"`, `"left"`, `"center"`, `"right"` = (object|string) Object in format `{dx: , dy: }` also has a string representation as a transformation string > Usage | el.transform(el.getAlign(el2, "top")); * or | var dy = el.getAlign(el2, "top").dy; \*/ Element.prototype.getAlign = function (el, way) { if (way == null && is(el, "string")) { way = el; el = null; } el = el || this.paper; var bx = el.getBBox ? el.getBBox() : box(el), bb = this.getBBox(), out = {}; way = way && way.match(firstLetter); way = way ? way[1].toLowerCase() : "c"; switch (way) { case "t": out.dx = 0; out.dy = bx.y - bb.y; break; case "b": out.dx = 0; out.dy = bx.y2 - bb.y2; break; case "m": out.dx = 0; out.dy = bx.cy - bb.cy; break; case "l": out.dx = bx.x - bb.x; out.dy = 0; break; case "r": out.dx = bx.x2 - bb.x2; out.dy = 0; break; default: out.dx = bx.cx - bb.cx; out.dy = 0; break; } out.toString = toString; return out; }; /*\ * Element.align [ method ] ** * Aligns the element relatively to given one via transformation. * If no elements specified, parent `<svg>` container will be used. - el (object) @optional alignment element - way (string) one of six values: `"top"`, `"middle"`, `"bottom"`, `"left"`, `"center"`, `"right"` = (object) this element > Usage | el.align(el2, "top"); * or | el.align("middle"); \*/ Element.prototype.align = function (el, way) { return this.transform("..." + this.getAlign(el, way)); }; });