Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Deltatime.js #622

Merged
merged 33 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
4366171
Create Deltatime.js
XeroName Jun 22, 2023
cce697f
added semicolons
XeroName Jun 22, 2023
c6787f1
Added Deltatime.svg
XeroName Jun 22, 2023
d4a8d3f
Rename images/Deltatime.svg to images/XeroName/Deltatime.svg
XeroName Jun 22, 2023
22c0864
Added FPS>0 boolean to Deltatime.js
XeroName Jun 30, 2023
2303919
images/README.md edited
XeroName Jun 30, 2023
2bf9a13
website/index.ejs edited
XeroName Jun 30, 2023
03ff639
Fixed error of Deltatime.js
XeroName Jul 3, 2023
3b2b0e9
Update images/README.md
XeroName Jul 3, 2023
bf5f566
Resolve conflict of images/README.md
XeroName Jul 3, 2023
f50884a
Merge branch 'master' into patch-1
XeroName Jul 3, 2023
a5d3c71
Update Deltatime.js to 1.2.2
XeroName Jul 3, 2023
c16e546
Major update of Deltatime.js
XeroName Jul 5, 2023
89b3f74
Update Deltatime.js
XeroName Jul 5, 2023
6b8fb20
Try to fix error of Deltatime.js
XeroName Jul 5, 2023
e242335
resolve conflict of README.md
XeroName Jul 10, 2023
d3bcccb
Merge branch 'master' into patch-1
XeroName Jul 10, 2023
bdcea33
Changed "Calculator" section name to "Calculating"
XeroName Jul 11, 2023
34b15be
move around in website
GarboMuffin Jul 17, 2023
d3ec402
Excessive commenting
GarboMuffin Jul 17, 2023
8ce2a83
Merge branch 'master' into patch-1
XeroName Jul 17, 2023
b3efca7
Update Deltatime.js as reviews
XeroName Jul 17, 2023
03da342
Create Deltatime.html
XeroName Jul 17, 2023
6aa8fd8
Update Deltatime.html
XeroName Jul 17, 2023
314b89e
Merge branch 'master' into patch-1
XeroName Jul 20, 2023
008b5a6
Merge branch 'master' of github.com:TurboWarp/extensions into patch-1
GarboMuffin Jul 23, 2023
e29a73a
Merge branch 'TurboWarp:master' into patch-1
XeroName Jul 28, 2023
cddc194
Merge branch 'master' into patch-1
XeroName Jul 31, 2023
3a95126
Merge branch 'master' into patch-1
XeroName Aug 8, 2023
bf47871
i am so sorry
GarboMuffin Aug 16, 2023
dd5f789
restore an fps block
GarboMuffin Aug 16, 2023
b48c0f3
again, sorry
GarboMuffin Aug 16, 2023
0169987
update description
GarboMuffin Aug 16, 2023
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
223 changes: 223 additions & 0 deletions extensions/XeroName/Deltatime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
// TurboWarp Extension : Deltatime by XeroName

/*
Referenced articles :
- https://www.growingwiththeweb.com/2017/12/fast-simple-js-fps-counter.html
- https://stackoverflow.com/questions/4787431/how-do-i-check-framerate-in-javascript
- https://developer.mozilla.org/en-US/docs/Web/API/Performance/now

Thanks to the "TheShovel", one of the TurboWarp Extension's developer named "ShovelUtils".
I learned how to use "Runtime Steps" of Scratch VM through that code.
*/

