aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2020-04-29 16:25:47 +1000
committerUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2020-04-29 16:25:47 +1000
commit747f8e810eea3bb4836b50f411a7de6f5d67cbfa (patch)
tree57938e929cca45f5bf6db0f928d33dd3af94ed49 /src
parent12f87d58bf7401af11c9d67789f2ffeda52f0372 (diff)
downloadsvg.js-747f8e810eea3bb4836b50f411a7de6f5d67cbfa.tar.gz
svg.js-747f8e810eea3bb4836b50f411a7de6f5d67cbfa.zip
finished tests for Point, PointArray, PathArray and SVGArray
Diffstat (limited to 'src')
-rw-r--r--src/types/PathArray.js74
-rw-r--r--src/types/PointArray.js52
2 files changed, 24 insertions, 102 deletions
diff --git a/src/types/PathArray.js b/src/types/PathArray.js
index f124b1b..03fdee3 100644
--- a/src/types/PathArray.js
+++ b/src/types/PathArray.js
@@ -11,6 +11,7 @@ import { subClassArray } from './ArrayPolyfill.js'
import Point from './Point.js'
import SVGArray from './SVGArray.js'
import parser from '../modules/core/parser.js'
+import Box from './Box.js'
const PathArray = subClassArray('PathArray', SVGArray)
@@ -222,72 +223,8 @@ extend(PathArray, {
return this
},
- // Test if the passed path array use the same path data commands as this path array
- equalCommands (pathArray) {
- var i, il, equalCommands
-
- pathArray = new PathArray(pathArray)
-
- equalCommands = this.length === pathArray.length
- for (i = 0, il = this.length; equalCommands && i < il; i++) {
- equalCommands = this[i][0] === pathArray[i][0]
- }
-
- return equalCommands
- },
-
- // Make path array morphable
- morph (pathArray) {
- pathArray = new PathArray(pathArray)
-
- if (this.equalCommands(pathArray)) {
- this.destination = pathArray
- } else {
- this.destination = null
- }
-
- return this
- },
-
- // Get morphed path array at given position
- at (pos) {
- // make sure a destination is defined
- if (!this.destination) return this
-
- var sourceArray = this
- var destinationArray = this.destination.value
- var array = []
- var pathArray = new PathArray()
- var i, il, j, jl
-
- // Animate has specified in the SVG spec
- // See: https://www.w3.org/TR/SVG11/paths.html#PathElement
- for (i = 0, il = sourceArray.length; i < il; i++) {
- array[i] = [ sourceArray[i][0] ]
- for (j = 1, jl = sourceArray[i].length; j < jl; j++) {
- array[i][j] = sourceArray[i][j] + (destinationArray[i][j] - sourceArray[i][j]) * pos
- }
- // For the two flags of the elliptical arc command, the SVG spec say:
- // Flags and booleans are interpolated as fractions between zero and one, with any non-zero value considered to be a value of one/true
- // Elliptical arc command as an array followed by corresponding indexes:
- // ['A', rx, ry, x-axis-rotation, large-arc-flag, sweep-flag, x, y]
- // 0 1 2 3 4 5 6 7
- if (array[i][0] === 'A') {
- array[i][4] = +(array[i][4] !== 0)
- array[i][5] = +(array[i][5] !== 0)
- }
- }
-
- // Directly modify the value of a path array, this is done this way for performance
- pathArray.value = array
- return pathArray
- },
-
// Absolutize and parse path to array
- parse (array = [ [ 'M', 0, 0 ] ]) {
- // if it's already a patharray, no need to parse it
- if (array instanceof PathArray) return array
-
+ parse (array = [ 'M', 0, 0 ]) {
// prepare for parsing
var s
var paramCnt = { M: 2, L: 2, H: 1, V: 1, C: 6, S: 4, Q: 4, T: 2, A: 7, Z: 0 }
@@ -300,9 +237,8 @@ extend(PathArray, {
.trim() // trim
.split(delimiter) // split into array
} else {
- array = array.reduce(function (prev, curr) {
- return [].concat.call(prev, curr)
- }, [])
+ // Flatten array
+ array = Array.prototype.concat.apply([], array)
}
// array now is an array containing all parts of a path e.g. ['M', '0', '0', 'L', '30', '30' ...]
@@ -337,6 +273,6 @@ extend(PathArray, {
// Get bounding box of path
bbox () {
parser().path.setAttribute('d', this.toString())
- return parser.nodes.path.getBBox()
+ return new Box(parser.nodes.path.getBBox())
}
})
diff --git a/src/types/PointArray.js b/src/types/PointArray.js
index 6a869d7..54df08f 100644
--- a/src/types/PointArray.js
+++ b/src/types/PointArray.js
@@ -2,6 +2,8 @@ import { delimiter } from '../modules/core/regex.js'
import { extend } from '../utils/adopter.js'
import { subClassArray } from './ArrayPolyfill.js'
import SVGArray from './SVGArray.js'
+import { Matrix } from '../main.js'
+import Box from './Box.js'
const PointArray = subClassArray('PointArray', SVGArray)
@@ -28,32 +30,13 @@ extend(PointArray, {
}
},
- // Get morphed array at given position
- at (pos) {
- // make sure a destination is defined
- if (!this.destination) return this
-
- // generate morphed point string
- for (var i = 0, il = this.length, array = []; i < il; i++) {
- array.push([
- this[i][0] + (this.destination[i][0] - this[i][0]) * pos,
- this[i][1] + (this.destination[i][1] - this[i][1]) * pos
- ])
- }
-
- return new PointArray(array)
- },
-
// Parse point string and flat array
- parse (array = [ [ 0, 0 ] ]) {
+ parse (array = [ 0, 0 ]) {
var points = []
- // if it is an array
+ // if it is an array, we flatten it and therefore clone it to 1 depths
if (array instanceof Array) {
- // and it is not flat, there is no need to parse it
- if (array[0] instanceof Array) {
- return array
- }
+ array = Array.prototype.concat.apply([], array)
} else { // Else, it is considered as a string
// parse points
array = array.trim().split(delimiter).map(parseFloat)
@@ -71,21 +54,24 @@ extend(PointArray, {
return points
},
- // transform points with matrix (similar to Point.transform)
transform (m) {
- const points = []
+ return this.clone().transformO(m)
+ },
+
+ // transform points with matrix (similar to Point.transform)
+ transformO (m) {
+ if (!Matrix.isMatrixLike(m)) {
+ m = new Matrix(m)
+ }
- for (let i = 0; i < this.length; i++) {
- const point = this[i]
+ for (let i = this.length; i--;) {
// Perform the matrix multiplication
- points.push([
- m.a * point[0] + m.c * point[1] + m.e,
- m.b * point[0] + m.d * point[1] + m.f
- ])
+ const [ x, y ] = this[i]
+ this[i][0] = m.a * x + m.c * y + m.e
+ this[i][1] = m.b * x + m.d * y + m.f
}
- // Return the required point
- return new PointArray(points)
+ return this
},
// Move point string
@@ -132,6 +118,6 @@ extend(PointArray, {
minX = Math.min(el[0], minX)
minY = Math.min(el[1], minY)
})
- return { x: minX, y: minY, width: maxX - minX, height: maxY - minY }
+ return new Box(minX, minY, maxX - minX, maxY - minY)
}
})