ng-zorro-antd
Version:
An enterprise-class UI components based on Ant Design and Angular
30 lines • 3.96 kB
JavaScript
/**
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/
/**
* Calculate position and scale
* @param containerEle
* @param targetEle
* @param scale: if scale is set, skip calculate scale value
*/
export const calculateTransform = (containerEle, targetEle, scale) => {
const containerEleSize = containerEle.getBoundingClientRect();
const targetEleSize = targetEle.getBBox();
if (!targetEleSize.width) {
// There is no g element anymore.
return null;
}
// TODO
// leave some place when re-scale
const scaleUnit = (containerEleSize.width - 48) / containerEleSize.width;
const k = scale || Math.min(containerEleSize.width / targetEleSize.width, containerEleSize.height / targetEleSize.height, 1) * scaleUnit;
const x = (containerEleSize.width - targetEleSize.width * k) / 2;
const y = (containerEleSize.height - targetEleSize.height * k) / 2;
return {
x,
y,
k
};
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXRpbHMuanMiLCJzb3VyY2VSb290IjoiL2hvbWUvdnN0cy93b3JrLzEvcy9jb21wb25lbnRzL2dyYXBoLyIsInNvdXJjZXMiOlsiY29yZS91dGlscy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTs7O0dBR0c7QUFJSDs7Ozs7R0FLRztBQUNILE1BQU0sQ0FBQyxNQUFNLGtCQUFrQixHQUFHLENBQUMsWUFBMkIsRUFBRSxTQUFzQixFQUFFLEtBQWMsRUFBMEIsRUFBRTtJQUNoSSxNQUFNLGdCQUFnQixHQUFHLFlBQVksQ0FBQyxxQkFBcUIsRUFBRSxDQUFDO0lBQzlELE1BQU0sYUFBYSxHQUFHLFNBQVMsQ0FBQyxPQUFPLEVBQUUsQ0FBQztJQUMxQyxJQUFJLENBQUMsYUFBYSxDQUFDLEtBQUssRUFBRTtRQUN4QixpQ0FBaUM7UUFDakMsT0FBTyxJQUFJLENBQUM7S0FDYjtJQUVELE9BQU87SUFDUCx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