gpt4 book ai didi

javascript - 单击添加的矩形消失在 Canvas fiddle html5中

转载 作者:行者123 更新时间:2023-12-03 02:10:03 27 4
gpt4 key购买 nike

我通过在 Canvas 内部单击来添加一个矩形,以开始编写文本...

问题

一旦我开始写文字矩形消失了。我认为问题是我没有将新矩形的任何值传递给函数......我该如何解决这个问题?或者单击添加文本

后可能会激活默认设置的矩形

https://jsfiddle.net/xpvt214o/26337/

示例进行编辑以仅添加基本问题。

$("#Add").on("click", function() {
$("#first_text").show();
$("#resetInput").show();
var rheight = 20;
ctx.fillStyle = '#E1FFC7';
ctx.fillRect(20, rheight, 300, 50);

});





var canvas = document.getElementById('cv');
ctx = canvas.getContext('2d');


// core drawing function
var drawMe = function() {
var img = document.getElementById('bg');
ctx.drawImage(img, 0, 0, 364, 662, 0, 0, 364, 662);
ctx.fillStyle = '#E1FFC7';


text = $("#first_text").val();
ctx.fillStyle = 'black';
ctx.fillText(text, 20, 20)


}




$("#resetInput").on("click", function() {

$("#first_text").val("");
drawMe();
});

$("#first_text").on("change keypress keyup keydown ", function() {


drawMe();
});





drawMe();
canvas {
margin: 40px;
}

#first_text,
#resetInput {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum
</p>
<button id="Add">add text</button>
<br/>

<input type="text" id="first_text"> <button id="resetInput">reset</button>


<br>
<canvas id="cv"></canvas>
<img id="bg" src="https://cloud.githubusercontent.com/assets/398893/15136779/4e765036-1639-11e6-9201-67e728e86f39.jpg" style="opacity:0;" />

最佳答案

每次从键盘输入调用 drawMe 时,您都会在已有的内容上进行绘制,从而在绿色框的顶部进行绘制。

您需要在 drawMe 函数中设置颜色后添加此内容:

ctx.fillRect(20, 20, 300, 50);

这是一个工作示例:

$("#Add").on("click", function() {
$("#first_text").show();
$("#resetInput").show();
var rheight = 20;
ctx.fillStyle = '#E1FFC7';
ctx.fillRect(20, rheight, 300, 50);
});

var canvas = document.getElementById('cv');
ctx = canvas.getContext('2d');

// core drawing function
var drawMe = function() {
var img = document.getElementById('bg');
ctx.drawImage(img, 0, 0, 364, 662, 0, 0, 364, 662);
ctx.fillStyle = '#E1FFC7';
if ($('#first_text:visible').length > 0) {
ctx.fillRect(20, 20, 300, 50);
}

text = $("#first_text").val();
ctx.fillStyle = 'black';
ctx.fillText(text, 20, 20)
}

$("#resetInput").on("click", function() {
$("#first_text").val("");
drawMe();
});

$("#first_text").on("change keypress keyup keydown ", function() {
drawMe();
});

drawMe();
canvas {
margin: 40px;
}

#first_text,
#resetInput {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum
</p>
<button id="Add">add text</button>
<br/>

<input type="text" id="first_text"> <button id="resetInput">reset</button>


<br>
<canvas id="cv"></canvas>
<img id="bg" src="https://cloud.githubusercontent.com/assets/398893/15136779/4e765036-1639-11e6-9201-67e728e86f39.jpg" style="opacity:0;" />

关于javascript - 单击添加的矩形消失在 Canvas fiddle html5中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49617227/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com