summaryrefslogtreecommitdiffstats
path: root/src/types
diff options
context:
space:
mode:
authorUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2021-01-13 17:26:33 +0100
committerUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2021-01-13 17:26:33 +0100
commit4524c20b48abe08e69c25a7382a289592d147496 (patch)
tree3e475fe673d5acd800180282e84df03d396106f3 /src/types
parent4f4dc2afa62cb899a5cae72c359150f8adf27edb (diff)
downloadsvg.js-4524c20b48abe08e69c25a7382a289592d147496.tar.gz
svg.js-4524c20b48abe08e69c25a7382a289592d147496.zip
update dependencies, apply new linter fixes
Diffstat (limited to 'src/types')
-rw-r--r--src/types/Box.js19
-rw-r--r--src/types/Color.js62
-rw-r--r--src/types/Matrix.js202
-rw-r--r--src/types/PathArray.js9
-rw-r--r--src/types/Point.js8
-rw-r--r--src/types/PointArray.js23
-rw-r--r--src/types/SVGNumber.js8
7 files changed, 190 insertions, 141 deletions
diff --git a/src/types/Box.js b/src/types/Box.js
index 71d1e2a..f8976d4 100644
--- a/src/types/Box.js
+++ b/src/types/Box.js
@@ -34,13 +34,18 @@ export default class Box {
}
init (source) {
- var base = [ 0, 0, 0, 0 ]
- source = typeof source === 'string' ? source.split(delimiter).map(parseFloat)
- : Array.isArray(source) ? source
- : typeof source === 'object' ? [ source.left != null ? source.left
- : source.x, source.top != null ? source.top : source.y, source.width, source.height ]
- : arguments.length === 4 ? [].slice.call(arguments)
- : base
+ const base = [ 0, 0, 0, 0 ]
+ source = typeof source === 'string'
+ ? source.split(delimiter).map(parseFloat)
+ : Array.isArray(source)
+ ? source
+ : typeof source === 'object'
+ ? [ source.left != null
+ ? source.left
+ : source.x, source.top != null ? source.top : source.y, source.width, source.height ]
+ : arguments.length === 4
+ ? [].slice.call(arguments)
+ : base
this.x = source[0] || 0
this.y = source[1] || 0
diff --git a/src/types/Color.js b/src/types/Color.js
index d5f0efa..c050ecc 100644
--- a/src/types/Color.js
+++ b/src/types/Color.js
@@ -28,13 +28,19 @@ function is (object, space) {
}
function getParameters (a, b) {
- const params = is(a, 'rgb') ? { _a: a.r, _b: a.g, _c: a.b, _d: 0, space: 'rgb' }
- : is(a, 'xyz') ? { _a: a.x, _b: a.y, _c: a.z, _d: 0, space: 'xyz' }
- : is(a, 'hsl') ? { _a: a.h, _b: a.s, _c: a.l, _d: 0, space: 'hsl' }
- : is(a, 'lab') ? { _a: a.l, _b: a.a, _c: a.b, _d: 0, space: 'lab' }
- : is(a, 'lch') ? { _a: a.l, _b: a.c, _c: a.h, _d: 0, space: 'lch' }
- : is(a, 'cmyk') ? { _a: a.c, _b: a.m, _c: a.y, _d: a.k, space: 'cmyk' }
- : { _a: 0, _b: 0, _c: 0, space: 'rgb' }
+ const params = is(a, 'rgb')
+ ? { _a: a.r, _b: a.g, _c: a.b, _d: 0, space: 'rgb' }
+ : is(a, 'xyz')
+ ? { _a: a.x, _b: a.y, _c: a.z, _d: 0, space: 'xyz' }
+ : is(a, 'hsl')
+ ? { _a: a.h, _b: a.s, _c: a.l, _d: 0, space: 'hsl' }
+ : is(a, 'lab')
+ ? { _a: a.l, _b: a.a, _c: a.b, _d: 0, space: 'lab' }
+ : is(a, 'lch')
+ ? { _a: a.l, _b: a.c, _c: a.h, _d: 0, space: 'lch' }
+ : is(a, 'cmyk')
+ ? { _a: a.c, _b: a.m, _c: a.y, _d: a.k, space: 'cmyk' }
+ : { _a: 0, _b: 0, _c: 0, space: 'rgb' }
params.space = b || params.space
return params
@@ -194,14 +200,20 @@ export default class Color {
// Calculate the hue and saturation
const delta = max - min
- const s = isGrey ? 0
- : l > 0.5 ? delta / (2 - max - min)
- : delta / (max + min)
- const h = isGrey ? 0
- : max === r ? ((g - b) / delta + (g < b ? 6 : 0)) / 6
- : max === g ? ((b - r) / delta + 2) / 6
- : max === b ? ((r - g) / delta + 4) / 6
- : 0
+ const s = isGrey
+ ? 0
+ : l > 0.5
+ ? delta / (2 - max - min)
+ : delta / (max + min)
+ const h = isGrey
+ ? 0
+ : max === r
+ ? ((g - b) / delta + (g < b ? 6 : 0)) / 6
+ : max === g
+ ? ((b - r) / delta + 2) / 6
+ : max === b
+ ? ((r - g) / delta + 4) / 6
+ : 0
// Construct and return the new color
const color = new Color(360 * h, 100 * s, 100 * l, 'hsl')
@@ -249,13 +261,19 @@ export default class Color {
// Now add the components as a convenience
const { _a, _b, _c, _d } = this
- const components = this.space === 'rgb' ? { r: _a, g: _b, b: _c }
- : this.space === 'xyz' ? { x: _a, y: _b, z: _c }
- : this.space === 'hsl' ? { h: _a, s: _b, l: _c }
- : this.space === 'lab' ? { l: _a, a: _b, b: _c }
- : this.space === 'lch' ? { l: _a, c: _b, h: _c }
- : this.space === 'cmyk' ? { c: _a, m: _b, y: _c, k: _d }
- : {}
+ const components = this.space === 'rgb'
+ ? { r: _a, g: _b, b: _c }
+ : this.space === 'xyz'
+ ? { x: _a, y: _b, z: _c }
+ : this.space === 'hsl'
+ ? { h: _a, s: _b, l: _c }
+ : this.space === 'lab'
+ ? { l: _a, a: _b, b: _c }
+ : this.space === 'lch'
+ ? { l: _a, c: _b, h: _c }
+ : this.space === 'cmyk'
+ ? { c: _a, m: _b, y: _c, k: _d }
+ : {}
Object.assign(this, components)
}
diff --git a/src/types/Matrix.js b/src/types/Matrix.js
index d4f516c..406cb84 100644
--- a/src/types/Matrix.js
+++ b/src/types/Matrix.js
@@ -15,40 +15,52 @@ export default class Matrix {
static formatTransforms (o) {
// Get all of the parameters required to form the matrix
- var flipBoth = o.flip === 'both' || o.flip === true
- var flipX = o.flip && (flipBoth || o.flip === 'x') ? -1 : 1
- var flipY = o.flip && (flipBoth || o.flip === 'y') ? -1 : 1
- var skewX = o.skew && o.skew.length ? o.skew[0]
- : isFinite(o.skew) ? o.skew
- : isFinite(o.skewX) ? o.skewX
- : 0
- var skewY = o.skew && o.skew.length ? o.skew[1]
- : isFinite(o.skew) ? o.skew
- : isFinite(o.skewY) ? o.skewY
- : 0
- var scaleX = o.scale && o.scale.length ? o.scale[0] * flipX
- : isFinite(o.scale) ? o.scale * flipX
- : isFinite(o.scaleX) ? o.scaleX * flipX
- : flipX
- var scaleY = o.scale && o.scale.length ? o.scale[1] * flipY
- : isFinite(o.scale) ? o.scale * flipY
- : isFinite(o.scaleY) ? o.scaleY * flipY
- : flipY
- var shear = o.shear || 0
- var theta = o.rotate || o.theta || 0
- var origin = new Point(o.origin || o.around || o.ox || o.originX, o.oy || o.originY)
- var ox = origin.x
- var oy = origin.y
+ const flipBoth = o.flip === 'both' || o.flip === true
+ const flipX = o.flip && (flipBoth || o.flip === 'x') ? -1 : 1
+ const flipY = o.flip && (flipBoth || o.flip === 'y') ? -1 : 1
+ const skewX = o.skew && o.skew.length
+ ? o.skew[0]
+ : isFinite(o.skew)
+ ? o.skew
+ : isFinite(o.skewX)
+ ? o.skewX
+ : 0
+ const skewY = o.skew && o.skew.length
+ ? o.skew[1]
+ : isFinite(o.skew)
+ ? o.skew
+ : isFinite(o.skewY)
+ ? o.skewY
+ : 0
+ const scaleX = o.scale && o.scale.length
+ ? o.scale[0] * flipX
+ : isFinite(o.scale)
+ ? o.scale * flipX
+ : isFinite(o.scaleX)
+ ? o.scaleX * flipX
+ : flipX
+ const scaleY = o.scale && o.scale.length
+ ? o.scale[1] * flipY
+ : isFinite(o.scale)
+ ? o.scale * flipY
+ : isFinite(o.scaleY)
+ ? o.scaleY * flipY
+ : flipY
+ const shear = o.shear || 0
+ const theta = o.rotate || o.theta || 0
+ const origin = new Point(o.origin || o.around || o.ox || o.originX, o.oy || o.originY)
+ const ox = origin.x
+ const oy = origin.y
// We need Point to be invalid if nothing was passed because we cannot default to 0 here. Thats why NaN
- var position = new Point(o.position || o.px || o.positionX || NaN, o.py || o.positionY || NaN)
- var px = position.x
- var py = position.y
- var translate = new Point(o.translate || o.tx || o.translateX, o.ty || o.translateY)
- var tx = translate.x
- var ty = translate.y
- var relative = new Point(o.relative || o.rx || o.relativeX, o.ry || o.relativeY)
- var rx = relative.x
- var ry = relative.y
+ const position = new Point(o.position || o.px || o.positionX || NaN, o.py || o.positionY || NaN)
+ const px = position.x
+ const py = position.y
+ const translate = new Point(o.translate || o.tx || o.translateX, o.ty || o.translateY)
+ const tx = translate.x
+ const ty = translate.y
+ const relative = new Point(o.relative || o.rx || o.relativeX, o.ry || o.relativeY)
+ const rx = relative.x
+ const ry = relative.y
// Populate all of the values
return {
@@ -74,12 +86,12 @@ export default class Matrix {
// left matrix, right matrix, target matrix which is overwritten
static matrixMultiply (l, r, o) {
// Work out the product directly
- var a = l.a * r.a + l.c * r.b
- var b = l.b * r.a + l.d * r.b
- var c = l.a * r.c + l.c * r.d
- var d = l.b * r.c + l.d * r.d
- var e = l.e + l.a * r.e + l.c * r.f
- var f = l.f + l.b * r.e + l.d * r.f
+ const a = l.a * r.a + l.c * r.b
+ const b = l.b * r.a + l.d * r.b
+ const c = l.a * r.c + l.c * r.d
+ const d = l.b * r.c + l.d * r.d
+ const e = l.e + l.a * r.e + l.c * r.f
+ const f = l.f + l.b * r.e + l.d * r.f
// make sure to use local variables because l/r and o could be the same
o.a = a
@@ -98,8 +110,8 @@ export default class Matrix {
// Transform around a center point
aroundO (cx, cy, matrix) {
- var dx = cx || 0
- var dy = cy || 0
+ const dx = cx || 0
+ const dy = cy || 0
return this.translateO(-dx, -dy).lmultiplyO(matrix).translateO(dx, dy)
}
@@ -111,29 +123,29 @@ export default class Matrix {
// Decomposes this matrix into its affine parameters
decompose (cx = 0, cy = 0) {
// Get the parameters from the matrix
- var a = this.a
- var b = this.b
- var c = this.c
- var d = this.d
- var e = this.e
- var f = this.f
+ const a = this.a
+ const b = this.b
+ const c = this.c
+ const d = this.d
+ const e = this.e
+ const f = this.f
// Figure out if the winding direction is clockwise or counterclockwise
- var determinant = a * d - b * c
- var ccw = determinant > 0 ? 1 : -1
+ const determinant = a * d - b * c
+ const ccw = determinant > 0 ? 1 : -1
// Since we only shear in x, we can use the x basis to get the x scale
// and the rotation of the resulting matrix
- var sx = ccw * Math.sqrt(a * a + b * b)
- var thetaRad = Math.atan2(ccw * b, ccw * a)
- var theta = 180 / Math.PI * thetaRad
- var ct = Math.cos(thetaRad)
- var st = Math.sin(thetaRad)
+ const sx = ccw * Math.sqrt(a * a + b * b)
+ const thetaRad = Math.atan2(ccw * b, ccw * a)
+ const theta = 180 / Math.PI * thetaRad
+ const ct = Math.cos(thetaRad)
+ const st = Math.sin(thetaRad)
// We can then solve the y basis vector simultaneously to get the other
// two affine parameters directly from these parameters
- var lam = (a * c + b * d) / determinant
- var sy = ((c * sx) / (lam * a - b)) || ((d * sx) / (lam * b + a))
+ const lam = (a * c + b * d) / determinant
+ const sy = ((c * sx) / (lam * a - b)) || ((d * sx) / (lam * b + a))
// Use the translations
const tx = e - cx + cx * ct * sx + cy * (lam * ct * sx - st * sy)
@@ -164,7 +176,7 @@ export default class Matrix {
// Check if two matrices are equal
equals (other) {
if (other === this) return true
- var comp = new Matrix(other)
+ const comp = new Matrix(other)
return closeEnough(this.a, comp.a) && closeEnough(this.b, comp.b)
&& closeEnough(this.c, comp.c) && closeEnough(this.d, comp.d)
&& closeEnough(this.e, comp.e) && closeEnough(this.f, comp.f)
@@ -176,23 +188,31 @@ export default class Matrix {
}
flipO (axis, around) {
- return axis === 'x' ? this.scaleO(-1, 1, around, 0)
- : axis === 'y' ? this.scaleO(1, -1, 0, around)
- : this.scaleO(-1, -1, axis, around || axis) // Define an x, y flip point
+ return axis === 'x'
+ ? this.scaleO(-1, 1, around, 0)
+ : axis === 'y'
+ ? this.scaleO(1, -1, 0, around)
+ : this.scaleO(-1, -1, axis, around || axis) // Define an x, y flip point
}
// Initialize
init (source) {
- var base = Matrix.fromArray([ 1, 0, 0, 1, 0, 0 ])
+ const base = Matrix.fromArray([ 1, 0, 0, 1, 0, 0 ])
// ensure source as object
- source = source instanceof Element ? source.matrixify()
- : typeof source === 'string' ? Matrix.fromArray(source.split(delimiter).map(parseFloat))
- : Array.isArray(source) ? Matrix.fromArray(source)
- : (typeof source === 'object' && Matrix.isMatrixLike(source)) ? source
- : (typeof source === 'object') ? new Matrix().transform(source)
- : arguments.length === 6 ? Matrix.fromArray([].slice.call(arguments))
- : base
+ source = source instanceof Element
+ ? source.matrixify()
+ : typeof source === 'string'
+ ? Matrix.fromArray(source.split(delimiter).map(parseFloat))
+ : Array.isArray(source)
+ ? Matrix.fromArray(source)
+ : (typeof source === 'object' && Matrix.isMatrixLike(source))
+ ? source
+ : (typeof source === 'object')
+ ? new Matrix().transform(source)
+ : arguments.length === 6
+ ? Matrix.fromArray([].slice.call(arguments))
+ : base
// Merge the source matrix with the base matrix
this.a = source.a != null ? source.a : base.a
@@ -212,26 +232,26 @@ export default class Matrix {
// Inverses matrix
inverseO () {
// Get the current parameters out of the matrix
- var a = this.a
- var b = this.b
- var c = this.c
- var d = this.d
- var e = this.e
- var f = this.f
+ const a = this.a
+ const b = this.b
+ const c = this.c
+ const d = this.d
+ const e = this.e
+ const f = this.f
// Invert the 2x2 matrix in the top left
- var det = a * d - b * c
+ const det = a * d - b * c
if (!det) throw new Error('Cannot invert ' + this)
// Calculate the top 2x2 matrix
- var na = d / det
- var nb = -b / det
- var nc = -c / det
- var nd = a / det
+ const na = d / det
+ const nb = -b / det
+ const nc = -c / det
+ const nd = a / det
// Apply the inverted matrix to the top right
- var ne = -(na * e + nc * f)
- var nf = -(nb * e + nd * f)
+ const ne = -(na * e + nc * f)
+ const nf = -(nb * e + nd * f)
// Construct the inverted matrix
this.a = na
@@ -249,8 +269,8 @@ export default class Matrix {
}
lmultiplyO (matrix) {
- var r = this
- var l = matrix instanceof Matrix
+ const r = this
+ const l = matrix instanceof Matrix
? matrix
: new Matrix(matrix)
@@ -264,8 +284,8 @@ export default class Matrix {
multiplyO (matrix) {
// Get the matrices
- var l = this
- var r = matrix instanceof Matrix
+ const l = this
+ const r = matrix instanceof Matrix
? matrix
: new Matrix(matrix)
@@ -391,17 +411,17 @@ export default class Matrix {
transform (o) {
// Check if o is a matrix and then left multiply it directly
if (Matrix.isMatrixLike(o)) {
- var matrix = new Matrix(o)
+ const matrix = new Matrix(o)
return matrix.multiplyO(this)
}
// Get the proposed transformations and the current transformations
- var t = Matrix.formatTransforms(o)
- var current = this
+ const t = Matrix.formatTransforms(o)
+ const current = this
const { x: ox, y: oy } = new Point(t.ox, t.oy).transform(current)
// Construct the resulting matrix
- var transformer = new Matrix()
+ const transformer = new Matrix()
.translateO(t.rx, t.ry)
.lmultiplyO(current)
.translateO(-ox, -oy)
@@ -460,8 +480,8 @@ export function screenCTM () {
for the inner coordinate system when getScreenCTM() is called on nested svgs.
However all other Browsers do that */
if (typeof this.isRoot === 'function' && !this.isRoot()) {
- var rect = this.rect(1, 1)
- var m = rect.node.getScreenCTM()
+ const rect = this.rect(1, 1)
+ const m = rect.node.getScreenCTM()
rect.remove()
return new Matrix(m)
}
diff --git a/src/types/PathArray.js b/src/types/PathArray.js
index e021238..10e7b07 100644
--- a/src/types/PathArray.js
+++ b/src/types/PathArray.js
@@ -4,7 +4,8 @@ import Box from './Box.js'
import { pathParser } from '../utils/pathParser.js'
function arrayToString (a) {
- for (var i = 0, il = a.length, s = ''; i < il; i++) {
+ let s = ''
+ for (let i = 0, il = a.length; i < il; i++) {
s += a[i][0]
if (a[i][1] != null) {
@@ -49,7 +50,7 @@ export default class PathArray extends SVGArray {
// Move path string
move (x, y) {
// get bounding box of current situation
- var box = this.bbox()
+ const box = this.bbox()
// get relative offset
x -= box.x
@@ -99,8 +100,8 @@ export default class PathArray extends SVGArray {
// Resize path string
size (width, height) {
// get bounding box of current situation
- var box = this.bbox()
- var i, l
+ const box = this.bbox()
+ let i, l
// If the box width or height is 0 then we ignore
// transformations on the respective axis
diff --git a/src/types/Point.js b/src/types/Point.js
index cb09bf3..f028e6d 100644
--- a/src/types/Point.js
+++ b/src/types/Point.js
@@ -15,9 +15,11 @@ export default class Point {
const base = { x: 0, y: 0 }
// ensure source as object
- const source = Array.isArray(x) ? { x: x[0], y: x[1] }
- : typeof x === 'object' ? { x: x.x, y: x.y }
- : { x: x, y: y }
+ const source = Array.isArray(x)
+ ? { x: x[0], y: x[1] }
+ : typeof x === 'object'
+ ? { x: x.x, y: x.y }
+ : { x: x, y: y }
// merge source
this.x = source.x == null ? base.x : source.x
diff --git a/src/types/PointArray.js b/src/types/PointArray.js
index 28e649c..69d0cd6 100644
--- a/src/types/PointArray.js
+++ b/src/types/PointArray.js
@@ -6,10 +6,10 @@ import Matrix from './Matrix.js'
export default class PointArray extends SVGArray {
// Get bounding box of points
bbox () {
- var maxX = -Infinity
- var maxY = -Infinity
- var minX = Infinity
- var minY = Infinity
+ let maxX = -Infinity
+ let maxY = -Infinity
+ let minX = Infinity
+ let minY = Infinity
this.forEach(function (el) {
maxX = Math.max(el[0], maxX)
maxY = Math.max(el[1], maxY)
@@ -21,7 +21,7 @@ export default class PointArray extends SVGArray {
// Move point string
move (x, y) {
- var box = this.bbox()
+ const box = this.bbox()
// get relative offset
x -= box.x
@@ -29,7 +29,7 @@ export default class PointArray extends SVGArray {
// move every point
if (!isNaN(x) && !isNaN(y)) {
- for (var i = this.length - 1; i >= 0; i--) {
+ for (let i = this.length - 1; i >= 0; i--) {
this[i] = [ this[i][0] + x, this[i][1] + y ]
}
}
@@ -39,7 +39,7 @@ export default class PointArray extends SVGArray {
// Parse point string and flat array
parse (array = [ 0, 0 ]) {
- var points = []
+ const points = []
// if it is an array, we flatten it and therefore clone it to 1 depths
if (array instanceof Array) {
@@ -54,7 +54,7 @@ export default class PointArray extends SVGArray {
if (array.length % 2 !== 0) array.pop()
// wrap points in two-tuples
- for (var i = 0, len = array.length; i < len; i = i + 2) {
+ for (let i = 0, len = array.length; i < len; i = i + 2) {
points.push([ array[i], array[i + 1] ])
}
@@ -63,8 +63,8 @@ export default class PointArray extends SVGArray {
// Resize poly string
size (width, height) {
- var i
- var box = this.bbox()
+ let i
+ const box = this.bbox()
// recalculate position of all points according to new size
for (i = this.length - 1; i >= 0; i--) {
@@ -87,8 +87,9 @@ export default class PointArray extends SVGArray {
// Convert array to string
toString () {
+ const array = []
// convert to a poly point string
- for (var i = 0, il = this.length, array = []; i < il; i++) {
+ for (let i = 0, il = this.length; i < il; i++) {
array.push(this[i].join(','))
}
diff --git a/src/types/SVGNumber.js b/src/types/SVGNumber.js
index f9bee28..25c87c1 100644
--- a/src/types/SVGNumber.js
+++ b/src/types/SVGNumber.js
@@ -83,9 +83,11 @@ export default class SVGNumber {
}
toString () {
- return (this.unit === '%' ? ~~(this.value * 1e8) / 1e6
- : this.unit === 's' ? this.value / 1e3
- : this.value
+ return (this.unit === '%'
+ ? ~~(this.value * 1e8) / 1e6
+ : this.unit === 's'
+ ? this.value / 1e3
+ : this.value
) + this.unit
}