2
0

Pattern.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import {Object2D} from "../Object2D.js";
  2. import {Box2} from "../math/Box2.js";
  3. /**
  4. * Pattern object draw a image repeated as a pattern.
  5. *
  6. * Its similar to the Image class but the image can be repeat infinitely.
  7. *
  8. * @class
  9. * @extends {Object2D}
  10. * @param {string} src Source image URL.
  11. */
  12. function Pattern(src)
  13. {
  14. Object2D.call(this);
  15. /**
  16. * Box object containing the size of the object.
  17. *
  18. * @type {Box2}
  19. */
  20. this.box = new Box2();
  21. /**
  22. * Image source DOM element. Used as a source for the pattern image.
  23. *
  24. * This element can be replaced by one of other type (e.g canvas, video).
  25. *
  26. * @type {Element}
  27. */
  28. this.image = document.createElement("img");
  29. /**
  30. * Repetition indicates how the pattern image should be repeated.
  31. *
  32. * Possible values are "repeat", "repeat-x", "repeat-y" or "no-repeat".
  33. *
  34. * More information about this attribute here https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/createPattern.
  35. *
  36. * @type {string}
  37. */
  38. this.repetition = "repeat"
  39. if(src !== undefined)
  40. {
  41. this.setImage(src);
  42. }
  43. }
  44. Pattern.prototype = Object.create(Object2D.prototype);
  45. Pattern.prototype.constructor = Pattern;
  46. Pattern.prototype.type = "Pattern";
  47. Object2D.register(Pattern, "Pattern");
  48. /**
  49. * Set the image source of the object. Can be anything accepted by the src field of an img element.
  50. *
  51. * Automatically sets the box size to match the image.
  52. *
  53. * @param {string} src Image source string.
  54. */
  55. Pattern.prototype.setImage = function(src)
  56. {
  57. var self = this;
  58. this.image.onload = function()
  59. {
  60. self.box.min.set(0, 0);
  61. self.box.max.set(this.naturalWidth, this.naturalHeight);
  62. };
  63. this.image.src = src;
  64. };
  65. Pattern.prototype.isInside = function(point)
  66. {
  67. return this.box.containsPoint(point);
  68. };
  69. Pattern.prototype.draw = function(context, viewport, canvas)
  70. {
  71. var width = this.box.max.x - this.box.min.x;
  72. var height = this.box.max.y - this.box.min.y;
  73. if(this.image.src.length > 0)
  74. {
  75. var pattern = context.createPattern(this.image, this.repetition);
  76. context.fillStyle = pattern;
  77. context.fillRect(this.box.min.x, this.box.min.y, width, height);
  78. }
  79. };
  80. Pattern.prototype.serialize = function(recursive)
  81. {
  82. var data = Object2D.prototype.serialize.call(this, recursive);
  83. data.box = this.box.toArray();
  84. data.image = this.image.src;
  85. data.repetition = this.repetition;
  86. return data;
  87. };
  88. Pattern.prototype.parse = function(data, root)
  89. {
  90. Object2D.prototype.parse.call(this, data, root);
  91. this.box.fromArray(data.box);
  92. this.image.src = data.image;
  93. this.repetition = data.repetition;
  94. };
  95. export {Pattern};