@dan-uni/dan-any 0.7.7 → 0.7.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dan-uni/dan-any",
3
- "version": "0.7.7",
3
+ "version": "0.7.8",
4
4
  "description": "A danmaku transformer lib, supporting danmaku from different platforms.",
5
5
  "keywords": [
6
6
  "bangumi",
@@ -17,7 +17,7 @@
17
17
  },
18
18
  "author": "rinne",
19
19
  "main": "dist/index.js",
20
- "module": "src/index.ts",
20
+ "module": "dist/index.js",
21
21
  "types": "dist/src/index.d.ts",
22
22
  "browser": "dist/index.umd.min.js",
23
23
  "scripts": {
@@ -36,6 +36,7 @@
36
36
  "canvas": "^3.1.2",
37
37
  "class-transformer": "^0.5.1",
38
38
  "class-validator": "^0.14.2",
39
+ "fabric": "^6.7.1",
39
40
  "fast-xml-parser": "^5.2.5",
40
41
  "fs-extra": "^11.3.0",
41
42
  "hh-mm-ss": "^1.2.0",
package/rslib.config.ts CHANGED
@@ -18,6 +18,7 @@ export default defineConfig({
18
18
  output: {
19
19
  filename: { js: 'index.umd.min.js' },
20
20
  target: 'web',
21
+ externals: ['fabric/node'],
21
22
  },
22
23
  dts: true,
23
24
  umdName: pkg.name,
@@ -1,4 +1,4 @@
1
- import { assertType, it } from 'vitest'
1
+ import { assert, assertType, it } from 'vitest'
2
2
 
3
3
  import { measureTextWidth } from '../util/layout'
4
4
 
@@ -7,5 +7,5 @@ it('canvas measureTextWidth', () => {
7
7
  const width = measureTextWidth('SimHei', 25, false, text)
8
8
  assertType<number>(width)
9
9
  console.info(width, text.length)
10
- // assert(width >= 25 * text.length)
10
+ assert(width >= 25 * text.length)
11
11
  })
@@ -1,4 +1,5 @@
1
- import { createCanvas } from 'canvas'
1
+ import { Canvas as WebCanvas } from 'fabric'
2
+ import { StaticCanvas as NodeCanvas } from 'fabric/node'
2
3
  import type { UniPool } from '../..'
3
4
  import type { Danmaku, SubtitleStyle } from '../types'
4
5
 
@@ -78,7 +79,17 @@ const splitGrids = ({
78
79
  }
79
80
 
80
81
  export const measureTextWidth = (() => {
81
- const canvasContext = createCanvas(50, 50).getContext('2d')
82
+ let isWeb
83
+ try {
84
+ isWeb = !!window
85
+ } catch {
86
+ isWeb = false
87
+ }
88
+ const Canvas = isWeb ? WebCanvas : NodeCanvas
89
+ const canvasContext = new Canvas(undefined, {
90
+ width: 50,
91
+ height: 50,
92
+ }).getContext()
82
93
  const supportTextMeasure = !!canvasContext.measureText('中')
83
94
 
84
95
  if (supportTextMeasure) {