gpt4 book ai didi

javascript - fabric js 从 mysql 数据库加载单个对象到 Canvas

转载 作者:行者123 更新时间:2023-11-29 06:09:20 25 4
gpt4 key购买 nike

我有一个位于视频之上的 Canvas 。当用户暂停视频时,他们可以将 fabricjs 对象添加到 Canvas 。当一个对象被添加到 Canvas 时,它会作为 JSON 保存到 mysql 数据库中的一个表中。

当用户单击一个按钮时,它将查询 mysql 数据库中的记录,并通过 ajax 返回数组中每条记录的对象。

当它循环遍历这个数组时,我希望 fabricjs 一次渲染每个对象,直到渲染完所有对象。

我试过使用:

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

它将加载所有对象,但会在每次加载前清除 Canvas ,并且只会显示最后一个对象。

我在这里试过这个例子:

http://codepen.io/Kienz/pen/otyEz但似乎无法让它为我工作。我也试过http://jsfiddle.net/Kienz/bvmkQ/但看不出哪里出了问题。

所以我来找专家了!我感谢所有的帮助。谢谢。

这是我在 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":""}');

这是我的 php 文件:

$teles = Telestration::getTeleByVideo();

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

这是我的 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 在你的 success 函数中使用 canvas.loadFromJSON 而不是 fabric.util.enlivenObjects() 函数,像这样:

 //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

希望对你有帮助,祝你好运

关于javascript - fabric js 从 mysql 数据库加载单个对象到 Canvas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39299628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com