|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// precision for consider cubic polynom as quadratic one |
| 4 | +var epsilon = 0.00000001; |
| 5 | + |
| 6 | +// New box : empty or parsed from string like '-10 10 300 400' |
| 7 | +// |
| 8 | +function Box(s) { |
| 9 | + if (!(this instanceof Box)) { return new Box(s); } |
| 10 | + |
| 11 | + // minX, minY, maxX, maxY : are not defined yet |
| 12 | + // but empty box has 0 x 0 size |
| 13 | + this.width = this.height = 0; |
| 14 | + |
| 15 | + // parse the string parameter |
| 16 | + if (s && s.constructor === String) { |
| 17 | + var a = s.trim().split(/\s+/).map(parseFloat); |
| 18 | + |
| 19 | + this.addX(a[0]).addX(a[0] + a[2]).addY(a[1]).addY(a[1] + a[3]); |
| 20 | + } |
| 21 | + |
| 22 | + return this; |
| 23 | +} |
| 24 | + |
| 25 | +// check if box is not defined yet |
| 26 | +// |
| 27 | +Box.prototype.isUndefined = function () { |
| 28 | + return (typeof this.minX === 'undefined') || (typeof this.minY === 'undefined'); |
| 29 | +}; |
| 30 | + |
| 31 | +// add new X coordinate |
| 32 | +// |
| 33 | +Box.prototype.addX = function (x) { |
| 34 | + if (typeof this.minX === 'undefined') { |
| 35 | + this.minX = this.maxX = x; |
| 36 | + this.width = 0; |
| 37 | + } else { |
| 38 | + this.minX = Math.min(this.minX, x); |
| 39 | + this.maxX = Math.max(this.maxX, x); |
| 40 | + this.width = this.maxX - this.minX; |
| 41 | + } |
| 42 | + |
| 43 | + return this; |
| 44 | +}; |
| 45 | + |
| 46 | +// add new Y coordinate |
| 47 | +// |
| 48 | +Box.prototype.addY = function (y) { |
| 49 | + if (typeof this.minY === 'undefined') { |
| 50 | + this.minY = this.maxY = y; |
| 51 | + this.height = 0; |
| 52 | + } else { |
| 53 | + this.minY = Math.min(this.minY, y); |
| 54 | + this.maxY = Math.max(this.maxY, y); |
| 55 | + this.height = this.maxY - this.minY; |
| 56 | + } |
| 57 | + |
| 58 | + return this; |
| 59 | +}; |
| 60 | + |
| 61 | +// add new point |
| 62 | +// |
| 63 | +Box.prototype.addPoint = function (x, y) { |
| 64 | + return this.addX(x).addY(y); |
| 65 | +}; |
| 66 | + |
| 67 | + |
| 68 | +// ------------------------------ |
| 69 | +// return [min,max] |
| 70 | +// of A[0] * (1-t) * (1-t) + A[1] * 2 * (1-t) * t + A[2] * t * t |
| 71 | +// for t in [0,1] |
| 72 | +// ------------------------------ |
| 73 | +function minmaxQ(A) { |
| 74 | + var min = Math.min(A[0], A[2]), |
| 75 | + max = Math.max(A[0], A[2]); |
| 76 | + |
| 77 | + if (A[1] > A[0] ? A[2] >= A[1] : A[2] <= A[1]) { |
| 78 | + // if no extremum in ]0,1[ |
| 79 | + return [ min, max ]; |
| 80 | + } |
| 81 | + |
| 82 | + // check if the extremum E is min or max |
| 83 | + var E = (A[0] * A[2] - A[1] * A[1]) / (A[0] - 2 * A[1] + A[2]); |
| 84 | + return E < min ? [ E, max ] : [ min, E ]; |
| 85 | +} |
| 86 | + |
| 87 | +// add new quadratic curve to X coordinate |
| 88 | +// |
| 89 | +Box.prototype.addXQ = function (A) { |
| 90 | + var minmax = minmaxQ(A); |
| 91 | + |
| 92 | + return this.addX(minmax[0]).addX(minmax[1]); |
| 93 | +}; |
| 94 | + |
| 95 | +// add new quadratic curve to Y coordinate |
| 96 | +// |
| 97 | +Box.prototype.addYQ = function (A) { |
| 98 | + var minmax = minmaxQ(A); |
| 99 | + |
| 100 | + return this.addY(minmax[0]).addY(minmax[1]); |
| 101 | +}; |
| 102 | + |
| 103 | + |
| 104 | +// ------------------------------ |
| 105 | +// return [min,max] |
| 106 | +// of A[0] * (1-t) * (1-t) * (1-t) + A[1] * 3 * (1-t) * (1-t) * t + A[2] * 3 * (1-t) * t * t + A[3] * t * t * t |
| 107 | +// for t in [0,1] |
| 108 | +// ------------------------------ |
| 109 | +function minmaxC(A) { |
| 110 | + // if the polynomial is (almost) quadratic and not cubic |
| 111 | + var K = A[0] - 3 * A[1] + 3 * A[2] - A[3]; |
| 112 | + if (Math.abs(K) < epsilon) { |
| 113 | + return minmaxQ([ A[0], -0.5 * A[0] + 1.5 * A[1], A[0] - 3 * A[1] + 3 * A[2] ]); |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + // the reduced discriminant of the derivative |
| 118 | + var T = -A[0] * A[2] + A[0] * A[3] - A[1] * A[2] - A[1] * A[3] + A[1] * A[1] + A[2] * A[2]; |
| 119 | + |
| 120 | + // if the polynomial is monotone in [0,1] |
| 121 | + if (T <= 0) { |
| 122 | + return [ Math.min(A[0], A[3]), Math.max(A[0], A[3]) ]; |
| 123 | + } |
| 124 | + var S = Math.sqrt(T); |
| 125 | + |
| 126 | + // potential extrema |
| 127 | + var max = Math.max(A[0], A[3]), |
| 128 | + min = Math.min(A[0], A[3]); |
| 129 | + |
| 130 | + var L = A[0] - 2 * A[1] + A[2]; |
| 131 | + // check local extrema |
| 132 | + for (var R = (L + S) / K, i = 1; i <= 2; R = (L - S) / K, i++) { |
| 133 | + if (R > 0 && R < 1) { |
| 134 | + // if the extrema is for R in [0,1] |
| 135 | + var Q = A[0] * (1 - R) * (1 - R) * (1 - R) + |
| 136 | + A[1] * 3 * (1 - R) * (1 - R) * R + |
| 137 | + A[2] * 3 * (1 - R) * R * R + |
| 138 | + A[3] * R * R * R; |
| 139 | + if (Q < min) { min = Q; } |
| 140 | + if (Q > max) { max = Q; } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return [ min, max ]; |
| 145 | +} |
| 146 | + |
| 147 | +// add new cubic curve to X coordinate |
| 148 | +// |
| 149 | +Box.prototype.addXC = function (A) { |
| 150 | + var minmax = minmaxC(A); |
| 151 | + |
| 152 | + return this.addX(minmax[0]).addX(minmax[1]); |
| 153 | +}; |
| 154 | + |
| 155 | +// add new cubic curve to Y coordinate |
| 156 | +// |
| 157 | +Box.prototype.addYC = function (A) { |
| 158 | + var minmax = minmaxC(A); |
| 159 | + |
| 160 | + return this.addY(minmax[0]).addY(minmax[1]); |
| 161 | +}; |
| 162 | + |
| 163 | +// return a string like '-10 10 300 400' |
| 164 | +// |
| 165 | +Box.prototype.toViewBoxString = function (pr) { |
| 166 | + // if empty box |
| 167 | + if (this.isUndefined()) { |
| 168 | + return '0 0 0 0'; |
| 169 | + } |
| 170 | + |
| 171 | + // else |
| 172 | + return ((typeof pr === 'undefined') ? |
| 173 | + [ this.minX, this.minY, this.width, this.height ] |
| 174 | + : |
| 175 | + [ |
| 176 | + this.minX.toFixed(pr), this.minY.toFixed(pr), |
| 177 | + this.width.toFixed(pr), this.height.toFixed(pr) |
| 178 | + ] |
| 179 | + ).join(' '); |
| 180 | +}; |
| 181 | + |
| 182 | +// return the transform that translate and scale to fit in a box |
| 183 | +// controlled by the following parameters : |
| 184 | +// - type: |
| 185 | +// - fit(=none) : scale the box (aspect ratio is not preserved) to fit in the box |
| 186 | +// - meet (the default) : scale the box (aspect ratio is preserved) as much as possible |
| 187 | +// to cover the destination box |
| 188 | +// - slice : scale the box (aspect ratio is preserved) as less as possible to cover the destination box |
| 189 | +// - move : translate only (no scale) the box according to x???y??? parameter |
| 190 | +// - position x(Min|Mid|Max)Y(Min|Mid|Max). |
| 191 | +// example : matrixToBox(src, '100 0 200 300 meet xMidYMin') |
| 192 | +// |
| 193 | +Box.prototype.matrixToBox = function (parameters) { |
| 194 | + var dst = new Box(parameters.match(/(-|\d|\.|\s)+/)[0]); |
| 195 | + |
| 196 | + // get the action (default is 'meet') |
| 197 | + var action = ((parameters + 'meet').match(/(fit|none|meet|slice|move)/))[0]; |
| 198 | + |
| 199 | + if (action === 'none') { // for compatibility with 'preserveAspectRatio' |
| 200 | + action = 'fit'; |
| 201 | + } |
| 202 | + |
| 203 | + // set the scale factors based on the action |
| 204 | + var rx, ry; |
| 205 | + switch (action) { |
| 206 | + case 'fit': |
| 207 | + rx = this.width ? dst.width / this.width : 1; |
| 208 | + ry = this.height ? dst.height / this.height : 1; |
| 209 | + break; |
| 210 | + case 'slice' : |
| 211 | + if (this.width !== 0 && this.height !== 0) { |
| 212 | + rx = ry = Math.max(dst.width / this.width, dst.height / this.height); |
| 213 | + break; |
| 214 | + } |
| 215 | + // else falls through |
| 216 | + case 'meet' : |
| 217 | + rx = ry = (this.width === 0 && this.height === 0) ? 1 : |
| 218 | + Math.min(dst.width / this.width, dst.height / this.height); |
| 219 | + break; |
| 220 | + case 'move': |
| 221 | + rx = ry = 1; |
| 222 | + break; |
| 223 | + } |
| 224 | + |
| 225 | + // get the position from string like 'xMidYMax' |
| 226 | + var position = {}; |
| 227 | + position.X = ((parameters + 'xMid').match(/x(Min|Mid|Max)/i))[1].toLowerCase(); |
| 228 | + position.Y = ((parameters + 'YMid').match(/Y(Min|Mid|Max)/i))[1].toLowerCase(); |
| 229 | + |
| 230 | + // variable that helps to loop over the two boxes |
| 231 | + var origin = {}, |
| 232 | + box = {}; |
| 233 | + box.src = this; |
| 234 | + box.dst = dst; |
| 235 | + |
| 236 | + // set the 'origin' of the two boxes based on the position parameters |
| 237 | + for (var c = 'X', i = 1; i <= 2; c = 'Y', i++) { |
| 238 | + for (var b = 'src', j = 1; j <= 2; b = 'dst', j++) { |
| 239 | + switch (position[c]) { |
| 240 | + case 'min': |
| 241 | + origin[b + c] = box[b]['min' + c]; |
| 242 | + break; |
| 243 | + case 'mid': |
| 244 | + origin[b + c] = (box[b]['min' + c] + box[b]['max' + c]) / 2; |
| 245 | + break; |
| 246 | + case 'max': |
| 247 | + origin[b + c] = box[b]['max' + c]; |
| 248 | + break; |
| 249 | + } |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + // return the matrix that is equivalent to |
| 254 | + // .translate(-box.src.originX,-box.src.originY) |
| 255 | + // .scale(rx,ry) |
| 256 | + // .translate(box.dst.originX,box.dst.originY); |
| 257 | + return [ rx, 0, 0, ry, origin.dstX - rx * origin.srcX, origin.dstY - ry * origin.srcY ]; |
| 258 | +}; |
| 259 | + |
| 260 | +module.exports = Box; |
0 commit comments