Merge pull request #347 from hbroer/ellipsis

Add Konva.Text.ellipsis
This commit is contained in:
Anton Lavrenov 2018-02-27 09:03:26 +08:00 committed by GitHub
commit ba14f47e0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 351 additions and 267 deletions

View File

@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
* Add ability to remove event by callback `node.off('event', callback)`.
* new `Konva.Filters.Contrast`.
* new `Konva.Util.haveIntersection()` to detect collusion
* add `Konva.Text.ellipsis` to add '…' to text string if width is fixed and wrap is set to 'none'
## Changed

2
konva.d.ts vendored
View File

@ -757,6 +757,7 @@ declare module Konva {
padding?: number;
lineHeight?: number;
wrap?: string;
ellipsis?: boolean;
}
class Text extends Shape {
@ -781,6 +782,7 @@ declare module Konva {
lineHeight(lineHeight: number): Text;
wrap(): string;
wrap(wrap: string): Text;
ellipsis(): boolean;
textDecoration(): string;
textDecoration(textDecoration: string): Text;
}

543
konva.js
View File

@ -2,7 +2,7 @@
* Konva JavaScript Framework v1.7.6
* http://konvajs.github.io/
* Licensed under the MIT
* Date: Thu Feb 22 2018
* Date: Mon Feb 26 2018
*
* Original work Copyright (C) 2011 - 2013 by Eric Rowell (KineticJS)
* Modified work Copyright (C) 2014 - present by Anton Lavrenov (Konva)
@ -4857,31 +4857,31 @@
Konva.Collection.mapMethods(Konva.Node);
})(Konva);
(function() {
'use strict';
/**
* Grayscale Filter
* @function
* @memberof Konva.Filters
* @param {Object} imageData
* @example
* node.cache();
* node.filters([Konva.Filters.Grayscale]);
*/
Konva.Filters.Grayscale = function(imageData) {
var data = imageData.data, len = data.length, i, brightness;
for (i = 0; i < len; i += 4) {
brightness = 0.34 * data[i] + 0.5 * data[i + 1] + 0.16 * data[i + 2];
// red
data[i] = brightness;
// green
data[i + 1] = brightness;
// blue
data[i + 2] = brightness;
}
};
})();
(function() {
'use strict';
/**
* Grayscale Filter
* @function
* @memberof Konva.Filters
* @param {Object} imageData
* @example
* node.cache();
* node.filters([Konva.Filters.Grayscale]);
*/
Konva.Filters.Grayscale = function(imageData) {
var data = imageData.data, len = data.length, i, brightness;
for (i = 0; i < len; i += 4) {
brightness = 0.34 * data[i] + 0.5 * data[i + 1] + 0.16 * data[i + 2];
// red
data[i] = brightness;
// green
data[i + 1] = brightness;
// blue
data[i + 2] = brightness;
}
};
})();
(function(Konva) {
'use strict';
@ -4929,30 +4929,30 @@
*/
})(Konva);
(function() {
'use strict';
/**
* Invert Filter
* @function
* @memberof Konva.Filters
* @param {Object} imageData
* @example
* node.cache();
* node.filters([Konva.Filters.Invert]);
*/
Konva.Filters.Invert = function(imageData) {
var data = imageData.data, len = data.length, i;
for (i = 0; i < len; i += 4) {
// red
data[i] = 255 - data[i];
// green
data[i + 1] = 255 - data[i + 1];
// blue
data[i + 2] = 255 - data[i + 2];
}
};
})();
(function() {
'use strict';
/**
* Invert Filter
* @function
* @memberof Konva.Filters
* @param {Object} imageData
* @example
* node.cache();
* node.filters([Konva.Filters.Invert]);
*/
Konva.Filters.Invert = function(imageData) {
var data = imageData.data, len = data.length, i;
for (i = 0; i < len; i += 4) {
// red
data[i] = 255 - data[i];
// green
data[i + 1] = 255 - data[i + 1];
// blue
data[i + 2] = 255 - data[i + 2];
}
};
})();
/*
the Gauss filter
@ -6857,215 +6857,216 @@
*/
})();
(function() {
'use strict';
/**
* Noise Filter. Randomly adds or substracts to the color channels
* @function
* @name Noise
* @memberof Konva.Filters
* @param {Object} imageData
* @author ippo615
* @example
* node.cache();
* node.filters([Konva.Filters.Noise]);
* node.noise(0.8);
*/
Konva.Filters.Noise = function(imageData) {
var amount = this.noise() * 255,
data = imageData.data,
nPixels = data.length,
half = amount / 2,
i;
for (i = 0; i < nPixels; i += 4) {
data[i + 0] += half - 2 * half * Math.random();
data[i + 1] += half - 2 * half * Math.random();
data[i + 2] += half - 2 * half * Math.random();
}
};
Konva.Factory.addGetterSetter(
Konva.Node,
'noise',
0.2,
null,
Konva.Factory.afterSetFilter
);
/**
* get/set noise amount. Must be a value between 0 and 1. Use with {@link Konva.Filters.Noise} filter.
* @name noise
* @method
* @memberof Konva.Node.prototype
* @param {Number} noise
* @returns {Number}
*/
})();
(function() {
'use strict';
/**
* Noise Filter. Randomly adds or substracts to the color channels
* @function
* @name Noise
* @memberof Konva.Filters
* @param {Object} imageData
* @author ippo615
* @example
* node.cache();
* node.filters([Konva.Filters.Noise]);
* node.noise(0.8);
*/
Konva.Filters.Noise = function(imageData) {
var amount = this.noise() * 255,
data = imageData.data,
nPixels = data.length,
half = amount / 2,
i;
/*eslint-disable max-depth */
(function() {
'use strict';
/**
* Pixelate Filter. Averages groups of pixels and redraws
* them as larger pixels
* @function
* @name Pixelate
* @memberof Konva.Filters
* @param {Object} imageData
* @author ippo615
* @example
* node.cache();
* node.filters([Konva.Filters.Pixelate]);
* node.pixelSize(10);
*/
Konva.Filters.Pixelate = function(imageData) {
var pixelSize = Math.ceil(this.pixelSize()),
width = imageData.width,
height = imageData.height,
x,
y,
i,
//pixelsPerBin = pixelSize * pixelSize,
red,
green,
blue,
alpha,
nBinsX = Math.ceil(width / pixelSize),
nBinsY = Math.ceil(height / pixelSize),
xBinStart,
xBinEnd,
yBinStart,
yBinEnd,
xBin,
yBin,
pixelsInBin;
imageData = imageData.data;
if (pixelSize <= 0) {
Konva.Util.error('pixelSize value can not be <= 0');
return;
}
for (xBin = 0; xBin < nBinsX; xBin += 1) {
for (yBin = 0; yBin < nBinsY; yBin += 1) {
// Initialize the color accumlators to 0
red = 0;
green = 0;
blue = 0;
alpha = 0;
// Determine which pixels are included in this bin
xBinStart = xBin * pixelSize;
xBinEnd = xBinStart + pixelSize;
yBinStart = yBin * pixelSize;
yBinEnd = yBinStart + pixelSize;
// Add all of the pixels to this bin!
pixelsInBin = 0;
for (x = xBinStart; x < xBinEnd; x += 1) {
if (x >= width) {
continue;
}
for (y = yBinStart; y < yBinEnd; y += 1) {
if (y >= height) {
continue;
}
i = (width * y + x) * 4;
red += imageData[i + 0];
green += imageData[i + 1];
blue += imageData[i + 2];
alpha += imageData[i + 3];
pixelsInBin += 1;
}
}
// Make sure the channels are between 0-255
red = red / pixelsInBin;
green = green / pixelsInBin;
blue = blue / pixelsInBin;
// Draw this bin
for (x = xBinStart; x < xBinEnd; x += 1) {
if (x >= width) {
continue;
}
for (y = yBinStart; y < yBinEnd; y += 1) {
if (y >= height) {
continue;
}
i = (width * y + x) * 4;
imageData[i + 0] = red;
imageData[i + 1] = green;
imageData[i + 2] = blue;
imageData[i + 3] = alpha;
}
}
}
}
};
Konva.Factory.addGetterSetter(
Konva.Node,
'pixelSize',
8,
null,
Konva.Factory.afterSetFilter
);
/**
* get/set pixel size. Use with {@link Konva.Filters.Pixelate} filter.
* @name pixelSize
* @method
* @memberof Konva.Node.prototype
* @param {Integer} pixelSize
* @returns {Integer}
*/
})();
for (i = 0; i < nPixels; i += 4) {
data[i + 0] += half - 2 * half * Math.random();
data[i + 1] += half - 2 * half * Math.random();
data[i + 2] += half - 2 * half * Math.random();
}
};
(function() {
'use strict';
/**
* Threshold Filter. Pushes any value above the mid point to
* the max and any value below the mid point to the min.
* This affects the alpha channel.
* @function
* @name Threshold
* @memberof Konva.Filters
* @param {Object} imageData
* @author ippo615
* @example
* node.cache();
* node.filters([Konva.Filters.Threshold]);
* node.threshold(0.1);
*/
Konva.Filters.Threshold = function(imageData) {
var level = this.threshold() * 255,
data = imageData.data,
len = data.length,
i;
for (i = 0; i < len; i += 1) {
data[i] = data[i] < level ? 0 : 255;
}
};
Konva.Factory.addGetterSetter(
Konva.Node,
'threshold',
0.5,
null,
Konva.Factory.afterSetFilter
);
/**
* get/set threshold. Must be a value between 0 and 1. Use with {@link Konva.Filters.Threshold} or {@link Konva.Filters.Mask} filter.
* @name threshold
* @method
* @memberof Konva.Node.prototype
* @param {Number} threshold
* @returns {Number}
*/
})();
Konva.Factory.addGetterSetter(
Konva.Node,
'noise',
0.2,
null,
Konva.Factory.afterSetFilter
);
/**
* get/set noise amount. Must be a value between 0 and 1. Use with {@link Konva.Filters.Noise} filter.
* @name noise
* @method
* @memberof Konva.Node.prototype
* @param {Number} noise
* @returns {Number}
*/
})();
/*eslint-disable max-depth */
(function() {
'use strict';
/**
* Pixelate Filter. Averages groups of pixels and redraws
* them as larger pixels
* @function
* @name Pixelate
* @memberof Konva.Filters
* @param {Object} imageData
* @author ippo615
* @example
* node.cache();
* node.filters([Konva.Filters.Pixelate]);
* node.pixelSize(10);
*/
Konva.Filters.Pixelate = function(imageData) {
var pixelSize = Math.ceil(this.pixelSize()),
width = imageData.width,
height = imageData.height,
x,
y,
i,
//pixelsPerBin = pixelSize * pixelSize,
red,
green,
blue,
alpha,
nBinsX = Math.ceil(width / pixelSize),
nBinsY = Math.ceil(height / pixelSize),
xBinStart,
xBinEnd,
yBinStart,
yBinEnd,
xBin,
yBin,
pixelsInBin;
imageData = imageData.data;
if (pixelSize <= 0) {
Konva.Util.error('pixelSize value can not be <= 0');
return;
}
for (xBin = 0; xBin < nBinsX; xBin += 1) {
for (yBin = 0; yBin < nBinsY; yBin += 1) {
// Initialize the color accumlators to 0
red = 0;
green = 0;
blue = 0;
alpha = 0;
// Determine which pixels are included in this bin
xBinStart = xBin * pixelSize;
xBinEnd = xBinStart + pixelSize;
yBinStart = yBin * pixelSize;
yBinEnd = yBinStart + pixelSize;
// Add all of the pixels to this bin!
pixelsInBin = 0;
for (x = xBinStart; x < xBinEnd; x += 1) {
if (x >= width) {
continue;
}
for (y = yBinStart; y < yBinEnd; y += 1) {
if (y >= height) {
continue;
}
i = (width * y + x) * 4;
red += imageData[i + 0];
green += imageData[i + 1];
blue += imageData[i + 2];
alpha += imageData[i + 3];
pixelsInBin += 1;
}
}
// Make sure the channels are between 0-255
red = red / pixelsInBin;
green = green / pixelsInBin;
blue = blue / pixelsInBin;
alpha = alpha / pixelsInBin;
// Draw this bin
for (x = xBinStart; x < xBinEnd; x += 1) {
if (x >= width) {
continue;
}
for (y = yBinStart; y < yBinEnd; y += 1) {
if (y >= height) {
continue;
}
i = (width * y + x) * 4;
imageData[i + 0] = red;
imageData[i + 1] = green;
imageData[i + 2] = blue;
imageData[i + 3] = alpha;
}
}
}
}
};
Konva.Factory.addGetterSetter(
Konva.Node,
'pixelSize',
8,
null,
Konva.Factory.afterSetFilter
);
/**
* get/set pixel size. Use with {@link Konva.Filters.Pixelate} filter.
* @name pixelSize
* @method
* @memberof Konva.Node.prototype
* @param {Integer} pixelSize
* @returns {Integer}
*/
})();
(function() {
'use strict';
/**
* Threshold Filter. Pushes any value above the mid point to
* the max and any value below the mid point to the min.
* This affects the alpha channel.
* @function
* @name Threshold
* @memberof Konva.Filters
* @param {Object} imageData
* @author ippo615
* @example
* node.cache();
* node.filters([Konva.Filters.Threshold]);
* node.threshold(0.1);
*/
Konva.Filters.Threshold = function(imageData) {
var level = this.threshold() * 255,
data = imageData.data,
len = data.length,
i;
for (i = 0; i < len; i += 1) {
data[i] = data[i] < level ? 0 : 255;
}
};
Konva.Factory.addGetterSetter(
Konva.Node,
'threshold',
0.5,
null,
Konva.Factory.afterSetFilter
);
/**
* get/set threshold. Must be a value between 0 and 1. Use with {@link Konva.Filters.Threshold} or {@link Konva.Filters.Mask} filter.
* @name threshold
* @method
* @memberof Konva.Node.prototype
* @param {Number} threshold
* @returns {Number}
*/
})();
(function() {
'use strict';
@ -14241,6 +14242,7 @@
WORD = 'word',
CHAR = 'char',
NONE = 'none',
ELLIPSIS = '…',
ATTR_CHANGE_LIST = [
'fontFamily',
'fontSize',
@ -14253,6 +14255,7 @@
'width',
'height',
'wrap',
'ellipsis',
'letterSpacing'
],
// cached variables
@ -14281,6 +14284,7 @@
* @param {Number} [config.padding]
* @param {Number} [config.lineHeight] default is 1
* @param {String} [config.wrap] can be word, char, or none. Default is word
* @param {Boolean} [config.ellipsis] can be true or false. Default is false
* @param {String} [config.fill] fill color
* @param {Image} [config.fillPatternImage] fill pattern image
* @param {Number} [config.fillPatternX]
@ -14633,13 +14637,15 @@
currentHeightPx = 0,
wrap = this.getWrap(),
shouldWrap = wrap !== NONE,
wrapAtWord = wrap !== CHAR && shouldWrap;
wrapAtWord = wrap !== CHAR && shouldWrap,
shouldAddEllipsis = this.getEllipsis() && !shouldWrap;
this.textArr = [];
getDummyContext().save();
getDummyContext().font = this._getContextFont();
for (var i = 0, max = lines.length; i < max; ++i) {
var line = lines[i];
var additionalWidth = shouldAddEllipsis ? this._getTextWidth(ELLIPSIS) : 0;
var lineWidth = this._getTextWidth(line);
if (fixedWidth && lineWidth > maxWidth) {
@ -14659,10 +14665,10 @@
while (low < high) {
var mid = (low + high) >>> 1,
substr = line.slice(0, mid + 1),
substrWidth = this._getTextWidth(substr);
substrWidth = this._getTextWidth(substr) + additionalWidth;
if (substrWidth <= maxWidth) {
low = mid + 1;
match = substr;
match = substr + (shouldAddEllipsis ? ELLIPSIS : '');
matchWidth = substrWidth;
} else {
high = mid;
@ -14879,6 +14885,23 @@
* text.wrap('word');
*/
Konva.Factory.addGetterSetter(Konva.Text, 'ellipsis', false);
/**
* get/set ellipsis. Can be true or false. Default is false.
* @name ellipsis
* @method
* @memberof Konva.Text.prototype
* @param {Boolean} ellipsis
* @returns {Boolean}
* @example
* // get ellipsis
* var ellipsis = text.ellipsis();
*
* // set ellipsis
* text.ellipsis('word');
*/
Konva.Factory.addGetterSetter(Konva.Text, 'letterSpacing', 0);
/**

8
konva.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -22,6 +22,7 @@
WORD = 'word',
CHAR = 'char',
NONE = 'none',
ELLIPSIS = '…',
ATTR_CHANGE_LIST = [
'fontFamily',
'fontSize',
@ -34,6 +35,7 @@
'width',
'height',
'wrap',
'ellipsis',
'letterSpacing'
],
// cached variables
@ -62,6 +64,7 @@
* @param {Number} [config.padding]
* @param {Number} [config.lineHeight] default is 1
* @param {String} [config.wrap] can be word, char, or none. Default is word
* @param {Boolean} [config.ellipsis] can be true or false. Default is false
* @@shapeParams
* @@nodeParams
* @example
@ -345,13 +348,15 @@
currentHeightPx = 0,
wrap = this.getWrap(),
shouldWrap = wrap !== NONE,
wrapAtWord = wrap !== CHAR && shouldWrap;
wrapAtWord = wrap !== CHAR && shouldWrap,
shouldAddEllipsis = this.getEllipsis() && !shouldWrap;
this.textArr = [];
getDummyContext().save();
getDummyContext().font = this._getContextFont();
for (var i = 0, max = lines.length; i < max; ++i) {
var line = lines[i];
var additionalWidth = shouldAddEllipsis ? this._getTextWidth(ELLIPSIS) : 0;
var lineWidth = this._getTextWidth(line);
if (fixedWidth && lineWidth > maxWidth) {
@ -371,10 +376,10 @@
while (low < high) {
var mid = (low + high) >>> 1,
substr = line.slice(0, mid + 1),
substrWidth = this._getTextWidth(substr);
substrWidth = this._getTextWidth(substr) + additionalWidth;
if (substrWidth <= maxWidth) {
low = mid + 1;
match = substr;
match = substr + (shouldAddEllipsis ? ELLIPSIS : '');
matchWidth = substrWidth;
} else {
high = mid;
@ -591,6 +596,23 @@
* text.wrap('word');
*/
Konva.Factory.addGetterSetter(Konva.Text, 'ellipsis', false);
/**
* get/set ellipsis. Can be true or false. Default is false.
* @name ellipsis
* @method
* @memberof Konva.Text.prototype
* @param {Boolean} ellipsis
* @returns {Boolean}
* @example
* // get ellipsis
* var ellipsis = text.ellipsis();
*
* // set ellipsis
* text.ellipsis('word');
*/
Konva.Factory.addGetterSetter(Konva.Text, 'letterSpacing', 0);
/**

View File

@ -364,6 +364,42 @@ suite('Text', function() {
assert.equal(text.getLineHeight(), 20);
});
// ======================================================
test('text single line with ellipsis', function() {
var stage = addStage();
var layer = new Konva.Layer();
var rect = new Konva.Rect({
x: 10,
y: 10,
width: 380,
height: 300,
fill: 'red'
});
var text = new Konva.Text({
x: 10,
y: 10,
text: "HEADING\n\nAll the world's a stage, merely players. They have their exits and their entrances; And one man in his time plays many parts.",
fontSize: 14,
fontFamily: 'Calibri',
fontStyle: 'normal',
fill: '#555',
width: 100,
padding: 0,
lineHeight: 20,
align: 'center',
wrap: 'none',
ellipsis: true
});
layer.add(rect).add(text);
stage.add(layer);
assert.equal(text.textArr.length, 3);
assert.equal(text.textArr[2].text.slice(-1), '…');
});
// ======================================================
test('text multi line with justify align', function() {
var stage = addStage();