RadialGradientStyle.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import {GradientStyle} from "./GradientStyle";
  2. import {Style} from "./Style";
  3. import {Vector2} from "../../math/Vector2";
  4. /**
  5. * Radial gradient interpolates colors from a point to another point around up to a starting and finishing radius value.
  6. *
  7. * If the start and end point are the same it interpolates around the starting and ending radius forming a circle. Outside of the radius 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 RadialGradientStyle()
  15. {
  16. GradientStyle.call(this);
  17. /**
  18. * The coordinates of the starting circle of the gradient.
  19. *
  20. * @type {Vector2}
  21. */
  22. this.start = new Vector2(0, 0);
  23. /**
  24. * The radius of the starting circle.
  25. *
  26. * @type {number}
  27. */
  28. this.startRadius = 10;
  29. /**
  30. * The coordinates of the ending circle of the gradient.
  31. *
  32. * @type {Vector2}
  33. */
  34. this.end = new Vector2(0, 0);
  35. /**
  36. * The radius of the ending circle.
  37. *
  38. * @type {number}
  39. */
  40. this.endRadius = 50;
  41. }
  42. RadialGradientStyle.prototype = Object.create(GradientStyle.prototype);
  43. Style.register(RadialGradientStyle, "RadialGradient");
  44. RadialGradientStyle.prototype.get = function(context)
  45. {
  46. var style = context.createRadialGradient(this.start.x, this.start.y, this.startRadius, this.end.x, this.end.y, this.endRadius);
  47. for(var i = 0; i < this.colors.length; i++)
  48. {
  49. style.addColorStop(this.colors[i].offset, this.colors[i].color);
  50. }
  51. return style;
  52. };
  53. RadialGradientStyle.prototype.serialize = function ()
  54. {
  55. var data = GradientStyle.prototype.serialize.call(this);
  56. Object.assign(data, {
  57. type: "RadialGradient",
  58. start: this.start.toArray(),
  59. end: this.end.toArray(),
  60. startRadius: this.startRadius,
  61. endRadius: this.endRadius
  62. });
  63. return data;
  64. };
  65. RadialGradientStyle.prototype.parse = function (data)
  66. {
  67. GradientStyle.prototype.parse.call(this, data);
  68. this.start.fromArray(data.start);
  69. this.end.fromArray(data.end);
  70. this.startRadius = data.startRadius;
  71. this.endRadius = data.endRadius;
  72. };
  73. export {RadialGradientStyle};