fix path data parsing. close #329

This commit is contained in:
Anton Lavrenov 2018-03-10 14:41:41 +07:00
parent 2944158479
commit cf60dd8777
4 changed files with 137 additions and 96 deletions

View File

@ -22,6 +22,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
* Some typescript fixes
* Pixelate filter fixes
* Fixes for path data parsing
## Removed

View File

@ -114,10 +114,10 @@
// Node extenders
/**
* initiate drag and drop
* @method
* @memberof Konva.Node.prototype
*/
* initiate drag and drop
* @method
* @memberof Konva.Node.prototype
*/
Konva.Node.prototype.startDrag = function() {
var dd = Konva.DD,
stage = this.getStage(),
@ -170,10 +170,10 @@
};
/**
* stop drag and drop
* @method
* @memberof Konva.Node.prototype
*/
* stop drag and drop
* @method
* @memberof Konva.Node.prototype
*/
Konva.Node.prototype.stopDrag = function() {
var dd = Konva.DD,
evt = {};
@ -201,10 +201,10 @@
};
/**
* determine if node is currently in drag and drop mode
* @method
* @memberof Konva.Node.prototype
*/
* determine if node is currently in drag and drop mode
* @method
* @memberof Konva.Node.prototype
*/
Konva.Node.prototype.isDragging = function() {
var dd = Konva.DD;
return !!(dd.node && dd.node._id === this._id && dd.isDragging);
@ -273,46 +273,48 @@
Konva.Factory.addGetterSetter(Konva.Node, 'dragBoundFunc');
/**
* get/set drag bound function. This is used to override the default
* drag and drop position
* @name dragBoundFunc
* @method
* @memberof Konva.Node.prototype
* @param {Function} dragBoundFunc
* @returns {Function}
* @example
* // get drag bound function
* var dragBoundFunc = node.dragBoundFunc();
*
* // create vertical drag and drop
* node.dragBoundFunc(function(pos){
* return {
* x: this.getAbsolutePosition().x,
* y: pos.y
* };
* });
*/
* get/set drag bound function. This is used to override the default
* drag and drop position.
* @name dragBoundFunc
* @method
* @memberof Konva.Node.prototype
* @param {Function} dragBoundFunc
* @returns {Function}
* @example
* // get drag bound function
* var dragBoundFunc = node.dragBoundFunc();
*
* // create vertical drag and drop
* node.dragBoundFunc(function(pos){
* // important pos - is absolute position of the node
* // you should return absolute position too
* return {
* x: this.getAbsolutePosition().x,
* y: pos.y
* };
* });
*/
Konva.Factory.addGetter(Konva.Node, 'draggable', false);
Konva.Factory.addOverloadedGetterSetter(Konva.Node, 'draggable');
/**
* get/set draggable flag
* @name draggable
* @method
* @memberof Konva.Node.prototype
* @param {Boolean} draggable
* @returns {Boolean}
* @example
* // get draggable flag
* var draggable = node.draggable();
*
* // enable drag and drop
* node.draggable(true);
*
* // disable drag and drop
* node.draggable(false);
*/
* get/set draggable flag
* @name draggable
* @method
* @memberof Konva.Node.prototype
* @param {Boolean} draggable
* @returns {Boolean}
* @example
* // get draggable flag
* var draggable = node.draggable();
*
* // enable drag and drop
* node.draggable(true);
*
* // disable drag and drop
* node.draggable(false);
*/
if (Konva.isBrowser) {
var html = Konva.document.documentElement;

View File

@ -2,24 +2,24 @@
(function() {
'use strict';
/**
* Path constructor.
* @author Jason Follas
* @constructor
* @memberof Konva
* @augments Konva.Shape
* @param {Object} config
* @param {String} config.data SVG data string
* @@shapeParams
* @@nodeParams
* @example
* var path = new Konva.Path({
* x: 240,
* y: 40,
* data: 'M12.582,9.551C3.251,16.237,0.921,29.021,7.08,38.564l-2.36,1.689l4.893,2.262l4.893,2.262l-0.568-5.36l-0.567-5.359l-2.365,1.694c-4.657-7.375-2.83-17.185,4.352-22.33c7.451-5.338,17.817-3.625,23.156,3.824c5.337,7.449,3.625,17.813-3.821,23.152l2.857,3.988c9.617-6.893,11.827-20.277,4.935-29.896C35.591,4.87,22.204,2.658,12.582,9.551z',
* fill: 'green',
* scale: 2
* });
*/
* Path constructor.
* @author Jason Follas
* @constructor
* @memberof Konva
* @augments Konva.Shape
* @param {Object} config
* @param {String} config.data SVG data string
* @@shapeParams
* @@nodeParams
* @example
* var path = new Konva.Path({
* x: 240,
* y: 40,
* data: 'M12.582,9.551C3.251,16.237,0.921,29.021,7.08,38.564l-2.36,1.689l4.893,2.262l4.893,2.262l-0.568-5.36l-0.567-5.359l-2.365,1.694c-4.657-7.375-2.83-17.185,4.352-22.33c7.451-5.338,17.817-3.625,23.156,3.824c5.337,7.449,3.625,17.813-3.821,23.152l2.857,3.988c9.617-6.893,11.827-20.277,4.935-29.896C35.591,4.87,22.204,2.658,12.582,9.551z',
* fill: 'green',
* scale: 2
* });
*/
Konva.Path = function(config) {
this.___init(config);
};
@ -236,7 +236,8 @@
};
};
Konva.Path.getPointOnEllipticalArc = function(cx, cy, rx, ry, theta, psi) {
var cosPsi = Math.cos(psi), sinPsi = Math.sin(psi);
var cosPsi = Math.cos(psi),
sinPsi = Math.sin(psi);
var pt = {
x: rx * Math.cos(theta),
y: ry * Math.sin(theta)
@ -315,26 +316,35 @@
// create array
var arr = cs.split('|');
var ca = [];
var coords = [];
// init context point
var cpx = 0;
var cpy = 0;
var re = /([-+]?((\d+\.\d+)|((\d+)|(\.\d+)))(?:e[-+]?\d+)?)/gi;
var match;
for (n = 1; n < arr.length; n++) {
var str = arr[n];
var c = str.charAt(0);
str = str.slice(1);
// remove ,- for consistency
str = str.replace(new RegExp(',-', 'g'), '-');
// add commas so that it's easy to split
str = str.replace(new RegExp('-', 'g'), ',-');
str = str.replace(new RegExp('e,-', 'g'), 'e-');
var p = str.split(',');
if (p.length > 0 && p[0] === '') {
p.shift();
coords.length = 0;
while ((match = re.exec(str))) {
coords.push(match[0]);
}
// convert strings to floats
for (var i = 0; i < p.length; i++) {
p[i] = parseFloat(p[i]);
// while ((match = re.exec(str))) {
// coords.push(match[0]);
// }
var p = [];
for (var j = 0, jlen = coords.length; j < jlen; j++) {
var parsed = parseFloat(coords[j]);
if (!isNaN(parsed)) {
p.push(parsed);
}
}
while (p.length > 0) {
if (isNaN(p[0])) {
// case for a trailing comma before next command
@ -343,7 +353,8 @@
var cmd = null;
var points = [];
var startX = cpx, startY = cpy;
var startX = cpx,
startY = cpy;
// Move var from within the switch to up here (jshint)
var prevCmd, ctlPtx, ctlPty; // Ss, Tt
var rx, ry, psi, fa, fs, x1, y1; // Aa
@ -772,22 +783,22 @@
Konva.Factory.addGetterSetter(Konva.Path, 'data');
/**
* set SVG path data string. This method
* also automatically parses the data string
* into a data array. Currently supported SVG data:
* M, m, L, l, H, h, V, v, Q, q, T, t, C, c, S, s, A, a, Z, z
* @name setData
* @method
* @memberof Konva.Path.prototype
* @param {String} SVG path command string
*/
* set SVG path data string. This method
* also automatically parses the data string
* into a data array. Currently supported SVG data:
* M, m, L, l, H, h, V, v, Q, q, T, t, C, c, S, s, A, a, Z, z
* @name setData
* @method
* @memberof Konva.Path.prototype
* @param {String} SVG path command string
*/
/**
* get SVG path data string
* @name getData
* @method
* @memberof Konva.Path.prototype
*/
* get SVG path data string
* @name getData
* @method
* @memberof Konva.Path.prototype
*/
Konva.Collection.mapMethods(Konva.Path);
})();

File diff suppressed because one or more lines are too long