Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Keyboard · Mouse · Gamepad – All under one simple API
Contro is a library that offers simple abstractions on top of existing Web input APIs and allows game developers to easily implements controls for keyboard, mouse and gamepad.
The easiest way to include Contro in your application is using a CDN:
<script src="https://unpkg.com/contro"></script>
If you're using npm
, you can also npm install contro
.
Imagine your game looked something like this ...
/** Gameloop */
const canvas = document.querySelector('#game')
const renderer = new MyGameLibrary.Renderer(canvas)
const catWorld = new CatWorld()
function loop() {
renderer.render(catWorld)
requestAnimationFrame(loop)
}
loop()
... and you wanted to spawn a new cat whenever the user clicked.
To accomplish this we need Contro's Mouse
class. It's an abstraction on top of JavaScript's MouseEvent
's and the Pointer Lock API optimized for being used within game loop functions.
Let's create a new Mouse
instance and pass in our canvas
, so Contro can register the required event listeners for us.
const mouse = new Contro.Mouse({ canvas })
Now we can use our new Mouse
instance to check whether the left mouse button is pressed.
if (mouse.isPressed(Contro.MouseButton.Left)) {
catWorld.spawnCat()
}
If this was real code and you ran it game you'd notice something weird: Whenever you held down your left mouse button cats would keep spawning and spawning until you released the button again.
This is because Mouse.isPressed()
always returns the current state of the mouse button and that for every single frame (iteration of your game loop). We only want one cat to spawn when we click, so we have to use Mouse.wasPressed()
.
Our final code looks like this:
/** Gameloop */
const canvas = document.querySelector('#game')
const renderer = new MyGameLibrary.Renderer(canvas)
const catWorld = new CatWorld()
const mouse = new Contro.Mouse()
function loop() {
if (mouse.wasPressed(Contro.MouseButton.Left)) {
catWorld.spawnCat()
}
renderer.render(catWorld)
requestAnimationFrame(loop)
}
loop()
More documentation coming soon.
FAQs
Game controls done right.
The npm package contro receives a total of 8 weekly downloads. As such, contro popularity was classified as not popular.
We found that contro demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.