-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·149 lines (120 loc) · 4.05 KB
/
index.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// v0.1.2
export default function elasticSVG(selector, opts) {
opts = opts || {};
let hasWidth = opts.hasOwnProperty("width");
let hasHeight = opts.hasOwnProperty("height");
let hasAspect = opts.hasOwnProperty("aspect");
// containing DOM element, which defaults to body
if (selector instanceof Element) {
var parent = selector;
} else {
var parent = document.querySelector(selector || "body");
}
if (!parent) {
console.log("Couldn't find a parent for elasticSVG using the selector '" + selector + "'");
return;
}
let initialWidth = parent.clientWidth;
// you can specify a width if you like, or we'll snap to size of container
var base = {
width: null,
height: null,
aspect: null,
scale: 1
};
// we need to remember the original width for scaling purposes
// you can either specify the height and width, which will computed the aspect ration, or one of these and the aspect ratio.
// If neither is specified, defaults to roughly the golden ratio
// specifying the height keeps the svg at a standard height and only resizes the width
// specifying the aspect ratio resizes both
if (hasWidth && hasHeight) {
if (hasAspect) {
console.log("Overriding aspect ratio since both `width` and `height` are specified");
opts.aspect = opts.height / opts.width;
}
base.width = opts.width;
base.height = opts.height;
} else { // if has neither an aspect or BOTH a width and height, default to approximately the golden ratio
opts.aspect = opts.aspect || 0.618;
if (hasWidth && !hasHeight) {
base.width = opts.width;
base.height = base.width * opts.aspect;
} else if (hasHeight) {
base.height = opts.height;
base.width = base.height / opts.aspect;
} else { // if only has aspect
base.width = initialWidth;
base.height = base.width * opts.aspect;
}
}
base.original_width = base.width;
// create a new SVG element
var xmlns = "http://www.w3.org/2000/svg";
var svg = document.createElementNS(xmlns, "svg");
svg.setAttributeNS(null, "width", base.width);
svg.setAttributeNS(null, "height", base.height);
parent.appendChild(svg);
// setting resize to "auto" sets the viewport to the original width and height so that the SVG always scales
if (opts.resize && opts.resize == "auto") {
svg.setAttributeNS(null, "viewBox", "0 0 " + base.width + " " + base.height);
}
// function called when the window resizes
function resize() {
let hasWidth = opts.hasOwnProperty("width");
let hasHeight = opts.hasOwnProperty("height");
let hasAspect = opts.hasOwnProperty("aspect");
base.width = parent.clientWidth;
svg.setAttributeNS(null, "width", base.width);
// if `height` is not specified, resize it to the aspect ratio
if (!hasHeight) {
base.height = base.width * opts.aspect;
svg.setAttributeNS(null, "height", base.height);
}
base.aspect = base.height / base.width;
base.scale = base.width / base.original_width;
// optional callback
if (opts.onResize) {
opts.onResize(base.width, base.height, base.scale, svg);
}
}
window.addEventListener("resize", function() {
resize();
});
// manually trigger a page resize and run the resize function once
window.dispatchEvent(new Event('resize'));
// resize(); // call this on load since sometimes the initial conditions are wider than container
// methods
base.setResize = function(f) {
opts.onResize = f;
}
base.changeAspect = function(aspect) {
opts.aspect = aspect;
base.height = base.width * opts.aspect;
svg.setAttributeNS(null, "height", base.height);
}
/*
base.changeWidth = function(width) {
base.width = width;
if (opts.aspect) {
opts.aspect = base.height / base.width;
} else {
opts.width = width;
}
svg.setAttributeNS(null, "width", base.width);
}
*/
base.changeHeight = function(height) {
base.height = height;
if (opts.aspect) {
opts.aspect = base.height / base.width;
} else {
opts.height = height;
}
svg.setAttributeNS(null, "height", base.height);
}
base.triggerResize = function() {
resize();
}
base.svg = svg;
return base;
}