创建图像 html2canvas 时图像文件夹上的 CORS 问题

时间:2021-02-03 13:35:11

标签: reactjs yii2 html2canvas

我正在尝试使用画布和 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>

0 个答案:

没有答案
相关问题