现在我将整个画布保存为图像并且工作正常,但我只想要一个已经绘制的矩形将其保存为图像。
我正在使用下面的代码来保存整个画布
//attempt to save base64 string to server using this var
var dataUrl = document.getElementById('canvas').toDataURL();
var windowContent = '<!DOCTYPE html>';
windowContent += '<html>'
windowContent += '<head><title>Print canvas</title></head>';
windowContent += '<body>'
windowContent += '<a href="' + dataUrl + '" target="_blank"><img src="' + dataUrl + '"></a>';
windowContent += '</body>';
windowContent += '</html>';
var printWin = window.open('','','width=890,height=820');
printWin.document.open();
printWin.document.write(windowContent);
printWin.document.close();
printWin.focus();
//Lets say i made a rectangle -- Just example
context.beginPath();
context.moveTo(x + 32, y + 32);
context.rect(locX+1, locY+1, 26, 26);
context.closePath();
context.fillStyle = color;
context.fill();