我使用html輸入標籤解決了這個問題。
HTML
<div class="canvas-wrapper">
<canvas id="canvas" width="500" height="500"></canvas>
</div>
CSS
.canvas-wrappter {
position: relative;
}
canvas {
border: 1px solid #000;
}
.itext {
width: 300px;
background: transparent;
position: absolute;
z-index: 2;
}
的JavaScript
var SINGLE_LINE = false;
var canvas = new fabric.Canvas('canvas');
// custom input area
if (SINGLE_LINE) {
var $itext = $('<input/>').attr('type', 'text').addClass('itext');
}
else {
var $itext = $('<textarea/>').addClass('itext');
}
var text = 'enter multi-byte text here 日本語';
var itext = new fabric.IText(text, {
left: 100,
top: 100,
fontSize: 20,
fill: '#000'
})
.on('editing:entered', function(e) {
var obj = this;
if (SINGLE_LINE) {
var keyDownCode = 0;
}
canvas.remove(obj);
// show input area
$itext.css({
left: obj.left,
top: obj.top,
'line-height': obj.lineHeight,
'font-family': obj.fontFamily,
'font-size': Math.floor(obj.fontSize * Math.min(obj.scaleX, obj.scaleY)) + 'px',
'font-weight': obj.fontWeight,
'font-style': obj.fontStyle,
color: obj.fill
})
.val(obj.text)
.appendTo($(canvas.wrapperEl).closest('.canvas-wrapper'));
// text submit event
if (SINGLE_LINE) {
// submit text by ENTER key
$itext.on('keydown', function(e) {
// save the key code of a pressed key while kanji conversion (it differs from the code for keyup)
keyDownCode = e.which;
})
.on('keyup', function(e) {
if (e.keyCode == 13 && e.which == keyDownCode) {
obj.exitEditing();
obj.set('text', $(this).val());
$(this).remove();
canvas.add(obj);
canvas.renderAll();
}
});
}
else {
// submit text by focusout
$itext.on('focusout', function(e) {
obj.exitEditing();
obj.set('text', $(this).val());
$(this).remove();
canvas.add(obj);
canvas.renderAll();
});
}
// focus on text
setTimeout(function() {
$itext.select();
}, 1);
});
canvas.add(itext);
canvas.setActiveObject(itext);
//itext.selectAll();
//itext.enterEditing();
見http://jsfiddle.net/3jk3jvy7/