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/Marker.js | |
parent | 9f2696e8a2cf7e4eebc1cc7e31027fe2070094fa (diff) | |
download | svg.js-a0b13ebcacfd74b9f521110c7225bb404325bcd3.tar.gz svg.js-a0b13ebcacfd74b9f521110c7225bb404325bcd3.zip |
reordered modules, add es6 build
Diffstat (limited to 'src/elements/Marker.js')
-rw-r--r-- | src/elements/Marker.js | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/src/elements/Marker.js b/src/elements/Marker.js new file mode 100644 index 0000000..2b0541b --- /dev/null +++ b/src/elements/Marker.js @@ -0,0 +1,81 @@ +import { nodeOrNew, register } from '../utils/adopter.js' +import { registerMethods } from '../utils/methods.js' +import Container from './Container.js' + +export default class Marker extends Container { + // Initialize node + constructor (node) { + super(nodeOrNew('marker', node), Marker) + } + + // Set width of element + width (width) { + return this.attr('markerWidth', width) + } + + // Set height of element + height (height) { + return this.attr('markerHeight', height) + } + + // Set marker refX and refY + ref (x, y) { + return this.attr('refX', x).attr('refY', y) + } + + // Update marker + update (block) { + // remove all content + this.clear() + + // invoke passed block + if (typeof block === 'function') { block.call(this, this) } + + return this + } + + // Return the fill id + toString () { + return 'url(#' + this.id() + ')' + } +} + +registerMethods({ + Container: { + marker (width, height, block) { + // Create marker element in defs + return this.defs().marker(width, height, block) + } + }, + Defs: { + // Create marker + marker (width, height, block) { + // Set default viewbox to match the width and height, set ref to cx and cy and set orient to auto + return this.put(new Marker()) + .size(width, height) + .ref(width / 2, height / 2) + .viewbox(0, 0, width, height) + .attr('orient', 'auto') + .update(block) + } + }, + marker: { + // Create and attach markers + marker (marker, width, height, block) { + var attr = ['marker'] + + // Build attribute name + if (marker !== 'all') attr.push(marker) + attr = attr.join('-') + + // Set marker attribute + marker = arguments[1] instanceof Marker + ? arguments[1] + : this.defs().marker(width, height, block) + + return this.attr(attr, marker) + } + } +}) + +register(Marker) |