Pattern.js 2.5 KB

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