konva 10.3.3 → 10.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. package/README.md +1 -1
  2. package/konva.js +1327 -1572
  3. package/konva.min.js +4 -4
  4. package/lib/Animation.d.ts +63 -3
  5. package/lib/Animation.js +86 -65
  6. package/lib/BezierFunctions.d.ts +10 -0
  7. package/lib/BezierFunctions.js +32 -0
  8. package/lib/Canvas.d.ts +53 -0
  9. package/lib/Canvas.js +93 -34
  10. package/lib/Container.d.ts +156 -9
  11. package/lib/Container.js +260 -25
  12. package/lib/Context.d.ts +247 -0
  13. package/lib/Context.js +312 -74
  14. package/lib/Core.js +1 -0
  15. package/lib/DragAndDrop.d.ts +1 -0
  16. package/lib/DragAndDrop.js +66 -17
  17. package/lib/Factory.d.ts +24 -1
  18. package/lib/Factory.js +14 -20
  19. package/lib/FastLayer.d.ts +14 -1
  20. package/lib/FastLayer.js +14 -1
  21. package/lib/Global.d.ts +114 -3
  22. package/lib/Global.js +134 -5
  23. package/lib/Group.d.ts +11 -0
  24. package/lib/Group.js +11 -0
  25. package/lib/Layer.d.ts +128 -3
  26. package/lib/Layer.js +260 -49
  27. package/lib/Node.d.ts +692 -30
  28. package/lib/Node.js +1534 -202
  29. package/lib/PointerEvents.d.ts +2 -1
  30. package/lib/PointerEvents.js +19 -1
  31. package/lib/Shape.d.ts +99 -14
  32. package/lib/Shape.js +1101 -67
  33. package/lib/Stage.d.ts +81 -1
  34. package/lib/Stage.js +166 -7
  35. package/lib/Tween.d.ts +159 -4
  36. package/lib/Tween.js +243 -24
  37. package/lib/Util.d.ts +147 -4
  38. package/lib/Util.js +332 -82
  39. package/lib/Validators.d.ts +0 -2
  40. package/lib/Validators.js +3 -25
  41. package/lib/_CoreInternals.d.ts +10 -6
  42. package/lib/_CoreInternals.js +1 -0
  43. package/lib/_FullInternals.d.ts +15 -6
  44. package/lib/_FullInternals.js +7 -0
  45. package/lib/canvas-backend.js +6 -0
  46. package/lib/filters/Blur.d.ts +18 -0
  47. package/lib/filters/Blur.js +97 -24
  48. package/lib/filters/Brighten.d.ts +21 -0
  49. package/lib/filters/Brighten.js +24 -0
  50. package/lib/filters/Brightness.d.ts +11 -0
  51. package/lib/filters/Brightness.js +16 -0
  52. package/lib/filters/Contrast.d.ts +10 -0
  53. package/lib/filters/Contrast.js +21 -0
  54. package/lib/filters/Emboss.d.ts +20 -0
  55. package/lib/filters/Emboss.js +68 -8
  56. package/lib/filters/Enhance.d.ts +14 -0
  57. package/lib/filters/Enhance.js +34 -0
  58. package/lib/filters/Grayscale.d.ts +9 -0
  59. package/lib/filters/Grayscale.js +12 -0
  60. package/lib/filters/HSL.d.ts +17 -0
  61. package/lib/filters/HSL.js +48 -5
  62. package/lib/filters/HSV.d.ts +18 -0
  63. package/lib/filters/HSV.js +49 -5
  64. package/lib/filters/Invert.d.ts +9 -0
  65. package/lib/filters/Invert.js +12 -0
  66. package/lib/filters/Kaleidoscope.d.ts +12 -0
  67. package/lib/filters/Kaleidoscope.js +83 -0
  68. package/lib/filters/Mask.d.ts +11 -0
  69. package/lib/filters/Mask.js +18 -0
  70. package/lib/filters/Noise.d.ts +19 -0
  71. package/lib/filters/Noise.js +19 -0
  72. package/lib/filters/Pixelate.d.ts +20 -0
  73. package/lib/filters/Pixelate.js +28 -1
  74. package/lib/filters/Posterize.d.ts +21 -0
  75. package/lib/filters/Posterize.js +22 -0
  76. package/lib/filters/RGB.d.ts +21 -0
  77. package/lib/filters/RGB.js +43 -28
  78. package/lib/filters/RGBA.d.ts +23 -0
  79. package/lib/filters/RGBA.js +28 -41
  80. package/lib/filters/Sepia.d.ts +9 -0
  81. package/lib/filters/Sepia.js +10 -0
  82. package/lib/filters/Solarize.d.ts +10 -0
  83. package/lib/filters/Solarize.js +11 -0
  84. package/lib/filters/Threshold.d.ts +22 -0
  85. package/lib/filters/Threshold.js +22 -0
  86. package/lib/shapes/Arc.d.ts +40 -0
  87. package/lib/shapes/Arc.js +89 -6
  88. package/lib/shapes/Arrow.d.ts +24 -1
  89. package/lib/shapes/Arrow.js +83 -0
  90. package/lib/shapes/Circle.d.ts +18 -0
  91. package/lib/shapes/Circle.js +36 -9
  92. package/lib/shapes/Ellipse.d.ts +34 -2
  93. package/lib/shapes/Ellipse.js +67 -9
  94. package/lib/shapes/Image.d.ts +49 -0
  95. package/lib/shapes/Image.js +151 -22
  96. package/lib/shapes/Label.d.ts +65 -1
  97. package/lib/shapes/Label.js +113 -20
  98. package/lib/shapes/Line.d.ts +50 -8
  99. package/lib/shapes/Line.js +116 -20
  100. package/lib/shapes/Path.d.ts +40 -6
  101. package/lib/shapes/Path.js +187 -128
  102. package/lib/shapes/Rect.d.ts +18 -0
  103. package/lib/shapes/Rect.js +36 -0
  104. package/lib/shapes/RegularPolygon.d.ts +22 -0
  105. package/lib/shapes/RegularPolygon.js +70 -0
  106. package/lib/shapes/Ring.d.ts +19 -0
  107. package/lib/shapes/Ring.js +51 -5
  108. package/lib/shapes/Sprite.d.ts +78 -0
  109. package/lib/shapes/Sprite.js +238 -25
  110. package/lib/shapes/Star.d.ts +23 -0
  111. package/lib/shapes/Star.js +62 -0
  112. package/lib/shapes/Text.d.ts +59 -1
  113. package/lib/shapes/Text.js +481 -97
  114. package/lib/shapes/TextPath.d.ts +49 -3
  115. package/lib/shapes/TextPath.js +278 -34
  116. package/lib/shapes/Transformer.d.ts +108 -11
  117. package/lib/shapes/Transformer.js +744 -102
  118. package/lib/shapes/Wedge.d.ts +22 -0
  119. package/lib/shapes/Wedge.js +67 -3
  120. package/lib/skia-backend.js +9 -0
  121. package/lib/types.d.ts +2 -23
  122. package/lib/types.js +1 -21
  123. package/package.json +12 -16
