简体   繁体   English

为Fabric.js对象创建“控制面板”

[英]Making a “control panel” for Fabric.js objects

This is probably more of a general javascript than fabric.js question. 这可能是比fabric.js问题更通用的javascript。

I am looking at the Fabric.js " kitchensink " demo. 我正在看Fabric.jskitchensink ”演示。 If you click the "Object" tab and then select an item in the drawing area on the left, the control panel gets populated with the selected object's properties and there is (or appears to be) one-way binding: changes in the control panel are reflected in the selected object. 如果单击“对象”选项卡,然后在左侧绘图区域中选择一个项目,则控制面板将填充有所选对象的属性,并且存在(或似乎是)单向绑定:控制面板中的更改反映在所选对象中。

I need to implement this sort of control panel and am not sure where to start. 我需要实现这种控制面板,并且不确定从哪里开始。 I've been looking over the page source of that demo but it is pretty dense. 我一直在查看该演示的页面源,但是它非常密集。

Can someone give me a nudge in the right direction on how to start on this? 有人可以在正确的方向上为我提供些帮助吗? Is there a lib or framework involved? 是否有库或框架? Am I overthinking it? 我在想什么吗? I'm coming from a Flex/Actionscript background and where doing this sort of thing is very easy and I still get flummoxed with the density of the Javascript|HTML|CSS combination. 我来自Flex / Actionscript背景,在这里做这种事情非常容易,而且我仍然对Javascript | HTML | CSS组合的密度感到困惑。

在此处输入图片说明

We use similar functionality in our customizable products page. 我们在可定制的产品页面中使用类似的功能。 It's a bit different from Kitchensink example. 这与Kitchensink的示例有些不同。 In our system you need to add a text object to canvas first. 在我们的系统中,您需要首先向画布添加一个文本对象。

The main workflow is like this: 主要工作流程如下:

  • Give an id to every newly object to canvas: 为每个新的画布对象指定一个ID:

      addText() { const textSample = new fabric.IText('Örnek yazı', { left: canvas.width * fabric.util.getRandomInt(20, 30) / 100, top: canvas.width * fabric.util.getRandomInt(20, 30) / 100, fontFamily: 'Georgia, Times, "Times New Roman", serif', fontSize: 30, id: myId //This is my id }); window.canvas.add(textSample); } 
  • When the item is selected on canvas, get its id and activate the textarea on right column. 在画布上选择该项目后,获取其ID并激活右列的textarea。

      let activeTextObjectId window.canvas.on({ 'object:selected': () => { if (window.canvas.getActiveObject().type === 'i-text') { activeTextObjectId = getIdOfSelectedObject() activateTextaArea() // I think you can handle this } } }) getIdOfSelectedObject() { return window.canvas.getActiveObject().id } 
  • So you have the id of the canvas object. 因此,您具有canvas对象的ID。 Now you can run a function to update text object when the textarea keyup event triggers (updateTextObjectInCanvas) 现在,您可以运行一个函数,以在textarea keyup事件触发时更新文本对象(updateTextObjectInCanvas)

     document.getElementById('myTextarea').onkeyup = function () { updateCanvasTextObject(this.value) } 
  • You need a function which gets the object from canvas by its id (getObjectFromCanvasById) and one that updates its text (updateCanvasTextObject). 您需要一个函数,该函数通过其ID(getObjectFromCanvasById)从画布获取对象,并通过一个函数更新其文本(updateCanvasTextObject)。

     updateCanvasTextObject(value) { const canvasTextObject = this.getObjectFromCanvasById(activeTextObjectId) canvasTextObject.text = value window.canvas.renderAll() } //Instead of getting object from canvas, you can directly edit using getActiveObject() method of fabric.js getObjectFromCanvasById(id) { const canvasObject = window.canvas.getObjects().filter((item) => { return item.id === parseInt(id) }) return canvasObject[0] } 

Sorry about the code it's messy and it can be optimized, but I think it gives an idea about the workflow. 抱歉,代码很杂乱并且可以优化,但是我认为它为工作流程提供了一个思路。 Of course you will need many controls and validations in a real world example. 当然,在实际示例中,您将需要许多控件和验证。 Also it will be much easier to build this app using a JS library and state management system together such as Vue.js and Vuex. 同样,使用JS库和状态管理系统(例如Vue.js和Vuex)一起构建此应用程序也将更加容易。 Hope that helps. 希望能有所帮助。

Here's real world working example of my code 这是我的代码的真实工作示例

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

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