Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/lib/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,5 +571,14 @@ export default [
banner: "Gen1x/mw-placeholder.avif",
creator: "G1nX",
},
{
name: "Cursor Controls",
description: "Lock and hide your pointer while recording your mouse movement best to use in 3d games.",
code: "codemaster/cursor-controls.js",
banner: "codemaster/cursor-controls.png",
creator: "moosanaeempc-lgtm",
creatorAlias: "codemaster",
isGitHub: true,
},
*/
];
89 changes: 89 additions & 0 deletions static/extensions/codemaster/cursor-controls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Please run this extension without sandbox

(function(Scratch) {
'use strict';

let moveX = 0;
let moveY = 0;
let lastRealX = null;
let lastRealY = null;

class CursorControls {
getInfo() {
return {
id: 'moosanaeempclgtmcursorcontrols',
name: 'Cursor Controls',
color1: '#4fc694',
blocks: [
{
opcode: 'lockPointer',
blockType: Scratch.BlockType.COMMAND,
text: 'lock pointer'
},
{
opcode: 'unlockPointer',
blockType: Scratch.BlockType.COMMAND,
text: 'unlock pointer'
},
{
opcode: 'isPointerLocked',
blockType: Scratch.BlockType.BOOLEAN,
text: 'pointer locked?'
},
{
opcode: 'getMovementX',
blockType: Scratch.BlockType.REPORTER,
text: 'mouse movement x'
},
{
opcode: 'getMovementY',
blockType: Scratch.BlockType.REPORTER,
text: 'mouse movement y'
}
]
};
}

lockPointer() {
const canvas = document.querySelector('canvas');
if (canvas) canvas.requestPointerLock();
}

unlockPointer() {
if (document.exitPointerLock) {
document.exitPointerLock();
}
}

isPointerLocked() {
return !!document.pointerLockElement;
}

getMovementX() { return moveX; }
getMovementY() { return moveY; }
}

document.addEventListener('mousemove', (e) => {
if (document.pointerLockElement) {
moveX += e.movementX;
moveY += -e.movementY;
} else {
if (lastRealX !== null && lastRealY !== null) {
moveX += e.clientX - lastRealX;
moveY += -(e.clientY - lastRealY);
}
}
lastRealX = e.clientX;
lastRealY = e.clientY;
});

const runtime = Scratch.vm ? Scratch.vm.runtime : Scratch.runtime;
if (runtime) {
runtime.on('RUNTIME_STEP_END', () => {
moveX = 0;
moveY = 0;
});
}

Scratch.extensions.register(new CursorControls());
})(Scratch);
Binary file added static/images/codemaster/cursor-controls.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.