[英]Is it possible to alternate between a background color and a background image?
I have this program where I can add lights to my scene (stored in an array lights
).我有这个程序,可以在其中向场景添加
lights
(存储在数组中)。 When I first start the program, I want everything to be black, so I call gl.clearColor(0, 0, 0, 1);
当我第一次启动程序时,我希望一切都是黑色的,所以我调用
gl.clearColor(0, 0, 0, 1);
at my setup.在我的设置中。 But when there is a light, I want to change the background to an image, and when there are no lights, I want everything to be black again.
但是有光的时候,我想把背景换成图像,没有光的时候,我想让一切都变黑。 I´m trying to call this in my render function:
我试图在我的渲染 function 中调用它:
if(lights.length == 0) {
canvas.style.background = "#000000 none";
} else {
canvas.style.background = "transparent url('nebula.png')";
}
But it's not wroking as I intended.但这并没有像我预期的那样工作。 Does someone know how to achieve this?
有人知道如何实现这一目标吗?
don't use background properties on canvas.不要在 canvas 上使用背景属性。
Use context.fillRect()
to set background color使用
context.fillRect()
设置背景颜色
Use context.fillImage()
to set background image使用
context.fillImage()
设置背景图片
Reference:参考:
https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/fillRect <= fillRect() https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/fillRect <= fillRect()
https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage <= fillImage() https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage <= fillImage()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.