-
Notifications
You must be signed in to change notification settings - Fork 314
Expand file tree
/
Copy pathmakeSvgController.js
More file actions
78 lines (65 loc) · 2.06 KB
/
Copy pathmakeSvgController.js
File metadata and controls
78 lines (65 loc) · 2.06 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
module.exports = makeSvgController;
module.exports.canAttach = isSVGElement;
function makeSvgController(svgElement, options) {
if (!isSVGElement(svgElement)) {
throw new Error('svg element is required for svg.panzoom to work');
}
var owner = svgElement.ownerSVGElement;
if (!owner) {
throw new Error(
'Do not apply panzoom to the root <svg> element. ' +
'Use its child instead (e.g. <g></g>). ' +
'As of March 2016 only FireFox supported transform on the root element');
}
if (!options.disableKeyboardInteraction) {
owner.setAttribute('tabindex', 0);
}
var api = {
getBBox: getBBox,
getScreenCTM: getScreenCTM,
getOwner: getOwner,
applyTransform: applyTransform,
initTransform: initTransform
};
return api;
function getOwner() {
return owner;
}
function getBBox() {
var boundingBox = svgElement.getBBox();
return {
left: boundingBox.x,
top: boundingBox.y,
width: boundingBox.width,
height: boundingBox.height,
};
}
function getScreenCTM() {
var ctm = owner.getCTM();
if (!ctm) {
// This is likely firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=873106
// The code below is not entirely correct, but still better than nothing
return owner.getScreenCTM();
}
return ctm;
}
function initTransform(transform) {
var screenCTM = svgElement.getCTM();
// The above line returns null on Firefox
if (screenCTM === null) {
screenCTM = document.createElementNS("http://www.w3.org/2000/svg", "svg").createSVGMatrix();
}
transform.x = screenCTM.e;
transform.y = screenCTM.f;
transform.scale = screenCTM.a;
owner.removeAttributeNS(null, 'viewBox');
svgElement.style.setProperty('transform', 'matrix(var(--pz-transform))');
}
function applyTransform(transform) {
svgElement.style.setProperty('--pz-transform',
`${transform.scale}, 0, 0, ${transform.scale}, ${transform.x}, ${transform.y}`);
}
}
function isSVGElement(element) {
return element && element.ownerSVGElement && element.getCTM;
}