尝试从下面的代码构建。从我看到的, tcanvas 未在您的代码中定义。
tcanvas
// The Javascript code I'm trying to have the save me button to download the image: var saveme = document.getElementById("save"); var img = document.getElementById("img"); var tcanvas = document.getElementById("tcanvas"); // Drawing something on the canvas var ctx = tcanvas.getContext("2d"); ctx.beginPath(); ctx.arc(100, 200, 40, 0, 2 * Math.PI); ctx.fill(); ctx.stroke(); saveme.onclick = function(event) { img.src = tcanvas.toDataURL(); img.style.display = 'inline'; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="button" value="save me" id="save" /> <div id="shirtDiv" class="page" style="width: 530px; height: 630px; position: relative; background-color: rgb(255, 255, 255);"> <div id="drawingArea" style="position: absolute;top: 100px;left: 160px;z-index: 10;width: 200px;height: 400px;"> <canvas id="tcanvas" width=200 height="400" class="hover" style="-webkit-user-select: none;"></canvas> <img id="img" /> </div> </div>