fix arrow with tension. close 197

This commit is contained in:
Anton Lavrenov 2018-05-04 15:56:08 +09:00
parent 2ac7e692f6
commit 76c20a27c3
6 changed files with 3423 additions and 11 deletions

View File

@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
* Fixed lineHeight behavior for `Konva.Text`
* Some performance optimizations for `Konva.Text`
* Better wrap algorithm for `Konva.Text`
* fixed `Konva.Arrow` with tension != 0
## [2.0.3][2018-04-21]

View File

@ -2,7 +2,7 @@
* Konva JavaScript Framework v2.0.3
* http://konvajs.github.io/
* Licensed under the MIT
* Date: Mon Apr 30 2018
* Date: Fri May 04 2018
*
* Original work Copyright (C) 2011 - 2013 by Eric Rowell (KineticJS)
* Modified work Copyright (C) 2014 - present by Anton Lavrenov (Konva)
@ -18494,9 +18494,24 @@
Konva.Line.prototype._sceneFunc.apply(this, arguments);
var PI2 = Math.PI * 2;
var points = this.points();
var tp = points;
var fromTension = this.getTension() !== 0 && points.length > 4;
if (fromTension) {
tp = this.getTensionPoints();
}
var n = points.length;
var dx = points[n - 2] - points[n - 4];
var dy = points[n - 1] - points[n - 3];
var dx, dy;
if (fromTension) {
dx = points[n - 2] - tp[n - 2];
dy = points[n - 1] - tp[n - 1];
} else {
dx = points[n - 2] - points[n - 4];
dy = points[n - 1] - points[n - 3];
}
var radians = (Math.atan2(dy, dx) + PI2) % PI2;
var length = this.pointerLength();
var width = this.pointerWidth();
@ -18514,8 +18529,14 @@
if (this.pointerAtBeginning()) {
ctx.save();
ctx.translate(points[0], points[1]);
if (fromTension) {
dx = tp[0] - points[0];
dy = tp[1] - points[1];
} else {
dx = points[2] - points[0];
dy = points[3] - points[1];
}
ctx.rotate((Math.atan2(-dy, -dx) + PI2) % PI2);
ctx.moveTo(0, 0);
ctx.lineTo(-length, width / 2);

4
konva.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -37,9 +37,24 @@
Konva.Line.prototype._sceneFunc.apply(this, arguments);
var PI2 = Math.PI * 2;
var points = this.points();
var tp = points;
var fromTension = this.getTension() !== 0 && points.length > 4;
if (fromTension) {
tp = this.getTensionPoints();
}
var n = points.length;
var dx = points[n - 2] - points[n - 4];
var dy = points[n - 1] - points[n - 3];
var dx, dy;
if (fromTension) {
dx = points[n - 2] - tp[n - 2];
dy = points[n - 1] - tp[n - 1];
} else {
dx = points[n - 2] - points[n - 4];
dy = points[n - 1] - points[n - 3];
}
var radians = (Math.atan2(dy, dx) + PI2) % PI2;
var length = this.pointerLength();
var width = this.pointerWidth();
@ -57,8 +72,14 @@
if (this.pointerAtBeginning()) {
ctx.save();
ctx.translate(points[0], points[1]);
if (fromTension) {
dx = tp[0] - points[0];
dy = tp[1] - points[1];
} else {
dx = points[2] - points[0];
dy = points[3] - points[1];
}
ctx.rotate((Math.atan2(-dy, -dx) + PI2) % PI2);
ctx.moveTo(0, 0);
ctx.lineTo(-length, width / 2);

View File

@ -62,4 +62,27 @@ suite('Arrow', function() {
'clearRect(0,0,578,200);save();transform(1,0,0,1,0,0);beginPath();moveTo(50,50);lineTo(100,100);setLineDash(5,5);lineDashOffset=0;lineWidth=5;strokeStyle=red;stroke();save();beginPath();translate(100,100);rotate(0.785);moveTo(0,0);lineTo(-20,10);lineTo(-20,-10);closePath();restore();setLineDash();fillStyle=blue;fill();lineWidth=5;strokeStyle=red;stroke();restore();'
);
});
test('direction with tension', function() {
var stage = addStage();
var layer = new Konva.Layer();
var arrow = new Konva.Arrow({
points: [50, 50, 100, 50, 100, 100],
stroke: 'red',
fill: 'red',
tension: 1,
pointerAtBeginning: true
});
layer.add(arrow);
stage.add(layer);
var trace = layer.getContext().getTrace();
assert.equal(
trace,
'clearRect(0,0,578,200);save();transform(1,0,0,1,0,0);beginPath();moveTo(50,50);quadraticCurveTo(75,25,100,50);quadraticCurveTo(125,75,100,100);lineWidth=2;strokeStyle=red;stroke();save();beginPath();translate(100,100);rotate(2.356);moveTo(0,0);lineTo(-10,5);lineTo(-10,-5);closePath();restore();save();translate(50,50);rotate(2.356);moveTo(0,0);lineTo(-10,5);lineTo(-10,-5);closePath();restore();setLineDash();fillStyle=red;fill();lineWidth=2;strokeStyle=red;stroke();restore();'
);
});
});

3346
yarn.lock Normal file

File diff suppressed because it is too large Load Diff