简体   繁体   English

从 Vue 方法更新 FabricJS 对象

[英]Updating FabricJS objects from Vue method

I am trying to update a Fabricjs canvas through Vue controls.我正在尝试通过 Vue 控件更新 Fabricjs canvas。 I am initializing the canvas through 'mounted()', but do not know how to access the canvas in a function without passing the canvas as an argument. I am initializing the canvas through 'mounted()', but do not know how to access the canvas in a function without passing the canvas as an argument.

Here is a demonstration of my predicament.这是我的困境的一个证明。 I'd like the button to resize the circle.我想要按钮来调整圆圈的大小。

HTML: HTML:

<div id="app">
  <a href="#" @click="resize()">RESIZE</>
  <canvas id="c"></canvas>
</div>

JS: JS:

 new Vue({
  el: "#app",
  data: {
    title: "Default title",
    message: null,
    canvas: null
  },
  mounted() {
    const canvas = new fabric.Canvas("c", {
      width: 500,
      height: 500
    });
    this.canvas = canvas;
    this.loadCir(canvas);
  },
  methods: {
    loadCir(canvas) {
      const cir = new fabric.Circle({
        fill: "red",
        radius: 50,
        top: 10,
        left: 10
      });
      cir.name = "circle";
      canvas.add(cir);
    },
    resize() {
      this.canvas.getObjects().forEach(function (targ) {
        if (targ.name == "circle") {
          targ.radius = 100;
        }
      });
    }
  }
});

https://codepen.io/shnlmn/pen/JjbrKNL https://codepen.io/shnlmn/pen/JjbrKNL

The result of this script is:该脚本的结果是:

TypeError: Cannot read property 'getObjects' of undefined

I feel like storing the canvas into data is not a great idea, but I am not sure how to make it accessible to the rest of the app.我觉得将 canvas 存储到数据中并不是一个好主意,但我不知道如何让应用程序的 rest 可以访问它。

What is the best way to make Fabricjs objects accessible from functions such as this?使 Fabricjs 对象可从此类函数访问的最佳方法是什么?

Looks like the next resizing part of your code isn't working.看起来您的代码的下一个调整大小部分不起作用。 But to help you get past your issue with the canvas.getObjects() returning undefined.但是为了帮助您解决 canvas.getObjects() 返回未定义的问题。

What you need to do is to when using data properties you need to just make sure everything is referencing the data property.您需要做的是,在使用data属性时,您只需确保所有内容都引用数据属性。 You were creating variables and saving them to the data property which is unneeded, you can just do all your work on this.canvas您正在创建变量并将它们保存到不需要的数据属性中,您可以在this.canvas上完成所有工作

new Vue({
  el: "#app",
  data: {
    title: "Default title",
    message: null,
    canvas: null
  },
  mounted() {
    this.canvas = new fabric.Canvas("c", {
      width: 500,
      height: 500
    });
    this.canvas.getContext('2d');
    this.loadCir();
  },
  methods: {
    loadCir() {
      const cir = new fabric.Circle({
        fill: "red",
        radius: 50,
        top: 10,
        left: 10
      });
      cir.name = "circle";
      this.canvas.add(cir);
    },
    resize() {
      this.canvas.getObjects().forEach(function (targ) {
        if (targ.name == "circle") {
          targ.height = 100;
        }
      });
    }
  }
});

Once you reference this.canvas everywhere and actually do the work On your data property, then your getObjects is defined.一旦您在任何地方引用this.canvas并实际在您的数据属性上完成工作,那么您的 getObjects 就被定义了。 However your resizing isn't working so you just need to get over that hump and you're away!但是,您的调整大小不起作用,因此您只需要克服那个困难就可以了!

#note: i was attempting to change the height of your circle, and not the radius #note:我试图改变你的圆的高度,而不是半径

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

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