简体   繁体   English

向图像添加文本,无 canvas,普通 javascript

[英]adding text to an image, no canvas, vanilla javascript

I am working on a meme generator project, it's to practice DOM manipulation.我正在做一个模因生成器项目,它是为了练习 DOM 操作。 So, I'm supposed to use vanilla JavaScript and no canvas.所以,我应该使用香草 JavaScript 而没有 canvas。 I'm having trouble finding answer with these parameters.我无法使用这些参数找到答案。 I am almost done with the project, I just need to add text to the picture that they submitted.我几乎完成了这个项目,我只需要在他们提交的图片中添加文本。

I have tried using innerText and innerHTML they seem to just replace it.我试过使用 innerText 和 innerHTML 他们似乎只是替换它。 I've tried append child, similar to how I got the appendchild with the image, but I either get an error or replace the image.我已经尝试过 append 孩子,类似于我如何获得带有图像的 appendchild,但我得到一个错误或替换图像。

I was pretty sure that I needed to add it add the picture with JavaScript then style it with CSS.我很确定我需要添加它,使用 JavaScript 添加图片,然后使用 CSS 对其进行样式设置。 Maybe just add a class with classList.也许只需添加一个带有 classList 的 class。

 console.log('Currentfile: memegenerator'); let img = document.getElementsByTagName('img'); addEventListener('submit', function(e) { e.preventDefault(); let UIurl = document.getElementById('picurl'); let memeToBe = UIurl.value; let img = document.createElement('img'); img.setAttribute('src', memeToBe); img.setAttribute('class', 'meme'); // append to the document with set attribute using said variable let memeLocation = document.getElementById('location'); memeLocation.appendChild(img); //url for pic test //https://www.fillmurray.com/640/360 //add text to image //get text values let inputText = document.getElementById('text_top'); let textValue = inputText.value; addEventListener('click', function(e) { let clickedElement = e.target; console.log(clickedElement); let targetCheck = clickedElement.classList.contains("meme"); if (targetCheck) { clickedElement.remove(); } })
 h1 { color: navy; }.center { text-align: center; }.main { width: 50%; margin: 0 auto; background-color: lightblue; border-radius: 0.5rem; border: 0.08rem solid black; box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2); transition: 0.3s; }.meme { width: 99%; /* margin: 2 auto; */ justify-content: center; background-color: lightblue; border-radius: 0.5rem; border: 0.08rem solid black; box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2); transition: 0.3s; } main:hover { box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2); } meme:hover { box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2); } body { background-color: #f0feff; }.button { float: right; } /* divider styles */ hr.rounded { border-top: 2px solid #bbb; border-radius: 5px; width: 90%; }.border_lower { border-bottom: 1px solid #bbb; border-radius: 5px; width: 90%; } /* form styles */ form { width: 60%; margin: 0 auto; } form input { margin: 2px; } form label { margin: 2px; } /* Container holding the image and the text */.container { position: relative; text-align: center; color: white; } /* Bottom left text */.bottom-left { position: absolute; bottom: 8px; left: 16px; } /* Top left text */.location { position: absolute; top: 8px; left: 16px; }
 <main class="main"> <h1 class="center">MEME GENERATOR:</h1> <hr class="rounded" /> <form action="#" class="form"> <label for="text_top">Text Upper:</label> <input type="text" name="text_top" id="text_top" /><br /> <label for="text_lower">Text Lower:</label> <input type="text" name="text_lower" id="text_lower" /><br /> <label for="picturl">Picture:</label> <input type="url" name="picurl" id="picurl" /><br /><br /><br /> <input class="button " type="submit" value="Add Meme:" /><br /> <hr /> </form> <div id="location"></div> <hr class="border_lower" /> <p class="center"><small>Thanks for visiting!</small></p> </main>

Since this isn't about saving the images but just for display purposes, I got it working.由于这不是为了保存图像而只是为了显示目的,所以我让它工作了。

The main problem that you were having is your approach seemed to focus more on the javascript side of things but missed out on the CSS part of it.您遇到的主要问题是您的方法似乎更多地关注 javascript 方面,但错过了 CSS 部分。

There are multiple ways to put images behind text, the most common two are:有多种方法可以将图像放在文本后面,最常见的两种是:

  1. Setting the images as a background image on the parent element (ie div) then just setting the text within that element将图像设置为父元素(即 div)上的背景图像,然后仅在该元素中设置文本
  2. Using CSS to absolute position the text on the image and use z-index to layer them使用 CSS 绝对 position 图像上的文本并使用 z-index 将它们分层

For my answer I chose #2.对于我的答案,我选择了#2。

Besides misc code clean ups, the main function that I did was:除了杂项代码清理之外,我所做的主要 function 是:

  1. I created a div, and gave it a class of meme我创建了一个 div,并给它一个模因的meme
  2. I added the image to that div我将图像添加到该 div
  3. I added the top and bottom text to their own divs and append those to the meme div我将顶部和底部文本添加到他们自己的 div 和 append 那些到 meme div
  4. Using CSS, I positioned the top and bottom text above the image使用 CSS,我将顶部和底部文本定位在图像上方

A few other things, when adding an eventListeners unless it is absolutely needed, I recommend tying them to a specific element and not just the document (or nothing at all which I believe is document anyway).其他一些事情,当添加eventListeners时,除非绝对需要,否则我建议将它们绑定到特定元素,而不仅仅是文档(或者我认为无论如何都不是文档)。 By applying it to the document, any click will be processed, but by tying it to the element, only clicks on that element will be processed.通过将其应用于文档,将处理任何点击,但通过将其绑定到元素,只会处理对该元素的点击。

 console.log('Currentfile: memegenerator'); let img = document.getElementsByTagName('img'); let form = document.querySelector('form'); form.addEventListener('submit', function(e) { e.preventDefault(); let meme = document.createElement("div"); let top_text = document.createElement("div"); let bottom_text = document.createElement("div"); let img = document.createElement("img"); let btn = document.createElement("button"); btn.setAttribute("type","button"); img.src = document.getElementById('picurl').value; top_text.classList.add("top_text"); top_text.innerHTML = document.getElementById('text_top').value; bottom_text.classList.add("bottom_text"); bottom_text.innerHTML = document.getElementById('text_lower').value; btn.innerHTML = "REMOVE"; meme.classList.add("meme"); meme.appendChild(top_text); meme.appendChild(bottom_text); meme.appendChild(img); meme.appendChild(btn); let memeLocation = document.getElementById('location'); memeLocation.appendChild(meme); document.getElementById('picurl').value = ""; document.getElementById('text_top').value = ""; document.getElementById('text_lower').value = ""; btn.addEventListener('click', function(e) { meme.remove(); }) });
 h1 { color: navy; }.center { text-align: center; }.main { width: 50%; margin: 0 auto; background-color: lightblue; border-radius: 0.5rem; border: 0.08rem solid black; box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2); transition: 0.3s; }.meme { width: 99%; /* margin: 2 auto; */ justify-content: center; border-radius: 0.5rem; border: 0.08rem solid black; box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2); transition: 0.3s; position:relative; }.top_text{ position:absolute; top:10px; left:30px; color:#ffffff; z-index:3 }.bottom_text{ position:absolute; bottom:20px; left:30px; color:#ffffff; z-index:3 }.meme img{ max-width:100%; z-index:2 } main:hover { box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2); } meme:hover { box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2); } body { background-color: #f0feff; }.button { float: right; } /* divider styles */ hr.rounded { border-top: 2px solid #bbb; border-radius: 5px; width: 90%; }.border_lower { border-bottom: 1px solid #bbb; border-radius: 5px; width: 90%; } /* form styles */ form { width: 60%; margin: 0 auto; } form input { margin: 2px; } form label { margin: 2px; } /* Container holding the image and the text */.container { position: relative; text-align: center; color: white; } /* Bottom left text */.bottom-left { position: absolute; bottom: 8px; left: 16px; } /* Top left text */.location { position: absolute; top: 8px; left: 16px; }
 <main class="main"> <h1 class="center">MEME GENERATOR:</h1> <hr class="rounded" /> <form action="#" class="form"> <label for="text_top">Text Upper:</label> <input type="text" name="text_top" id="text_top" /><br /> <label for="text_lower">Text Lower:</label> <input type="text" name="text_lower" id="text_lower" /><br /> <label for="picturl">Picture:</label> <input type="url" name="picurl" value="https.//www.fillmurray:com/640/360" id="picurl" /><br /><br /><br /> <input type="submit" value="Add Meme:" /><br /> <hr /> </form> <div id="location"></div> <hr class="border_lower" /> <p class="center"><small>Thanks for visiting!</small></p> </main>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM