107 lines
3.2 KiB
JavaScript
107 lines
3.2 KiB
JavaScript
// Poly points array
|
|
SVG.PointArray = function(array, fallback) {
|
|
SVG.Array.call(this, array, fallback || [[0,0]])
|
|
}
|
|
|
|
// Inherit from SVG.Array
|
|
SVG.PointArray.prototype = new SVG.Array
|
|
SVG.PointArray.prototype.constructor = SVG.PointArray
|
|
|
|
SVG.extend(SVG.PointArray, {
|
|
// Convert array to string
|
|
toString: function() {
|
|
// convert to a poly point string
|
|
for (var i = 0, il = this.value.length, array = []; i < il; i++)
|
|
array.push(this.value[i].join(','))
|
|
|
|
return array.join(' ')
|
|
}
|
|
// Convert array to line object
|
|
, toLine: function() {
|
|
return {
|
|
x1: this.value[0][0]
|
|
, y1: this.value[0][1]
|
|
, x2: this.value[1][0]
|
|
, y2: this.value[1][1]
|
|
}
|
|
}
|
|
// Get morphed array at given position
|
|
, at: function(pos) {
|
|
// make sure a destination is defined
|
|
if (!this.destination) return this
|
|
|
|
// generate morphed point string
|
|
for (var i = 0, il = this.value.length, array = []; i < il; i++)
|
|
array.push([
|
|
this.value[i][0] + (this.destination[i][0] - this.value[i][0]) * pos
|
|
, this.value[i][1] + (this.destination[i][1] - this.value[i][1]) * pos
|
|
])
|
|
|
|
return new SVG.PointArray(array)
|
|
}
|
|
// Parse point string and flat array
|
|
, parse: function(array) {
|
|
var points = []
|
|
|
|
array = array.valueOf()
|
|
|
|
// if it is an array
|
|
if (Array.isArray(array)) {
|
|
// and it is not flat, there is no need to parse it
|
|
if(Array.isArray(array[0])) {
|
|
// make sure to use a clone
|
|
return array.map(function (el) { return el.slice() })
|
|
} else if (array[0].x != null){
|
|
// allow point objects to be passed
|
|
return array.map(function (el) { return [el.x, el.y] })
|
|
}
|
|
} else { // Else, it is considered as a string
|
|
// parse points
|
|
array = array.trim().split(SVG.regex.delimiter).map(parseFloat)
|
|
}
|
|
|
|
// validate points - https://svgwg.org/svg2-draft/shapes.html#DataTypePoints
|
|
// Odd number of coordinates is an error. In such cases, drop the last odd coordinate.
|
|
if (array.length % 2 !== 0) array.pop()
|
|
|
|
// wrap points in two-tuples and parse points as floats
|
|
for(var i = 0, len = array.length; i < len; i = i + 2)
|
|
points.push([ array[i], array[i+1] ])
|
|
|
|
return points
|
|
}
|
|
// Move point string
|
|
, move: function(x, y) {
|
|
var box = this.bbox()
|
|
|
|
// get relative offset
|
|
x -= box.x
|
|
y -= box.y
|
|
|
|
// move every point
|
|
if (!isNaN(x) && !isNaN(y))
|
|
for (var i = this.value.length - 1; i >= 0; i--)
|
|
this.value[i] = [this.value[i][0] + x, this.value[i][1] + y]
|
|
|
|
return this
|
|
}
|
|
// Resize poly string
|
|
, size: function(width, height) {
|
|
var i, box = this.bbox()
|
|
|
|
// recalculate position of all points according to new size
|
|
for (i = this.value.length - 1; i >= 0; i--) {
|
|
if(box.width) this.value[i][0] = ((this.value[i][0] - box.x) * width) / box.width + box.x
|
|
if(box.height) this.value[i][1] = ((this.value[i][1] - box.y) * height) / box.height + box.y
|
|
}
|
|
|
|
return this
|
|
}
|
|
// Get bounding box of points
|
|
, bbox: function() {
|
|
SVG.parser.poly.setAttribute('points', this.toString())
|
|
|
|
return SVG.parser.poly.getBBox()
|
|
}
|
|
})
|