mirror of
https://github.com/konvajs/konva.git
synced 2025-09-18 09:50:05 +08:00
further optimized the path parser algo
This commit is contained in:
50
dist/kinetic-core.js
vendored
50
dist/kinetic-core.js
vendored
@@ -4130,44 +4130,38 @@ Kinetic.Path.prototype = {
|
||||
* rendering
|
||||
*/
|
||||
getCommandsArray: function() {
|
||||
var c = this.attrs.commands;
|
||||
// command string
|
||||
var cs = this.attrs.commands;
|
||||
// command chars
|
||||
var cc = ['M', 'l', 'L', 'v', 'V', 'h', 'H', 'z'];
|
||||
// remove white spaces
|
||||
c = c.replace(new RegExp(' ', 'g'), '');
|
||||
cs = cs.replace(new RegExp(' ', 'g'), '');
|
||||
for(var n = 0; n < cc.length; n++) {
|
||||
c = c.replace(new RegExp(cc[n], 'g'), '|' + cc[n]);
|
||||
cs = cs.replace(new RegExp(cc[n], 'g'), '|' + cc[n]);
|
||||
}
|
||||
var arr = c.split('|');
|
||||
// create array
|
||||
var arr = cs.split('|');
|
||||
var ca = [];
|
||||
// init context point
|
||||
var cpx = 0;
|
||||
var cpy = 0;
|
||||
for(var n = 1; n < arr.length; n++) {
|
||||
var str = arr[n];
|
||||
var command = str.charAt(0);
|
||||
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'), ',-');
|
||||
var points = str.split(',');
|
||||
if(points.length > 0 && points[0] === '') {
|
||||
points.shift();
|
||||
var p = str.split(',');
|
||||
if(p.length > 0 && p[0] === '') {
|
||||
p.shift();
|
||||
}
|
||||
// convert strings to floats
|
||||
for(var i = 0; i < points.length; i++) {
|
||||
points[i] = parseFloat(points[i]);
|
||||
for(var i = 0; i < p.length; i++) {
|
||||
p[i] = parseFloat(p[i]);
|
||||
}
|
||||
ca.push({
|
||||
command: command,
|
||||
points: points
|
||||
});
|
||||
}
|
||||
// convert l, H, h, V, and v to L
|
||||
var cpx = 0;
|
||||
var cpy = 0;
|
||||
for(var n = 0; n < ca.length; n++) {
|
||||
var c = ca[n].command;
|
||||
var p = ca[n].points;
|
||||
// update context point
|
||||
// convert l, H, h, V, and v to L
|
||||
switch(c) {
|
||||
case 'M':
|
||||
cpx = p[0];
|
||||
@@ -4194,13 +4188,16 @@ Kinetic.Path.prototype = {
|
||||
cpy = p[0];
|
||||
break;
|
||||
}
|
||||
|
||||
// reassign command
|
||||
if(c == 'l' || c == 'V' || c == 'v' || c == 'H' || c == 'h') {
|
||||
ca[n].command = 'L';
|
||||
ca[n].points[0] = cpx;
|
||||
ca[n].points[1] = cpy;
|
||||
c = 'L';
|
||||
p[0] = cpx;
|
||||
p[1] = cpy;
|
||||
}
|
||||
ca.push({
|
||||
command: c,
|
||||
points: p
|
||||
});
|
||||
}
|
||||
return ca;
|
||||
},
|
||||
@@ -4215,6 +4212,7 @@ Kinetic.Path.prototype = {
|
||||
* also automatically parses the commands string
|
||||
* into a commands array. Currently supported SVG commands:
|
||||
* M, L, l, H, h, V, v, z
|
||||
* @param {String} SVG path command string
|
||||
*/
|
||||
setCommands: function(commands) {
|
||||
this.attrs.commands = commands;
|
||||
|
2
dist/kinetic-core.min.js
vendored
2
dist/kinetic-core.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -53,44 +53,38 @@ Kinetic.Path.prototype = {
|
||||
* rendering
|
||||
*/
|
||||
getCommandsArray: function() {
|
||||
var c = this.attrs.commands;
|
||||
// command string
|
||||
var cs = this.attrs.commands;
|
||||
// command chars
|
||||
var cc = ['M', 'l', 'L', 'v', 'V', 'h', 'H', 'z'];
|
||||
// remove white spaces
|
||||
c = c.replace(new RegExp(' ', 'g'), '');
|
||||
cs = cs.replace(new RegExp(' ', 'g'), '');
|
||||
for(var n = 0; n < cc.length; n++) {
|
||||
c = c.replace(new RegExp(cc[n], 'g'), '|' + cc[n]);
|
||||
cs = cs.replace(new RegExp(cc[n], 'g'), '|' + cc[n]);
|
||||
}
|
||||
var arr = c.split('|');
|
||||
// create array
|
||||
var arr = cs.split('|');
|
||||
var ca = [];
|
||||
// init context point
|
||||
var cpx = 0;
|
||||
var cpy = 0;
|
||||
for(var n = 1; n < arr.length; n++) {
|
||||
var str = arr[n];
|
||||
var command = str.charAt(0);
|
||||
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'), ',-');
|
||||
var points = str.split(',');
|
||||
if(points.length > 0 && points[0] === '') {
|
||||
points.shift();
|
||||
var p = str.split(',');
|
||||
if(p.length > 0 && p[0] === '') {
|
||||
p.shift();
|
||||
}
|
||||
// convert strings to floats
|
||||
for(var i = 0; i < points.length; i++) {
|
||||
points[i] = parseFloat(points[i]);
|
||||
for(var i = 0; i < p.length; i++) {
|
||||
p[i] = parseFloat(p[i]);
|
||||
}
|
||||
ca.push({
|
||||
command: command,
|
||||
points: points
|
||||
});
|
||||
}
|
||||
// convert l, H, h, V, and v to L
|
||||
var cpx = 0;
|
||||
var cpy = 0;
|
||||
for(var n = 0; n < ca.length; n++) {
|
||||
var c = ca[n].command;
|
||||
var p = ca[n].points;
|
||||
// update context point
|
||||
// convert l, H, h, V, and v to L
|
||||
switch(c) {
|
||||
case 'M':
|
||||
cpx = p[0];
|
||||
@@ -117,13 +111,16 @@ Kinetic.Path.prototype = {
|
||||
cpy = p[0];
|
||||
break;
|
||||
}
|
||||
|
||||
// reassign command
|
||||
if(c == 'l' || c == 'V' || c == 'v' || c == 'H' || c == 'h') {
|
||||
ca[n].command = 'L';
|
||||
ca[n].points[0] = cpx;
|
||||
ca[n].points[1] = cpy;
|
||||
c = 'L';
|
||||
p[0] = cpx;
|
||||
p[1] = cpy;
|
||||
}
|
||||
ca.push({
|
||||
command: c,
|
||||
points: p
|
||||
});
|
||||
}
|
||||
return ca;
|
||||
},
|
||||
@@ -138,6 +135,7 @@ Kinetic.Path.prototype = {
|
||||
* also automatically parses the commands string
|
||||
* into a commands array. Currently supported SVG commands:
|
||||
* M, L, l, H, h, V, v, z
|
||||
* @param {String} SVG path command string
|
||||
*/
|
||||
setCommands: function(commands) {
|
||||
this.attrs.commands = commands;
|
||||
|
Reference in New Issue
Block a user