Skip to content

Commit

Permalink
feat: add dpr
Browse files Browse the repository at this point in the history
  • Loading branch information
surunzi committed Nov 27, 2024
1 parent 7f4ba1f commit 1cb0201
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 0 deletions.
37 changes: 37 additions & 0 deletions DOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -5655,6 +5655,43 @@ function download(
download('test', 'test.txt');
```

## dpr

Device pixel ratio helper.

<details>
<summary>Type Definition</summary>

```typescript
namespace dpr {
interface IDpr extends Emitter {
get(): number;
}
}
const dpr: dpr.IDpr;
```

</details>

### on

Bind change event.

### off

Unbind change event.

### get

Get current device pixel ratio.

```javascript
dpr.on('change', function(dpr) {
console.log(dpr); // -> 2
});
dpr.get(); // -> 1
```

## durationFormat

Simple duration format function.
Expand Down
37 changes: 37 additions & 0 deletions DOC_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -5647,6 +5647,43 @@ function download(
download('test', 'test.txt');
```

## dpr

设备像素比工具库。

<details>
<summary>类型定义</summary>

```typescript
namespace dpr {
interface IDpr extends Emitter {
get(): number;
}
}
const dpr: dpr.IDpr;
```

</details>

### on

绑定 change 事件。

### off

解绑 change 事件。

### get

获取当前设备像素比。

```javascript
dpr.on('change', function(dpr) {
console.log(dpr); // -> 2
});
dpr.get(); // -> 1
```

## durationFormat

简单时间格式化。
Expand Down
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
"Dispatcher",
"dotCase",
"download",
"dpr",
"durationFormat",
"each",
"easing",
Expand Down
5 changes: 5 additions & 0 deletions demo/dpr.demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<require>dpr</require>
<script>
console.log(dpr.get());
dpr.on('change', dpr => console.log(dpr));
</script>
15 changes: 15 additions & 0 deletions i18n/dpr.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## CN

设备像素比工具库。

### on

绑定 change 事件。

### off

解绑 change 事件。

### get

获取当前设备像素比。
14 changes: 14 additions & 0 deletions index.json
Original file line number Diff line number Diff line change
Expand Up @@ -2250,6 +2250,20 @@
"browser"
]
},
"dpr": {
"demo": true,
"dependencies": [
"Emitter",
"MediaQuery"
],
"description": "Device pixel ratio helper.",
"env": [
"browser"
],
"test": [
"browser"
]
},
"durationFormat": {
"dependencies": [
"toInt",
Expand Down
57 changes: 57 additions & 0 deletions src/dpr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* Device pixel ratio helper.
*
* ### on
*
* Bind change event.
*
* ### off
*
* Unbind change event.
*
* ### get
*
* Get current device pixel ratio.
*/

/* example
* dpr.on('change', function(dpr) {
* console.log(dpr); // -> 2
* });
* dpr.get(); // -> 1
*/

/* module
* env: browser
*/

/* typescript
* export declare namespace dpr {
* interface IDpr extends Emitter {
* get(): number;
* }
* }
* export declare const dpr: dpr.IDpr;
*/

_('Emitter MediaQuery');

const m = new MediaQuery(`(resolution: ${get()}dppx)`);

exports = {
get
};

Emitter.mixin(exports);

function get() {
return window.devicePixelRatio || 1;
}

function change() {
const dpr = get();
m.setQuery(`(resolution: ${dpr}dppx)`);
exports.emit('change', dpr);
}

m.on('match', change);
m.on('unmatch', change);
1 change: 1 addition & 0 deletions test/dpr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
expect(dpr.get()).to.equal(window.devicePixelRatio);

0 comments on commit 1cb0201

Please sign in to comment.