6
I do research and there is no way to make it work, the first try works well, but the second no longer, that is, it does not erase the text that was there and replace it with the new one (in the corresponding field), I assume that my function clearRect()
is not taking the right arguments:
var canvas = document.getElementById("myCanvas");
canvas.style.background = 'red';
var ctx = canvas.getContext("2d");
$('.define').on('click', function() {
var txt = $($(this).data('target')).val();
var txt_color = $(this).data('colortext');
var txt_size = $(this).data('fontsize');
var x = $(this).data('x');
var y = $(this).data('y');
ctx.font = txt_size+ " Arial";
ctx.fillStyle = txt_color;
ctx.clearRect(x, y, canvas.width, ctx.measureText(txt).height);
ctx.fillText(txt, x, y);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<canvas id="myCanvas"></canvas><br>
<input id="title"><br>
<button class="define" data-target="#title" data-colortext="black" data-fontsize="30px" data-x="10" data-y="50">Definir Titulo</button><br><br>
<input id="text"><br>
<button class="define" data-target="#text" data-colortext="blue" data-fontsize="20px" data-x="10" data-y="80">Definir Texto</button>
Thanks, I think it’s the right way, but there’s a problem. I just want you to act in the corresponding field, that is, if it is the text it deletes and rewrites only the text, in this way you are deleting the two, so I also had just
– Miguel
@Miguel we will solve the problem together then. I will analyze! You want to stay each in their proper locations right?
– viana
Exactly Ack, that’s right... Altering one should not interfere with the other
– Miguel