Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate an image based on text and CSS?

Tags:

javascript

I'm developing a web app. It generates signatures for a game, with information from its API.

I'll need to store the images with a script that gathers the information and turns it into an image.

Do you know a way to turn text + CSS into an image?

like image 380
Lucas T. Avatar asked Jul 12 '13 15:07

Lucas T.


People also ask

How do you put an image in text in CSS?

CSS Code: The CSS property is used to set the image as background in the text. The background-image property is used to set an image as background. The -webkit-text-fill-color property is used to give the text a transparent color, the background image will show through the text, thus completing the clipping effect.

How do you make pictures with text?

Go to https://smallseotools.com/text-to-image/(if you aren't already there) Write the desired text or paste it from the clipboard in the big text box. Next, you have to choose the desired options such as Text Color, Font Style, Font Size, Background Color, and Text Background Color for the out image.


1 Answers

Yes this can be done, what you want to do is draw the text on a canvas, and then save the canvas. you do not need to have the canvas show, you can hide it like any other html element, just add it, draw the text on it, and save it.

Here is a link on a library that saves: https://github.com/hongru/canvas2image

Some sample code drawing text on canvas:

<canvas id="myCanvas" width="200" height="100" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>

<script>

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.font="30px Arial";
ctx.fillText("Your Text",10,50);

// save img
Canvas2Image.saveAsImage(c, 200, 100, 'png');
</script>
like image 200
nycynik Avatar answered Nov 05 '22 07:11

nycynik