@@ -3,9 +3,34 @@ import { Shape } from "../Shape.js";
3
3
  import { getNumberOrArrayOfNumbersValidator, getNumberValidator, } from "../Validators.js";
4
4
  import { _registerNode } from "../Global.js";
5
5
  import { Util } from "../Util.js";
6
+ /**
7
+ * RegularPolygon constructor. Examples include triangles, squares, pentagons, hexagons, etc.
8
+ * @constructor
9
+ * @memberof Konva
10
+ * @augments Konva.Shape
11
+ * @param {Object} config
12
+ * @param {Number} [config.cornerRadius]
13
+ * @param {Number} config.sides
14
+ * @param {Number} config.radius
15
+ * @@shapeParams
16
+ * @@nodeParams
17
+ * @example
18
+ * var hexagon = new Konva.RegularPolygon({
19
+ * x: 100,
20
+ * y: 200,
21
+ * sides: 6,
22
+ * radius: 70,
23
+ * fill: 'red',
24
+ * stroke: 'black',
25
+ * strokeWidth: 4
26
+ * });
27
+ */
6
28
  export class RegularPolygon extends Shape {
7
29
  _sceneFunc(context) {
8
30
  const points = this._getPoints(), radius = this.radius(), sides = this.sides(), cornerRadius = this.cornerRadius();
31
+ if (!points.length) {
32
+ return;
33
+ }
9
34
  context.beginPath();
10
35
  if (!cornerRadius) {
11
36
  context.moveTo(points[0].x, points[0].y);
@@ -33,6 +58,9 @@ export class RegularPolygon extends Shape {
33
58
  }
34
59
  getSelfRect() {
35
60
  const points = this._getPoints();
61
+ if (!points.length) {
62
+ return { x: 0, y: 0, width: 0, height: 0 };
63
+ }
36
64
  let minX = points[0].x;
37
65
  let maxX = points[0].x;
38
66
  let minY = points[0].y;
@@ -67,6 +95,48 @@ RegularPolygon.prototype.className = 'RegularPolygon';
67
95
  RegularPolygon.prototype._centroid = true;
68
96
  RegularPolygon.prototype._attrsAffectingSize = ['radius'];
69
97
  _registerNode(RegularPolygon);
98
+ /**
99
+ * get/set radius
100
+ * @method
101
+ * @name Konva.RegularPolygon#radius
102
+ * @param {Number} radius
103
+ * @returns {Number}
104
+ * @example
105
+ * // get radius
106
+ * var radius = shape.radius();
107
+ *
108
+ * // set radius
109
+ * shape.radius(10);
110
+ */
70
111
  Factory.addGetterSetter(RegularPolygon, 'radius', 0, getNumberValidator());
112
+ /**
113
+ * get/set sides
114
+ * @method
115
+ * @name Konva.RegularPolygon#sides
116
+ * @param {Number} sides
117
+ * @returns {Number}
118
+ * @example
119
+ * // get sides
120
+ * var sides = shape.sides();
121
+ *
122
+ * // set sides
123
+ * shape.sides(10);
124
+ */
71
125
  Factory.addGetterSetter(RegularPolygon, 'sides', 0, getNumberValidator());
126
+ /**
127
+ * get/set corner radius
128
+ * @method
129
+ * @name Konva.RegularPolygon#cornerRadius
130
+ * @param {Number} cornerRadius
131
+ * @returns {Number}
132
+ * @example
133
+ * // get corner radius
134
+ * var cornerRadius = poly.cornerRadius();
135
+ *
136
+ * // set corner radius
137
+ * poly.cornerRadius(10);
138
+ *
139
+ * // set different corner radius values (pentagon)
140
+ * poly.cornerRadius([0, 10, 20, 30, 40]);
141
+ */
72
142
  Factory.addGetterSetter(RegularPolygon, 'cornerRadius', 0, getNumberOrArrayOfNumbersValidator(4));
@@ -6,6 +6,25 @@ export interface RingConfig extends ShapeConfig {
6
6
  innerRadius: number;
7
7
  outerRadius: number;
8
8
  }
9
+ /**
10
+ * Ring constructor
11
+ * @constructor
12
+ * @augments Konva.Shape
13
+ * @memberof Konva
14
+ * @param {Object} config
15
+ * @param {Number} config.innerRadius
16
+ * @param {Number} config.outerRadius
17
+ * @@shapeParams
18
+ * @@nodeParams
19
+ * @example
20
+ * var ring = new Konva.Ring({
21
+ * innerRadius: 40,
22
+ * outerRadius: 80,
23
+ * fill: 'red',
24
+ * stroke: 'black',
25
+ * strokeWidth: 5
26
+ * });
27
+ */
9
28
  export declare class Ring extends Shape<RingConfig> {
10
29
  _sceneFunc(context: Context): void;
11
30
  getWidth(): number;
@@ -3,20 +3,40 @@ import { Shape } from "../Shape.js";
3
3
  import { getNumberValidator } from "../Validators.js";
4
4
  import { _registerNode } from "../Global.js";
5
5
  const PIx2 = Math.PI * 2;
6
+ /**
7
+ * Ring constructor
8
+ * @constructor
9
+ * @augments Konva.Shape
10
+ * @memberof Konva
11
+ * @param {Object} config
12
+ * @param {Number} config.innerRadius
13
+ * @param {Number} config.outerRadius
14
+ * @@shapeParams
15
+ * @@nodeParams
16
+ * @example
17
+ * var ring = new Konva.Ring({
18
+ * innerRadius: 40,
19
+ * outerRadius: 80,
20
+ * fill: 'red',
21
+ * stroke: 'black',
22
+ * strokeWidth: 5
23
+ * });
24
+ */
6
25
  export class Ring extends Shape {
7
26
  _sceneFunc(context) {
27
+ const innerRadius = Math.abs(this.innerRadius()), outerRadius = Math.abs(this.outerRadius());
8
28
  context.beginPath();
9
- context.arc(0, 0, this.innerRadius(), 0, PIx2, false);
10
- context.moveTo(this.outerRadius(), 0);
11
- context.arc(0, 0, this.outerRadius(), PIx2, 0, true);
29
+ context.arc(0, 0, innerRadius, 0, PIx2, false);
30
+ context.moveTo(outerRadius, 0);
31
+ context.arc(0, 0, outerRadius, PIx2, 0, true);
12
32
  context.closePath();
13
33
  context.fillStrokeShape(this);
14
34
  }
15
35
  getWidth() {
16
- return this.outerRadius() * 2;
36
+ return Math.abs(this.outerRadius()) * 2;
17
37
  }
18
38
  getHeight() {
19
- return this.outerRadius() * 2;
39
+ return Math.abs(this.outerRadius()) * 2;
20
40
  }
21
41
  setWidth(width) {
22
42
  this.outerRadius(width / 2);
@@ -29,5 +49,31 @@ Ring.prototype.className = 'Ring';
29
49
  Ring.prototype._centroid = true;
30
50
  Ring.prototype._attrsAffectingSize = ['innerRadius', 'outerRadius'];
31
51
  _registerNode(Ring);
52
+ /**
53
+ * get/set innerRadius
54
+ * @method
55
+ * @name Konva.Ring#innerRadius
56
+ * @param {Number} innerRadius
57
+ * @returns {Number}
58
+ * @example
59
+ * // get inner radius
60
+ * var innerRadius = ring.innerRadius();
61
+ *
62
+ * // set inner radius
63
+ * ring.innerRadius(20);
64
+ */
32
65
  Factory.addGetterSetter(Ring, 'innerRadius', 0, getNumberValidator());
66
+ /**
67
+ * get/set outerRadius
68
+ * @name Konva.Ring#outerRadius
69
+ * @method
70
+ * @param {Number} outerRadius
71
+ * @returns {Number}
72
+ * @example
73
+ * // get outer radius
74
+ * var outerRadius = ring.outerRadius();
75
+ *
76
+ * // set outer radius
77
+ * ring.outerRadius(20);
78
+ */
33
79
  Factory.addGetterSetter(Ring, 'outerRadius', 0, getNumberValidator());
@@ -10,17 +10,95 @@ export interface SpriteConfig extends ShapeConfig {
10
10
  image: HTMLImageElement;
11
11
  frameRate?: number;
12
12
  }
13
+ /**
14
+ * Sprite constructor
15
+ * @constructor
16
+ * @memberof Konva
17
+ * @augments Konva.Shape
18
+ * @param {Object} config
19
+ * @param {String} config.animation animation key
20
+ * @param {Object} config.animations animation map
21
+ * @param {Integer} [config.frameIndex] animation frame index
22
+ * @param {Image} config.image image object
23
+ * @param {Integer} [config.frameRate] animation frame rate
24
+ * @@shapeParams
25
+ * @@nodeParams
26
+ * @example
27
+ * var imageObj = new Image();
28
+ * imageObj.onload = function() {
29
+ * var sprite = new Konva.Sprite({
30
+ * x: 200,
31
+ * y: 100,
32
+ * image: imageObj,
33
+ * animation: 'standing',
34
+ * animations: {
35
+ * standing: [
36
+ * // x, y, width, height (6 frames)
37
+ * 0, 0, 49, 109,
38
+ * 52, 0, 49, 109,
39
+ * 105, 0, 49, 109,
40
+ * 158, 0, 49, 109,
41
+ * 210, 0, 49, 109,
42
+ * 262, 0, 49, 109
43
+ * ],
44
+ * kicking: [
45
+ * // x, y, width, height (6 frames)
46
+ * 0, 109, 45, 98,
47
+ * 45, 109, 45, 98,
48
+ * 95, 109, 63, 98,
49
+ * 156, 109, 70, 98,
50
+ * 229, 109, 60, 98,
51
+ * 287, 109, 41, 98
52
+ * ]
53
+ * },
54
+ * frameRate: 7,
55
+ * frameIndex: 0
56
+ * });
57
+ * };
58
+ * imageObj.src = '/path/to/image.jpg'
59
+ */
13
60
  export declare class Sprite extends Shape<SpriteConfig> {
14
61
  _updated: boolean;
15
62
  anim: Animation;
16
63
  interval: any;
17
64
  constructor(config: SpriteConfig);
65
+ _getFrame(): {
66
+ x: any;
67
+ y: any;
68
+ width: any;
69
+ height: any;
70
+ offsetX: any;
71
+ offsetY: any;
72
+ } | undefined;
18
73
  _sceneFunc(context: Context): void;
19
74
  _hitFunc(context: Context): void;
75
+ getSelfRect(): {
76
+ x: any;
77
+ y: any;
78
+ width: any;
79
+ height: any;
80
+ };
20
81
  _useBufferCanvas(): boolean;
21
82
  _setInterval(): void;
83
+ /**
84
+ * start sprite animation
85
+ * @method
86
+ * @name Konva.Sprite#start
87
+ */
22
88
  start(): void;
89
+ /**
90
+ * stop sprite animation
91
+ * @method
92
+ * @name Konva.Sprite#stop
93
+ */
23
94
  stop(): void;
95
+ destroy(): this;
96
+ /**
97
+ * determine if animation of sprite is running or not. returns true or false
98
+ * @method
99
+ * @name Konva.Sprite#isRunning
100
+ * @returns {Boolean}
101
+ */
24
102
  isRunning(): boolean;
25
103
  _updateIndex(): void;
26
104
  frameIndex: GetSet<number, this>;
@@ -3,21 +3,71 @@ import { Shape } from "../Shape.js";
3
3
  import { Animation } from "../Animation.js";
4
4
  import { getNumberValidator } from "../Validators.js";
5
5
  import { _registerNode } from "../Global.js";
6
+ /**
7
+ * Sprite constructor
8
+ * @constructor
9
+ * @memberof Konva
10
+ * @augments Konva.Shape
11
+ * @param {Object} config
12
+ * @param {String} config.animation animation key
13
+ * @param {Object} config.animations animation map
14
+ * @param {Integer} [config.frameIndex] animation frame index
15
+ * @param {Image} config.image image object
16
+ * @param {Integer} [config.frameRate] animation frame rate
17
+ * @@shapeParams
18
+ * @@nodeParams
19
+ * @example
20
+ * var imageObj = new Image();
21
+ * imageObj.onload = function() {
22
+ * var sprite = new Konva.Sprite({
23
+ * x: 200,
24
+ * y: 100,
25
+ * image: imageObj,
26
+ * animation: 'standing',
27
+ * animations: {
28
+ * standing: [
29
+ * // x, y, width, height (6 frames)
30
+ * 0, 0, 49, 109,
31
+ * 52, 0, 49, 109,
32
+ * 105, 0, 49, 109,
33
+ * 158, 0, 49, 109,
34
+ * 210, 0, 49, 109,
35
+ * 262, 0, 49, 109
36
+ * ],
37
+ * kicking: [
38
+ * // x, y, width, height (6 frames)
39
+ * 0, 109, 45, 98,
40
+ * 45, 109, 45, 98,
41
+ * 95, 109, 63, 98,
42
+ * 156, 109, 70, 98,
43
+ * 229, 109, 60, 98,
44
+ * 287, 109, 41, 98
45
+ * ]
46
+ * },
47
+ * frameRate: 7,
48
+ * frameIndex: 0
49
+ * });
50
+ * };
51
+ * imageObj.src = '/path/to/image.jpg'
52
+ */
6
53
  export class Sprite extends Shape {
7
54
  constructor(config) {
8
55
  super(config);
9
56
  this._updated = true;
10
57
  this.anim = new Animation(() => {
58
+ // if we don't need to redraw layer we should return false
11
59
  const updated = this._updated;
12
60
  this._updated = false;
13
61
  return updated;
14
62
  });
15
63
  this.on('animationChange.konva', function () {
64
+ // reset index when animation changes
16
65
  this.frameIndex(0);
17
66
  });
18
67
  this.on('frameIndexChange.konva', function () {
19
68
  this._updated = true;
20
69
  });
70
+ // smooth change for frameRate
21
71
  this.on('frameRateChange.konva', function () {
22
72
  if (!this.anim.isRunning()) {
23
73
  return;
@@ -26,38 +76,60 @@ export class Sprite extends Shape {
26
76
  this._setInterval();
27
77
  });
28
78
  }
79
+ // the current frame: its box in the sprite sheet and where it is drawn,
80
+ // or nothing when the animation key or the frame index does not exist
81
+ // (then nothing is drawn, like an image without an image)
82
+ _getFrame() {
83
+ var _a, _b;
84
+ const anim = this.animation(), set = (_a = this.animations()) === null || _a === void 0 ? void 0 : _a[anim], index = this.frameIndex(), ix4 = index * 4;
85
+ if (!set || !Number.isInteger(index) || ix4 < 0 || ix4 + 4 > set.length) {
86
+ return;
87
+ }
88
+ const offset = ((_b = this.frameOffsets()) === null || _b === void 0 ? void 0 : _b[anim]) || [];
89
+ return {
90
+ x: set[ix4],
91
+ y: set[ix4 + 1],
92
+ width: set[ix4 + 2],
93
+ height: set[ix4 + 3],
94
+ offsetX: offset[index * 2] || 0,
95
+ offsetY: offset[index * 2 + 1] || 0,
96
+ };
97
+ }
29
98
  _sceneFunc(context) {
30
- const anim = this.animation(), index = this.frameIndex(), ix4 = index * 4, set = this.animations()[anim], offsets = this.frameOffsets(), x = set[ix4 + 0], y = set[ix4 + 1], width = set[ix4 + 2], height = set[ix4 + 3], image = this.image();
99
+ const frame = this._getFrame();
100
+ if (!frame) {
101
+ return;
102
+ }
103
+ const { x, y, width, height, offsetX, offsetY } = frame;
104
+ const image = this.image();
31
105
  if (this.hasFill() || this.hasStroke()) {
32
106
  context.beginPath();
33
- context.rect(0, 0, width, height);
107
+ context.rect(offsetX, offsetY, width, height);
34
108
  context.closePath();
35
109
  context.fillStrokeShape(this);
36
110
  }
37
111
  if (image) {
38
- if (offsets) {
39
- const offset = offsets[anim], ix2 = index * 2;
40
- context.drawImage(image, x, y, width, height, offset[ix2 + 0], offset[ix2 + 1], width, height);
41
- }
42
- else {
43
- context.drawImage(image, x, y, width, height, 0, 0, width, height);
44
- }
112
+ context.drawImage(image, x, y, width, height, offsetX, offsetY, width, height);
45
113
  }
46
114
  }
47
115
  _hitFunc(context) {
48
- const anim = this.animation(), index = this.frameIndex(), ix4 = index * 4, set = this.animations()[anim], offsets = this.frameOffsets(), width = set[ix4 + 2], height = set[ix4 + 3];
116
+ const { x, y, width, height } = this.getSelfRect();
49
117
  context.beginPath();
50
- if (offsets) {
51
- const offset = offsets[anim];
52
- const ix2 = index * 2;
53
- context.rect(offset[ix2 + 0], offset[ix2 + 1], width, height);
54
- }
55
- else {
56
- context.rect(0, 0, width, height);
57
- }
118
+ context.rect(x, y, width, height);
58
119
  context.closePath();
59
120
  context.fillShape(this);
60
121
  }
122
+ getSelfRect() {
123
+ const frame = this._getFrame();
124
+ return frame
125
+ ? {
126
+ x: frame.offsetX,
127
+ y: frame.offsetY,
128
+ width: frame.width,
129
+ height: frame.height,
130
+ }
131
+ : { x: 0, y: 0, width: 0, height: 0 };
132
+ }
61
133
  _useBufferCanvas() {
62
134
  return super._useBufferCanvas(true);
63
135
  }
@@ -67,40 +139,181 @@ export class Sprite extends Shape {
67
139
  that._updateIndex();
68
140
  }, 1000 / this.frameRate());
69
141
  }
142
+ /**
143
+ * start sprite animation
144
+ * @method
145
+ * @name Konva.Sprite#start
146
+ */
70
147
  start() {
71
148
  if (this.isRunning()) {
72
149
  return;
73
150
  }
74
151
  const layer = this.getLayer();
152
+ /*
153
+ * animation object has no executable function because
154
+ * the updates are done with a fixed FPS with the setInterval
155
+ * below. The anim object only needs the layer reference for
156
+ * redraw
157
+ */
75
158
  this.anim.setLayers(layer);
76
159
  this._setInterval();
77
160
  this.anim.start();
78
161
  }
162
+ /**
163
+ * stop sprite animation
164
+ * @method
165
+ * @name Konva.Sprite#stop
166
+ */
79
167
  stop() {
80
168
  this.anim.stop();
81
169
  clearInterval(this.interval);
82
170
  }
171
+ destroy() {
172
+ this.stop();
173
+ return super.destroy();
174
+ }
175
+ /**
176
+ * determine if animation of sprite is running or not. returns true or false
177
+ * @method
178
+ * @name Konva.Sprite#isRunning
179
+ * @returns {Boolean}
180
+ */
83
181
  isRunning() {
84
182
  return this.anim.isRunning();
85
183
  }
86
184
  _updateIndex() {
87
- const index = this.frameIndex(), animation = this.animation(), animations = this.animations(), anim = animations[animation], len = anim.length / 4;
88
- if (index < len - 1) {
89
- this.frameIndex(index + 1);
90
- }
91
- else {
92
- this.frameIndex(0);
93
- }
185
+ var _a;
186
+ const index = this.frameIndex(), set = (_a = this.animations()) === null || _a === void 0 ? void 0 : _a[this.animation()], len = set ? set.length / 4 : 0;
187
+ this.frameIndex(index < len - 1 ? index + 1 : 0);
94
188
  }
95
189
  }
96
190
  Sprite.prototype.className = 'Sprite';
97
191
  _registerNode(Sprite);
192
+ // add getters setters
98
193
  Factory.addGetterSetter(Sprite, 'animation');
194
+ /**
195
+ * get/set animation key
196
+ * @name Konva.Sprite#animation
197
+ * @method
198
+ * @param {String} anim animation key
199
+ * @returns {String}
200
+ * @example
201
+ * // get animation key
202
+ * var animation = sprite.animation();
203
+ *
204
+ * // set animation key
205
+ * sprite.animation('kicking');
206
+ */
99
207
  Factory.addGetterSetter(Sprite, 'animations');
208
+ /**
209
+ * get/set animations map
210
+ * @name Konva.Sprite#animations
211
+ * @method
212
+ * @param {Object} animations
213
+ * @returns {Object}
214
+ * @example
215
+ * // get animations map
216
+ * var animations = sprite.animations();
217
+ *
218
+ * // set animations map
219
+ * sprite.animations({
220
+ * standing: [
221
+ * // x, y, width, height (6 frames)
222
+ * 0, 0, 49, 109,
223
+ * 52, 0, 49, 109,
224
+ * 105, 0, 49, 109,
225
+ * 158, 0, 49, 109,
226
+ * 210, 0, 49, 109,
227
+ * 262, 0, 49, 109
228
+ * ],
229
+ * kicking: [
230
+ * // x, y, width, height (6 frames)
231
+ * 0, 109, 45, 98,
232
+ * 45, 109, 45, 98,
233
+ * 95, 109, 63, 98,
234
+ * 156, 109, 70, 98,
235
+ * 229, 109, 60, 98,
236
+ * 287, 109, 41, 98
237
+ * ]
238
+ * });
239
+ */
100
240
  Factory.addGetterSetter(Sprite, 'frameOffsets');
241
+ /**
242
+ * get/set frame offsets map
243
+ * @name Konva.Sprite#frameOffsets
244
+ * @method
245
+ * @param {Object} frameOffsets
246
+ * @returns {Object}
247
+ * @example
248
+ * // get frame offsets map
249
+ * var offsets = sprite.frameOffsets();
250
+ *
251
+ * // set frame offsets map
252
+ * sprite.frameOffsets({
253
+ * standing: [
254
+ * // x, y (6 frames)
255
+ * 0, 0,
256
+ * 0, 0,
257
+ * 5, 0,
258
+ * 0, 0,
259
+ * 0, 3,
260
+ * 2, 0
261
+ * ],
262
+ * kicking: [
263
+ * // x, y (6 frames)
264
+ * 0, 5,
265
+ * 5, 0,
266
+ * 10, 0,
267
+ * 0, 0,
268
+ * 2, 1,
269
+ * 0, 0
270
+ * ]
271
+ * });
272
+ */
101
273
  Factory.addGetterSetter(Sprite, 'image');
274
+ /**
275
+ * get/set image
276
+ * @name Konva.Sprite#image
277
+ * @method
278
+ * @param {Image} image
279
+ * @returns {Image}
280
+ * @example
281
+ * // get image
282
+ * var image = sprite.image();
283
+ *
284
+ * // set image
285
+ * sprite.image(imageObj);
286
+ */
102
287
  Factory.addGetterSetter(Sprite, 'frameIndex', 0, getNumberValidator());
288
+ /**
289
+ * set/set animation frame index
290
+ * @name Konva.Sprite#frameIndex
291
+ * @method
292
+ * @param {Integer} frameIndex
293
+ * @returns {Integer}
294
+ * @example
295
+ * // get animation frame index
296
+ * var frameIndex = sprite.frameIndex();
297
+ *
298
+ * // set animation frame index
299
+ * sprite.frameIndex(3);
300
+ */
103
301
  Factory.addGetterSetter(Sprite, 'frameRate', 17, getNumberValidator());
302
+ /**
303
+ * get/set frame rate in frames per second. Increase this number to make the sprite
304
+ * animation run faster, and decrease the number to make the sprite animation run slower
305
+ * The default is 17 frames per second
306
+ * @name Konva.Sprite#frameRate
307
+ * @method
308
+ * @param {Integer} frameRate
309
+ * @returns {Integer}
310
+ * @example
311
+ * // get frame rate
312
+ * var frameRate = sprite.frameRate();
313
+ *
314
+ * // set frame rate to 2 frames per second
315
+ * sprite.frameRate(2);
316
+ */
104
317
  Factory.backCompat(Sprite, {
105
318
  index: 'frameIndex',
106
319
  getIndex: 'getFrameIndex',
@@ -7,6 +7,29 @@ export interface StarConfig extends ShapeConfig {
7
7
  innerRadius: number;
8
8
  outerRadius: number;
9
9
  }
10
+ /**
11
+ * Star constructor
12
+ * @constructor
13
+ * @memberof Konva
14
+ * @augments Konva.Shape
15
+ * @param {Object} config
16
+ * @param {Integer} config.numPoints
17
+ * @param {Number} config.innerRadius
18
+ * @param {Number} config.outerRadius
19
+ * @@shapeParams
20
+ * @@nodeParams
21
+ * @example
22
+ * var star = new Konva.Star({
23
+ * x: 100,
24
+ * y: 200,
25
+ * numPoints: 5,
26
+ * innerRadius: 70,
27
+ * outerRadius: 70,
28
+ * fill: 'red',
29
+ * stroke: 'black',
30
+ * strokeWidth: 4
31
+ * });
32
+ */
10
33
  export declare class Star extends Shape<StarConfig> {
11
34
  _sceneFunc(context: Context): void;
12
35
  getWidth(): number;