Skip to content

Commit

Permalink
refactor: use ES modules
Browse files Browse the repository at this point in the history
- move default options from static property to assign in constructor
- add default options value

BREAKING CHANGE: switch to type module
  • Loading branch information
dmnsgn committed Apr 10, 2021
1 parent a7b1c4b commit 09411d2
Show file tree
Hide file tree
Showing 15 changed files with 3,327 additions and 3,931 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
end_of_line = lf
charset = utf-8
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
insert_final_newline = true
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
.DS_Store
types
lib
12 changes: 10 additions & 2 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
demo.js
screenshot.gif
web_modules
examples
docs
coverage
test
.github
screenshot.*
index.html
tsconfig.json
.editorconfig
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (C) 2019 the internet and Damien Seguin
Copyright (C) 2019 Damien Seguin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
67 changes: 30 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,53 @@
# raf-perf [![stable](http://badges.github.io/stability-badges/dist/stable.svg)](http://github.com/badges/stability-badges)

[![npm version](https://badge.fury.io/js/raf-perf.svg)](https://www.npmjs.com/package/raf-perf)
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
# raf-perf

[![npm version](https://img.shields.io/npm/v/raf-perf)](https://www.npmjs.com/package/raf-perf)
[![stability-stable](https://img.shields.io/badge/stability-stable-green.svg)](https://www.npmjs.com/package/raf-perf)
[![npm minzipped size](https://img.shields.io/bundlephobia/minzip/raf-perf)](https://www.npmjs.com/package/raf-perf)
[![dependencies](https://img.shields.io/david/dmnsgn/raf-perf)](https://github.com/dmnsgn/raf-perf/blob/main/package.json)
[![types](https://img.shields.io/npm/types/raf-perf)](https://github.com/microsoft/TypeScript)
[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-fa6673.svg)](https://conventionalcommits.org)
[![styled with prettier](https://img.shields.io/badge/styled_with-Prettier-f8bc45.svg?logo=prettier)](https://github.com/prettier/prettier)
[![linted with eslint](https://img.shields.io/badge/linted_with-ES_Lint-4B32C3.svg?logo=eslint)](https://github.com/eslint/eslint)
[![license](https://img.shields.io/github/license/dmnsgn/raf-perf)](https://github.com/dmnsgn/raf-perf/blob/main/LICENSE.md)

RAF loop with an adaptive fps and performance ratio calculated from either a sample count or a sample duration. Typically used when doing intensive graphics computation in canvas.

![](https://raw.githubusercontent.com/dmnsgn/raf-perf/master/screenshot.gif)
[![paypal](https://img.shields.io/badge/donate-paypal-informational?logo=paypal)](https://paypal.me/dmnsgn)
[![coinbase](https://img.shields.io/badge/donate-coinbase-informational?logo=coinbase)](https://commerce.coinbase.com/checkout/56cbdf28-e323-48d8-9c98-7019e72c97f3)
[![twitter](https://img.shields.io/twitter/follow/dmnsgn?style=social)](https://twitter.com/dmnsgn)

![](https://raw.githubusercontent.com/dmnsgn/raf-perf/main/screenshot.gif)

## Installation

```bash
npm install raf-perf
```

[![NPM](https://nodei.co/npm/raf-perf.png)](https://nodei.co/npm/raf-perf/)

## Usage

```js
const RafPerf = require("raf-perf");
import RafPerf from "raf-perf";
import createCanvasContext from "canvas-context";

const canvas = document.createElement("canvas");
const context = canvas.getContext("2d");
const { context, canvas } = createCanvasContext("2d", {
width: window.innerWidth,
height: window.innerHeight,
offscreen: true,
});
document.body.appendChild(canvas);

const engine = new RafPerf();

engine.on("tick", dt => {
engine.on("tick", (dt) => {
// Clear
context.clearRect(0, 0, canvas.width, canvas.height);

// Draw
// ...
});

engine.on("perf", ratio => {
engine.on("perf", (ratio) => {
if (!ratio) return;

console.log(`Performance ratio: ${ratio}`);
Expand All @@ -56,33 +70,12 @@ const destroy = () => {

## API

### `const engine = new RafPerf(options)`

| Option | Type | Default | Description |
| --------------------------------------- | -------- | ------- | ------------------------------------------- |
| **options.fps** | number? | 60 | Throttle fps |
| **options.performances.enabled** | boolean? | true | Evaluate performances |
| **options.performances.samplesCount** | number? | 200 | Number of samples to evaluate performances |
| **options.performances.sampleDuration** | number? | 4000 | Duration of sample to evaluate performances |

`samplesCount` and `sampleDuration` are used concurrently. Set `sampleDuration` to a _falsy_ value if you only want to sample performances only from a number of frames.

### `engine.start()`

Run the `requestAnimationFrame` loop and start checking performances if `options.performances.enabled` is `true`.

### `engine.stop()`

Run `cancelAnimationFrame` if necessary and reset the engine.

### `engine.on("tick", (dt: number) => void)`

Event triggered on tick, throttled by `options.fps`.
<!-- api-start -->

### `engine.on("perf", (ratio: number) => void)`
Auto-generated API content.

Event triggered when performance ratio (`this.frameDuration / averageDeltaTime`) is updated. Understand a ratio of the fps, for instance for a fps value of 24, `ratio < 0.5` means that the averaged `fps < 12` and you should probably do something about it.
<!-- api-end -->

## License

MIT. See [license file](https://github.com/dmnsgn/raf-perf/blob/master/LICENSE.md).
MIT. See [license file](https://github.com/dmnsgn/raf-perf/blob/main/LICENSE.md).
48 changes: 0 additions & 48 deletions demo.js

This file was deleted.

96 changes: 96 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>raf-perf by Damien Seguin (https://github.com/dmnsgn)</title>
<style>
:root {
--color-dark: #404040;
--color-light: #f2f2f2;
--color-accent: #fd5e62;
}

body {
margin: 0;
overflow: hidden;
overscroll-behavior: none;
font-family: sans-serif;
color: var(--color-dark);
background-color: var(--color-light);
}

main {
padding: 0 20px;
max-width: 600px;
margin: auto;
}

.RectCount {
color: var(--color-accent);
font-size: 30px;
font-weight: bold;
}
</style>
</head>
<body>
<main>
<h1>raf-perf</h1>
<p class="RectCount"></p>
</main>
<script async src="web_modules/es-module-shims.js"></script>
<script src="web_modules/import-map.json" type="importmap"></script>

<script type="module">
import RafPerf from "./index.js";
import createCanvasContext from "canvas-context";

const { context, canvas } = createCanvasContext("2d", {
width: 600,
height: 600,
alpha: false,
});
document.querySelector("main").appendChild(canvas);

const maxRectCount = 580;
let rectCount = maxRectCount;
const RectCountElement = document.querySelector(".RectCount");

const engine = new RafPerf();

const color = window
.getComputedStyle(document.documentElement)
.getPropertyValue("--color-accent");

engine.on("tick", (dt) => {
context.clearRect(0, 0, canvas.width, canvas.height);

for (let i = 0; i < rectCount; i++) {
context.shadowColor = color;
context.shadowBlur = 30;

context.fillStyle = color;
context.fillRect(40 + i * 0.5, 40 + i * 0.5, 200, 100);
}
RectCountElement.innerText = `${rectCount} rect with shadowBlur`;
});

engine.on("perf", (ratio) => {
if (!ratio) return;

console.log(`Performance ratio: ${ratio}`);

if (ratio < 0.9) {
console.log("Too many draws");
rectCount -= 10;
} else if (ratio >= 0.9 && rectCount < maxRectCount) {
console.log("Draw more");
rectCount += 5;
}
});

engine.start();
</script>
</body>
</html>
Loading

0 comments on commit 09411d2

Please sign in to comment.