Javascript 转换为JSON时出现问题
各位。我使用Fabric.js开发项目,在转换为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,-
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');'