gpt4 book ai didi

javascript - fabric.js 加载带有插入图像的 json 正在破坏 Canvas

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

每当我将图像添加到 fabric.js 上的 Canvas 时,我将其保存并在隐藏字段中将其字符串化为 JSON,从 JSON 数据加载的 Canvas 在 console.log 上有 0 个对象,隐藏字段中有 JSON 数据/变量不完整。如果我加载已字符串化的 JSON 数据,图像将被插入并且是我可以编辑的 JSON 中的唯一对象。我的程序可以通过添加文本、形状等保存到 JSON,但是当我添加图像时会发生这种情况。

这是我用来添加图像的代码。

function insertImage(src){
fabric.Image.fromURL(src, function(image) {
var scale = 200 / image.width;
image.set({ left: 40, top: 40, scaleX: scale, scaleY: scale });

canvas.centerObject(image);
canvas.add(image);
canvas.renderAll();
});
}

$( ".image-graphic" ).click(function(){
var imageGraphic = $( this ).attr('src');
insertImage(imageGraphic);
});

这是之后运行的代码,将其转换为隐藏字段中的 JSON 数据。

$( "#testImage" ).click(function(){
var $additionalPropertiesData = canvas.toJSON(['selectable','lockmovementX', 'lockmovementY', 'hasControls', 'hasBorders', 'id']);
$( ".frontData" ).val(JSON.stringify($additionalPropertiesData));
var $frontPreviewData = $( ".frontData" ).val();
});

这是插入图像之前 JSON 数据的样子

