admin管理员组文章数量:1333618
I'm using canvas.getContext('2d', {alpha: false});
(wiki info) to remove canvas opacity and improve performance of animation. Unfortunately after that, canvas element gain white background. Is there an option to change the default color of canvas background/color?
I tried drawImage()
to fill whole area, but (because of animation) I think it is not the best solution:
var canvas = document.querySelector('#mycanvas'),
ctx = canvas.getContext('2d', {alpha: false});
function run() {
ctx.clearRect(0, 0, size.width, size.height);
ctx.drawImage(bg, 0, 0, size.width, size.height);
draw();
update();
requestAnimationFrame(run);
}
I really care about performance, so it would be useful to change the color only once.
Any ideas?
I'm using canvas.getContext('2d', {alpha: false});
(wiki info) to remove canvas opacity and improve performance of animation. Unfortunately after that, canvas element gain white background. Is there an option to change the default color of canvas background/color?
I tried drawImage()
to fill whole area, but (because of animation) I think it is not the best solution:
var canvas = document.querySelector('#mycanvas'),
ctx = canvas.getContext('2d', {alpha: false});
function run() {
ctx.clearRect(0, 0, size.width, size.height);
ctx.drawImage(bg, 0, 0, size.width, size.height);
draw();
update();
requestAnimationFrame(run);
}
I really care about performance, so it would be useful to change the color only once.
Any ideas?
Share Improve this question asked Oct 16, 2015 at 19:39 AmayAmay 1,5215 gold badges29 silver badges57 bronze badges1 Answer
Reset to default 6No. The default background is always black.
The CanvasRenderingContext2D.clearRect() method of the Canvas 2D API sets all pixels in the rectangle defined by starting point (x, y) and size (width, height) to transparent black, erasing any previously drawn content.
Instead of clearing the canvas, just set fillRect to fill with the color you want.
var canvas = document.querySelector('#mycanvas'),
ctx = canvas.getContext('2d', {alpha: false});
function run() {
ctx.fillStyle = "#FF0000";
ctx.fillRect(0, 0, size.width, size.height);
draw();
update();
requestAnimationFrame(run);
}
本文标签: javascriptCanvas opaquechange default background colorStack Overflow
版权声明:本文标题:javascript - Canvas opaque - change default background color - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742281582a2446191.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论