Javascript 用HTML5画布矩形(像素大小)绘制的图形会得到一个黑色矩形而不是蓝色?
我希望在Firefox的画布上画一个320 X 240像素的蓝色大矩形,但我得到的是一个黑色矩形。我画像素实际上只是为了实验,由于某些原因,这不能正常工作。代码中的所有内容在JavaScript draw函数中似乎都是有意义的,但这是一个错误吗?任何想法,这是代码Javascript 用HTML5画布矩形(像素大小)绘制的图形会得到一个黑色矩形而不是蓝色?,javascript,html,firefox,canvas,pixels,Javascript,Html,Firefox,Canvas,Pixels,我希望在Firefox的画布上画一个320 X 240像素的蓝色大矩形,但我得到的是一个黑色矩形。我画像素实际上只是为了实验,由于某些原因,这不能正常工作。代码中的所有内容在JavaScript draw函数中似乎都是有意义的,但这是一个错误吗?任何想法,这是代码 <html> <head> <title>Canvas</title> <script type="text/javascript"> fun
<html>
<head>
<title>Canvas</title>
<script type="text/javascript">
function draw()
{
var canvas = document.getElementById("canvas");
if (canvas.getContext)
{
var ctx = canvas.getContext("2d");
var red = 0;
var green = 0;
var blue = 255;
ctx.fillStyle = "rgb( red, green, blue)";
for(var i = 0; i < 320; i++)
{
for(var j = 0; j < 240; j++)
{
ctx.fillRect (i , j , 1, 1);
}
}
}
}
</script>
<style type="text/css">
body
{
margin: 50px 50px;
}
canvas
{
border: 1px solid black;
}
#container
{
position: relative;
width: 640px;
height: 480px;
margin: 0 auto;
}
</style>
</head>
<body onload="draw();">
<div id = "container">
<canvas id="canvas" width="640px" height="480px"></canvas>
</div>
</body>
</html>
帆布
函数绘图()
{
var canvas=document.getElementById(“canvas”);
if(canvas.getContext)
{
var ctx=canvas.getContext(“2d”);
var-red=0;
绿色变量=0;
var蓝=255;
ctx.fillStyle=“rgb(红、绿、蓝)”;
对于(变量i=0;i<320;i++)
{
对于(var j=0;j<240;j++)
{
ctx.fillRect(i,j,1,1);
}
}
}
}
身体
{
保证金:50px 50px;
}
帆布
{
边框:1px纯黑;
}
#容器
{
位置:相对位置;
宽度:640px;
高度:480px;
保证金:0自动;
}
您需要使用..“+variable+”..
让字符串使用变量的值。正如您正在传递的rgb(红、绿、蓝)
作为fillStyle,然后默认为黑色,因为它是无效的
ctx.fillStyle = "rgb("+red+","+green+","+blue+")";
您需要使用…“+variable+”…
让字符串使用变量的值。正如您正在传递的rgb(红、绿、蓝)
作为fillStyle,然后默认为黑色,因为它是无效的
ctx.fillStyle = "rgb("+red+","+green+","+blue+")";