gpt4 book ai didi

javascript - 试图在 Canvas 中填充两种不同的颜色 fill()

转载 作者:行者123 更新时间:2023-11-30 12:53:31 26 4
gpt4 key购买 nike

我正在尝试使用 Canvas 和 Javascript 绘制一座房子。如果您看一下我的 drawHouse() 方法,我想将屋顶涂成红色,将其他所有东西涂成白色。但是只有白色填充了所有颜色。

Jsfiddle link

<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="utf-8" />
<title>My Site's Title</title>
</head>
<body>

<canvas id="myDrawing" width="300" height="300" style="border:1px solid #EEE">
</canvas>
<script>
var canvas = document.getElementById("myDrawing");
var ctx = canvas.getContext("2d");

//draws a house
function drawImage() {
drawSky();
drawGrass();
drawHouse();
}



//sets the dimensions of the canvas
var x = canvas.width / 2;
var y = 400;
var radius = 200;
var startAngle = 0;
var endAngle = 22 * Math.PI;



//draws the sky

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

context.beginPath();
context.rect(0, 0, 300, 300);
context.fillStyle = '#0099FF';
context.fill();
context.lineWidth = 1;
context.strokeStyle = 'black';
context.stroke();
}
//draws green grass
function drawGrass() {
ctx.beginPath();
ctx.arc(x, y, radius, startAngle, endAngle);
ctx.stroke();
ctx.fillStyle = "#009900";
ctx.fill();

}

function drawHouse() {
var c = document.getElementById('myDrawing');
var ctx = c.getContext('2d');
ctx.beginPath();
ctx.moveTo(95, 165);
ctx.lineTo(140, 215);
ctx.lineTo(260, 215);
ctx.lineTo(240, 165);
ctx.lineTo(95, 165);
ctx.closePath();
ctx.fillStyle = "#C81E1E";
ctx.fill();


ctx.moveTo(139, 215);
ctx.lineTo(94, 165);
ctx.lineTo(45, 215)
ctx.closePath();
ctx.fillStyle = "white";
ctx.fill();


ctx.moveTo(48, 212);
ctx.lineTo(48, 275);
ctx.lineTo(139, 275);
ctx.lineTo(139, 215);
ctx.lineTo(45, 215);
ctx.closePath();
ctx.fillStyle = "white";
ctx.fill();
ctx.stroke();


ctx.moveTo(139, 275);
ctx.lineTo(260, 267);
ctx.lineTo(260, 215);
ctx.lineTo(140, 215);

ctx.closePath();
ctx.fillStyle = "white";
ctx.fill();
ctx.stroke();

}
drawImage();
</script>
</body>
</html>

最佳答案

您在 drawHouse 函数中缺少对 ctx.beginPath 的调用。 fillStyle 是针对每个路径的,因此需要有多个路径。

这是 fiddle :http://jsfiddle.net/sVDSs/6/

            var canvas = document.getElementById("myDrawing");
var ctx = canvas.getContext("2d");

//draws a house
function drawImage() {
drawSky();
drawGrass();
drawHouse();
}



//sets the dimensions of the canvas
var x = canvas.width / 2;
var y = 400;
var radius = 200;
var startAngle = 0;
var endAngle = 22 * Math.PI;



//draws the sky

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

context.beginPath();
context.rect(0, 0, 300, 300);
context.fillStyle = '#0099FF';
context.fill();
context.lineWidth = 1;
context.strokeStyle = 'black';
context.stroke();
}
//draws green grass
function drawGrass() {
ctx.beginPath();
ctx.arc(x, y, radius, startAngle, endAngle);
ctx.stroke();
ctx.fillStyle = "#009900";
ctx.fill();

}

function drawHouse() {
var c = document.getElementById('myDrawing');
var ctx = c.getContext('2d');
ctx.beginPath();
ctx.moveTo(95, 165);
ctx.lineTo(140, 215);
ctx.lineTo(260, 215);
ctx.lineTo(240, 165);
ctx.lineTo(95, 165);
ctx.fillStyle = "red";
ctx.closePath();
ctx.fill();

ctx.beginPath(); // THIS IS THE LINE
ctx.moveTo(139, 215);
ctx.lineTo(94, 165);
ctx.lineTo(45, 215)
ctx.closePath();
ctx.fillStyle = "white";
ctx.fill();


ctx.moveTo(48, 212);
ctx.lineTo(48, 275);
ctx.lineTo(139, 275);
ctx.lineTo(139, 215);
ctx.lineTo(45, 215);
ctx.closePath();
ctx.fillStyle = "white";
ctx.fill();
ctx.stroke();


ctx.moveTo(139, 275);
ctx.lineTo(260, 267);
ctx.lineTo(260, 215);
ctx.lineTo(140, 215);

ctx.closePath();
ctx.fillStyle = "white";
ctx.fill();
ctx.stroke();

}
drawImage();

关于javascript - 试图在 Canvas 中填充两种不同的颜色 fill(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20111160/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com