将Canvas中的对象与JSON字符串进行比较

时间:2017-01-02 13:57:26

标签: javascript json canvas fabricjs

有没有办法可以循环播放画布中的所有对象,并将其JSON字符串与我存储的JSON字符串进行比较。如果它们相等,则将Object Object设置为ActiveObject。 下面是我尝试过的代码,但没有成功的结果

var datatocompare = '[{"type":"group","originX":"center","originY":"center","left":161.82,"top":76.48,"width":174.38,"height":59.05,"fill":"rgb(0,0,0)","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":"","objects":[{"type":"line","originX":"center","originY":"center","left":0,"top":-0.01,"width":172.38,"height":1,"fill":"transparent","stroke":"transparent","strokeWidth":2,"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":"","x1":75.6300048828125,"y1":75.98500061035156,"x2":248.00997924804687,"y2":75.98500061035156},{"type":"rect","originX":"left","originY":"top","left":-86.19,"top":-30.03,"width":172.38,"height":59.05,"fill":"yellow","stroke":"red","strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":0.3,"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","rx":0,"ry":0,"x":0,"y":0}]}]';
for (var i = 0; i < canvas.getObjects().length; i++) {
var dummy = canvas.getObjects();
if(datatocompare == JSON.stringify(dummy))
{
canvas.setActiveObject(canvas.getObjects());
}
}

0 个答案:

没有答案