{
"objects": [
{
"type": "i-text",
"originX": "center",
"originY": "top",
"left": 544.68,
"top": 144.16,
"width": 307.75,
"height": 65.54,
"fill": "#000",
"stroke": null,
"strokeWidth": 1,
"strokeDashArray": null,
"strokeLineCap": "butt",
"strokeLineJoin": "miter",
"strokeMiterLimit": 10,
"scaleX": 1.36,
"scaleY": 1.36,
"angle": 0,
"flipX": false,
"flipY": false,
"opacity": 1,
"shadow": null,
"visible": true,
"clipTo": null,
"backgroundColor": "",
"fillRule": "nonzero",
"globalCompositeOperation": "source-over",
"selectable": true,
"hasControls": true,
"hasBorders": true,
"id": "fullName",
"text": "YOUR NAME",
"fontSize": "50",
"fontWeight": "bold",
"fontFamily": "PT Serif",
"fontStyle": "",
"lineHeight": 1.16,
"textDecoration": "",
"textAlign": "center",
"textBackgroundColor": "",
"styles": {}
},
{
"type": "i-text",
"originX": "center",
"originY": "top",
"left": 544.68,
"top": 228.48,
"width": 135.85,
"height": 28.84,
"fill": "#000",
"stroke": null,
"strokeWidth": 1,
"strokeDashArray": null,
"strokeLineCap": "butt",
"strokeLineJoin": "miter",
"strokeMiterLimit": 10,
"scaleX": 1.36,
"scaleY": 1.36,
"angle": 0,
"flipX": false,
"flipY": false,
"opacity": 1,
"shadow": null,
"visible": true,
"clipTo": null,
"backgroundColor": "",
"fillRule": "nonzero",
"globalCompositeOperation": "source-over",
"selectable": true,
"hasControls": true,
"hasBorders": true,
"id": "jobTitle",
"text": "Your Job Title",
"fontSize": 22,
"fontWeight": "normal",
"fontFamily": "PT Serif",
"fontStyle": "",
"lineHeight": 1.16,
"textDecoration": "",
"textAlign": "center",
"textBackgroundColor": "",
"styles": {}
},
{
"type": "i-text",
"originX": "left",
"originY": "top",
"left": 45.02,
"top": 472.44,
"width": 109.35,
"height": 20.97,
"fill": "#000",
"stroke": null,
"strokeWidth": 1,
"strokeDashArray": null,
"strokeLineCap": "butt",
"strokeLineJoin": "miter",
"strokeMiterLimit": 10,
"scaleX": 1.36,
"scaleY": 1.36,
"angle": 0,
"flipX": false,
"flipY": false,
"opacity": 1,
"shadow": null,
"visible": true,
"clipTo": null,
"backgroundColor": "",
"fillRule": "nonzero",
"globalCompositeOperation": "source-over",
"selectable": true,
"hasControls": true,
"hasBorders": true,
"id": "phoneNumber",
"text": "O:888.888.8888",
"fontSize": "16",
"fontWeight": "normal",
"fontFamily": "PT Serif",
"fontStyle": "italic",
"lineHeight": 1.16,
"textDecoration": "",
"textAlign": "left",
"textBackgroundColor": "",
"styles": {}
},
{
"type": "i-text",
"originX": "center",
"originY": "top",
"left": 544.68,
"top": 384.88,
"width": 237.27,
"height": 28.84,
"fill": "#000",
"stroke": null,
"strokeWidth": 1,
"strokeDashArray": null,
"strokeLineCap": "butt",
"strokeLineJoin": "miter",
"strokeMiterLimit": 10,
"scaleX": 1.36,
"scaleY": 1.36,
"angle": 0,
"flipX": false,
"flipY": false,
"opacity": 1,
"shadow": null,
"visible": true,
"clipTo": null,
"backgroundColor": "",
"fillRule": "nonzero",
"globalCompositeOperation": "source-over",
"selectable": true,
"hasControls": true,
"hasBorders": true,
"id": "companyName",
"text": "COMPANY NAME HERE",
"fontSize": 22,
"fontWeight": "normal",
"fontFamily": "PT Serif",
"fontStyle": "",
"lineHeight": 1.16,
"textDecoration": "",
"textAlign": "center",
"textBackgroundColor": "",
"styles": {}
},
{
"type": "i-text",
"originX": "left",
"originY": "top",
"left": 461.61,
"top": 436.56,
"width": 122.17,
"height": 18.35,
"fill": "#000",
"stroke": null,
"strokeWidth": 1,
"strokeDashArray": null,
"strokeLineCap": "butt",
"strokeLineJoin": "miter",
"strokeMiterLimit": 10,
"scaleX": 1.36,
"scaleY": 1.36,
"angle": 0,
"flipX": false,
"flipY": false,
"opacity": 1,
"shadow": null,
"visible": true,
"clipTo": null,
"backgroundColor": "",
"fillRule": "nonzero",
"globalCompositeOperation": "source-over",
"selectable": true,
"hasControls": true,
"hasBorders": true,
"id": "addressLineOne",
"text": "Company Address 1",
"fontSize": "14",
"fontWeight": "normal",
"fontFamily": "Open Sans",
"fontStyle": "italic",
"lineHeight": 1.16,
"textDecoration": "",
"textAlign": "left",
"textBackgroundColor": "",
"styles": {}
},
{
"type": "i-text",
"originX": "center",
"originY": "top",
"left": 544.68,
"top": 474.64,
"width": 87.47,
"height": 18.35,
"fill": "#000",
"stroke": null,
"strokeWidth": 1,
"strokeDashArray": null,
"strokeLineCap": "butt",
"strokeLineJoin": "miter",
"strokeMiterLimit": 10,
"scaleX": 1.36,
"scaleY": 1.36,
"angle": 0,
"flipX": false,
"flipY": false,
"opacity": 1,
"shadow": null,
"visible": true,
"clipTo": null,
"backgroundColor": "",
"fillRule": "nonzero",
"globalCompositeOperation": "source-over",
"selectable": true,
"hasControls": true,
"hasBorders": true,
"id": "addressLineTwo",
"text": "Address Line 2",
"fontSize": "14",
"fontWeight": "normal",
"fontFamily": "PT Serif",
"fontStyle": "italic",
"lineHeight": 1.16,
"textDecoration": "",
"textAlign": "center",
"textBackgroundColor": "",
"styles": {}
},
{
"type": "i-text",
"originX": "left",
"originY": "top",
"left": 47.08,
"top": 433.17,
"width": 107.83,
"height": 20.97,
"fill": "#000",
"stroke": null,
"strokeWidth": 1,
"strokeDashArray": null,
"strokeLineCap": "butt",
"strokeLineJoin": "miter",
"strokeMiterLimit": 10,
"scaleX": 1.36,
"scaleY": 1.36,
"angle": 0,
"flipX": false,
"flipY": false,
"opacity": 1,
"shadow": null,
"visible": true,
"clipTo": null,
"backgroundColor": "",
"fillRule": "nonzero",
"globalCompositeOperation": "source-over",
"selectable": true,
"hasControls": true,
"hasBorders": true,
"id": "cellNumber",
"text": "C:888.888.8888",
"fontSize": "16",
"fontWeight": "normal",
"fontFamily": "PT Serif",
"fontStyle": "italic",
"lineHeight": 1.16,
"textDecoration": "",
"textAlign": "left",
"textBackgroundColor": "",
"styles": {}
},
{
"type": "i-text",
"originX": "right",
"originY": "top",
"left": 1044.34,
"top": 433,
"width": 153.52,
"height": 20.97,
"fill": "#000",
"stroke": null,
"strokeWidth": 1,
"strokeDashArray": null,
"strokeLineCap": "butt",
"strokeLineJoin": "miter",
"strokeMiterLimit": 10,
"scaleX": 1.36,
"scaleY": 1.36,
"angle": 0,
"flipX": false,
"flipY": false,
"opacity": 1,
"shadow": null,
"visible": true,
"clipTo": null,
"backgroundColor": "",
"fillRule": "nonzero",
"globalCompositeOperation": "source-over",
"selectable": true,
"hasControls": true,
"hasBorders": true,
"id": "emailAddress",
"text": "youremail@email.com",
"fontSize": "16",
"fontWeight": "normal",
"fontFamily": "PT Serif",
"fontStyle": "italic",
"lineHeight": 1.16,
"textDecoration": "",
"textAlign": "right",
"textBackgroundColor": "",
"styles": {}
},
{
"type": "i-text",
"originX": "right",
"originY": "top",
"left": 1042.98,
"top": 472.44,
"width": 172.43,
"height": 20.97,
"fill": "#000",
"stroke": null,
"strokeWidth": 1,
"strokeDashArray": null,
"strokeLineCap": "butt",
"strokeLineJoin": "miter",
"strokeMiterLimit": 10,
"scaleX": 1.36,
"scaleY": 1.36,
"angle": 0,
"flipX": false,
"flipY": false,
"opacity": 1,
"shadow": null,
"visible": true,
"clipTo": null,
"backgroundColor": "",
"fillRule": "nonzero",
"globalCompositeOperation": "source-over",
"selectable": true,
"hasControls": true,
"hasBorders": true,
"id": "webAddress",
"text": "www.websiteaddress.com",
"fontSize": "16",
"fontWeight": "normal",
"fontFamily": "PT Serif",
"fontStyle": "italic",
"lineHeight": 1.16,
"textDecoration": "",
"textAlign": "right",
"textBackgroundColor": "",
"styles": {}
}
],
"background": "",
"backgroundImage": {
"type": "image",
"originX": "left",
"originY": "top",
"left": 0,
"top": 0,
"width": 1088,
"height": 638,
"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": "",
"fillRule": "nonzero",
"globalCompositeOperation": "source-over",
"src": "/images/sample.jpg",
"filters": [],
"crossOrigin": "",
"alignX": "none",
"alignY": "none",
"meetOrSlice": "meet"
}
}

