mirror of
https://github.com/konvajs/konva.git
synced 2025-08-24 08:52:36 +08:00
cleaned up jshint errors in Image.js
This commit is contained in:
parent
bc11554452
commit
3c4cd6e9ed
@ -15,6 +15,7 @@
|
|||||||
"quotmark": "single",
|
"quotmark": "single",
|
||||||
"unused": true,
|
"unused": true,
|
||||||
"globals": {
|
"globals": {
|
||||||
"Kinetic": false
|
"Kinetic": false,
|
||||||
|
"document": false
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -8,10 +8,8 @@
|
|||||||
DASH = '-',
|
DASH = '-',
|
||||||
EMPTY_STRING = '',
|
EMPTY_STRING = '',
|
||||||
LEFT = 'left',
|
LEFT = 'left',
|
||||||
NEW_LINE = '\n',
|
|
||||||
TEXT = 'text',
|
TEXT = 'text',
|
||||||
TEXT_UPPER = 'Text',
|
TEXT_UPPER = 'Text',
|
||||||
TOP = 'top',
|
|
||||||
MIDDLE = 'middle',
|
MIDDLE = 'middle',
|
||||||
NORMAL = 'normal',
|
NORMAL = 'normal',
|
||||||
PX_SPACE = 'px ',
|
PX_SPACE = 'px ',
|
||||||
@ -91,14 +89,12 @@
|
|||||||
},
|
},
|
||||||
drawFunc: function(context) {
|
drawFunc: function(context) {
|
||||||
var p = this.getPadding(),
|
var p = this.getPadding(),
|
||||||
fontStyle = this.getFontStyle(),
|
|
||||||
fontSize = this.getFontSize(),
|
|
||||||
fontFamily = this.getFontFamily(),
|
|
||||||
textHeight = this.getTextHeight(),
|
textHeight = this.getTextHeight(),
|
||||||
lineHeightPx = this.getLineHeight() * textHeight,
|
lineHeightPx = this.getLineHeight() * textHeight,
|
||||||
textArr = this.textArr,
|
textArr = this.textArr,
|
||||||
textArrLen = textArr.length,
|
textArrLen = textArr.length,
|
||||||
totalWidth = this.getWidth();
|
totalWidth = this.getWidth(),
|
||||||
|
n;
|
||||||
|
|
||||||
context.setAttr('font', this._getContextFont());
|
context.setAttr('font', this._getContextFont());
|
||||||
context.setAttr('textBaseline', MIDDLE);
|
context.setAttr('textBaseline', MIDDLE);
|
||||||
@ -108,7 +104,7 @@
|
|||||||
context.translate(0, p + textHeight / 2);
|
context.translate(0, p + textHeight / 2);
|
||||||
|
|
||||||
// draw text lines
|
// draw text lines
|
||||||
for(var n = 0; n < textArrLen; n++) {
|
for(n = 0; n < textArrLen; n++) {
|
||||||
var obj = textArr[n],
|
var obj = textArr[n],
|
||||||
text = obj.text,
|
text = obj.text,
|
||||||
width = obj.width;
|
width = obj.width;
|
||||||
@ -202,118 +198,118 @@
|
|||||||
_getContextFont: function() {
|
_getContextFont: function() {
|
||||||
return this.getFontStyle() + SPACE + this.getFontSize() + PX_SPACE + this.getFontFamily();
|
return this.getFontStyle() + SPACE + this.getFontSize() + PX_SPACE + this.getFontFamily();
|
||||||
},
|
},
|
||||||
_addTextLine: function (line, width, height) {
|
_addTextLine: function (line, width) {
|
||||||
return this.textArr.push({text: line, width: width});
|
return this.textArr.push({text: line, width: width});
|
||||||
},
|
},
|
||||||
_getTextWidth: function (text) {
|
_getTextWidth: function (text) {
|
||||||
return dummyContext.measureText(text).width;
|
return dummyContext.measureText(text).width;
|
||||||
},
|
},
|
||||||
_setTextData: function () {
|
_setTextData: function () {
|
||||||
var lines = this.getText().split('\n'),
|
var lines = this.getText().split('\n'),
|
||||||
fontSize = +this.getFontSize(),
|
fontSize = +this.getFontSize(),
|
||||||
textWidth = 0,
|
textWidth = 0,
|
||||||
lineHeightPx = this.getLineHeight() * fontSize,
|
lineHeightPx = this.getLineHeight() * fontSize,
|
||||||
width = this.attrs.width,
|
width = this.attrs.width,
|
||||||
height = this.attrs.height,
|
height = this.attrs.height,
|
||||||
fixedWidth = width !== AUTO,
|
fixedWidth = width !== AUTO,
|
||||||
fixedHeight = height !== AUTO,
|
fixedHeight = height !== AUTO,
|
||||||
padding = this.getPadding(),
|
padding = this.getPadding(),
|
||||||
maxWidth = width - padding * 2,
|
maxWidth = width - padding * 2,
|
||||||
maxHeightPx = height - padding * 2,
|
maxHeightPx = height - padding * 2,
|
||||||
currentHeightPx = 0,
|
currentHeightPx = 0,
|
||||||
wrap = this.getWrap(),
|
wrap = this.getWrap(),
|
||||||
shouldWrap = wrap !== NONE,
|
shouldWrap = wrap !== NONE,
|
||||||
wrapAtWord = wrap !== CHAR && shouldWrap;
|
wrapAtWord = wrap !== CHAR && shouldWrap;
|
||||||
|
|
||||||
this.textArr = [];
|
this.textArr = [];
|
||||||
dummyContext.save();
|
dummyContext.save();
|
||||||
dummyContext.font = this.getFontStyle() + SPACE + fontSize + PX_SPACE + this.getFontFamily();
|
dummyContext.font = this.getFontStyle() + SPACE + fontSize + PX_SPACE + this.getFontFamily();
|
||||||
for (var i = 0, max = lines.length; i < max; ++i) {
|
for (var i = 0, max = lines.length; i < max; ++i) {
|
||||||
var line = lines[i],
|
var line = lines[i],
|
||||||
lineWidth = this._getTextWidth(line);
|
lineWidth = this._getTextWidth(line);
|
||||||
if (fixedWidth && lineWidth > maxWidth) {
|
if (fixedWidth && lineWidth > maxWidth) {
|
||||||
/*
|
/*
|
||||||
* if width is fixed and line does not fit entirely
|
* if width is fixed and line does not fit entirely
|
||||||
* break the line into multiple fitting lines
|
* break the line into multiple fitting lines
|
||||||
*/
|
*/
|
||||||
while (line.length > 0) {
|
while (line.length > 0) {
|
||||||
/*
|
/*
|
||||||
* use binary search to find the longest substring that
|
* use binary search to find the longest substring that
|
||||||
* that would fit in the specified width
|
* that would fit in the specified width
|
||||||
*/
|
*/
|
||||||
var low = 0, high = line.length,
|
var low = 0, high = line.length,
|
||||||
match = '', matchWidth = 0;
|
match = '', matchWidth = 0;
|
||||||
while (low < high) {
|
while (low < high) {
|
||||||
var mid = (low + high) >>> 1,
|
var mid = (low + high) >>> 1,
|
||||||
substr = line.slice(0, mid + 1),
|
substr = line.slice(0, mid + 1),
|
||||||
substrWidth = this._getTextWidth(substr);
|
substrWidth = this._getTextWidth(substr);
|
||||||
if (substrWidth <= maxWidth) {
|
if (substrWidth <= maxWidth) {
|
||||||
low = mid + 1;
|
low = mid + 1;
|
||||||
match = substr;
|
match = substr;
|
||||||
matchWidth = substrWidth;
|
matchWidth = substrWidth;
|
||||||
} else {
|
} else {
|
||||||
high = mid;
|
high = mid;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
* 'low' is now the index of the substring end
|
* 'low' is now the index of the substring end
|
||||||
* 'match' is the substring
|
* 'match' is the substring
|
||||||
* 'matchWidth' is the substring width in px
|
* 'matchWidth' is the substring width in px
|
||||||
*/
|
*/
|
||||||
if (match) {
|
if (match) {
|
||||||
// a fitting substring was found
|
// a fitting substring was found
|
||||||
if (wrapAtWord) {
|
if (wrapAtWord) {
|
||||||
// try to find a space or dash where wrapping could be done
|
// try to find a space or dash where wrapping could be done
|
||||||
var wrapIndex = Math.max(match.lastIndexOf(SPACE),
|
var wrapIndex = Math.max(match.lastIndexOf(SPACE),
|
||||||
match.lastIndexOf(DASH)) + 1;
|
match.lastIndexOf(DASH)) + 1;
|
||||||
if (wrapIndex > 0) {
|
if (wrapIndex > 0) {
|
||||||
// re-cut the substring found at the space/dash position
|
// re-cut the substring found at the space/dash position
|
||||||
low = wrapIndex;
|
low = wrapIndex;
|
||||||
match = match.slice(0, low);
|
match = match.slice(0, low);
|
||||||
matchWidth = this._getTextWidth(match);
|
matchWidth = this._getTextWidth(match);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this._addTextLine(match, matchWidth);
|
this._addTextLine(match, matchWidth);
|
||||||
currentHeightPx += lineHeightPx;
|
currentHeightPx += lineHeightPx;
|
||||||
if (!shouldWrap ||
|
if (!shouldWrap ||
|
||||||
(fixedHeight && currentHeightPx + lineHeightPx > maxHeightPx)) {
|
(fixedHeight && currentHeightPx + lineHeightPx > maxHeightPx)) {
|
||||||
/*
|
/*
|
||||||
* stop wrapping if wrapping is disabled or if adding
|
* stop wrapping if wrapping is disabled or if adding
|
||||||
* one more line would overflow the fixed height
|
* one more line would overflow the fixed height
|
||||||
*/
|
*/
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
line = line.slice(low);
|
line = line.slice(low);
|
||||||
if (line.length > 0) {
|
if (line.length > 0) {
|
||||||
// Check if the remaining text would fit on one line
|
// Check if the remaining text would fit on one line
|
||||||
lineWidth = this._getTextWidth(line);
|
lineWidth = this._getTextWidth(line);
|
||||||
if (lineWidth <= maxWidth) {
|
if (lineWidth <= maxWidth) {
|
||||||
// if it does, add the line and break out of the loop
|
// if it does, add the line and break out of the loop
|
||||||
this._addTextLine(line, lineWidth);
|
this._addTextLine(line, lineWidth);
|
||||||
currentHeightPx += lineHeightPx;
|
currentHeightPx += lineHeightPx;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// not even one character could fit in the element, abort
|
// not even one character could fit in the element, abort
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// element width is automatically adjusted to max line width
|
// element width is automatically adjusted to max line width
|
||||||
this._addTextLine(line, lineWidth);
|
this._addTextLine(line, lineWidth);
|
||||||
currentHeightPx += lineHeightPx;
|
currentHeightPx += lineHeightPx;
|
||||||
textWidth = Math.max(textWidth, lineWidth);
|
textWidth = Math.max(textWidth, lineWidth);
|
||||||
}
|
}
|
||||||
// if element height is fixed, abort if adding one more line would overflow
|
// if element height is fixed, abort if adding one more line would overflow
|
||||||
if (fixedHeight && currentHeightPx + lineHeightPx > maxHeightPx) {
|
if (fixedHeight && currentHeightPx + lineHeightPx > maxHeightPx) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
dummyContext.restore();
|
dummyContext.restore();
|
||||||
this.textHeight = fontSize;
|
this.textHeight = fontSize;
|
||||||
this.textWidth = textWidth;
|
this.textWidth = textWidth;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Kinetic.Util.extend(Kinetic.Text, Kinetic.Shape);
|
Kinetic.Util.extend(Kinetic.Text, Kinetic.Shape);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user