我正在尝试将图像(我的图形的背景图像)与canvasJS图形合并。
在将这些画布合并到画布后,我想获得一个这个画布的DataURL,这将允许我下载它的图像(图形的图像及其各自的背景图像)。
我一直在玩JS Fiddles试图让它工作一段时间,但我似乎无法弄明白。
这是我目前的小提琴:
http://jsfiddle.net/dyt943s0/19/
我通过将其他两个小提琴的代码合并在一起创造了小提琴:
1)https://jsfiddle.net/cuajw85L/1/(合并两个画布并创建它们的图像)。 和 2)http://jsfiddle.net/canvasjs/muqb7d3n/(创建CanvasJS图的图像)
以下是我一直在努力的小提琴上的代码:
JS:
var chart = new CanvasJS.Chart("chartContainer",
{
title: {
text: "Exporting chart using toDataurl"
},
data: [
{
type: "spline",
dataPoints: [
{ x: 10, y: 4 },
{ x: 20, y: 7 },
{ x: 30, y: 2 },
{ x: 40, y: 3 },
{ x: 50, y: 5 }
]
}
]
});
chart.render();
var img=new Image();
img.crossOrigin='anonymous';
img.onload=start;
img.src="https://dl.dropboxusercontent.com/u/139992952/multple/fish.jpg";
function start(){
var bottleCanvas = document.getElementById('bottleCanvas');
var designCanvas = $(".canvasjs-chart-canvas");
var ctxb=bottleCanvas.getContext('2d');
var ctxd=editorCanvas.getContext('2d');
ctxb.drawImage(img,0,0);
ctxd.fillRect(50,50,50,50);
downloadCanvas();
}
function downloadCanvas() {
var bottleCanvas = document.getElementById('bottleCanvas');
var designCanvas = $(".canvasjs-chart-canvas");
var bottleContext = bottleCanvas.getContext('2d');
bottleContext.drawImage(designCanvas, 69, 50);
var dataURL = bottleCanvas.toDataURL("image/png");
var link = document.getElementById('btn-download');
link.download = "bottle-design.png";
link.href = bottleCanvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
}
HTML:
<br/><!-- Just so that JSFiddle's Result label doesn't overlap the Chart -->
<canvas id="bottleCanvas" width=300 height=300></canvas>
<canvas id="editorCanvas" width=300 height=300></canvas>
<div id="chartContainer" style="height: 360px; width: 100%;"></div>
<a href="#" id="btn-download" download="chart.png" target="_blank">Download</a>
答案 0 :(得分:0)
想出来!
而不是var designCanvas = $(".canvasjs-chart-canvas");
,请使用var designCanvas = $("#chartContainer .canvasjs-chart-canvas").get(0);