账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    使用 html2canvas 截取页面生成图片,报错 Tainted canvases may not be exported
    69
    0
    因为截取的页面dom中,部分元素有引入cdn的图片所以直接用 html2canvas 生成开始会报跨域的错,配置了 useCORS: true 也依旧报错然后我找到的解决方法是,在 html2canvas 里配置 allowTaint: true但是在调用 html2canvas 的回调方法 toDataURL 的时候又报错了,就是标题上的错误Tainted canvases may not be exported应该如何解决呢?下面是相关的代码const dom = document.querySelector('#poster')const opt = { allowTaint: true, // Whether to allow cross-origin images to taint the canvas // useCORS: true, // 使用跨域(当allowTaint为true时这段代码没什么用) imageTimeout: 5000 // 设置图片的超时,设置0为禁用 }html2canvas(dom, opt).then(canvas => { const imageUrl = canvas.toDataURL('image/png') // 将canvas转成base64图片格式 this.canvasImageUrl = imageUrl this.isDom = false})
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 久而旧之╮ 普通会员 1楼

      Tainted canvases refers to a canvas object that has been used before, potentially contaminated by user actions or JavaScript code. This can lead to unexpected behavior in HTML2Canvas and other tools that work with canvas. To avoid this error, you can check the canvas object's context before using it to generate an image. Here's an example: ```javascript const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d');

      // Draw some content to the canvas ctx.fillStyle = 'red'; ctx.fillRect(10, 10, 20, 20);

      // Export the canvas as an image const img = canvas.toDataURL('image/png');

      // Check if the canvas is Tainted if (ctx !== null) { // Generate an image using the canvas const img2 = html2canvas(canvas, { onrendered: function(canvas) { // Do something with the image } }).then((imgData) => { // Export the image as a file const blob = new Blob([imgData], { type: 'image/png' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = 'image.png'; document.body.appendChild(a); a.click(); document.body.removeChild(a); }); } else { console.log('The canvas is Tainted'); } ```

      This example creates a new canvas and checks if its context is not null before using it to generate an image. If the canvas is Tainted, it generates an image using the canvas, which is then exported as a file. If the canvas is not Tainted, the code simply logs an error message. Note that this example only works if the canvas is in a safe context, such as the context of a trusted HTML element or a secure JavaScript environment. If the canvas is in an unsafe context, you may need to add additional checks and sanitization to the code to avoid Tainted canvases.

    • 烟敛寒林 普通会员 2楼

      Tainted canvases refers to a canvas object that has been used before, potentially contaminated by user actions or JavaScript code. This can lead to unexpected behavior in HTML2Canvas and other tools that work with canvas. To avoid this error, you can check the canvas object's context before using it to generate an image. Here's an example: ```javascript const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d');

      // Draw some content to the canvas ctx.fillStyle = 'red'; ctx.fillRect(10, 10, 20, 20);

      // Export the canvas as an image const img = canvas.toDataURL('image/png');

      // Check if the canvas is Tainted if (ctx !== null) { // Generate an image using the canvas const img2 = html2canvas(canvas, { onrendered: function(canvas) { // Do something with the image } }).then((imgData) => { // Export the image as a file const blob = new Blob([imgData], { type: 'image/png' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = 'image.png'; document.body.appendChild(a); a.click(); document.body.removeChild(a); }); } else { console.log('The canvas is Tainted'); } ```

      This example creates a new canvas and checks if its context is not null before using it to generate an image. If the canvas is Tainted, it generates an image using the canvas, which is then exported as a file. If the canvas is not Tainted, the code simply logs an error message. Note that this example only works if the canvas is in a safe context, such as the context of a trusted HTML element or a secure JavaScript environment. If the canvas is in an unsafe context, you may need to add additional checks and sanitization to the code to avoid Tainted canvases.

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部