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.

Gradient.js 1.5KB

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