PatternStyle.js 2.0 KB

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