您好,欢迎访问一九零五行业门户网

如何使用FabricJS更改IText对象的URL字符串的格式?

在本教程中,我们将学习如何使用 fabricjs 更改 itext 对象的 url 字符串的格式。 itext 类是在 fabricjs 版本 1.4 中引入的,它扩展了 fabric.text 并用于创建 itext 实例。 itext 实例使我们可以自由选择、剪切、粘贴或添加新文本,而无需额外配置。还有各种支持的按键组合和鼠标/触摸组合使文本具有交互性,而 text 中未提供这些组合。
然而,基于 itext 的 textbox 允许我们调整文本矩形的大小并自动换行。对于 itext 来说情况并非如此,因为高度不会根据换行进行调整。我们可以通过使用各种属性来操作 itext 对象。同样,我们可以使用 format 属性来更改 itext 对象的 url 字符串的格式。
语法todataurl({ format: string }: object): string
参数options(可选) - 此参数是一个对象,它为 itext 对象的 url 表示形式提供额外的自定义。使用此参数可以更改高度、质量、乘数和许多其他属性,其中格式是属性
选项键format - 该属性接受一个string值,它允许我们定义输出图像的格式。接受的值为“jpeg”或“png”。默认值为“png”。
示例 1不使用格式属性的默认值
让我们看一个代码示例,以查看使用 todataurl 方法而不使用 format 属性时记录的输出。一旦我们从开发工具打开控制台,我们就可以看到 itext 对象的 url 表示。我们可以复制该 url 并将其粘贴到新选项卡的地址栏中以查看最终输出。由于我们没有指定图像的格式,因此它将根据设置的默认值“png”。
<!doctype html><html><head> <!-- adding the fabric js library--> <script src=https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js></script></head><body> <h2>default value without using the format property</h2> <p> you can open console from dev tools and see that the url representation of the itext object has a png format by default </p> <canvas id=canvas></canvas> <script> // initiate a canvas instance var canvas = new fabric.canvas(canvas); canvas.setwidth(document.body.scrollwidth); canvas.setheight(250); // initiate a shadow object var shadow = new fabric.shadow({ blur: 25, color: grey, offsetx: 12, offsety: 15, }); // initiate an itext object var itext = new fabric.itext( add sample text here.lorem ipsum dolor sit amet consectetur adipiscing.,{ width: 300, left: 60, top: 70, fill: #c70039, backgroundcolor: #c1dfed, stroke: #c70039, originx: center, shadow: shadow, } ); // add it to the canvas canvas.add(itext); // using the todataurl method console.log(itext.todataurl()); </script></body></html>
示例 2使用 todataurl 方法和 format 属性
让我们看一个代码示例,看看当 todataurl 方法与 format 属性一起使用时,itext 对象是什么样子。在这种情况下,我们将能够指定最终图像的格式。由于此处分配的值为“jpeg”,因此最终图像将为“jpeg”格式。
<!doctype html><html><head> <!-- adding the fabric js library--> <script src=https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js></script></head><body> <h2>using the todataurl method along with format property</h2> <p> you can open console from dev tools and see that the url representation of the itext object has a jpeg format now </p> <canvas id=canvas></canvas> <script> // initiate a canvas instance var canvas = new fabric.canvas(canvas); canvas.setwidth(document.body.scrollwidth); canvas.setheight(250); // initiate a shadow object var shadow = new fabric.shadow({ blur: 25, color: grey, offsetx: 12, offsety: 15, }); // initiate an itext object var itext = new fabric.itext( add sample text here.lorem ipsum dolor sit amet consectetur adipiscing.,{ width: 300, left: 60, top: 70, fill: #c70039, backgroundcolor: #c1dfed, stroke: #c70039, originx: center, shadow: shadow, } ); // add it to the canvas canvas.add(itext); // using the todataurl method console.log(itext.todataurl({format: jpeg})); </script></body></html>
以上就是如何使用fabricjs更改itext对象的url字符串的格式?的详细内容。
其它类似信息

推荐信息