diff options
author | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-06 13:48:05 +0100 |
---|---|---|
committer | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-06 13:48:05 +0100 |
commit | a0b13ebcacfd74b9f521110c7225bb404325bcd3 (patch) | |
tree | a07c5cc422645e31d7dfef81ce4e54f03f0945f6 /src/elements/Line.js | |
parent | 9f2696e8a2cf7e4eebc1cc7e31027fe2070094fa (diff) | |
download | svg.js-a0b13ebcacfd74b9f521110c7225bb404325bcd3.tar.gz svg.js-a0b13ebcacfd74b9f521110c7225bb404325bcd3.zip |
reordered modules, add es6 build
Diffstat (limited to 'src/elements/Line.js')
-rw-r--r-- | src/elements/Line.js | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/elements/Line.js b/src/elements/Line.js new file mode 100644 index 0000000..b9bc4e8 --- /dev/null +++ b/src/elements/Line.js @@ -0,0 +1,63 @@ +import { extend, nodeOrNew, register } from '../utils/adopter.js' +import { proportionalSize } from '../utils/utils.js' +import { registerMethods } from '../utils/methods.js' +import PointArray from '../types/PointArray.js' +import Shape from './Shape.js' +import * as pointed from '../modules/core/pointed.js' + +export default class Line extends Shape { + // Initialize node + constructor (node) { + super(nodeOrNew('line', node), Line) + } + + // Get array + array () { + return new PointArray([ + [ this.attr('x1'), this.attr('y1') ], + [ this.attr('x2'), this.attr('y2') ] + ]) + } + + // Overwrite native plot() method + plot (x1, y1, x2, y2) { + if (x1 == null) { + return this.array() + } else if (typeof y1 !== 'undefined') { + x1 = { x1: x1, y1: y1, x2: x2, y2: y2 } + } else { + x1 = new PointArray(x1).toLine() + } + + return this.attr(x1) + } + + // Move by left top corner + move (x, y) { + return this.attr(this.array().move(x, y).toLine()) + } + + // Set element size to given width and height + size (width, height) { + var p = proportionalSize(this, width, height) + return this.attr(this.array().size(p.width, p.height).toLine()) + } +} + +extend(Line, pointed) + +registerMethods({ + Container: { + // Create a line element + line (...args) { + // make sure plot is called as a setter + // x1 is not necessarily a number, it can also be an array, a string and a PointArray + return Line.prototype.plot.apply( + this.put(new Line()) + , args[0] != null ? args : [0, 0, 0, 0] + ) + } + } +}) + +register(Line) |