简体   繁体   English

Fabric JS从MySQL数据库将单个对象加载到画布

[英]fabric js load single objects to canvas from mysql database

I have a canvas that lives on top of a video. 我有一块可以放在视频上方的画布。 When a user pauses the video they are able to add fabricjs objects to the canvas. 当用户暂停视频时,他们能够将fabricjs对象添加到画布。 When an object is added to the canvas it is saved to a table in a mysql database as JSON. 将对象添加到画布后,它会以JSON格式保存到mysql数据库的表中。

When the user clicks a button it will query the mysql database for records and return via ajax the objects for each record in an array. 当用户单击一个按钮时,它将查询mysql数据库中的记录,并通过ajax返回数组中每个记录的对象。

As it loops through this array I want fabricjs to render each object one at a time until all objects have been rendered. 当它遍历此数组时,我希望fabricjs一次渲染每个对象,直到渲染完所有对象。

I have tried using : 我尝试使用:

canvas.loadFromJSON(rData, canvas.renderAll.bind(canvas), function(o, object) {
  fabric.log(o, object);
});

It will load all objects but clears the canvas before each load and will only display the last object. 它将加载所有对象,但在每次加载之前清除画布,并且仅显示最后一个对象。

I have tried the example here: 我在这里尝试了示例:

http://codepen.io/Kienz/pen/otyEz but cannot seem to get it to work for me. http://codepen.io/Kienz/pen/otyEz,但似乎无法为我工作。 I have also tried http://jsfiddle.net/Kienz/bvmkQ/ but cannot see what is wrong. 我也尝试过http://jsfiddle.net/Kienz/bvmkQ/,但是看不到有什么问题。

So I have come to the experts! 所以我来找专家! I appreciate all and any help. 我感谢所有帮助。 Thank you. 谢谢。

Here is my table in mysql wth 2 records: 这是我在mysql中有2条记录的表:

CREATE TABLE IF NOT EXISTS `telestrations` (
  `id_telestration` int(11) NOT NULL AUTO_INCREMENT,
  `object` longtext NOT NULL,
  PRIMARY KEY (`id_telestration`),
  UNIQUE KEY `id_telestration` (`id_telestration`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=65 ;

--
-- Dumping data for table `telestrations`
--

INSERT INTO `telestrations` (`id_telestration`, `object`) VALUES
(63, '{"objects":[{"type":"i-text","originX":"left","originY":"top","left":161.05,"top":359.29,"width":69.3,"height":20.97,"fill":"#e6977e","stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","text":"AAAAAA","fontSize":16,"fontWeight":"bold","fontFamily":"arial","fontStyle":"","lineHeight":1.16,"textDecoration":"","textAlign":"left","textBackgroundColor":"","styles":{"0":{"1":{},"2":{},"3":{},"4":{},"5":{}}}}],"background":""}'),
(64, '{"objects":[{"type":"i-text","originX":"left","originY":"top","left":463.68,"top":353.84,"width":69.3,"height":20.97,"fill":"#20f20e","stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","text":"BBBBBB","fontSize":16,"fontWeight":"bold","fontFamily":"arial","fontStyle":"","lineHeight":1.16,"textDecoration":"","textAlign":"left","textBackgroundColor":"","styles":{"0":{"1":{},"2":{},"3":{},"4":{},"5":{}}}}],"background":""}');

Here is my php file: 这是我的php文件:

$teles = Telestration::getTeleByVideo();

$objArray = array();
foreach($teles as $tele){
    $obj = $tele->getValueEncoded('object');
    $objArray[] = $obj;
}
echo json_encode($objArray);

Here is my JavaScript: 这是我的JavaScript:

document.getElementById("get_json").onclick = function(ev) {    
  $.ajax({
    url: 'getTelestrations.php',
    data: dataString,
    type: 'POST',
    dataType:"json",
    success: function(data){
      for (var i = 0; i < data.length; i++) {
        rData = data[i].replace(/&quot;/g, '\"');
        //Do something
        canvas.loadFromJSON(rData, canvas.renderAll.bind(canvas), function(o, object) {
          fabric.log(o, object);
        }); 
      }
    }
  }); 
}

Hello inside your success function use instead of canvas.loadFromJSON , the fabric.util.enlivenObjects() funtion, like this: 您好,您可以在成功函数中使用fabric.util.enlivenObjects()函数而不是canvas.loadFromJSON ,如下所示:

 //inside the success function, you get the results data from the server and loop inside the items, allright if you have only one object but use      loop for more.
   results.data.forEach(function (object) {
       var tmpObject = JSON.parse(object.table_data);

       fabric.util.enlivenObjects([tmpObject], function (objects) {
             var origRenderOnAddRemove = canvas.renderOnAddRemove;
             canvas.renderOnAddRemove = false;
             console.log(objects);
             objects.forEach(function (o) {
                     canvas.add(o);
                     console.log(o);
              });

            canvas.renderOnAddRemove = origRenderOnAddRemove;
            canvas.renderAll();
         });//end enlivenObjects
    });//end data.forEach

Hope this helps, good luck 希望这有帮助,祝你好运

I was able to figure out how to load each object. 我能够弄清楚如何加载每个对象。 It turns that the json returned from my mysql was not "workable" for fabricjs. 事实证明,从我的mysql返回的json对于fabricjs而言“不可行”。 I had to clean my json and then the objects would load. 我必须清理我的json,然后才能加载对象。

I only made changes to my javascript: 我只对我的JavaScript进行过更改:

    $.ajax({
        url: 'getTelestrations.php',
        data: dataString,
        type: 'POST',
        dataType:"json",
        success: function(data){
            for (var i = 0; i < data.length; i++) {
                //clean results for json
                json_result = data[i].replace(/&quot;/g, '\"');             //remove quotes from entire result  
                json_clean = json_result.replace(/"objects"/, 'objects');       //remove quotes around first object
                jsonObj = JSON.parse(JSON.stringify(json_clean));               // parse the entire result
                jsonobj2 = eval('(' + jsonObj + ')');
                // Add object to canvas
                var obj = new fabric[fabric.util.string.camelize(fabric.util.string.capitalize(jsonobj2.objects[0].type))].fromObject(jsonobj2.objects[0]); 
                canvas.add(obj); 
                canvas.renderAll();
            }
        }
    }); 

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

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