Home > Web Front-end > HTML Tutorial > canvas.toDataURL image/png 报错处理方法推荐 _HTML/Xhtml_网页制作

canvas.toDataURL image/png 报错处理方法推荐 _HTML/Xhtml_网页制作

WBOY
Release: 2016-05-24 09:05:35
Original
1600 people have browsed it

问题背景:

遇到一个需求,要对播放的视频进行截图,视频使用video标签来播放,然后点击视频播放区域时截取实时的帧图片。

代码很简单如下:

JavaScript Code复制内容到剪贴板
  1. var video = document.getElementById('video');   
  2.   
  3. var canvas = document.getElementById('canvas');   
  4.   
  5. var ctx = canvas.getContext('2d');   
  6.   
  7. var img = document.getElementById('img');   
  8.   
  9. function snapshot() {   
  10.    ctx.drawImage(video,0,0);   
  11.    img.src =  canvas.toDataURL('image/png');   
  12. }   
  13.   
  14. video.addEventListener('click', snapshot, false);   

问题提示:

Uncaught SecurityError: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.

经过查阅和分析,发现这个其实是由于视频文件所在的域和图片和页面所在域不同,出现跨域传输的问题。

解决方案:

将视频文件放到页面所在域下。

原文地址:http://blog.csdn.net/luochao_tj/article/details/44942125

Related labels:
png
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template