canvg 是否还有办法在渲染完成时触发代码?

发布于 2025-01-14 12:30:46 字数 118 浏览 2 评论 0原文

曾经有一种方法可以添加回调函数renderCallback

我到处搜索,但似乎找不到任何关于如何在 canvg 3.x 中复制它的示例。

有谁可以指出我正确的方向吗?

There used to be a way to add a callback function renderCallback.

I've searched high and low but can't seem to find any examples of how to replicate that in canvg 3.x.

Is there anyone who can point me in the right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乞讨 2025-01-21 12:30:46

所以我不确定为什么 canvg 文档没有提到这一点,我可以告诉(也许他们认为这是不言而喻的?)但在版本 3.x 及以上的 render() 方法返回一个承诺。

就我而言,我试图将 SVG 写入画布,这样我就可以从中提取 PNG 图像。

所以我一直在寻找这样的解决方案:

let canvas = document.getElementById( 'canvas' );

let ctx = canvasgetContext( '2d' );

let v = canvg.Canvg.fromString( ctx, svg );

let render = v.render();

render.then(function() {
    let img = canvas.toDataURL( 'image/png' );
});

So I'm not sure why the canvg docs make no mention of this that I can tell (maybe they thought it was just self evident?) but in version 3.x and above the render() method returns a promise.

In my case I was trying to write an SVG to a canvas so I could then pull a PNG image from it.

So something like this was the solution I was looking for:

let canvas = document.getElementById( 'canvas' );

let ctx = canvasgetContext( '2d' );

let v = canvg.Canvg.fromString( ctx, svg );

let render = v.render();

render.then(function() {
    let img = canvas.toDataURL( 'image/png' );
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文