简体   繁体   English

Vue JS和Fabric JS逻辑

[英]Vue js and fabric js logic

I am using Vue.js and one of my components is 2D canvas editor (by using fabric.js library). 我正在使用Vue.js,而我的组件之一是2D画布编辑器(通过使用fabric.js库)。

The thing is, that the code for this editor and for the operations that I am making in it is getting pretty verbose to be part of the component script tag. 事实是,要成为该编辑器以及我在其中进行的操作的代码,要成为组件脚本标签的一部分变得非常冗长。

I tried using mixins and divided the code into separate mixins like canvasMoving , copyPaste , grouping . 我尝试使用mixin,并将代码分成单独的混合,例如canvasMovingcopyPastegrouping

Now while this works, I feel like this is still not the way to go, that maybe I should use specialized classes. 现在,尽管这行得通,但我觉得这仍然不是要走的路,也许我应该使用专门的类。 Also I belive mixin is when you have a functionality to share between multiple components. 我还相信mixin是当您具有在多个组件之间共享的功能时。

Because for example the copyPaste mixin, sometimes needs methods, that are contained in the mixin grouping . 因为例如copyPaste mixin有时需要mixin grouping中包含的方法。 This then feels really wrong to me, that since the component includes both of those mixins, it works ok, but if I would remove one of them, it would stop working. 然后,这对我来说真的很不对劲,因为该组件同时包含这两个mixins,所以它可以正常工作,但是如果我删除其中一个,它将停止工作。

More over all of these mixins works with the canvas, but the canvas is initialized only in one of them and again, they can access it, because the component includes all the mixins, but if I remove the mixin that initializes the canvas, they all stop working because this.canvas will be undefined. 所有这些mixin都可以在画布上使用,但是只能在其中一个画布上对其进行初始化,并且它们可以再次访问它,因为该组件包括所有mixin,但是如果我删除了初始化画布的mixin,它们将全部停止工作,因为this.canvas将是未定义的。

What is the correct approach here? 这里正确的方法是什么? I was thinking about classes with dependency injection, like having master class lets say Editor and it would have its dependencies (grouping, copyPaste, drawing) or something like that. 我正在考虑使用依赖项注入的类,例如让主类说Editor ,它将具有其依赖项(分组,copyPaste,绘图)或类似的东西。

Then the only thing I do not know is how to connect my separate classes with the Vue.js component. 然后,我唯一不知道的是如何将单独的类与Vue.js组件连接。 Put the master class in the data object of my component? 将主类放在组件的data对象中?

So in the end I solved this by using normal classes instead of mixins. 所以最后我通过使用普通类而不是mixins解决了这个问题。 I also used bottlejs for dependency injection. 我还使用bottlejs进行依赖注入。

Now each class in its constructor specifies other classes, that it needs to use, so it is immediately clear which classes it is dependent on. 现在,其构造函数中的每个类都指定了需要使用的其他类,因此可以立即清楚其依赖于哪些类。

Also before when one mixin needed to call other mixin's method, it was a simple this.methodName() call with no knowledge about where this method is located, whereas now it is clearly stated by this._otherClass.methodName() 同样,在一个混入需要调用其他混入的方法之前,这是一个简单的this.methodName()调用,不知道此方法位于何处,而现在它是由this._otherClass.methodName()明确声明的。

Since I needed access to canvas, store and also to emit events, I created class Editor, that has a method init(canvas, store, eventBus) , because I can only create the Fabric canvas, after the HTML canvas is displayed. 由于需要访问画布,存储并发出事件,因此我创建了类编辑器,该类具有init(canvas, store, eventBus) ,因为在显示HTML画布之后,我只能创建Fabric画布。 Bottle creates this class with empty fields and I call the init function with the parameters in mounted stage of my component. Bottle用空字段创建此类,然后在组件的安装阶段使用参数调用init函数。

All of my classes are then ancestors of EditorProvider class, which only has one getter for this Editor class (that it gets in a constructor and stores in a field), from where I can get any of the specified fields. 然后,我所有的类都是EditorProvider类的祖先,该类只有一个getter(该类在构造函数中存储并存储在字段中),可以从中获取任何指定的字段。 So the call to store in any of my classes looks like: 因此,在我的任何课程中进行存储的调用看起来像:

this.editor.store.commit('anything')

Call to canvas: 调用画布:

this.editor.canvas.renderAll()

Call to eventbus: 调用eventbus:

this.editor.eventBus.emit('eventName')

My component now just imports the bottlejs and has access to all the classes by their names. 我的组件现在仅导入bottlejs,并可以通过其名称访问所有类。 The interaction is mainly by the canvas and window events, so I event created an EventHandler class, that moves all these event listeners from the component to separate class. 交互主要是通过canvas和window事件进行的,因此我创建了一个EventHandler类,该事件将所有这些事件侦听器从组件移到单独的类。 So in the end in the component I only have HTML template and a few lines of imports and the script tag, which is now much more readable and the dependencies are clearly visible. 因此,最后在该组件中,我只有HTML模板,几行导入和script标记,这些标记现在更具可读性,并且清晰可见。

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

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