-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathget_bbox.js
57 lines (40 loc) · 1.29 KB
/
get_bbox.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'use strict';
var d3Select = require('../../strict-d3').select;
var ATTRS = ['x', 'y', 'width', 'height'];
// In-house implementation of SVG getBBox that takes clip paths into account
module.exports = function getBBox(element) {
var elementBBox = element.getBBox();
var s = d3Select(element);
var clipPathAttr = s.attr('clip-path');
if(!clipPathAttr) return elementBBox;
// only supports 'url(#<id>)' at the moment
var clipPathId = clipPathAttr.substring(6, clipPathAttr.length - 2);
var clipBBox = getClipBBox(clipPathId);
return minBBox(elementBBox, clipBBox);
};
function getClipBBox(clipPathId) {
var clipPath = d3Select('#' + clipPathId);
var clipBBox;
try {
// this line throws an error in FF (38 and 45 at least)
clipBBox = clipPath.node().getBBox();
} catch(e) {
// use DOM attributes as fallback
var path = d3Select(clipPath.node().firstChild);
clipBBox = {};
ATTRS.forEach(function(attr) {
clipBBox[attr] = path.attr(attr);
});
}
return clipBBox;
}
function minBBox(bbox1, bbox2) {
var out = {};
function min(attr) {
return Math.min(bbox1[attr], bbox2[attr]);
}
ATTRS.forEach(function(attr) {
out[attr] = min(attr);
});
return out;
}