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/ClipPath.js | |
parent | 9f2696e8a2cf7e4eebc1cc7e31027fe2070094fa (diff) | |
download | svg.js-a0b13ebcacfd74b9f521110c7225bb404325bcd3.tar.gz svg.js-a0b13ebcacfd74b9f521110c7225bb404325bcd3.zip |
reordered modules, add es6 build
Diffstat (limited to 'src/elements/ClipPath.js')
-rw-r--r-- | src/elements/ClipPath.js | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/elements/ClipPath.js b/src/elements/ClipPath.js new file mode 100644 index 0000000..2828d6e --- /dev/null +++ b/src/elements/ClipPath.js @@ -0,0 +1,57 @@ +import { nodeOrNew, register } from '../utils/adopter.js' +import { registerMethods } from '../utils/methods.js' +import Container from './Container.js' +import baseFind from '../modules/core/selector.js' + +export default class ClipPath extends Container { + constructor (node) { + super(nodeOrNew('clipPath', node), ClipPath) + } + + // Unclip all clipped elements and remove itself + remove () { + // unclip all targets + this.targets().forEach(function (el) { + el.unclip() + }) + + // remove clipPath from parent + return super.remove() + } + + targets () { + return baseFind('svg [clip-path*="' + this.id() + '"]') + } +} + +registerMethods({ + Container: { + // Create clipping element + clip: function () { + return this.defs().put(new ClipPath()) + } + }, + Element: { + // Distribute clipPath to svg element + clipWith (element) { + // use given clip or create a new one + let clipper = element instanceof ClipPath + ? element + : this.parent().clip().add(element) + + // apply mask + return this.attr('clip-path', 'url("#' + clipper.id() + '")') + }, + + // Unclip element + unclip () { + return this.attr('clip-path', null) + }, + + clipper () { + return this.reference('clip-path') + } + } +}) + +register(ClipPath) |