mirror of
https://github.com/konvajs/konva.git
synced 2025-06-28 05:36:38 +08:00
add tests
This commit is contained in:
parent
10b21689ba
commit
9645c5c063
4
konva.min.js
vendored
4
konva.min.js
vendored
File diff suppressed because one or more lines are too long
@ -1522,7 +1522,7 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
|
||||
* @name Konva.Node#getStage
|
||||
* @returns {Konva.Stage}
|
||||
*/
|
||||
getStage(): Stage | undefined {
|
||||
getStage(): any {
|
||||
return this._getCache(STAGE, this._getStage);
|
||||
}
|
||||
|
||||
|
@ -391,8 +391,8 @@ export class Shape<Config extends ShapeConfig = ShapeConfig> extends Node<
|
||||
// why do we need buffer canvas?
|
||||
// it give better result when a shape has
|
||||
// stroke with fill and with some opacity
|
||||
_useBufferCanvas(caching) {
|
||||
return (
|
||||
_useBufferCanvas(caching): boolean {
|
||||
return !!(
|
||||
(!caching || this.hasShadow()) &&
|
||||
this.perfectDrawEnabled() &&
|
||||
this.getAbsoluteOpacity() !== 1 &&
|
||||
@ -708,8 +708,8 @@ export class Shape<Config extends ShapeConfig = ShapeConfig> extends Node<
|
||||
return this;
|
||||
}
|
||||
|
||||
hasPointerCapture(pointerId: number) {
|
||||
PointerEvents.releaseCapture(pointerId, this);
|
||||
hasPointerCapture(pointerId: number): boolean {
|
||||
return PointerEvents.hasPointerCapture(pointerId, this);
|
||||
}
|
||||
|
||||
setPointerCapture(pointerId: number) {
|
||||
|
@ -52,6 +52,9 @@ var STAGE = 'Stage',
|
||||
CONTENT_DBL_TAP = 'contentDbltap',
|
||||
CONTENT_TAP = 'contentTap',
|
||||
CONTENT_TOUCHMOVE = 'contentTouchmove',
|
||||
CONTENT_POINTERMOVE = 'contentPointermove',
|
||||
CONTENT_POINTERDOWN = 'contentPointerdown',
|
||||
CONTENT_POINTERUP = 'contentPointerup',
|
||||
CONTENT_WHEEL = 'contentWheel',
|
||||
RELATIVE = 'relative',
|
||||
KONVA_CONTENT = 'konvajs-content',
|
||||
@ -390,14 +393,10 @@ export class Stage extends Container {
|
||||
addEvent(this, EVENTS[n]);
|
||||
}
|
||||
}
|
||||
<<<<<<< HEAD
|
||||
_mouseenter(evt) {
|
||||
this.setPointersPositions(evt);
|
||||
this._fire(MOUSEENTER, { evt: evt, target: this, currentTarget: this });
|
||||
}
|
||||
=======
|
||||
|
||||
>>>>>>> WIP
|
||||
_mouseover(evt) {
|
||||
this.setPointersPositions(evt);
|
||||
this._fire(CONTENT_MOUSEOVER, { evt: evt });
|
||||
|
@ -36,7 +36,7 @@ export interface ImageConfig extends ShapeConfig {
|
||||
*/
|
||||
export class Image extends Shape<ImageConfig> {
|
||||
_useBufferCanvas() {
|
||||
return (
|
||||
return !!(
|
||||
(this.hasShadow() || this.getAbsoluteOpacity() !== 1) &&
|
||||
this.hasStroke() &&
|
||||
this.getStage()
|
||||
|
@ -150,11 +150,13 @@ export class Sprite extends Shape<SpriteConfig> {
|
||||
context.closePath();
|
||||
context.fillShape(this);
|
||||
}
|
||||
|
||||
_useBufferCanvas() {
|
||||
return (
|
||||
(this.hasShadow() || this.getAbsoluteOpacity() !== 1) && this.hasStroke()
|
||||
);
|
||||
}
|
||||
|
||||
_setInterval() {
|
||||
var that = this;
|
||||
this.interval = setInterval(function() {
|
||||
|
234
test/functional/PointerEvents-test.js
Normal file
234
test/functional/PointerEvents-test.js
Normal file
@ -0,0 +1,234 @@
|
||||
/* eslint-disable max-nested-callbacks */
|
||||
suite('PointerEvents', function() {
|
||||
// ======================================================
|
||||
test('pointerdown pointerup pointermove', function(done) {
|
||||
var stage = addStage();
|
||||
var layer = new Konva.Layer();
|
||||
var circle = new Konva.Circle({
|
||||
x: stage.getWidth() / 2,
|
||||
y: stage.getHeight() / 2,
|
||||
radius: 70,
|
||||
fill: 'red',
|
||||
stroke: 'black',
|
||||
strokeWidth: 4
|
||||
});
|
||||
|
||||
// mobile events
|
||||
var pointerdown = false;
|
||||
var pointerup = false;
|
||||
var pointermove = false;
|
||||
|
||||
/*
|
||||
* mobile
|
||||
*/
|
||||
circle.on('pointerdown', function() {
|
||||
pointerdown = true;
|
||||
});
|
||||
|
||||
circle.on('pointerup', function() {
|
||||
pointerup = true;
|
||||
});
|
||||
|
||||
circle.on('pointermove', function() {
|
||||
pointermove = true;
|
||||
});
|
||||
|
||||
layer.add(circle);
|
||||
stage.add(layer);
|
||||
|
||||
var top = stage.content.getBoundingClientRect().top;
|
||||
|
||||
// touchstart circle
|
||||
stage._pointerdown({
|
||||
clientX: 289,
|
||||
clientY: 100 + top,
|
||||
pointerId: 1,
|
||||
preventDefault: function() {}
|
||||
});
|
||||
|
||||
assert(pointerdown, '1) pointerdown should be true');
|
||||
assert(!pointermove, '1) pointermove should be false');
|
||||
assert(!pointerup, '1) pointerup should be false');
|
||||
|
||||
// pointerup circle
|
||||
stage._pointerup({
|
||||
touches: [],
|
||||
preventDefault: function() {}
|
||||
});
|
||||
|
||||
assert(pointerdown, '2) pointerdown should be true');
|
||||
assert(!pointermove, '2) pointermove should be false');
|
||||
assert(pointerup, '2) pointerup should be true');
|
||||
|
||||
// pointerdown circle
|
||||
stage._pointerdown({
|
||||
touches: [
|
||||
{
|
||||
clientX: 289,
|
||||
clientY: 100 + top
|
||||
}
|
||||
],
|
||||
preventDefault: function() {}
|
||||
});
|
||||
|
||||
assert(pointerdown, '3) pointerdown should be true');
|
||||
assert(!pointermove, '3) pointermove should be false');
|
||||
assert(pointerup, '3) pointerup should be true');
|
||||
|
||||
// pointerup circle to triger dbltap
|
||||
stage._pointerup({
|
||||
touches: [],
|
||||
preventDefault: function() {}
|
||||
});
|
||||
// end drag is tied to document mouseup and pointerup event
|
||||
// which can't be simulated. call _endDrag manually
|
||||
//Konva.DD._endDrag();
|
||||
|
||||
assert(pointerdown, '4) pointerdown should be true');
|
||||
assert(!pointermove, '4) pointermove should be false');
|
||||
assert(pointerup, '4) pointerup should be true');
|
||||
|
||||
setTimeout(function() {
|
||||
// pointermove circle
|
||||
stage._pointermove({
|
||||
touches: [
|
||||
{
|
||||
clientX: 290,
|
||||
clientY: 100 + top
|
||||
}
|
||||
],
|
||||
preventDefault: function() {}
|
||||
});
|
||||
|
||||
assert(pointerdown, '5) pointerdown should be true');
|
||||
assert(pointermove, '5) pointermove should be true');
|
||||
assert(pointerup, '5) pointerup should be true');
|
||||
|
||||
done();
|
||||
}, 17);
|
||||
});
|
||||
|
||||
// ======================================================
|
||||
test('pointer capture', function(done) {
|
||||
var stage = addStage();
|
||||
var layer = new Konva.Layer();
|
||||
var circle = new Konva.Circle({
|
||||
x: stage.getWidth() / 2,
|
||||
y: stage.getHeight() / 2,
|
||||
radius: 70,
|
||||
fill: 'red',
|
||||
stroke: 'black',
|
||||
strokeWidth: 4
|
||||
});
|
||||
|
||||
var circle2 = new Konva.Circle({
|
||||
x: stage.getWidth() / 2,
|
||||
y: 20,
|
||||
radius: 20,
|
||||
fill: 'red',
|
||||
stroke: 'black',
|
||||
strokeWidth: 4
|
||||
});
|
||||
|
||||
// mobile events
|
||||
var downCount = 0;
|
||||
var otherDownCount = 0;
|
||||
|
||||
var pointerup = false;
|
||||
var pointermove = false;
|
||||
|
||||
circle2.on('pointerdown', function() {
|
||||
otherDownCount++;
|
||||
});
|
||||
|
||||
circle.on('pointerdown', function(event) {
|
||||
downCount++;
|
||||
this.setPointerCapture(event.pointerId);
|
||||
});
|
||||
|
||||
circle.on('pointerup', function(evt) {
|
||||
assert(this.hasPointerCapture(evt.pointerId), 'circle released capture');
|
||||
pointerup = true;
|
||||
});
|
||||
|
||||
circle.on('pointermove', function(evt) {
|
||||
assert(this.hasPointerCapture(evt.pointerId), 'circle has capture');
|
||||
pointermove = true;
|
||||
});
|
||||
|
||||
layer.add(circle);
|
||||
layer.add(circle2);
|
||||
stage.add(layer);
|
||||
|
||||
var top = stage.content.getBoundingClientRect().top;
|
||||
|
||||
// on circle 2 to confirm it works
|
||||
stage._pointerdown({
|
||||
clientX: 289,
|
||||
clientY: 10 + top,
|
||||
pointerId: 0,
|
||||
preventDefault: function() {}
|
||||
});
|
||||
|
||||
assert(otherDownCount === 1, '6) otherDownCount should be 1');
|
||||
assert(downCount === 0, '6) downCount should be 0');
|
||||
assert(!pointermove, '6) pointermove should be false');
|
||||
assert(!pointerup, '6) pointerup should be false');
|
||||
|
||||
// on circle with capture
|
||||
stage._pointerdown({
|
||||
clientX: 289,
|
||||
clientY: 100 + top,
|
||||
pointerId: 1,
|
||||
preventDefault: function() {}
|
||||
});
|
||||
|
||||
assert(otherDownCount === 1, '7) otherDownCount should be 1');
|
||||
assert(downCount === 1, '7) downCount should be 1');
|
||||
assert(!pointermove, '7) pointermove should be false');
|
||||
assert(!pointerup, '7) pointerup should be true');
|
||||
|
||||
// second pointerdown
|
||||
stage._pointerdown({
|
||||
clientX: 289,
|
||||
clientY: 10 + top,
|
||||
pointerId: 1,
|
||||
preventDefault: function() {}
|
||||
});
|
||||
|
||||
assert(otherDownCount === 1, '8) otherDownCount should be 1');
|
||||
assert(downCount === 2, '8) pointerdown should be 2');
|
||||
assert(!pointermove, '8) pointermove should be false');
|
||||
assert(!pointerup, '8) pointerup should be true');
|
||||
|
||||
setTimeout(function() {
|
||||
// pointermove over circle 2
|
||||
stage._pointermove({
|
||||
clientX: 290,
|
||||
clientY: 10 + top,
|
||||
pointerId: 1,
|
||||
preventDefault: function() {}
|
||||
});
|
||||
|
||||
assert(otherDownCount === 1, '9) otherDownCount should be 1');
|
||||
assert(pointermove, '9) pointermove should be true');
|
||||
|
||||
stage._pointerup({
|
||||
pointerId: 1,
|
||||
preventDefault: function() {}
|
||||
});
|
||||
|
||||
stage._pointerdown({
|
||||
clientX: 289,
|
||||
clientY: 10 + top,
|
||||
pointerId: 1,
|
||||
preventDefault: function() {}
|
||||
});
|
||||
|
||||
assert(otherDownCount === 2, '10) otherDownCount should be 1');
|
||||
assert(pointerup, '10) pointerup should be true');
|
||||
|
||||
done();
|
||||
}, 17);
|
||||
});
|
||||
});
|
@ -177,6 +177,7 @@
|
||||
|
||||
<script src="functional/MouseEvents-test.js"></script>
|
||||
<script src="functional/TouchEvents-test.js"></script>
|
||||
<script src="functional/PointerEvents-test.js"></script>
|
||||
<script src="functional/DragAndDropEvents-test.js"></script>
|
||||
|
||||
<!--=============== manual tests ================-->
|
||||
@ -188,4 +189,4 @@
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
</html>
|
||||
|
@ -329,4 +329,44 @@ Konva.Stage.prototype.simulateTouchEnd = function(pos) {
|
||||
Konva.DD._endDragAfter(evt);
|
||||
};
|
||||
|
||||
Konva.Stage.prototype.simulatePointerDown = function(pos) {
|
||||
var top = this.content.getBoundingClientRect().top;
|
||||
|
||||
this._mousedown({
|
||||
clientX: pos.x,
|
||||
clientY: pos.y + top,
|
||||
button: pos.button || 0,
|
||||
pointerId: pos.pointerId || 1
|
||||
});
|
||||
};
|
||||
|
||||
Konva.Stage.prototype.simulatePointerMove = function(pos) {
|
||||
var top = this.content.getBoundingClientRect().top;
|
||||
|
||||
var evt = {
|
||||
clientX: pos.x,
|
||||
clientY: pos.y + top,
|
||||
button: pos.button || 0,
|
||||
pointerId: pos.pointerId || 1
|
||||
};
|
||||
|
||||
this._mousemove(evt);
|
||||
Konva.DD._drag(evt);
|
||||
};
|
||||
|
||||
Konva.Stage.prototype.simulatePointerUp = function(pos) {
|
||||
var top = this.content.getBoundingClientRect().top;
|
||||
|
||||
var evt = {
|
||||
clientX: pos.x,
|
||||
clientY: pos.y + top,
|
||||
button: pos.button || 0,
|
||||
pointerId: pos.pointerId || 1
|
||||
};
|
||||
|
||||
Konva.DD._endDragBefore(evt);
|
||||
this._mouseup(evt);
|
||||
Konva.DD._endDragAfter(evt);
|
||||
};
|
||||
|
||||
init();
|
||||
|
Loading…
Reference in New Issue
Block a user