gpt4 book ai didi

Javascript Canvas 绘图 - 形状已填充

转载 作者:行者123 更新时间:2023-12-01 00:57:49 26 4
gpt4 key购买 nike

我正在尝试绘制一系列围绕中心点运行的圆,这是一个非常非常基本的版本: https://mgvez.github.io/jsorrery/

到目前为止我已经成功了,但是形状被填充了,我不知道为什么。它实际上看起来很酷,但不是我想要的;)

var origin_x = 200;
var origin_y = 200;

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

class planet {
constructor(orbital_velocity, orbital_radius, radius) {
this.orbital_velocity = orbital_velocity;
this.orbital_radius = orbital_radius;
this.radius = radius;
this.draw()
}

draw() {
const theta = t*this.orbital_velocity
const x = origin_x + this.orbital_radius*Math.cos(theta)
const y = origin_y + this.orbital_radius*Math.sin(theta)
ctx.arc(x, y, this.radius, 0, 2*Math.PI, 'anticlockwise')
ctx.fill();
}
}


let t = 0;
window.setInterval(function(){
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.beginPath();
const a = new planet(2*Math.PI, 30, 1 );
const b = new planet(Math.PI, 50, 1 );
const c = new planet(0.5*Math.PI, 70, 1 );
const d = new planet(0.25*Math.PI, 90, 1 );
t += 0.1;
}, 40);
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col text-center">
<canvas id="canvas" width="400" height="400" />
</div>
</div>
</div>
</body>
</html>

我已阅读文档,但我不明白为什么会发生这种情况...我需要填充各个圆圈(圆弧),而不是填充它们之间的形状...

最佳答案

您需要调用ctx.beginPath()ctx.closePath(),否则 Canvas 认为您正在绘制一个复杂的元素/polygon 并尝试填充它。

var origin_x = 200;
var origin_y = 200;

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

class planet {
constructor(orbital_velocity, orbital_radius, radius) {
this.orbital_velocity = orbital_velocity;
this.orbital_radius = orbital_radius;
this.radius = radius;
this.draw()
}

draw() {
const theta = t*this.orbital_velocity
const x = origin_x + this.orbital_radius*Math.cos(theta)
const y = origin_y + this.orbital_radius*Math.sin(theta)
ctx.beginPath();
ctx.arc(x, y, this.radius, 0, 2*Math.PI, 'anticlockwise')
ctx.fill();
ctx.closePath();
}
}


let t = 0;
window.setInterval(function(){
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.beginPath();
const a = new planet(2*Math.PI, 30, 1 );
const b = new planet(Math.PI, 50, 1 );
const c = new planet(0.5*Math.PI, 70, 1 );
const d = new planet(0.25*Math.PI, 90, 1 );
t += 0.1;
}, 40);
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col text-center">
<canvas id="canvas" width="400" height="400" />
</div>
</div>
</div>
</body>
</html>

关于Javascript Canvas 绘图 - 形状已填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56386593/

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