这是插入图像后发生的情况。此 JSON 数据无法正确加载并在 console.log 上显示带有 0 个对象的 Canvas 。

{
"objects": [
{
"type": "i-text",
"originX": "center",
"originY": "top",
"left": 544.68,
"top": 144.16,
"width": 307.75,
"height": 65.54,
"fill": "#000",
"stroke": null,
"strokeWidth": 1,
"strokeDashArray": null,
"strokeLineCap": "butt",
"strokeLineJoin": "miter",
"strokeMiterLimit": 10,
"scaleX": 1.36,
"scaleY": 1.36,
"angle": 0,
"flipX": false,
"flipY": false,
"opacity": 1,
"shadow": null,
"visible": true,
"clipTo": null,
"backgroundColor": "",
"fillRule": "nonzero",
"globalCompositeOperation": "source-over",
"selectable": true,
"hasControls": true,
"hasBorders": true,
"id": "fullName",
"text": "YOUR NAME",
"fontSize": "50",
"fontWeight": "bold",
"fontFamily": "PT Serif",
"fontStyle": "",
"lineHeight": 1.16,
"textDecoration": "",
"textAlign": "center",
"textBackgroundColor": "",
"styles": {}
},
{
"type": "i-text",
"originX": "center",
"originY": "top",
"left": 544.68,
"top": 228.48,
"width": 135.85,
"height": 28.84,
"fill": "#000",
"stroke": null,
"strokeWidth": 1,
"strokeDashArray": null,
"strokeLineCap": "butt",
"strokeLineJoin": "miter",
"strokeMiterLimit": 10,
"scaleX": 1.36,
"scaleY": 1.36,
"angle": 0,
"flipX": false,
"flipY": false,
"opacity": 1,
"shadow": null,
"visible": true,
"clipTo": null,
"backgroundColor": "",
"fillRule": "nonzero",
"globalCompositeOperation": "source-over",
"selectable": true,
"hasControls": true,
"hasBorders": true,
"id": "jobTitle",
"text": "Your Job Title",
"fontSize": 22,
"fontWeight": "normal",
"fontFamily": "PT Serif",
"fontStyle": "",
"lineHeight": 1.16,
"textDecoration": "",
"textAlign": "center",
"textBackgroundColor": "",
"styles": {}
},
{
"type": "i-text",
"originX": "left",
"originY": "top",
"left": 45.02,
"top": 472.44,
"width": 109.35,
"height": 20.97,
"fill": "#000",
"stroke": null,
"strokeWidth": 1,
"strokeDashArray": null,
"strokeLineCap": "butt",
"strokeLineJoin": "miter",
"strokeMiterLimit": 10,
"scaleX": 1.36,
"scaleY": 1.36,
"angle": 0,
"flipX": false,
"flipY": false,
"opacity": 1,
"shadow": null,
"visible": true,
"clipTo": null,
"backgroundColor": "",
"fillRule": "nonzero",
"globalCompositeOperation": "source-over",
"selectable": true,
"hasControls": true,
"hasBorders": true,
"id": "phoneNumber",
"text": "O:888.888.8888",
"fontSize": "16",
"fontWeight": "normal",
"fontFamily": "PT Serif",
"fontStyle": "italic",
"lineHeight": 1.16,
"textDecoration": "",
"textAlign": "left",
"textBackgroundColor": "",
"styles": {}
},
{
"type": "i-text",
"originX": "center",
"originY": "top",
"left": 544.68,
"top": 384.88,
"width": 237.27,
"height": 28.84,
"fill": "#000",
"stroke": null,
"strokeWidth": 1,
"strokeDashArray": null,
"strokeLineCap": "butt",
"strokeLineJoin": "miter",
"strokeMiterLimit": 10,
"scaleX": 1.36,
"scaleY": 1.36,
"angle": 0,
"flipX": false,
"flipY": false,
"opacity": 1,
"shadow": null,
"visible": true,
"clipTo": null,
"backgroundColor": "",
"fillRule": "nonzero",
"globalCompositeOperation": "source-over",
"selectable": true,
"hasControls": true,
"hasBorders": true,
"id": "companyName",
"text": "COMPANY NAME HERE",
"fontSize": 22,
"fontWeight": "normal",
"fontFamily": "PT Serif",
"fontStyle": "",
"lineHeight": 1.16,
"textDecoration": "",
"textAlign": "center",
"textBackgroundColor": "",
"styles": {}
},
{
"type": "i-text",
"originX": "left",
"originY": "top",
"left": 461.61,
"top": 436.56,
"width": 122.17,
"height": 18.35,
"fill": "#000",
"stroke": null,
"strokeWidth": 1,
"strokeDashArray": null,
"strokeLineCap": "butt",
"strokeLineJoin": "miter",
"strokeMiterLimit": 10,
"scaleX": 1.36,
"scaleY": 1.36,
"angle": 0,
"flipX": false,
"flipY": false,
"opacity": 1,
"shadow": null,
"visible": true,
"clipTo": null,
"backgroundColor": "",
"fillRule": "nonzero",
"globalCompositeOperation": "source-over",
"selectable": true,
"hasControls": true,
"hasBorders": true,
"id": "addressLineOne",
"text": "Company Address 1",
"fontSize": "14",
"fontWeight": "normal",
"fontFamily": "Open Sans",
"fontStyle": "italic",
"lineHeight": 1.16,
"textDecoration": "",
"textAlign": "left",
"textBackgroundColor": "",
"styles": {}
},
{
"type": "i-text",
"originX": "center",
"originY": "top",
"left": 544.68,
"top": 474.64,
"width": 87.47,
"height": 18.35,
"fill": "#000",
"stroke": null,
"strokeWidth": 1,
"strokeDashArray": null,
"strokeLineCap": "butt",
"strokeLineJoin": "miter",
"strokeMiterLimit": 10,
"scaleX": 1.36,
"scaleY": 1.36,
"angle": 0,
"flipX": false,
"flipY": false,
"opacity": 1,
"shadow": null,
"visible": true,
"clipTo": null,
"backgroundColor": "",
"fillRule": "nonzero",
"globalCompositeOperation": "source-over",
"selectable": true,
"hasControls": true,
"hasBorders": true,
"id": "addressLineTwo",
"text": "Address Line 2",
"fontSize": "14",
"fontWeight": "normal",
"fontFamily": "PT Serif",
"fontStyle": "italic",
"lineHeight": 1.16,
"textDecoration": "",
"textAlign": "center",
"textBackgroundColor": "",
"styles": {}
},
{
"type": "i-text",
"originX": "left",
"originY": "top",
"left": 47.08,
"top": 433.17,
"width": 107.83,
"height": 20.97,
"fill": "#000",
"stroke": null,
"strokeWidth": 1,
"strokeDashArray": null,
"strokeLineCap": "butt",
"strokeLineJoin": "miter",
"strokeMiterLimit": 10,
"scaleX": 1.36,
"scaleY": 1.36,
"angle": 0,
"flipX": false,
"flipY": false,
"opacity": 1,
"shadow": null,
"visible": true,
"clipTo": null,
"backgroundColor": "",
"fillRule": "nonzero",
"globalCompositeOperation": "source-over",
"selectable": true,
"hasControls": true,
"hasBorders": true,
"id": "cellNumber",
"text": "C:888.888.8888",
"fontSize": "16",
"fontWeight": "normal",
"fontFamily": "PT Serif",
"fontStyle": "italic",
"lineHeight": 1.16,
"textDecoration": "",
"textAlign": "left",
"textBackgroundColor": "",
"styles": {}
},
{
"type": "i-text",
"originX": "right",
"originY": "top",
"left": 1044.34,
"top": 433,
"width": 153.52,
"height": 20.97,
"fill": "#000",
"stroke": null,
"strokeWidth": 1,
"strokeDashArray": null,
"strokeLineCap": "butt",
"strokeLineJoin": "miter",
"strokeMiterLimit": 10,
"scaleX": 1.36,
"scaleY": 1.36,
"angle": 0,
"flipX": false,
"flipY": false,
"opacity": 1,
"shadow": null,
"visible": true,
"clipTo": null,
"backgroundColor": "",
"fillRule": "nonzero",
"globalCompositeOperation": "source-over",
"selectable": true,
"hasControls": true,
"hasBorders": true,
"id": "emailAddress",
"text": "youremail@email.com",
"fontSize": "16",
"fontWeight": "normal",
"fontFamily": "PT Serif",
"fontStyle": "italic",
"lineHeight": 1.16,
"textDecoration": "",
"textAlign": "right",
"textBackgroundColor": "",
"styles": {}
},
{
"type": "i-text",
"originX": "right",
"originY": "top",
"left": 1042.98,
"top": 472.44,
"width": 172.43,
"height": 20.97,
"fill": "#000",
"stroke": null,
"strokeWidth": 1,
"strokeDashArray": null,
"strokeLineCap": "butt",
"strokeLineJoin": "miter",
"strokeMiterLimit": 10,
"scaleX": 1.36,
"scaleY": 1.36,
"angle": 0,
"flipX": false,
"flipY": false,
"opacity": 1,
"shadow": null,
"visible": true,
"clipTo": null,
"backgroundColor": "",
"fillRule": "nonzero",
"globalCompositeOperation": "source-over",
"selectable": true,
"hasControls": true,
"hasBorders": true,
"id": "webAddress",
"text": "www.websiteaddress.com",
"fontSize": "16",
"fontWeight": "normal",
"fontFamily": "PT Serif",
"fontStyle": "italic",
"lineHeight": 1.16,
"textDecoration": "",
"textAlign": "right",
"textBackgroundColor": "",
"styles": {}
},
{
"type": "image",
"originX": "left",
"originY": "top",
"left": 300,
"top": 130.27,
"width": 396,
"height": 413,
"fill": "rgb(0,0,0)",
"stroke": null,
"strokeWidth": 1,
"strokeDashArray": null,
"strokeLineCap": "butt",
"strokeLineJoin": "miter",
"strokeMiterLimit": 10,
"scaleX": 0.51,
"scaleY": 0.51,
"angle": 0,
"flipX": false,
"flipY": false,
"opacity": 1,
"shadow": null,
"visible": true,
"clipTo": null,
"backgroundColor": "",
"fillRule": "nonzero",
"globalCompositeOperation": "source-over",
"selectable": false,
"hasControls": false,
"hasBorders": false,
"src": "/images/stock/8ball.jpg",
"filters": [],
"crossOrigin": "",
"alignX": "none",
"alignY": "none",
"meetOrSlice": "meet"
}
],
"background": "",
"backgroundImage": {
"type": "image",
"originX": "left",
"originY": "top",
"left": 0,
"top": 0,
"width": 1088,
"height": 638,
"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": "",
"fillRule": "nonzero",
"globalCompositeOperation": "source-over",
"src": "/images/sample.jpg",
"filters": [],
"crossOrigin": "",
"alignX": "none",
"alignY": "none",
"meetOrSlice": "meet"
}
}