(function (Scratch) {
'use strict';

const icon = 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNjAwIiBoZWlnaHQ9IjYwMCIgdmlld0JveD0iMCAwIDYwMCA2MDAiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CjxjaXJjbGUgY3g9IjMwMCIgY3k9IjMwMCIgcj0iMzAwIiBmaWxsPSIjMjAyMDIwIi8+CjxwYXRoIGQ9Ik04Ny44NjggNTEyLjEzMkM2MC4wMTA0IDQ4NC4yNzQgMzcuOTEyNSA0NTEuMjAzIDIyLjgzNjEgNDE0LjgwNUM3Ljc1OTcyIDM3OC40MDcgLTMuNDQ0MTZlLTA2IDMzOS4zOTcgMCAzMDBDMy40NDQxN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VjYXA9InJvdW5kIi8+CjxwYXRoIGQ9Ik0zMDAgMTY1SDQwME01MDAgMTY1SDQwME00MDAgMTY1VjQxNU0yNzkuNDUxIDQxMy42MjlMMTgwLjkyOCAxNjcuMzIxQzE4MC41OTMgMTY2LjQ4MyAxNzkuNDA3IDE2Ni40ODMgMTc5LjA3MiAxNjcuMzIxTDgwLjU0ODYgNDEzLjYyOUM4MC4yODU4IDQxNC4yODUgODAuNzY5NiA0MTUgODEuNDc3IDQxNUgyNzguNTIzQzI3OS4yMyA0MTUgMjc5LjcxNCA0MTQuMjg1IDI3OS40NTEgNDEzLjYyOVoiIHN0cm9rZT0id2hpdGUiIHN0cm9rZS13aWR0aD0iMzIiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIvPgo8L3N2Zz4K';

if (!Scratch.extensions.unsandboxed) {
throw new Error('Deltatime by XeroName : This extension must run unsandboxed');
}

const vm = Scratch.vm;
const cast = Scratch.Cast;

let maxFiltStren = 907; // Max value of Filter Strength
XeroName marked this conversation as resolved.
Show resolved Hide resolved
let filtStren = 8; // Default value of Filter Strength = 8
let calcStandFPS = 30; // Standard value of Calculator

let fT = 0, last = performance.now(), rawFPS, vmFPS, vmDt;
const oldStep = vm.runtime._step;

vm.runtime._step = function () {
oldStep.call(this);
XeroName marked this conversation as resolved.
Show resolved Hide resolved
const now = performance.now();
let thisFT = now - last;
fT += (thisFT - fT) / filtStren;

rawFPS = 1000 / fT;

if (rawFPS < 0.5) {
vmFPS = rawFPS.toFixed(2);
} else {
vmFPS = Math.round(rawFPS);
}

if (vmFPS > 0) {
vmDt = 1 / vmFPS; // At normal situation
} else {
vmDt = 0; // Prevent situation of "DIV/0"
}
last = now;
};

class Dt {
getInfo() {
return {
id: 'dtbyxeroname',
name: 'Deltatime',
docsURI: 'https://xeroname.github.io/dtbyxeroname/',
XeroName marked this conversation as resolved.
Show resolved Hide resolved
color1: '#333333',
color2: '#444444',
color3: '#ffffff',
menuIconURI: icon,
blocks: [
{
opcode: 'getDt_vm',
blockType: Scratch.BlockType.REPORTER,
text: 'ΔT'
},
{
opcode: 'getFPS_vm',
blockType: Scratch.BlockType.REPORTER,
text: 'FPS'
},
{
opcode: 'getTimeRes',
blockType: Scratch.BlockType.REPORTER,
text: 'Get [TYPE] of [TARGET]',
arguments: {
TYPE: {
type: Scratch.ArgumentType.STRING,
menu: 'type_menu'
},

TARGET: {
type: Scratch.ArgumentType.STRING,
menu: 'target_menu'
}
},
hideFromPalette: true
XeroName marked this conversation as resolved.
Show resolved Hide resolved
},
{
opcode: 'isFPSposi',
blockType: Scratch.BlockType.BOOLEAN,
text: 'FPS > 0',
hideFromPalette: true
},
{
blockType: "label",
text: "Filtering"
},
{
opcode: 'setFilterStrength',
blockType: Scratch.BlockType.COMMAND,
text: 'Set Filter Strength : [STRENGTH]',
arguments: {
STRENGTH: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: '8'
}
}
},
{
opcode: 'getFilterStrength',
blockType: Scratch.BlockType.REPORTER,
text: 'Filter Strength'
},
{
blockType: "label",
text: "Calculating"
},
{
opcode: 'setCalculatorStandard',
blockType: Scratch.BlockType.COMMAND,
text: 'Set Calculator Standard to [FPS] FPS',
arguments: {
FPS: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: '60'
}
}
},
{
opcode: 'getCalculatorStandard',
blockType: Scratch.BlockType.REPORTER,
text: 'Standard FPS of Calculator',
disableMonitor: true
},
{
opcode: 'calcMultiplyValue',
blockType: Scratch.BlockType.REPORTER,
text: 'Calc [DISTANCE]',
XeroName marked this conversation as resolved.
Show resolved Hide resolved
arguments: {
DISTANCE: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: '10'
}
}
}
],
menus: {
target_menu: {
acceptReporters: true,
items: [
{
text: 'This Project',
value: 'vm'
}
]
},

type_menu: {
acceptReporters: true,
items: [
{
text: 'ΔT',
value: 'dt'
},
{
text: 'FPS',
value: 'fps'
}
]
}
}
};
}

getDt_vm() {
return vmDt;
}
getFPS_vm() {
return vmFPS;
}
getTimeRes({ TYPE, TARGET }) {
const resTyp = cast.toString(TYPE).toLowerCase();
const targetRes = cast.toString(TARGET).toLowerCase();
switch (resTyp) {
case 'dt' : return vmDt;
case 'fps' : return vmFPS;
}
}
isFPSposi() {
return (vmFPS > 0);
}
setCalculatorStandard({ FPS }) {
calcStandFPS = cast.toNumber(FPS);
}
getCalculatorStandard() {
return calcStandFPS;
}
calcMultiplyValue({ DISTANCE }) {
const moveDist = cast.toNumber(DISTANCE);
return moveDist * calcStandFPS;
}
getFilterStrength() {
return filtStren;
}
setFilterStrength({ STRENGTH }) {
const fStren = cast.toNumber(STRENGTH);

if (fStren <= 1) {
filtStren = 1;
} else if (fStren >= maxFiltStren) {
filtStren = maxFiltStren;
} else {
filtStren = fStren;
}
}
}

Scratch.extensions.register(new Dt());
})(Scratch);
3 changes: 3 additions & 0 deletions images/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,6 @@ All images in this folder are licensed under the [GNU General Public License ver

## clipboard.svg
- Created by [@AdamMady](https://github.com/AdamMady/)

## XeroName/Deltatime.svg
- Created by [@XeroName](https://scratch.mit.edu/users/plant2014/) in https://github.com/TurboWarp/extensions/pull/622
8 changes: 8 additions & 0 deletions images/XeroName/Deltatime.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions website/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,12 @@
<p>Display notifications.</p>
</div>

<div class="extension">
<%- banner('XeroName/Deltatime') %>
<h3>Deltatime</h3>
<p>Precise <a href="https://en.wikipedia.org/wiki/Delta_timing">delta timing</a> blocks with adjustable filter. Created by <a href="https://scratch.mit.edu/users/plant2014/">XeroName</a>.</p>
</div>

<div class="extension">
<%- banner('ar') %>
<h3>Augmented Reality</h3>
Expand Down