我正在尝试建立一个站点,用户输入文本的位置会将文本吸引到正在显示的图像的气泡中。我有三个问题:
用户可以在画布上输入文本,但是当用户删除字符时,画布上绘制的文本不会更新
用户输入的文本完全没有自动换行,但是当我直接插入字符串时,自动换行。我想用用户输入的文字换行
每当用户开始在新的输入框中键入内容时,画布中的所有文本都会被删除。
我一直在寻找在线教程,并在此处找到答案,但是它们都不是我要使其在此处正常运行所需要的确切解决方案。任何其他建议都会有所帮助。
谢谢!
<html>
<input id="input-text" type="text" onkeyup="usertextChange(this.value)"
maxlength="6" />
<input id="input-text2" type="text" onkeyup="usertextChange2(this.value)"
maxlength="5" />
<input id="input-text3" type="text" onkeyup="usertextChange3(this.value)"
maxlength="12" />
<input id="input-text4" type="text" onkeyup="usertextChange4(this.value)"
maxlength="18" />
<div class="art-container">
<canvas id="canvas" width="576" height="576">
Canvas requires a browser that supports HTML5.
</canvas>
<img crossOrigin="Anonymous" id="no-crying"
src="https://cdn.glitch.com/4ed5f9d8-97ad-4c53-b855-3e8d508ba2f3%2FDVSN-
FUTURE-NO-CRYIN-FINAL-NoText.jpg?v=1572122142300"/>
</div>
</html>
<style>
#input-text, #input-text2, #input-text3, #input-text4 {
width: 90%;
font-size: 18px;
height: 24px;
text-transform: uppercase;
padding: 0 8px;
background-color: transparent;
color: red;
border: 2px solid black;
outline: none;
border-radius: 4px;
margin-bottom: 12px;
margin-left: auto;
margin-right: auto;
font-weight: 500;
font-family: bubblegum;
}
#no-crying {
display: none;
}
</style>
<script>
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var maxWidth = 90;
var lineHeight = 45;
var x = 35;
var y = 315;
var text = document.getElementById('input-text1').value;
var text2 = document.getElementById('input-text2').value;
var text3 = document.getElementById('input-text3').value;
var text4 = document.getElementById('input-text4').value;
function drawImage(text) {
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
context.clearRect(0, 0, canvas.width, canvas.height);
var img = document.getElementById('no-crying');
context.drawImage(img, 0, 0, canvas.width, canvas.height);
}
function wrapText(context, text, x, y, maxWidth, lineHeight) {
var words = text.split(' ');
var line = '';
for(var n = 0; n < words.length; n++) {
var testLine = line + words[n] + ' ';
var metrics = context.measureText(testLine);
var testWidth = metrics.width;
if (testWidth > maxWidth && n > 0) {
context.fillText(line, x, y);
line = words[n] + ' ';
y += lineHeight;
}
else {
line = testLine;
}
}
context.fillText(line, x, y);
}
window.onload = function() {
drawImage();
}
// USER IMPUT FUNCTIONS
window.usertextChange = function(val){
context.clearRect(0, 0, canvas.width, canvas.height);
context.restore();
drawImage();
context.font = '26px Bubblegum';
context.fillStyle = "#000000";
context.fillText(val, 39, 315);
context.save();
wrapText(context, text, x, y, maxWidth, lineHeight);
}
window.usertextChange2 = function(val){
context.restore();
context.font = '22px Bubblegum';
context.fillStyle = "#000000";
context.fillText(val, 45, 370);
context.save();
wrapText(context, text, x, y, maxWidth, lineHeight);
}
window.usertextChange3 = function(val){
context.restore();
context.font = '26px Bubblegum';
context.fillStyle = "#000000";
context.fillText(val, 25, 420);
context.save();
wrapText(context, text, x, y, maxWidth, lineHeight);
}
window.usertextChange4 = function(val){
context.restore();
context.font = '24px Bubblegum';
context.fillStyle = "#000000";
context.fillText(val, 48, 360);
wrapText(context, text, x, y, maxWidth, lineHeight);
}
</script>
EDIT 我还希望在添加用户输入文本后下载图像。什么是最好的实施方法。这是我根据最近的回复得出的结论
function init() {
text1 = '';
text2 = '';
text3 = '';
text4 = '';
backgroundImg = new Image();
backgroundImg.src = 'https://cdn.glitch.com/4ed5f9d8-97ad-4c53-b855-
3e8d508ba2f3%2FDVSN-FUTURE-NO-CRYIN-FINAL-NoText.jpg?v=1572122142300';
backgroundImg.setAttribute('crossOrigin', 'anonymous');
function addLink() {
var link = document.createElement('a');
link.innerHTML = 'Download!';
link.addEventListener('click', function(e) {
link.href = canvas.toDataURL();
link.download = "salt-bae.png";
}, false);
link.className = "instruction";
document.getElementById('input-container').appendChild(link);
}
window.onload = function() {
drawImage();
addLink();
}
答案 0 :(得分:1)
很少评论:
您将var text = document.getElementById('input-text1').value;
与ID为 input-text1 一起使用,而在HTML中,ID为 input-text 。
您可以使用从未使用过的drawImage(text)
参数来定义函数text
。在此函数中,您将再次创建canvas
和context
变量。
您将隐藏的图像插入DOM中,以便稍后在画布上绘制。我认为创建新的Image
对象会更容易。
函数wrapText()
有点模棱两可。它会格式化文本吗?它显示什么吗?您可以将其重命名为displayWrapText()
或getWrappedText()
。
然后,关于您的问题:
当用户删除字符时,文本不会更新,因为除输入#1之外,当用户键入内容时,它不会清除气泡,并且会在前一个文本上绘制新文本(较深)。这就是为什么您看不到差异的原因。删除适用于第一个输入,因为您清除了气泡并再次绘制了全文。
Tbh,我尝试时忘了这个问题,现在它运作良好...无论如何,请为maxWidth
函数提供一个合适的wrapText()
:大气泡。但是,如果字符串不包含任何空格,则不会包装。
当用户键入第一个输入时,它将清除画布,但仅在第一个气泡内再次显示文本,因此其他显示为空。您需要清除画布,然后在所有相应的气泡中重新绘制文本。
我所做的是应用我之前告诉您的内容,并且改进了事件处理(请参见bubbling),因为每个输入的代码几乎相同。为了能够在每次更新后重新绘制所有气泡,我会在每次更新时存储它们的值。
const canvas = document.getElementById('canvas');
const context = canvas.getContext('2d');
const lineHeight = 30;
let backgroundImg;
let text1;
let text2;
let text3;
let text4;
init();
function init() {
text1 = '';
text2 = '';
text3 = '';
text4 = '';
backgroundImg = new Image();
backgroundImg.src = 'https://cdn.glitch.com/4ed5f9d8-97ad-4c53-b855-3e8d508ba2f3%2FDVSN-FUTURE-NO-CRYIN-FINAL-NoText.jpg?v=1572122142300';
context.drawImage(backgroundImg, 0, 0, canvas.width, canvas.height);
document.querySelector('#input-container').addEventListener('keyup', function(e) {
const num = parseInt(e.target.getAttribute('data-bubble'), 10);
const text = e.target.value;
saveText(num, text);
draw(canvas, context, backgroundImg);
}, false);
}
function draw(canvas, context, backgroundImg) {
context.clearRect(0, 0, canvas.width, canvas.height);
context.drawImage(backgroundImg, 0, 0, canvas.width, canvas.height);
drawBubble(canvas, context, getTextFrom(1), 39, 315, '26px Bubblegum', '#000000', 110, lineHeight); // bubble 1
drawBubble(canvas, context, getTextFrom(2), 45, 370, '22px Bubblegum', '#000000', 70, lineHeight); // bubble 2
drawBubble(canvas, context, getTextFrom(3), 20, 425, '26px Bubblegum', '#000000', 120, lineHeight); // bubble 3
drawBubble(canvas, context, getTextFrom(4), 20, 515, '24px Bubblegum', '#000000', 120, lineHeight); // bubble 4
}
function getTextFrom(num) {
switch(num) {
case 1: return text1;
case 2: return text2;
case 3: return text3;
case 4: return text4;
default: '';
}
}
function saveText(num, text) {
switch(num) {
case 1: text1 = text; break;
case 2: text2 = text; break;
case 3: text3 = text; break;
case 4: text4 = text; break;
}
}
function displayWrappedText(context, text, x, y, maxWidth, lineHeight) {
var words = text.split(' ');
var line = '';
for (var n = 0; n < words.length; n++) {
var testLine = line + words[n] + ' ';
var metrics = context.measureText(testLine);
var testWidth = metrics.width;
if (testWidth > maxWidth && n > 0) {
context.fillText(line, x, y);
line = words[n] + ' ';
y += lineHeight;
} else {
line = testLine;
}
}
context.fillText(line, x, y);
}
function drawBubble(canvas, context, text, x, y, font, color, maxWidth, lineHeight) {
context.font = font;
context.fillStyle = color;
//context.fillText(text, x, y);
displayWrappedText(context, text, x, y, maxWidth, lineHeight);
}
#input-container > input {
width: 90%;
font-size: 18px;
height: 24px;
text-transform: uppercase;
padding: 0 8px;
background-color: transparent;
color: red;
border: 2px solid black;
outline: none;
border-radius: 4px;
margin-bottom: 12px;
margin-left: auto;
margin-right: auto;
font-weight: 500;
font-family: bubblegum;
}
<div id="input-container">
<input class="js-input-text" data-bubble="1" type="text" maxlength="6" />
<input class="js-input-text" data-bubble="2" type="text" maxlength="5" />
<input class="js-input-text" data-bubble="3" type="text" maxlength="12" />
<input class="js-input-text" data-bubble="4" type="text" maxlength="18" />
</div>
<div class="art-container">
<canvas id="canvas" width="576" height="576">
Canvas requires a browser that supports HTML5.
</canvas>
</div>