我正在尝试使用画布和 toDataURL 获取 div 快照。 Div 快照是完美的,但其中没有 png。在这个配置中,我可以看到正确的画布,但有错误:
Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.
html2canvas(fieldSnap, {
allowTaint: true,
logging: true,
// useCORS: true,
width: 900,
height: 900,
backgroundColor: '#f2f2f2'
}).then(function(canvas) {
document.body.appendChild(canvas);
const imgData = canvas.toDataURL('image/png');
...
});
.htaccess 在 web 文件夹中:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
<IfModule mod_setenvif.c>
<IfModule mod_headers.c>
<FilesMatch "\.(bmp|cur|gif|ico|jpe?g|png|svgz?|webp|avifs?)$">
SetEnvIf Origin ":" IS_CORS
Header set Access-Control-Allow-Origin "http://localhost:4000" env=IS_CORS
</FilesMatch>
</IfModule>
</IfModule>