Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 转换为JSON时出现问题_Javascript_Json_Canvas_Fabricjs - Fatal编程技术网

Javascript 转换为JSON时出现问题

Javascript 转换为JSON时出现问题,javascript,json,canvas,fabricjs,Javascript,Json,Canvas,Fabricjs,各位。我使用Fabric.js开发项目,在转换为JSON时发现了一个问题 我裁剪了一个物体。对于裁剪,我使用以下功能: activeObject.clipTo = function(ctx) { ctx.beginPath(); ctx.fillStyle="#A2322E"; ctx.moveTo(-leftPointCropShape, -topPointCropShape+storonaX); ctx.lineTo(leftPointCropShape,-

各位。我使用Fabric.js开发项目,在转换为JSON时发现了一个问题

我裁剪了一个物体。对于裁剪,我使用以下功能:

 activeObject.clipTo = function(ctx) {
    ctx.beginPath();
    ctx.fillStyle="#A2322E";
    ctx.moveTo(-leftPointCropShape, -topPointCropShape+storonaX);
    ctx.lineTo(leftPointCropShape,-topPointCropShape);
    ctx.lineTo(leftPointCropShape,topPointCropShape);
    ctx.lineTo(-leftPointCropShape,topPointCropShape);
    ctx.closePath();
    ctx.fill();
};`
JSON.stringify(canvas.toDatalessJSON())之后我有下一个字符串:

> {"objects":[{"type":"image","originX":"left","originY":"top","left":254,"top":44,"width":88,"height":144,"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":"function(ctx)
{\r\n\t\tctx.beginPath();\r\n\t//ctx.fillStyle=\"#A2322E\";
\r\n\tctx.moveTo(-leftPointCropShape,-topPointCropShape+storonaX);
\r\n\tctx.lineTo(leftPointCropShape,-topPointCropShape);
\r\n\tctx.lineTo(leftPointCropShape,topPointCropShape);
\r\n\tctx.lineTo(-leftPointCropShape,topPointCropShape);
\r\n\tctx.closePath();\r\n\t//ctx.fill();\r\n\t}",
"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"multiply","src":"http://localhost/test20/image/elements/shtora/3.png","filters":[],"crossOrigin":"","alignX":"none","alignY":"none","meetOrSlice":"meet"}],"background":"#F5F5F5"}
>
问题是JSON中有变量的名称,而不是它们的值

ctx.lineTo(leftPointCropShape,-topPointCropShape);
是织物的臭虫吗


如何在JSON中插入值而不是变量名“leftPointCropShape”、“topPointCropShape”?

看起来您正在尝试对函数进行字符串化。你不能那样做。更重要的是,为什么要这样做?当然可以,但它不是很有用。它可能对代码生成有用。直到
JSON.stringify
replacer参数起作用为止。:)撇开玩笑不谈,你能做的就是通过将字符串具体化来创建你自己的字符串。类似于:'\r\n\tctx.moveTo(+'-leftPointCropShape'+,'-topPointCropShape+storonaX');'看起来您正在尝试对函数进行字符串化。你不能那样做。更重要的是,为什么要这样做?当然可以,但它不是很有用。它可能对代码生成有用。直到
JSON.stringify
replacer参数起作用为止。:)撇开玩笑不谈,你能做的就是通过将字符串具体化来创建你自己的字符串。类似于:'\r\n\tctx.moveTo(+'-leftPointCropShape'+,'-topPointCropShape+storonaX');'