LinearGradientStyle.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import {Style} from "./Style";
  2. import {GradientStyle} from "./GradientStyle";
  3. /**
  4. * Linear gradient style, represents a gradient of colors from a point to another interpolating in between.
  5. *
  6. * Behind the of the two points used the color is solid.
  7. *
  8. * The get method returns a CanvasGradient https://developer.mozilla.org/en-US/docs/Web/API/CanvasGradient when generated.
  9. *
  10. * @class
  11. * @extends {GradientStyle}
  12. */
  13. function LinearGradientStyle()
  14. {
  15. GradientStyle.call(this);
  16. /**
  17. * The coordinates of the starting point of the gradient.
  18. *
  19. * @type {Vector2}
  20. */
  21. this.start = new Vector2(0, 0);
  22. /**
  23. * The coordinates of the ending point of the gradient.
  24. *
  25. * @type {Vector2}
  26. */
  27. this.end = new Vector2(0, 0);
  28. }
  29. LinearGradientStyle.prototype = Object.create(GradientStyle.prototype);
  30. Style.register(LinearGradientStyle, "LinearGradient");
  31. LinearGradientStyle.prototype.get = function(context)
  32. {
  33. return context.createLinearGradient(this.start.x, this.start.y, this.end.x, this.end.y);
  34. };
  35. LinearGradientStyle.prototype.serialize = function ()
  36. {
  37. return {
  38. type: "LinearGradient",
  39. start: this.start.toArray(),
  40. end: this.end.toArray()
  41. };
  42. };
  43. LinearGradientStyle.prototype.serialize = function ()
  44. {
  45. var data = GradientStyle.prototype.serialize.call(this);
  46. Object.assign(data, {
  47. type: "LinearGradient"
  48. });
  49. return data;
  50. };
  51. LinearGradientStyle.prototype.parse = function (data)
  52. {
  53. GradientStyle.prototype.parse.call(this, data);
  54. this.start.fromArray(data.start);
  55. this.end.fromArray(data.end);
  56. };
  57. export {LinearGradientStyle};