如何根据画布大小转换JSON数组变量中的值

时间:2014-05-11 21:53:55

标签: javascript arrays json canvas fabricjs

我用

JSON.stringify(mycanvas)

获取我的画布数组,例如:

{"objects":[{"type":"rect","originX":"left","originY":"top","left":0,"top":0,"width":118.33,"height":100,"fill":"rgba(227,92,12, 0.75)","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":"","rx":0,"ry":0,"x":0,"y":0}],"background":""}

这是另一个例子:

{"objects":[{"type":"rect","originX":"left","originY":"top","left":0,"top":441,"width":118,"height":100,"fill":"rgba(48,26,56, 0.75)","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":"","rx":0,"ry":0,"x":0,"y":0},{"type":"rect","originX":"left","originY":"top","left":118,"top":441,"width":118,"height":100,"fill":"rgba(8,170,1, 0.75)","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":"","rx":0,"ry":0,"x":0,"y":0},{"type":"rect","originX":"left","originY":"top","left":236,"top":441,"width":118,"height":100,"fill":"rgba(119,101,179, 0.75)","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":"","rx":0,"ry":0,"x":0,"y":0},{"type":"rect","originX":"left","originY":"top","left":354,"top":441,"width":118,"height":100,"fill":"rgba(50,94,159, 0.75)","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":"","rx":0,"ry":0,"x":0,"y":0},{"type":"rect","originX":"left","originY":"top","left":472,"top":441,"width":118,"height":100,"fill":"rgba(117,67,247, 0.75)","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":"","rx":0,"ry":0,"x":0,"y":0},{"type":"rect","originX":"left","originY":"top","left":590,"top":441,"width":118,"height":100,"fill":"rgba(221,146,196, 0.75)","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":"","rx":0,"ry":0,"x":0,"y":0},{"type":"rect","originX":"left","originY":"top","left":708,"top":441,"width":118,"height":100,"fill":"rgba(43,133,188, 0.75)","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":"","rx":0,"ry":0,"x":0,"y":0},{"type":"rect","originX":"left","originY":"top","left":826,"top":441,"width":118,"height":100,"fill":"rgba(83,43,237, 0.75)","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":"","rx":0,"ry":0,"x":0,"y":0},{"type":"rect","originX":"left","originY":"top","left":944,"top":441,"width":118,"height":100,"fill":"rgba(43,159,214, 0.75)","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":"","rx":0,"ry":0,"x":0,"y":0},{"type":"rect","originX":"left","originY":"top","left":1062,"top":441,"width":118,"height":100,"fill":"rgba(83,227,74, 0.75)","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":"","rx":0,"ry":0,"x":0,"y":0},{"type":"rect","originX":"left","originY":"top","left":1180,"top":441,"width":118,"height":100,"fill":"rgba(40,49,51, 0.75)","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":"","rx":0,"ry":0,"x":0,"y":0},{"type":"rect","originX":"left","originY":"top","left":1298,"top":417.24,"width":118,"height":100,"fill":"rgba(177,126,184, 0.75)","stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":1,"scaleY":1.24,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","rx":0,"ry":0,"x":0,"y":0}],"background":""}

当我尝试将相同的配置重新打开到新/空画布时,我遇到以下问题: JSON数组中的值与最初创建的画布大小匹配。当我的新/空画布具有另一个尺寸时,我需要按比例转换所有度量,因此它非常适合向上或缩小到我的新画布。

我不知道,如果玩宽度,高度只有帮助,请认为顶部和左侧值也需要转换。

我将如何做到这一点?

0 个答案:

没有答案