canvas画布会黑屏吗,通过canvas.toDataURL将画布保存到图像会生成黑色矩形

Im using Pixi.js and trying to save a frame of the animation to an image. canvas.toDataUrl should work, but all i get is a black rectangle. See live example here

the code I use to extract the image data and set the image is:

var canvas = $('canvas')[0];

var context = canvas.getContext('2d');

$('button').click(function() {

var data = renderer.view.toDataURL("image/png", 1);

//tried var data = canvas.toDataURL();

$('img').attr('src', data);

})

解决方案

[NOTE]

While this answer is the accepted one, please do read the one by @gman just below, it does contain a way better way of doing.

Your problem is that you are using webGL context, then you need to set the preserveDrawingBuffer property of the webGL context to true in order to be able to call toDataURL() method.

Or alternatively, you can force pixi to use the 2D context, by using the CanvasRenderer Class

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值