I'm trying to load a masked photo in black and fill the space with text. I want the text to be responsive to the image's shape and make sure the whole content of the txt file is shown on the photo. here is the code
I only used chatgpt to make this code since I'm still learning. I expect it to put the contents of my txt file and display it inside the black space of the image.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Text in Black Spaces</title>
<style>
.container {
position: relative;
display: inline-block;
}
#image {
position: absolute;
top: 0;
left: 0;
z-index: 1;
}
#svg-container {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 2;
pointer-events: none;
}
#text {
font-size: 20px;
font-family: Arial, sans-serif;
fill: white;
}
</style>
</head>
<body>
<div class="container">
<img id="image" src="https://i.stack.imgur.com/XXRG0.jpg" alt="Your Image">
<div id="svg-container">
<svg id="svg" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">
<text id="text"></text>
</svg>
</div>
</div>
<input type="file" id="fileInput" accept=".txt">
<script>
document.addEventListener("DOMContentLoaded", function() {
const image = document.getElementById("image");
const svgText = document.getElementById("text");
const fileInput = document.getElementById("fileInput");
fileInput.addEventListener("change", function() {
const file = this.files[0];
const reader = new FileReader();
reader.onload = function(event) {
const text = event.target.result;
svgText.textContent = text;
}
reader.readAsText(file);
});
const originalImage = new Image();
originalImage.src = image.src;
originalImage.onload = function() {
const canvas = document.createElement("canvas");
canvas.width = originalImage.width;
canvas.height = originalImage.height;
const ctx = canvas.getContext("2d");
ctx.drawImage(originalImage, 0, 0);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const pixels = imageData.data;
for (let i = 0; i < pixels.length; i += 4) {
const r = pixels[i];
const g = pixels[i + 1];
const b = pixels[i + 2];
if (r === 0 && g === 0 && b === 0) {
pixels[i] = 255;
pixels[i + 1] = 255;
pixels[i + 2] = 255;
pixels[i + 3] = 0;
}
}
ctx.putImageData(imageData, 0, 0);
const newDataUrl = canvas.toDataURL();
image.src = newDataUrl;
}
});
</script>
</body>
</html>

I used clippy and made a Polygon on your image, then I applied it to
#textin my exampleHere's the snippet, is this what you want?
I hope this helps!