PatternStyle.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import {Style} from "./Style";
  2. import {GradientStyle} from "./GradientStyle";
  3. import {Matrix} from "../../math/Matrix";
  4. /**
  5. * Pattern style represents an opaque object describing a pattern, based on an image, a canvas, or a video.
  6. *
  7. * The get method returns a CanvasPattern object https://developer.mozilla.org/en-US/docs/Web/API/CanvasPattern created by the context.createPattern() method.
  8. *
  9. * @class
  10. * @extends {Style}
  11. * @param {CanvasImageSource} source Source element of the pattern.
  12. */
  13. function PatternStyle(source)
  14. {
  15. Style.call(this);
  16. /**
  17. * Source of the pattern style. Can be a image, video or another canvas element
  18. *
  19. * By default a empty image element is created.
  20. *
  21. * @type {CanvasImageSource}
  22. */
  23. this.source = source || document.createElement("img");
  24. /**
  25. * Repetition indicates how the pattern image should be repeated.
  26. *
  27. * Possible values are "repeat", "repeat-x", "repeat-y" or "no-repeat".
  28. *
  29. * More information about this attribute here https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/createPattern.
  30. *
  31. * @type {string}
  32. */
  33. this.repetition = "repeat"
  34. /**
  35. * Transformation matrix applied to the pattern.
  36. *
  37. * The transformation allows to move, rotate and scale the pattern freely
  38. *
  39. * @type {Matrix}
  40. */
  41. this.matrix = new Matrix();
  42. }
  43. PatternStyle.prototype = Object.create(Style.prototype);
  44. Style.register(PatternStyle, "Pattern");
  45. /**
  46. * Applies an 2x3 transformation matrix representing a linear transform to the pattern.
  47. *
  48. * @param {number[]} transform 2x3 Transformation matrix.
  49. */
  50. PatternStyle.prototype.setTransform = function(transform)
  51. {
  52. this.matrix.m = transform;
  53. this.needsUpdate = true;
  54. };
  55. PatternStyle.prototype.get = function(context)
  56. {
  57. if(this.needsUpdate || this.cache === null)
  58. {
  59. this.cache = context.createPattern(this.source, this.repetition);
  60. this.cache.setTransform(this.matrix.cssTransform());
  61. this.needsUpdate = false;
  62. }
  63. return this.cache;
  64. };
  65. PatternStyle.prototype.serialize = function ()
  66. {
  67. var data = GradientStyle.prototype.serialize.call(this);
  68. Object.assign(data, {
  69. type: "Pattern",
  70. matrix: this.matrix.m,
  71. repetition: this.repetition,
  72. source: this.source
  73. });
  74. return data;
  75. };
  76. PatternStyle.prototype.parse = function (data)
  77. {
  78. GradientStyle.prototype.parse.call(this, data);
  79. this.matrix = new Matrix(data.matrix);
  80. this.repetition = data.repetition;
  81. this.source = data.source;
  82. };
  83. export {PatternStyle};