![](/img/trans.png)
[英]How do you get used area of a KonvaJs Stage to pass as height/width properties into the toImage function?
[英]KonvaJS: How to change the text class properties (fontSize, height & width) on resize in?
我試圖在KonvaJS中調整文本層的大小。 我的代碼如下:
var Canvas = (function() {
var funct = {};
var stageWidth,stageHeight,stage,layer,circle,rect,myPlayer,textNode,tr;
funct.stageInit = function(){
stage = new Konva.Stage({
container: 'canvas',
width: stageWidth,
height: stageHeight
});
layer = new Konva.Layer();
stage.add(layer);
funct.addText();
funct.makeTextResizeable();
layer.draw();
}
funct.addText = function(){
textNode = new Konva.Text({
text: 'Some text here',
x: 50,
y: 50,
width: 50,
height: 50,
fontSize: 20,
draggable: true,
});
layer.add(textNode);
textNode.on('dblclick', () => {
// create textarea over canvas with absolute position
// first we need to find its positon
var textPosition = textNode.getAbsolutePosition();
var stageBox = stage.getContainer().getBoundingClientRect();
var areaPosition = {
x: textPosition.x + stageBox.left,
y: textPosition.y + stageBox.top
};
// create textarea and style it
var textarea = document.createElement('textarea');
document.body.appendChild(textarea);
textarea.value = textNode.text();
textarea.style.position = 'absolute';
textarea.style.top = areaPosition.y + 'px';
textarea.style.left = areaPosition.x + 'px';
textarea.style.width = textNode.width();
textarea.style.zIndex = 15;
textarea.focus();
textarea.addEventListener('keydown', function (e) {
if (e.keyCode === 13) {
textNode.text(textarea.value);
layer.draw();
document.body.removeChild(textarea);
}
});
});
}
funct.makeTextResizeable = function(){
tr = new Konva.Transformer({
boundBoxFunc: function (oldBoundBox, newBoundBox) {
if (newBoundBox.width > 200) {
return oldBoundBox;
}
textNode.attrs.fontSize = (newBoundBox.width/oldBoundBox.width)*textNode.attrs.fontSize;
textNode.attrs.width = (newBoundBox.width/oldBoundBox.width)*textNode.attrs.width;
textNode.attrs.height = (newBoundBox.width/oldBoundBox.width)*textNode.attrs.height;
console.log(textNode.attrs.fontSize);
return newBoundBox
},
isTransforming: function(){
console.log('a');
}
});
layer.add(tr);
tr.attachTo(textNode);
}
funct.fitStageIntoParentContainer = function(){
var container = document.querySelector('#canvas-container');
var containerWidth = container.offsetWidth;
var scale = containerWidth / stageWidth;
stage.width(stageWidth * scale);
stage.height(stageHeight * scale);
stage.scale({ x: scale, y: scale });
stage.draw();
}
funct.Onresize = function(){
window.addEventListener('resize', function(){
funct.fitStageIntoParentContainer();
});
}
funct.init = function(){
stageHeight = $('.vjs-poster').height();
stageWidth = $('.vjs-poster').width();
funct.stageInit();
funct.Onresize();
}
return funct;
})();
我想允許通過拖動並相應地更改字體大小來調整文本層的大小。 我還希望根據文字層的大小來包裝/展開文字。
https://jsfiddle.net/gentrobot/5gk1h67u/
要求是能夠在運行時動態添加文本,調整其大小並重新定位,然后獲取文本的最終大小,文本容器和文本容器的坐標以進行保存。 然后,根據這些信息,將生成具有動態添加的文本的HTML,該文本位於主元素上。
我可以調整文本的大小,但是調整大小時無法順利調整大小,也無法自動換行。 但是,一旦完成,我也可以提取最終坐標和大小。
也許您不需要在轉換文本時更改scale
。 您可以在每個transform
事件上重置它:
tr.on('transform', function() {
textNode.setAttrs({
width: textNode.width() * textNode.scaleX(),
height: textNode.height() * textNode.scaleY(),
scaleX: 1,
scaleY: 1,
});
})
同樣,為了獲得更好的用戶體驗,您可以限制一些大小:
tr = new Konva.Transformer({
boundBoxFunc: function (oldBoundBox, newBoundBox) {
if (newBoundBox.width > 200 || newBoundBox.width < textNode.fontSize()) {
return oldBoundBox;
} else if (newBoundBox.height < textNode.fontSize()) {
return oldBoundBox;
}
return newBoundBox
}
});
演示: https : //jsfiddle.net/dgL1kvcb/1/
如果在轉換時也需要更改fontSize
則需要定義其邏輯。 如果要同時改變換行和字體大小,它將如何工作?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.