Commit e70705be authored by Jeff Avallone's avatar Jeff Avallone

Extracting bounding box code into its own function

parent 754868b9
import React from 'react';
import ReactDOM from 'react-dom';
import SVG from 'rendering/SVG/layout';
import Text from 'rendering/Text/layout';
......@@ -19,4 +22,20 @@ const layout = data => {
});
};
const getBBox = content => {
const container = document.createElement('div');
document.body.appendChild(container);
ReactDOM.render(
<svg width="0" height="0" viewBox="0 0 0 0">
<g>{ content }</g>
</svg>, container);
const box = container.querySelector('svg > g').getBBox();
document.body.removeChild(container);
return box;
};
export default layout;
export { getBBox };
import React from 'react';
import ReactDOM from 'react-dom';
import { getBBox } from 'layout';
import Text from 'rendering/Text';
const layoutText = data => {
const container = document.createElement('div');
document.body.appendChild(container);
const {x, y, width, height } = getBBox(
<Text { ...data.props }>{ data.children }</Text>);
ReactDOM.render(
<svg width="0" height="0" viewBox="0 0 0 0">
<Text { ...data.props }>{ data.children }</Text>
</svg>,
container);
const box = container.querySelector('svg > text').getBBox();
document.body.removeChild(container);
data.box = {
width: box.width,
height: box.height
};
data.props.transform = `translate(${ -box.x } ${ -box.y })`;
data.box = { width, height };
data.props.transform = `translate(${ -x } ${ -y })`;
return data;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment