简体   繁体   English

javascript / html5 - 文字转图片

[英]javascript / html5 - text to image

I need help for converting one text to image.我需要帮助将文本转换为图像。 Is there any way to do this by js or jquery.. ?有没有办法通过js或jquery..来做到这一点?

any help would be appreciated.任何帮助,将不胜感激。

Use canvas and print the text on the canvas. 使用画布并在画布上打印文本。 After that canvas can be exported to an image. 之后,可以将画布导出到图像。

<canvas id="e" width="200" height="200"></canvas>
<script>
  var canvas = document.getElementById("e");
  var context = canvas.getContext("2d");
  context.fillStyle = "blue";
  context.font = "bold 16px Arial";
  context.fillText("Your text", 100, 100);
</script>

Draw your text into a canvas tag. 将您的文本绘制到canvas标签中。

var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');

ctx.fillText(myText, x, y);

There is a library that attempts to do this. 有一个尝试执行此操作的库。

http://hertzen.com/experiments/jsfeedback/ http://hertzen.com/experiments/jsfeedback/

Reads the DOM, from the html and render it to a canvas, fail on some, but in general works. 从html读取DOM并将其渲染到画布上,虽然有些失败,但是在一般情况下还是可行的。

I suggest you use puppeteer with ejs for better speed.我建议您将 puppeteer 与 ejs 一起使用以提高速度。 I used it and got like 80 MS speed (average), which is way faster than canvas or any other lib, If you want to use it.我使用它并获得了 80 MS 的速度(平均),这比 canvas 或任何其他库快得多,如果你想使用它。 feel free to let me know.随时让我知道。

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

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