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.

Pattern.js 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { nodeOrNew, register, wrapWithAttrCheck } from '../utils/adopter.js'
  2. import { registerMethods } from '../utils/methods.js'
  3. import Box from '../types/Box.js'
  4. import Container from './Container.js'
  5. import baseFind from '../modules/core/selector.js'
  6. export default class Pattern extends Container {
  7. // Initialize node
  8. constructor (node, attrs = node) {
  9. super(nodeOrNew('pattern', node), attrs)
  10. }
  11. // custom attr to handle transform
  12. attr (a, b, c) {
  13. if (a === 'transform') a = 'patternTransform'
  14. return super.attr(a, b, c)
  15. }
  16. bbox () {
  17. return new Box()
  18. }
  19. targets () {
  20. return baseFind('svg [fill*=' + this.id() + ']')
  21. }
  22. // Alias string conversion to fill
  23. toString () {
  24. return this.url()
  25. }
  26. // Update pattern by rebuilding
  27. update (block) {
  28. // remove content
  29. this.clear()
  30. // invoke passed block
  31. if (typeof block === 'function') {
  32. block.call(this, this)
  33. }
  34. return this
  35. }
  36. // Return the fill id
  37. url () {
  38. return 'url(#' + this.id() + ')'
  39. }
  40. }
  41. registerMethods({
  42. Container: {
  43. // Create pattern element in defs
  44. pattern (...args) {
  45. return this.defs().pattern(...args)
  46. }
  47. },
  48. Defs: {
  49. pattern: wrapWithAttrCheck(function (width, height, block) {
  50. return this.put(new Pattern()).update(block).attr({
  51. x: 0,
  52. y: 0,
  53. width: width,
  54. height: height,
  55. patternUnits: 'userSpaceOnUse'
  56. })
  57. })
  58. }
  59. })
  60. register(Pattern, 'Pattern')