我正在使用 JS 创建一个画布,然后在所述画布中创建一个组件,并且我希望该组件周围有蓝色边框。我可以用 javascript 或 CSS 来做到这一点吗?
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<style>
canvas {
border:1px solid #d3d3d3;
background-color: #f1f1f1;
}
</style>
</head>
<body onload="startGame()">
<script>
var myGamePiece;
function startGame() {
myGameArea.start();
myGamePiece = new component(30, 30, "red", 10, 120);
}
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]);
}
}
function component(width, height, color, x, y) {
this.width = width;
this.height = height;
this.x = x;
this.y = y;
ctx = myGameArea.context;
ctx.fillStyle = color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
</script>
<p>We have added a component to our game, a red square!</p>
</body>
</html>
这是代码的简化版本。我试图为名为 myGamePiece 的组件提供边框。
一只斗牛犬
当年话下
HUX布斯
相关分类