@leafer/canvas 1.7.0 → 1.8.0

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": "@leafer/canvas",
3
- "version": "1.7.0",
3
+ "version": "1.8.0",
4
4
  "description": "@leafer/canvas",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -22,15 +22,15 @@
22
22
  "leaferjs"
23
23
  ],
24
24
  "dependencies": {
25
- "@leafer/file": "1.7.0",
26
- "@leafer/list": "1.7.0",
27
- "@leafer/math": "1.7.0",
28
- "@leafer/data": "1.7.0",
29
- "@leafer/path": "1.7.0",
30
- "@leafer/debug": "1.7.0",
31
- "@leafer/platform": "1.7.0"
25
+ "@leafer/file": "1.8.0",
26
+ "@leafer/list": "1.8.0",
27
+ "@leafer/math": "1.8.0",
28
+ "@leafer/data": "1.8.0",
29
+ "@leafer/path": "1.8.0",
30
+ "@leafer/debug": "1.8.0",
31
+ "@leafer/platform": "1.8.0"
32
32
  },
33
33
  "devDependencies": {
34
- "@leafer/interface": "1.7.0"
34
+ "@leafer/interface": "1.8.0"
35
35
  }
36
36
  }
package/src/Canvas.ts CHANGED
@@ -3,10 +3,12 @@ import { ICanvasAttr, ITextMetrics, ICanvasContext2D, IPath2D, IObject, InnerId,
3
3
  function contextAttr(realName?: string) {
4
4
  return (target: Canvas, key: string) => {
5
5
  if (!realName) realName = key
6
- Object.defineProperty(target, key, {
7
- get() { return (this.context as IObject)[realName] },
8
- set(value: unknown) { (this.context as IObject)[realName] = value }
9
- } as ThisType<Canvas>)
6
+ const property = {
7
+ get() { return ((this as unknown as Canvas).context as IObject)[realName] },
8
+ set(value: unknown) { ((this as unknown as Canvas).context as IObject)[realName] = value }
9
+ }
10
+ if (key === 'strokeCap') (property as any).set = function (value: unknown) { ((this as unknown as Canvas).context as IObject)[realName] = value === 'none' ? 'butt' : value }
11
+ Object.defineProperty(target, key, property)
10
12
  }
11
13
  }
12
14
 
@@ -25,8 +25,8 @@ export class LeaferCanvasBase extends Canvas implements ILeaferCanvas {
25
25
  public get height(): number { return this.size.height }
26
26
 
27
27
  public get pixelRatio(): number { return this.size.pixelRatio }
28
- public get pixelWidth(): number { return this.width * this.pixelRatio }
29
- public get pixelHeight(): number { return this.height * this.pixelRatio }
28
+ public get pixelWidth(): number { return this.width * this.pixelRatio || 0 } // 防止出现 NaN
29
+ public get pixelHeight(): number { return this.height * this.pixelRatio || 0 }
30
30
 
31
31
  public get pixelSnap(): boolean { return this.config.pixelSnap }
32
32
  public set pixelSnap(value: boolean) { this.config.pixelSnap = value }
@@ -155,18 +155,26 @@ export class LeaferCanvasBase extends Canvas implements ILeaferCanvas {
155
155
  if (w) this.setTransform(w.a, w.b, w.c, w.d, w.e, w.f)
156
156
  }
157
157
 
158
- public setStroke(color: string | object, strokeWidth: number, options?: ICanvasStrokeOptions): void {
158
+ public setStroke(color: string | object, strokeWidth: number, options?: ICanvasStrokeOptions, childOptions?: ICanvasStrokeOptions): void {
159
159
  if (strokeWidth) this.strokeWidth = strokeWidth
160
160
  if (color) this.strokeStyle = color
161
- if (options) this.setStrokeOptions(options)
161
+ if (options) this.setStrokeOptions(options, childOptions)
162
162
  }
163
163
 
164
- public setStrokeOptions(options: ICanvasStrokeOptions): void {
165
- this.strokeCap = options.strokeCap === 'none' ? 'butt' : options.strokeCap
166
- this.strokeJoin = options.strokeJoin
167
- this.dashPattern = options.dashPattern
168
- this.dashOffset = options.dashOffset
169
- this.miterLimit = options.miterLimit
164
+ public setStrokeOptions(options: ICanvasStrokeOptions, childOptions?: ICanvasStrokeOptions): void {
165
+ let { strokeCap, strokeJoin, dashPattern, dashOffset, miterLimit } = options
166
+ if (childOptions) {
167
+ if (childOptions.strokeCap) strokeCap = childOptions.strokeCap
168
+ if (childOptions.strokeJoin) strokeJoin = childOptions.strokeJoin
169
+ if (childOptions.dashPattern !== undefined) dashPattern = childOptions.dashPattern
170
+ if (childOptions.dashOffset !== undefined) dashOffset = childOptions.dashOffset
171
+ if (childOptions.miterLimit) miterLimit = childOptions.miterLimit
172
+ }
173
+ this.strokeCap = strokeCap
174
+ this.strokeJoin = strokeJoin
175
+ this.dashPattern = dashPattern
176
+ this.dashOffset = dashOffset
177
+ this.miterLimit = miterLimit
170
178
  }
171
179
 
172
180
  public saveBlendMode(blendMode: IBlendMode): void {
package/types/index.d.ts CHANGED
@@ -120,8 +120,8 @@ declare class LeaferCanvasBase extends Canvas implements ILeaferCanvas {
120
120
  setCursor(_cursor: ICursorType | ICursorType[]): void;
121
121
  setWorld(matrix: IMatrixWithOptionHalfData, parentMatrix?: IMatrixData): void;
122
122
  useWorldTransform(worldTransform?: IMatrixData): void;
123
- setStroke(color: string | object, strokeWidth: number, options?: ICanvasStrokeOptions): void;
124
- setStrokeOptions(options: ICanvasStrokeOptions): void;
123
+ setStroke(color: string | object, strokeWidth: number, options?: ICanvasStrokeOptions, childOptions?: ICanvasStrokeOptions): void;
124
+ setStrokeOptions(options: ICanvasStrokeOptions, childOptions?: ICanvasStrokeOptions): void;
125
125
  saveBlendMode(blendMode: IBlendMode): void;
126
126
  restoreBlendMode(): void;
127
127
  hitFill(_point: IPointData, _fillRule?: IWindingRule): boolean;