admin管理员组文章数量:1296922
Again I've been finding the solution for this, if you're not understand my question, here's some demonstration.
I set my name as "Peter"
and I have a congratulation poster which has a space for put a name in it
when the user reach this page, the poster will generate the name and the poster and they can save both of the picture with the text in it. ** I have no demonstration code for this because I really have no idea how to do it. and I wish there's a possible way to do it as well...
Again I've been finding the solution for this, if you're not understand my question, here's some demonstration.
I set my name as "Peter"
and I have a congratulation poster which has a space for put a name in it
when the user reach this page, the poster will generate the name and the poster and they can save both of the picture with the text in it. ** I have no demonstration code for this because I really have no idea how to do it. and I wish there's a possible way to do it as well...
Share Improve this question edited Jul 27, 2022 at 20:59 Dharman♦ 33.4k27 gold badges101 silver badges147 bronze badges asked Dec 12, 2021 at 14:22 Peter LooPeter Loo 791 gold badge1 silver badge3 bronze badges 2- oh also the poster is a picture file format (jpg or png) – Peter Loo Commented Dec 12, 2021 at 14:23
- Wele to Stack Overflow! Please visit help center, take tour to see what and How to Ask. Do some research, search for related topics on SO; if you get stuck, post a minimal reproducible example of your attempt, noting input and expected output, preferably in a Stacksnippet – mplungjan Commented Dec 12, 2021 at 14:32
2 Answers
Reset to default 5You can use HTML Canvas for this.
just draw the image when it loads on canvas and then draw the texts on it.
And export the canvas using canvas.toDataURL("image/png")
. this will create a data URL for the stuff drawn on canvas. After getting the URL just put that as href
in an HTML <a>
tag and set the download
attribute to it.
on clicking the link it will start downloading
<canvas id="canvas" width="400" height="150"></canvas>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let img = new Image("Image_URL_here");
img.addEventListener("load", ()=>{
ctx.drawImage(img,0,0);
ctx.font = '50px serif';
ctx.fillText('Hello world', 50, 90);
});
It's the right response, Kumar. But, according with documentation,
"If you try to call drawImage() before the image has finished loading, it won't do anything (or, in older browsers, may even throw an exception). So you need to be sure to use the load event so you don't try this before the image has loaded" WEB Api - MDN Web Docs
This change make your example works here. Full code (sorry, I mixed the sample with MDN graph code 8D):
stack.html:
<html lang="en">
<head>
<script src="stack1.js"></script>
</head>
<body onload="draw();">
<canvas id="canvas" width="180" height="180"></canvas>
</body>
</html>
stack1.js:
function draw() {
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let img = new Image();
img.addEventListener("load", ()=>{
ctx.drawImage(img,0,0);
ctx.font = '50px serif';
ctx.fillText('Hello world', 50, 90);
});
img.src = "backdrop.png";
}
Catch the image "backdrop.png" from MDN to your local directory to make it works.
版权声明:本文标题:javascript - How to add text to an image and users can also saved the image with the added text? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741607085a2388038.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论