简体   繁体   English

Fabric.js直接从JSON转到PNG

[英]Fabric.js going directly from JSON to PNG

I need to create PNG thumbnails of saved stringified JSON from fabric.js 我需要从fabric.js创建保存的字符串化JSON的PNG缩略图

I have a database I am saving the JSON data from the canvas to, but I need to create a PNG thumbnail gallery from this saved JSON data. 我有一个数据库,我正在从画布保存JSON数据,但我需要从这个保存的JSON数据创建一个PNG缩略图库。

Rather than creating a bunch of canvases on the page and doing something like this. 而不是在页面上创建一堆画布并做这样的事情。

canvas.loadFromJSON(JSONDATA); 
thumbImage = canvas.toDataURL('png');
$(this).attr('src', thumbImage);

I need to just directly create PNGs from the JSON Data. 我需要直接从JSON数据创建PNG。 Is this possible, if so how do I do it? 这是可能的,如果是这样,我该怎么做?

The JSON data is a set of instructions for FabricJS to use to create a canvas. JSON数据是FabricJS用于创建画布的一组指令。 A canvas can then be converted to an image. 然后可以将画布转换为图像。

For that reason, yes, you will need to create the canvas and then create the image. 因此,是的,您需要创建画布然后创建图像。

An alternative method would be to create a server running NodeJS, which can run FabricJS. 另一种方法是创建一个运行NodeJS的服务器,它可以运行FabricJS。 Ultimately you'd perform the same task there - create a canvas and then generate the image. 最终你会在那里执行相同的任务 - 创建一个画布,然后生成图像。 But the benefit here is that being that it's a server process it would save the files directly to the server and thus this task could be automated. 但这里的好处是,它是一个服务器进程,它将文件直接保存到服务器,因此这个任务可以自动化。

But setting up the server and writing the script to perform this task may take more effort than your task requires - depends on how often you'll need to do this. 但是设置服务器并编写脚本来执行此任务可能比您的任务需要更多的努力 - 取决于您需要执行此操作的频率。

This post discusses how to install NodeJS and FabricJS . 这篇文章讨论了如何安装NodeJS和FabricJS That'll get the server up and running, but you then need to also write the server script. 这将使服务器启动并运行,但您还需要编写服务器脚本。

you have an another alternative way is to creat a generation canvas and give it a none displaying, create a hidden canvas to generate your png's, like that you can have generated images from a hidden content canvas. 你有另一种替代方法是创建一个生成画布并给它一个无显示,创建一个隐藏的画布来生成你的png,就像你可以从隐藏的内容画布生成图像。

<canvas id='c' width='150' heigh='150' style='display: none;'><canvas>

<script>
    var canvas = new fabric.Canvas('c');
    canvas.loadFromJSON(json);
    canvas.toDataURL('png');
</script>

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

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