You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ClipPath.js 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { nodeOrNew, register, wrapWithAttrCheck } from '../utils/adopter.js'
  2. import { registerMethods } from '../utils/methods.js'
  3. import Container from './Container.js'
  4. import baseFind from '../modules/core/selector.js'
  5. export default class ClipPath extends Container {
  6. constructor (node, attrs = node) {
  7. super(nodeOrNew('clipPath', node), attrs)
  8. }
  9. // Unclip all clipped elements and remove itself
  10. remove () {
  11. // unclip all targets
  12. this.targets().forEach(function (el) {
  13. el.unclip()
  14. })
  15. // remove clipPath from parent
  16. return super.remove()
  17. }
  18. targets () {
  19. return baseFind('svg [clip-path*=' + this.id() + ']')
  20. }
  21. }
  22. registerMethods({
  23. Container: {
  24. // Create clipping element
  25. clip: wrapWithAttrCheck(function () {
  26. return this.defs().put(new ClipPath())
  27. })
  28. },
  29. Element: {
  30. // Distribute clipPath to svg element
  31. clipper () {
  32. return this.reference('clip-path')
  33. },
  34. clipWith (element) {
  35. // use given clip or create a new one
  36. const clipper = element instanceof ClipPath
  37. ? element
  38. : this.parent().clip().add(element)
  39. // apply mask
  40. return this.attr('clip-path', 'url(#' + clipper.id() + ')')
  41. },
  42. // Unclip element
  43. unclip () {
  44. return this.attr('clip-path', null)
  45. }
  46. }
  47. })
  48. register(ClipPath, 'ClipPath')