我现在有一个帆布条形码,它是由JsBarcode的react实现生成的,叫做react-条形码。目前,我可以右键单击画布,并打开它作为一个新的选项卡图像。如何通过单击按钮来实现此功能?
我已经检查了几个关于这个问题的答案,但是它们都使用jquery。我正在寻找实现与反应或纯js。
发布于 2019-03-05 10:28:19
HTMLCanvasElement.toDataURL()方法返回一个数据URI,其中包含由type参数指定的格式的图像表示(默认为PNG)。返回的图像分辨率为96 dpi。
我认为这样可以阻止window.open,但只需复制已记录的数据并将其粘贴到一个新的选项卡中即可。
const canvas = document.getElementById("canvas");
const button = document.getElementById("click");
const context = canvas.getContext("2d");
context.fillStyle = "#FF0000";
context.fillRect(20, 20, 150, 100);
button.addEventListener("click", function(){
const dataUrl = canvas.toDataURL("png");
console.log(dataUrl);
const win = window.open(dataUrl, '_blank');
});<canvas id="canvas" width="100" height="100"></canvas>
<button id="click">Click</button>
发布于 2020-07-30 14:16:43
如果图像数据大于git允许的最大值,请使用下面的代码来显示它
function openImage(base64URL){
var win = window.open();
win.document.write('<iframe src="' + base64URL + '" frameborder="0" style="border:0;
top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%;" allowfullscreen>
</iframe>');
}https://stackoverflow.com/questions/55000468
复制相似问题