toDataURL() of webgl canvas returning transparent image [duplicate]

萝らか妹 提交于 2019-12-19 03:22:56

问题


I can't get toDataUrl() to work. Here's my code where I'm trying to get an image of the canvas and use it as the source of an existing image element.

var canvas = document.getElementById('glcanvas');
canvas.setAttribute('crossOrigin','anonymous');
var img = document.getElementById('imageToShowCanvas');
img.src = canvas.toDataURL();

What imageToShowCanvas is showing is a broken url. When I console.log the result of my call to toDataUrl() I get a link showing a transparent image with the height and width of the canvas.

I've heard of cors problems causing issues like this but I'm using chrome with –allow-file-access-from-files flag and there aren't any security errors in the console. Is there an easier way to get a screenshot of my webGl canvas?


回答1:


Thank you LJ, I just needed to enable the preserveDrawingBuffer flag when getting the webGl context. Screenshots are now working perfectly!



来源:https://stackoverflow.com/questions/31710748/todataurl-of-webgl-canvas-returning-transparent-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!