[英]Border-radius on canvas elements
I'm creating a little game based on this code: https://www.w3schools.com/graphics/tryit.asp?filename=trygame_default_gravity我正在根据以下代码创建一个小游戏: https://www.w3schools.com/graphics/tryit.asp?filename=trygame_default_gravity
everything ok, but I would like to put the border-radius
on green stakes, does anyone have any ideas?一切都好,但我想把
border-radius
放在绿色桩上,有人有什么想法吗? the part that creates the components is this:创建组件的部分是这样的:
function component(width, height, color, x, y, type) {
this.type = type;
this.score = 0;
this.width = width;
this.height = height;
this.speedX = 0;
this.speedY = 0;
this.x = x;
this.y = y;
this.gravity = 0;
this.gravitySpeed = 0;
this.update = function() {
ctx = myGameArea.context;
if (this.type == "text") {
ctx.font = this.width + " " + this.height;
ctx.fillStyle = color;
ctx.fillText(this.text, this.x, this.y);
} else {
ctx.fillStyle = color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
}
this.newPos = function() {
this.gravitySpeed += this.gravity;
this.x += this.speedX;
this.y += this.speedY + this.gravitySpeed;
this.hitBottom();
}
this.hitBottom = function() {
var rockbottom = myGameArea.canvas.height - this.height;
if (this.y > rockbottom) {
this.y = rockbottom;
this.gravitySpeed = 0;
}
}
this.crashWith = function(otherobj) {
var myleft = this.x;
var myright = this.x + (this.width);
var mytop = this.y;
var mybottom = this.y + (this.height);
var otherleft = otherobj.x;
var otherright = otherobj.x + (otherobj.width);
var othertop = otherobj.y;
var otherbottom = otherobj.y + (otherobj.height);
var crash = true;
if ((mybottom < othertop) || (mytop > otherbottom) || (myright < otherleft) || (myleft > otherright)) {
crash = false;
}
return crash;
}
} }
While the part where he creates the green stakes is this:而他创建绿色木桩的部分是:
if (myGameArea.frameNo == 1 || everyinterval(150)) {
x = myGameArea.canvas.width;
minHeight = 20;
maxHeight = 200;
height = Math.floor(Math.random()*(maxHeight-minHeight+1)+minHeight);
minGap = 50;
maxGap = 200;
gap = Math.floor(Math.random()*(maxGap-minGap+1)+minGap);
myObstacles.push(new component(10, height, "green", x, 0));
myObstacles.push(new component(10, x - height - gap, "green", x, height + gap));
}
The components you draw on the <canvas>
are not html elements, therefore you cant style them with CSS.您在
<canvas>
上绘制的组件不是 html 元素,因此您不能使用 CSS 对它们进行样式设置。
What you want is to replace your fillRect()
function with a function that can draw rectangles with rounded corners.您想要的是用可以绘制圆角矩形的 function 替换您的
fillRect()
function。
One such function is provided as an answer to this stackoverflow question: How to draw a rounded rectangle using HTML Canvas?提供了一个这样的 function 作为此 stackoverflow 问题的答案: 如何使用 HTML Canvas 绘制圆角矩形?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.