0
私はすべての描画テキストをクリアすることなく、関数でテキストを置き換えようとしています。今は関数を置き換えることができますが、キャンバス全体をクリアするだけです。私はそれがもう少しダイナミックなので、(例えば)第3の機能が残るようにしたいと思います。キャンバス全体を消去せずに関数を置き換える?
これまで私がこれまでに得たことは次のとおりです。事前に
var $ = function(id) {
return document.getElementById(id)
};
var canvas = this.__canvas = new fabric.Canvas('c');
canvas.setHeight(300);
canvas.setWidth(500);
function textOne() {
canvas.clear();
canvas.add(new fabric.IText('One', {
left: 50,
top: 100,
fontFamily: 'arial',
fill: '#333',
fontSize: 50
}));
}
// Text that should stay
canvas.add(new fabric.IText('This Should Stay The Same\nEdited Or Not', {
left: 300,
top: 45,
fontFamily: 'Monsieur La Doulaise',
fontSize: 27,
hasBorders: false,
hasControls: false,
selectable: true,
lockRotation: true,
lockMovementX: true,
lockMovementY: true,
align: 'mid',
originX: 'center',
originY: 'center',
centeredScaling: true,
}));
function textTwo() {
canvas.clear();
canvas.add(new fabric.IText('Two', {
left: 200,
top: 100,
fontFamily: 'arial black',
fill: '#333',
fontSize: 50
}));
}
canvas {
border: 1px solid #dddddd;
border-radius: 3px;
margin-top: 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.5.0/fabric.min.js"></script>
<button onclick="textOne()">One</button>
<button onclick="textTwo()">Two</button>
<canvas id="c"></canvas>
感謝を:元のテキストがクリアされているかに注意してください!
可能な重複https://stackoverflow.com/questions/18864487/change-text-once-it-is-added-キャンバス上) – juliobetta
そうは思わないよ –
はい。それは!私はそれに基づいてあなたの質問の答えを知っている! – juliobetta