我正在测试 HTML标签以在我的网站内为Games4Change<canvas></canvas>创建一个香草 JavaScript 游戏。
当我使用 测试画布的宽度和高度时ctx.fillRect(955, 0, 5, 5);,我想知道如何使用 来检测 JavaScript 形状console.log();,因为它们的笔划/边框溢出或超出 HTML<canvas></canvas>边框的范围。
此外,我想知道如何让 JavaScript 检测图像及其边界溢出 HTML<canvas></canvas>边框,console.log();因为我将切换到我的游戏的图像而不是形状。
另外,如何使用 Javascript 检测鼠标光标是否超出 HTML<canvas></canvas>边框console.log();?
HTML
<canvas class="center" id="gameCanvas"></canvas>
CSS
#gameCanvas {
width: 960px;
height: 480px;
border: 5px solid rgba(52,52,52,1.00);/**
background-color: rgba(241,213,179,1.00);**/
}
香草 JavaScript
//Gets the html canvas that displays the game and allows art/rendering of the game in 2 dimensional plane.
let canvas = document.getElementById("gameCanvas");
let ctx = canvas.getContext("2d");
// Determine the size the canvas is being displayed
var width = canvas.clientWidth;
var height = canvas.clientHeight;
console.log(canvas.clientWidth);
console.log(canvas.clientHeight);
//Clears all content everytime the game is reloaded by frame of the rectangular screen.
ctx.clearRect(0, 0, 960, 480);
ctx.fillStyle = "#FF0000";
ctx.fillRect(955, 0, 5, 5);
12345678_0001
相关分类