У меня есть кусок HTML кода с холстом, который я хотел бы дублировать нажатием кнопки. Я пробовал этот код до сих пор, но я немного не понимаю, чего не хватает. Если бы вы могли включить любой фрагмент кода, это было бы очень полезно для меня, поскольку я новичок. Спасибо
<canvas id="myCanvas" width="800px" height="800px"></canvas>
<script>
var oldCnv = document.getElementById("myCanvas");
function cloneCanvas(oldCanvas) {
//create a new canvas
var newCanvas = document.createElement("canvas");
var context = newCanvas.getContext("2d");
//set dimensions
newCanvas.width = oldCanvas.width;
newCanvas.height = oldCanvas.height;
//apply the old canvas to the new one
context.drawImage(oldCanvas, 0, 0);
//return the new canvas
return newCanvas;
//append the new canvas on the page
document.body.appendChild(newCanvas);
}
</script>
<button onclick="cloneCanvas(oldCnv)">add canvas</button>