2
0

LinearGradientStyle.js 1.7 KB

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