Last Updated: February 01, 2019
·
3.031K
· soolaugust

Change Text to Image with Javascript

Sometimes, we want to change a text an image, for best practice, I think better way is canvas - MDN :

var canvasElement = document.createElement('canvas');
canvasElement.width = 186;
canvasElement.height = 222;
var canvas = canvasElement.getContext('2d');
canvas.fillStyle = "#f6d021";
canvas.textAlign = "center";
canvas.textBaseline = "middle";
canvas.font = "bold 40px KaiTi, arial, helvetica, sans-serif";
canvas.fillText("This will be animage",0,0);

Now we have created an canvas element,then we can change this element to Base64 URL then use it as src of <img />:

<img src= canvas.canvas.toDataURL()  />

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Related Tags
#javascript
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside