javascript - JS canvas getContext then transferControlToOffscreen - Stack Overflow

Working code:var canvas = document.createElement('canvas');var offscreen = canvas.transferCo

Working code:

var canvas = document.createElement('canvas');
var offscreen = canvas.transferControlToOffscreen();

When using .getContext():

var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
var offscreen = canvas.transferControlToOffscreen();
// InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable

Further attempts do not work either:

var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
delete ctx;
var offscreen = canvas.transferControlToOffscreen();

Or even so:

var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
ctx.reset();
delete ctx;
var offscreen = canvas.transferControlToOffscreen();

Nothing changes with webgl instead of 2d

So, how can i get the offscreen of a canvas which has something drawn on it, and then send it to a worker? Or must i use something like getImageData() and send the result alongside the offscreen of a brand new canvas, and let the worker copy with putImageData()? I do not want to make these additional operations (because will run slower).

Another way would have been to use cloneNode(), but the canvas image does not get copied.

Ive looked trough all methods of canvas and of context, but nothing seems to fix the problem.

I am using FF 67 with offscreen enabled. I suppose there is no difference in Chrome.

Working code:

var canvas = document.createElement('canvas');
var offscreen = canvas.transferControlToOffscreen();

When using .getContext():

var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
var offscreen = canvas.transferControlToOffscreen();
// InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable

Further attempts do not work either:

var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
delete ctx;
var offscreen = canvas.transferControlToOffscreen();

Or even so:

var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
ctx.reset();
delete ctx;
var offscreen = canvas.transferControlToOffscreen();

Nothing changes with webgl instead of 2d

So, how can i get the offscreen of a canvas which has something drawn on it, and then send it to a worker? Or must i use something like getImageData() and send the result alongside the offscreen of a brand new canvas, and let the worker copy with putImageData()? I do not want to make these additional operations (because will run slower).

Another way would have been to use cloneNode(), but the canvas image does not get copied.

Ive looked trough all methods of canvas and of context, but nothing seems to fix the problem.

I am using FF 67 with offscreen enabled. I suppose there is no difference in Chrome.

Share Improve this question asked Jun 6, 2019 at 17:04 ituyituy 2414 silver badges11 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

It's simple: You cannot transfer control from a canvas that has a rendering context.

But you can:

const canvas = new OffscreenCanvas(100, 1);
const ctx = canvas.getContext('2d');

Wich is the same as doing:

var canvas = document.createElement('canvas');
var offscreen = canvas.transferControlToOffscreen();
var ctx = offscreen.getContext('2d');

At the end of all you transferToImageBitmap:

var bitmap = offscreen.transferToImageBitmap();

Hope this helps!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744184304a4562132.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信