2021-04-30 09:24:27 -05:00
|
|
|
import { assert } from 'chai';
|
|
|
|
|
2025-08-20 09:39:20 +12:00
|
|
|
import { addStage, Konva } from './test-utils.ts';
|
2021-04-30 09:24:27 -05:00
|
|
|
|
|
|
|
describe('Animation', function () {
|
2017-02-24 09:15:33 -05:00
|
|
|
// ======================================================
|
2021-04-30 09:24:27 -05:00
|
|
|
it('test start and stop', function () {
|
2017-02-24 09:15:33 -05:00
|
|
|
var stage = addStage();
|
|
|
|
var layer = new Konva.Layer();
|
|
|
|
var rect = new Konva.Rect({
|
|
|
|
x: 200,
|
|
|
|
y: 100,
|
|
|
|
width: 100,
|
|
|
|
height: 50,
|
|
|
|
fill: 'green',
|
|
|
|
stroke: 'black',
|
2020-05-08 09:59:35 -05:00
|
|
|
strokeWidth: 4,
|
2013-09-11 12:02:03 -07:00
|
|
|
});
|
|
|
|
|
2017-02-24 09:15:33 -05:00
|
|
|
layer.add(rect);
|
|
|
|
stage.add(layer);
|
|
|
|
|
|
|
|
var amplitude = 150;
|
|
|
|
var period = 1000;
|
|
|
|
// in ms
|
2021-04-30 09:24:27 -05:00
|
|
|
var centerX = stage.width() / 2 - 100 / 2;
|
2017-02-24 09:15:33 -05:00
|
|
|
|
2020-05-08 09:59:35 -05:00
|
|
|
var anim = new Konva.Animation(function (frame) {
|
2021-04-30 09:24:27 -05:00
|
|
|
rect.x(
|
2020-05-08 09:59:35 -05:00
|
|
|
amplitude * Math.sin((frame.time * 2 * Math.PI) / period) + centerX
|
2017-07-28 18:40:07 +02:00
|
|
|
);
|
|
|
|
}, layer);
|
2017-02-24 09:15:33 -05:00
|
|
|
var a = Konva.Animation.animations;
|
|
|
|
var startLen = a.length;
|
|
|
|
|
|
|
|
assert.equal(a.length, startLen, '1should be no animations running');
|
|
|
|
|
|
|
|
anim.start();
|
|
|
|
assert.equal(a.length, startLen + 1, '2should be 1 animation running');
|
|
|
|
|
|
|
|
anim.stop();
|
|
|
|
assert.equal(a.length, startLen, '3should be no animations running');
|
|
|
|
|
|
|
|
anim.start();
|
|
|
|
assert.equal(a.length, startLen + 1, '4should be 1 animation running');
|
|
|
|
|
|
|
|
anim.start();
|
|
|
|
assert.equal(a.length, startLen + 1, '5should be 1 animation runningg');
|
|
|
|
|
|
|
|
anim.stop();
|
|
|
|
assert.equal(a.length, startLen, '6should be no animations running');
|
|
|
|
|
|
|
|
anim.stop();
|
|
|
|
assert.equal(a.length, startLen, '7should be no animations running');
|
|
|
|
});
|
|
|
|
|
|
|
|
// ======================================================
|
2021-04-30 09:24:27 -05:00
|
|
|
it('layer batch draw', function () {
|
2017-02-24 09:15:33 -05:00
|
|
|
var stage = addStage();
|
|
|
|
var layer = new Konva.Layer();
|
|
|
|
var rect = new Konva.Rect({
|
|
|
|
x: 200,
|
|
|
|
y: 100,
|
|
|
|
width: 100,
|
|
|
|
height: 50,
|
|
|
|
fill: 'green',
|
|
|
|
stroke: 'black',
|
2020-05-08 09:59:35 -05:00
|
|
|
strokeWidth: 4,
|
2013-09-11 12:02:03 -07:00
|
|
|
});
|
|
|
|
|
2017-02-24 09:15:33 -05:00
|
|
|
layer.add(rect);
|
|
|
|
stage.add(layer);
|
2013-07-21 23:41:05 -07:00
|
|
|
|
2021-04-30 09:24:27 -05:00
|
|
|
var draws = 0;
|
2013-07-21 23:41:05 -07:00
|
|
|
|
2020-05-08 09:59:35 -05:00
|
|
|
layer.on('draw', function () {
|
2017-02-24 09:15:33 -05:00
|
|
|
//console.log('draw')
|
|
|
|
draws++;
|
|
|
|
});
|
|
|
|
|
|
|
|
layer.draw();
|
|
|
|
layer.draw();
|
|
|
|
layer.draw();
|
|
|
|
|
|
|
|
assert.equal(draws, 3, 'draw count should be 3');
|
|
|
|
|
|
|
|
layer.batchDraw();
|
|
|
|
layer.batchDraw();
|
|
|
|
layer.batchDraw();
|
|
|
|
|
|
|
|
assert.notEqual(draws, 6, 'should not be 6 draws');
|
|
|
|
});
|
|
|
|
|
|
|
|
// ======================================================
|
2021-04-30 09:24:27 -05:00
|
|
|
it('stage batch draw', function () {
|
2017-02-24 09:15:33 -05:00
|
|
|
var stage = addStage();
|
|
|
|
var layer = new Konva.Layer();
|
|
|
|
var rect = new Konva.Rect({
|
|
|
|
x: 200,
|
|
|
|
y: 100,
|
|
|
|
width: 100,
|
|
|
|
height: 50,
|
|
|
|
fill: 'green',
|
|
|
|
stroke: 'black',
|
2020-05-08 09:59:35 -05:00
|
|
|
strokeWidth: 4,
|
2017-02-24 09:15:33 -05:00
|
|
|
});
|
2013-07-21 23:41:05 -07:00
|
|
|
|
2017-02-24 09:15:33 -05:00
|
|
|
layer.add(rect);
|
|
|
|
stage.add(layer);
|
2013-07-21 23:41:05 -07:00
|
|
|
|
2021-04-30 09:24:27 -05:00
|
|
|
var draws = 0;
|
2013-07-21 23:41:05 -07:00
|
|
|
|
2020-05-08 09:59:35 -05:00
|
|
|
layer.on('draw', function () {
|
2017-02-24 09:15:33 -05:00
|
|
|
//console.log('draw')
|
|
|
|
draws++;
|
|
|
|
});
|
2013-07-21 23:41:05 -07:00
|
|
|
|
2017-02-24 09:15:33 -05:00
|
|
|
stage.draw();
|
|
|
|
stage.draw();
|
|
|
|
stage.draw();
|
2013-07-21 23:41:05 -07:00
|
|
|
|
2017-02-24 09:15:33 -05:00
|
|
|
assert.equal(draws, 3, 'draw count should be 3');
|
2013-07-21 23:41:05 -07:00
|
|
|
|
2017-02-24 09:15:33 -05:00
|
|
|
stage.batchDraw();
|
|
|
|
stage.batchDraw();
|
|
|
|
stage.batchDraw();
|
|
|
|
|
|
|
|
assert.notEqual(draws, 6, 'should not be 6 draws');
|
|
|
|
});
|
|
|
|
});
|