使用fabric js时获取canvas对象 [英] Get the canvas object while using fabric js

查看:3005
本文介绍了使用fabric js时获取canvas对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Fabric.js并且我在一个地方创建了一个结构画布对象。

I'm using Fabric.js and I've created a fabric canvas object at one place.

var x = new fabric.Canvas("mycanvas");

现在在另一个地方,我想访问这个'x'不可用的对象。那么我怎样才能获得相同的结构画布对象。

Now at another place, I want to access this object where 'x' won't be available. So how can I get the same fabric canvas object.

我不想改变x的范围或者将x作为arg传递。

I don't wanna change the scope of x or pass x as arg.

另外,如何从结构画布对象获取toDataURL?

Also, how to get the toDataURL from the fabric canvas object?

推荐答案

假设 mycanvas 是Canvas元素的ID,您可以在Canvas元素本身上存储对Fabric对象的引用:

Assuming that mycanvas is the ID of a Canvas element, you could store the reference to the fabric object on the Canvas element itself:

var x = new fabric.Canvas("mycanvas");
document.getElementById("mycanvas").fabric = x;

然后,您可以随时检索该对象:

You can then retrieve that object any time you want with:

var y = document.getElementById("mycanvas").fabric;
var url = y.toDataURL("png", 1);

这篇关于使用fabric js时获取canvas对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