1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | exports.Sprite = void 0;
|
---|
4 | const Factory_1 = require("../Factory");
|
---|
5 | const Shape_1 = require("../Shape");
|
---|
6 | const Animation_1 = require("../Animation");
|
---|
7 | const Validators_1 = require("../Validators");
|
---|
8 | const Global_1 = require("../Global");
|
---|
9 | class Sprite extends Shape_1.Shape {
|
---|
10 | constructor(config) {
|
---|
11 | super(config);
|
---|
12 | this._updated = true;
|
---|
13 | this.anim = new Animation_1.Animation(() => {
|
---|
14 | var updated = this._updated;
|
---|
15 | this._updated = false;
|
---|
16 | return updated;
|
---|
17 | });
|
---|
18 | this.on('animationChange.konva', function () {
|
---|
19 | this.frameIndex(0);
|
---|
20 | });
|
---|
21 | this.on('frameIndexChange.konva', function () {
|
---|
22 | this._updated = true;
|
---|
23 | });
|
---|
24 | this.on('frameRateChange.konva', function () {
|
---|
25 | if (!this.anim.isRunning()) {
|
---|
26 | return;
|
---|
27 | }
|
---|
28 | clearInterval(this.interval);
|
---|
29 | this._setInterval();
|
---|
30 | });
|
---|
31 | }
|
---|
32 | _sceneFunc(context) {
|
---|
33 | var 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();
|
---|
34 | if (this.hasFill() || this.hasStroke()) {
|
---|
35 | context.beginPath();
|
---|
36 | context.rect(0, 0, width, height);
|
---|
37 | context.closePath();
|
---|
38 | context.fillStrokeShape(this);
|
---|
39 | }
|
---|
40 | if (image) {
|
---|
41 | if (offsets) {
|
---|
42 | var offset = offsets[anim], ix2 = index * 2;
|
---|
43 | context.drawImage(image, x, y, width, height, offset[ix2 + 0], offset[ix2 + 1], width, height);
|
---|
44 | }
|
---|
45 | else {
|
---|
46 | context.drawImage(image, x, y, width, height, 0, 0, width, height);
|
---|
47 | }
|
---|
48 | }
|
---|
49 | }
|
---|
50 | _hitFunc(context) {
|
---|
51 | var anim = this.animation(), index = this.frameIndex(), ix4 = index * 4, set = this.animations()[anim], offsets = this.frameOffsets(), width = set[ix4 + 2], height = set[ix4 + 3];
|
---|
52 | context.beginPath();
|
---|
53 | if (offsets) {
|
---|
54 | var offset = offsets[anim];
|
---|
55 | var ix2 = index * 2;
|
---|
56 | context.rect(offset[ix2 + 0], offset[ix2 + 1], width, height);
|
---|
57 | }
|
---|
58 | else {
|
---|
59 | context.rect(0, 0, width, height);
|
---|
60 | }
|
---|
61 | context.closePath();
|
---|
62 | context.fillShape(this);
|
---|
63 | }
|
---|
64 | _useBufferCanvas() {
|
---|
65 | return super._useBufferCanvas(true);
|
---|
66 | }
|
---|
67 | _setInterval() {
|
---|
68 | var that = this;
|
---|
69 | this.interval = setInterval(function () {
|
---|
70 | that._updateIndex();
|
---|
71 | }, 1000 / this.frameRate());
|
---|
72 | }
|
---|
73 | start() {
|
---|
74 | if (this.isRunning()) {
|
---|
75 | return;
|
---|
76 | }
|
---|
77 | var layer = this.getLayer();
|
---|
78 | this.anim.setLayers(layer);
|
---|
79 | this._setInterval();
|
---|
80 | this.anim.start();
|
---|
81 | }
|
---|
82 | stop() {
|
---|
83 | this.anim.stop();
|
---|
84 | clearInterval(this.interval);
|
---|
85 | }
|
---|
86 | isRunning() {
|
---|
87 | return this.anim.isRunning();
|
---|
88 | }
|
---|
89 | _updateIndex() {
|
---|
90 | var index = this.frameIndex(), animation = this.animation(), animations = this.animations(), anim = animations[animation], len = anim.length / 4;
|
---|
91 | if (index < len - 1) {
|
---|
92 | this.frameIndex(index + 1);
|
---|
93 | }
|
---|
94 | else {
|
---|
95 | this.frameIndex(0);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 | exports.Sprite = Sprite;
|
---|
100 | Sprite.prototype.className = 'Sprite';
|
---|
101 | (0, Global_1._registerNode)(Sprite);
|
---|
102 | Factory_1.Factory.addGetterSetter(Sprite, 'animation');
|
---|
103 | Factory_1.Factory.addGetterSetter(Sprite, 'animations');
|
---|
104 | Factory_1.Factory.addGetterSetter(Sprite, 'frameOffsets');
|
---|
105 | Factory_1.Factory.addGetterSetter(Sprite, 'image');
|
---|
106 | Factory_1.Factory.addGetterSetter(Sprite, 'frameIndex', 0, (0, Validators_1.getNumberValidator)());
|
---|
107 | Factory_1.Factory.addGetterSetter(Sprite, 'frameRate', 17, (0, Validators_1.getNumberValidator)());
|
---|
108 | Factory_1.Factory.backCompat(Sprite, {
|
---|
109 | index: 'frameIndex',
|
---|
110 | getIndex: 'getFrameIndex',
|
---|
111 | setIndex: 'setFrameIndex',
|
---|
112 | });
|
---|