最佳答案

你如何加载你的 json 数据?

尝试:

canvas.loadFromJSON(json, canvas.renderAll.bind(canvas));

这对我有用。

加载图片时需要回调函数。

我的(归结)解决方案看起来像

// activepage contains the currently used canvas

this.clickOpenHandler = function(event) {

var file = event.target.files[0];
// file holds now an [file Object]

if (file) {
var reader = new FileReader();
reader.onload = function(e_onload) {
var content = e_onload.target.result;
// content holds now an [object File], like {"objects":[{"type":"rect","originX":"left", ...

activepage.canvas.loadFromJSON(content, function () {
activepage.canvas.renderAll.bind(activepage.canvas);

// Here the canvas already holds the read objects.
console.log(activepage.canvas.getObjects().length); // gives 2 in my testcase

activepage.canvas.renderAll();
$("#pty_open").val(null); // reset input element
});
}
reader.readAsText(file);
}
};

使用这个 HTML 输入元素,您可以选择一个本地 JSON 文件,这会触发 onchange 事件:

   <input id="pty_open" type="file" />

希望这个例子对你有帮助。

PS:注意 onchange 事件。它仅在文件名与上一个文件名不同时触发。当您第二次选择同一个文件时,onchange 不会触发并且什么也不会发生。我用 $("#pty_open").val(null); 语句阻止了这种情况。

关于javascript - fabric.js 加载带有插入图像的 json 正在破坏 Canvas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31274656/

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