I am trying to clone the canvas element drawn by https://github.com/Kaiido/SVG2Bitmap/blob/master/SVG2Bitmap.js
library, and it is not working. The library successfully creates and appends canvas
element to html
, but when I try cloning it, it just appends empty canvas
elements. Here is the code I am trying to use:
let svg = d3.select("div#main svg").node();
let canvas = d3.select("#canvas0")
.append("canvas")
.attr("width", 500)
.attr("height", 500)
.node();
SVG2Bitmap(svg, canvas);
this.props.datum.map((clusterObj, idx) => {
if (idx > 0) {
let canvasId = "div#canvas" + idx;
let clonedCanvas = this.canvasClone(canvas);
d3.select(canvasId)
.append(function() {
return clonedCanvas;
});
}
})
Apparently I am selecting the correct div
tags with d3
and appending canvas
elements to them, but they are empty, nothing is drawn in them.
canvasClone()
function was taken from: Appending a div containing canvas is not working
And is:
function canvasClone(c1) {
var c2 = document.createElement('canvas');
c2.width=c1.width;
c2.height=c1.height;
c2.getContext("2d").drawImage(c1, 0,0);
return c2;
}
The only warning message that I see in the console is:
Any suggestions would be greatly appreciated.
Update
I tried also:
let ctx = c1.getContext("2d");
let imageData = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height);
c2.getContext("2d").putImageData(imageData, 0, 0);
And nothing happens, not working
Update
I tried doing the following, considering that I can use the callback function, but it is still unsuccessfull:
let svg = d3.select("div#main svg").node();
SVG2Bitmap(svg, function(canvas) {
this.props.datum.map((clusterObj, idx) => {
let canvasId = "div#canvas" + idx;
let clonedCanvas = this.canvasClone(canvas);
d3.select(canvasId)
.append(function() {
return clonedCanvas;
});
})
});