Im trying to set up this site where the user input text will draw text into the speech bubble of the image being displayed. I have three issues:
Users can input text to canvas, but the text drawn on canvas isn't updated when users delete characters
The text isn't wrapping at all with user inputs but it wraps when I directly insert a string. I want the text to wrap with the user input
Anytime a user starts typing into a new input box all of the text in the canvas gets erased.
I've looked for tutorials online and answers here but none of them are the exact solution that I need to get this working properly here. Any other suggestions would be helpful.
Thanks!
<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 Im also looking to download the image after adding user input text. What is the best way to implement. Here is what i came up with based off recent responses
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();
}