[英]drawing circle functions in canvas
我需要画一个圆圈。 它可能很简单,那里可能有大量教程,但我找不到适合我的特定问题的教程。 我需要告诉它从这里画一个圆圈:
function startGame() {
//circle needs to be called from here.
//this is how i draw a square
square = new component(40, 40, "blue", 10, 10);
myGameArea.start();
}
然后我需要从这里实际绘制它:
function component(width, height, color, x, y) {
this.width = width;
this.height = height;
this.speedX = 0;
this.speedY = 0;
this.x = x;
this.y = y;
//this is where it draws the stuff
this.update = function() {
var ctx = myGameArea.context;
ctx.fillStyle = color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
this.newPos = function() {
this.x += this.speedX;
this.y += this.speedY;
}
}
这个 function 目前被编程为绘制正方形而不是圆形。 我需要能够从那里绘制和重新绘制圆圈。 如果我创建一个新功能,它需要使用 function this.update
,这将覆盖当前的功能。 我需要用它来更新游戏资产,比如exampleSquare.update()
。
var myGameArea = {
canvas : document.createElement("canvas"),
start : function() {
this.canvas.width = 480;
this.canvas.height = 270;
this.context = this.canvas.getContext("2d");
document.body.insertBefore(this.canvas, document.body.childNodes[0]);
this.frameNo = 0;
this.interval = setInterval(updateGameArea, 20);
},
clear : function() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
}
这称为
function updateGameArea() {
myGameArea.clear();
}
为了简单起见,我留空了。 如果你需要我的完整代码,你可以在这里找到它: https://game.octopuscat.repl.co/它有点乱,有很多未实现的东西,所以这是一个简化版本。
向组件 function 添加说明符。例如,您可以使用字符串作为第一个参数来定义是否要绘制圆形或方形:
function component(type, width, height, color, x, y) {
if(type == "square") {
...
//draw
} else if(type == "circle") {
...
//draw
}
}
然后你可以重新调整参数的用途,如果它是一个圆圈
... if(type == "circle") {
var radius = width
var diameter = height
//draw
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.