@fluid-experimental/ink 2.1.0-276985 → 2.1.0-281041

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/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Contains a [Distributed Data Structure](https://fluidframework.com/docs/build/dds/) (DDS) for representing digital ink strokes.
4
4
 
5
- <!-- AUTO-GENERATED-CONTENT:START (LIBRARY_PACKAGE_README:scripts=FALSE&packageScopeNotice=EXPERIMENTAL) -->
5
+ <!-- AUTO-GENERATED-CONTENT:START (LIBRARY_PACKAGE_README_HEADER:) -->
6
6
 
7
7
  <!-- prettier-ignore-start -->
8
8
  <!-- NOTE: This section is automatically generated using @fluid-tools/markdown-magic. Do not update these generated contents directly. -->
@@ -28,10 +28,63 @@ To get started, install the package by running the following command:
28
28
  npm i @fluid-experimental/ink
29
29
  ```
30
30
 
31
+ <!-- prettier-ignore-end -->
32
+
33
+ <!-- AUTO-GENERATED-CONTENT:END -->
34
+
35
+ <!-- AUTO-GENERATED-CONTENT:START (LIBRARY_PACKAGE_README_FOOTER:) -->
36
+
37
+ <!-- prettier-ignore-start -->
38
+ <!-- NOTE: This section is automatically generated using @fluid-tools/markdown-magic. Do not update these generated contents directly. -->
39
+
31
40
  ## API Documentation
32
41
 
33
42
  API documentation for **@fluid-experimental/ink** is available at <https://fluidframework.com/docs/apis/ink>.
34
43
 
44
+ ## Minimum Client Requirements
45
+
46
+ These are the platform requirements for the current version of Fluid Framework Client Packages.
47
+ These requirements err on the side of being too strict since within a major version they can be relaxed over time, but not made stricter.
48
+ For Long Term Support (LTS) versions this can require supporting these platforms for several years.
49
+
50
+ It is likely that other configurations will work, but they are not supported: if they stop working, we do not consider that a bug.
51
+ If you would benefit from support for something not listed here, file an issue and the product team will evaluate your request.
52
+ When making such a request please include if the configuration already works (and thus the request is just that it becomes officially supported), or if changes are required to get it working.
53
+
54
+ ### Supported Runtimes
55
+
56
+ - NodeJs ^20.10.0 except that we will drop support for it [when NodeJs 20 loses its upstream support on 2026-04-30](https://github.com/nodejs/release#release-schedule), and will support a newer LTS version of NodeJS (22) at least 1 year before 20 is end-of-life. This same policy applies to NodeJS 22 when it is end of life (2027-04-30).
57
+ - Modern browsers supporting the es2022 standard library: in response to asks we can add explicit support for using babel to polyfill to target specific standards or runtimes (meaning we can avoid/remove use of things that don't polyfill robustly, but otherwise target modern standards).
58
+
59
+ ### Supported Tools
60
+
61
+ - TypeScript 5.4:
62
+ - All [`strict`](https://www.typescriptlang.org/tsconfig) options are supported.
63
+ - [`strictNullChecks`](https://www.typescriptlang.org/tsconfig) is required.
64
+ - [Configuration options deprecated in 5.0](https://github.com/microsoft/TypeScript/issues/51909) are not supported.
65
+ - `exactOptionalPropertyTypes` is currently not fully supported.
66
+ If used, narrowing members of Fluid Framework types types using `in`, `Reflect.has`, `Object.hasOwn` or `Object.prototype.hasOwnProperty` should be avoided as they may incorrectly exclude `undefined` from the possible values in some cases.
67
+ - [webpack](https://webpack.js.org/) 5
68
+ - We are not intending to be prescriptive about what bundler to use.
69
+ Other bundlers which can handle ES Modules should work, but webpack is the only one we actively test.
70
+
71
+ ### Module Resolution
72
+
73
+ [`Node16`, `NodeNext`, or `Bundler`](https://www.typescriptlang.org/tsconfig#moduleResolution) resolution should be used with TypeScript compilerOptions to follow the [Node.js v12+ ESM Resolution and Loading algorithm](https://nodejs.github.io/nodejs.dev/en/api/v20/esm/#resolution-and-loading-algorithm).
74
+ Node10 resolution is not supported as it does not support Fluid Framework's API structuring pattern that is used to distinguish stable APIs from those that are in development.
75
+
76
+ ### Module Formats
77
+
78
+ - ES Modules:
79
+ ES Modules are the preferred way to consume our client packages (including in NodeJs) and consuming our client packages from ES Modules is fully supported.
80
+ - CommonJs:
81
+ Consuming our client packages as CommonJs is supported only in NodeJS and only for the cases listed below.
82
+ This is done to accommodate some workflows without good ES Module support.
83
+ If you have a workflow you would like included in this list, file an issue.
84
+ Once this list of workflows motivating CommonJS support is empty, we may drop support for CommonJS one year after notice of the change is posted here.
85
+
86
+ - Testing with Jest (which lacks [stable ESM support](https://jestjs.io/docs/ecmascript-modules) due to [unstable APIs in NodeJs](https://github.com/nodejs/node/issues/37648))
87
+
35
88
  ## Contribution Guidelines
36
89
 
37
90
  There are many ways to [contribute](https://github.com/microsoft/FluidFramework/blob/main/CONTRIBUTING.md) to Fluid.
@@ -1 +1 @@
1
- {"version":3,"file":"inkCanvas.d.ts","sourceRoot":"","sources":["../src/inkCanvas.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,IAAI,EAAiD,MAAM,iBAAiB,CAAC;AAyG9F;;GAEG;AACH,qBAAa,SAAS;IAMpB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,KAAK;IANvB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA2B;IACnD,OAAO,CAAC,QAAQ,CAAC,oBAAoB,CAAkC;IACvE,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAO;gBAGhB,MAAM,EAAE,iBAAiB,EACzB,KAAK,EAAE,IAAI;IAwBtB,WAAW,CAAC,KAAK,EAAE,MAAM;IAIzB,MAAM;IAYN,KAAK;IAKL,sBAAsB;IAa7B,OAAO,CAAC,iBAAiB;IAYzB,OAAO,CAAC,iBAAiB;IASzB,OAAO,CAAC,eAAe;IAOvB,OAAO,CAAC,0BAA0B;IAclC,OAAO,CAAC,aAAa;IAiBrB;;OAEG;IACH,OAAO,CAAC,WAAW;IAInB,OAAO,CAAC,MAAM;IAcd,OAAO,CAAC,iBAAiB;IAOzB,OAAO,CAAC,YAAY;CASpB"}
1
+ {"version":3,"file":"inkCanvas.d.ts","sourceRoot":"","sources":["../src/inkCanvas.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,IAAI,EAAiD,MAAM,iBAAiB,CAAC;AA4G9F;;GAEG;AACH,qBAAa,SAAS;IAMpB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,KAAK;IANvB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA2B;IACnD,OAAO,CAAC,QAAQ,CAAC,oBAAoB,CAAkC;IACvE,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAO;gBAGhB,MAAM,EAAE,iBAAiB,EACzB,KAAK,EAAE,IAAI;IAwBtB,WAAW,CAAC,KAAK,EAAE,MAAM;IAIzB,MAAM;IAeN,KAAK;IAKL,sBAAsB;IAa7B,OAAO,CAAC,iBAAiB;IAYzB,OAAO,CAAC,iBAAiB;IASzB,OAAO,CAAC,eAAe;IAOvB,OAAO,CAAC,0BAA0B;IAclC,OAAO,CAAC,aAAa;IAqBrB;;OAEG;IACH,OAAO,CAAC,WAAW;IAInB,OAAO,CAAC,MAAM;IAgBd,OAAO,CAAC,iBAAiB;IAOzB,OAAO,CAAC,YAAY;CAWpB"}
package/dist/inkCanvas.js CHANGED
@@ -28,18 +28,21 @@ class Vector {
28
28
  }
29
29
  }
30
30
  function drawPolygon(context, points) {
31
- if (points.length === 0) {
31
+ const firstPoint = points[0];
32
+ if (firstPoint === undefined) {
32
33
  return;
33
34
  }
34
35
  context.beginPath();
35
36
  // Move to the first point
36
- context.moveTo(points[0].x, points[0].y);
37
+ context.moveTo(firstPoint.x, firstPoint.y);
37
38
  // Draw the rest of the segments
38
39
  for (let i = 1; i < points.length; i++) {
40
+ // Non null asserting, this must exist because the we are iterating through the length of points
41
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
39
42
  context.lineTo(points[i].x, points[i].y);
40
43
  }
41
44
  // And then close the shape
42
- context.lineTo(points[0].x, points[0].y);
45
+ context.lineTo(firstPoint.x, firstPoint.y);
43
46
  context.closePath();
44
47
  context.fill();
45
48
  }
@@ -112,7 +115,10 @@ class InkCanvas {
112
115
  this.clearCanvas();
113
116
  const strokes = this.model.getStrokes();
114
117
  // Time of the first operation in stroke 0 is our starting time
115
- const startTime = strokes[0].points[0].time;
118
+ const startTime = strokes[0]?.points[0]?.time;
119
+ if (startTime === undefined) {
120
+ throw new Error("Couldn't get start time");
121
+ }
116
122
  for (const stroke of strokes) {
117
123
  this.animateStroke(stroke, 0, startTime);
118
124
  }
@@ -173,7 +179,11 @@ class InkCanvas {
173
179
  return;
174
180
  }
175
181
  // Draw the requested stroke
182
+ // Non null asserting, this must exist because the operationIndex must be less than the length of stroke.points
183
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
176
184
  const current = stroke.points[operationIndex];
185
+ // Non null asserting, this must exist because its either the first index or operationIndex minus one which is less than the length of stroke.points
186
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
177
187
  const previous = stroke.points[Math.max(0, operationIndex - 1)];
178
188
  const time = operationIndex === 0 ? current.time - startTime : current.time - previous.time;
179
189
  setTimeout(() => {
@@ -191,6 +201,8 @@ class InkCanvas {
191
201
  this.clearCanvas();
192
202
  const strokes = this.model.getStrokes();
193
203
  for (const stroke of strokes) {
204
+ // Non null asserting since previous would not be used if stroke.points is empty
205
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
194
206
  let previous = stroke.points[0];
195
207
  for (const current of stroke.points) {
196
208
  // For the down, current === previous === stroke.operations[0]
@@ -210,7 +222,10 @@ class InkCanvas {
210
222
  const dirtyStrokeId = operation.id;
211
223
  const stroke = this.model.getStroke(dirtyStrokeId);
212
224
  // If this is the only point in the stroke, we'll use it for both the start and end of the segment
213
- const prevPoint = stroke.points[stroke.points.length - (stroke.points.length >= 2 ? 2 : 1)];
225
+ const prevPoint =
226
+ // Non null asserting, this must exist its within the length of stroke.points
227
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
228
+ stroke.points[stroke.points.length - (stroke.points.length >= 2 ? 2 : 1)];
214
229
  this.drawStrokeSegment(stroke.pen, prevPoint, operation.point);
215
230
  }
216
231
  }
@@ -1 +1 @@
1
- {"version":3,"file":"inkCanvas.js","sourceRoot":"","sources":["../src/inkCanvas.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M,CAAC,MAAM,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QAC3E,IAAI,CAAC,iBAAiB,CAAC,MAAM,CAAC,GAAG,EAAE,SAAS,EAAE,SAAS,CAAC,KAAK,CAAC,CAAC;IAChE,CAAC;CACD;AAjKD,8BAiKC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { IColor, IInk, IInkPoint, IInkStroke, IPen, IStylusOperation } from \"./interfaces.js\";\n\ninterface IPoint {\n\tx: number;\n\ty: number;\n}\n\nclass Vector {\n\t/**\n\t * Returns the vector resulting from rotating vector by angle\n\t */\n\tpublic static rotate(vector: Vector, angle: number): Vector {\n\t\treturn new Vector(\n\t\t\tvector.x * Math.cos(angle) - vector.y * Math.sin(angle),\n\t\t\tvector.x * Math.sin(angle) + vector.y * Math.cos(angle),\n\t\t);\n\t}\n\n\t/**\n\t * Returns the normalized form of the given vector\n\t */\n\tpublic static normalize(vector: Vector): Vector {\n\t\tconst length = vector.length();\n\t\treturn new Vector(vector.x / length, vector.y / length);\n\t}\n\n\tconstructor(\n\t\tpublic x: number,\n\t\tpublic y: number,\n\t) {}\n\n\tpublic length(): number {\n\t\treturn Math.sqrt(this.x * this.x + this.y * this.y);\n\t}\n}\n\nfunction drawPolygon(context: CanvasRenderingContext2D, points: IPoint[]) {\n\tif (points.length === 0) {\n\t\treturn;\n\t}\n\n\tcontext.beginPath();\n\t// Move to the first point\n\tcontext.moveTo(points[0].x, points[0].y);\n\n\t// Draw the rest of the segments\n\tfor (let i = 1; i < points.length; i++) {\n\t\tcontext.lineTo(points[i].x, points[i].y);\n\t}\n\n\t// And then close the shape\n\tcontext.lineTo(points[0].x, points[0].y);\n\tcontext.closePath();\n\tcontext.fill();\n}\n\nfunction drawCircle(context: CanvasRenderingContext2D, center: IPoint, radius: number) {\n\tcontext.beginPath();\n\tcontext.moveTo(center.x, center.y);\n\tcontext.arc(center.x, center.y, radius, 0, Math.PI * 2);\n\tcontext.closePath();\n\tcontext.fill();\n}\n\nfunction drawShapes(\n\tcontext: CanvasRenderingContext2D,\n\tstartPoint: IInkPoint,\n\tendPoint: IInkPoint,\n\tpen: IPen,\n): void {\n\tconst dirVector = new Vector(endPoint.x - startPoint.x, endPoint.y - startPoint.y);\n\tconst len = dirVector.length();\n\n\tconst widthAtStart = pen.thickness * startPoint.pressure;\n\tconst widthAtEnd = pen.thickness * endPoint.pressure;\n\n\tif (len + Math.min(widthAtStart, widthAtEnd) > Math.max(widthAtStart, widthAtEnd)) {\n\t\t// Circles don't completely overlap, need a trapezoid\n\t\tconst normalizedLateralVector = new Vector(-dirVector.y / len, dirVector.x / len);\n\n\t\tconst trapezoidP0 = {\n\t\t\tx: startPoint.x + widthAtStart * normalizedLateralVector.x,\n\t\t\ty: startPoint.y + widthAtStart * normalizedLateralVector.y,\n\t\t};\n\t\tconst trapezoidP1 = {\n\t\t\tx: startPoint.x - widthAtStart * normalizedLateralVector.x,\n\t\t\ty: startPoint.y - widthAtStart * normalizedLateralVector.y,\n\t\t};\n\t\tconst trapezoidP2 = {\n\t\t\tx: endPoint.x - widthAtEnd * normalizedLateralVector.x,\n\t\t\ty: endPoint.y - widthAtEnd * normalizedLateralVector.y,\n\t\t};\n\t\tconst trapezoidP3 = {\n\t\t\tx: endPoint.x + widthAtEnd * normalizedLateralVector.x,\n\t\t\ty: endPoint.y + widthAtEnd * normalizedLateralVector.y,\n\t\t};\n\n\t\tdrawPolygon(context, [trapezoidP0, trapezoidP1, trapezoidP2, trapezoidP3]);\n\t}\n\n\t// End circle\n\t// TODO should only draw if not eclipsed by the previous circle, be careful about single-point\n\tdrawCircle(context, { x: endPoint.x, y: endPoint.y }, widthAtEnd);\n}\n\n/**\n * @internal\n */\nexport class InkCanvas {\n\tprivate readonly context: CanvasRenderingContext2D;\n\tprivate readonly localActiveStrokeMap: Map<number, string> = new Map();\n\tprivate readonly currentPen: IPen;\n\n\tconstructor(\n\t\tprivate readonly canvas: HTMLCanvasElement,\n\t\tprivate readonly model: IInk,\n\t) {\n\t\tthis.model.on(\"clear\", this.redraw.bind(this));\n\t\tthis.model.on(\"stylus\", this.handleStylus.bind(this));\n\t\tthis.canvas.style.touchAction = \"none\";\n\n\t\tthis.canvas.addEventListener(\"pointerdown\", this.handlePointerDown.bind(this));\n\t\tthis.canvas.addEventListener(\"pointermove\", this.handlePointerMove.bind(this));\n\t\tthis.canvas.addEventListener(\"pointerup\", this.handlePointerUp.bind(this));\n\n\t\tconst context = this.canvas.getContext(\"2d\");\n\t\tif (context === null) {\n\t\t\tthrow new Error(\"InkCanvas requires a canvas with 2d rendering context\");\n\t\t}\n\t\tthis.context = context;\n\n\t\tthis.currentPen = {\n\t\t\tcolor: { r: 0, g: 161, b: 241, a: 0 },\n\t\t\tthickness: 7,\n\t\t};\n\n\t\tthis.sizeCanvasBackingStore();\n\t}\n\n\tpublic setPenColor(color: IColor) {\n\t\tthis.currentPen.color = color;\n\t}\n\n\tpublic replay() {\n\t\tthis.clearCanvas();\n\n\t\tconst strokes = this.model.getStrokes();\n\n\t\t// Time of the first operation in stroke 0 is our starting time\n\t\tconst startTime = strokes[0].points[0].time;\n\t\tfor (const stroke of strokes) {\n\t\t\tthis.animateStroke(stroke, 0, startTime);\n\t\t}\n\t}\n\n\tpublic clear() {\n\t\tthis.model.clear();\n\t\tthis.redraw();\n\t}\n\n\tpublic sizeCanvasBackingStore() {\n\t\tconst canvasBoundingClientRect = this.canvas.getBoundingClientRect();\n\t\t// Scale the canvas size to match the physical pixel to avoid blurriness\n\t\tconst scale = window.devicePixelRatio;\n\t\tthis.canvas.width = Math.floor(canvasBoundingClientRect.width * scale);\n\t\tthis.canvas.height = Math.floor(canvasBoundingClientRect.height * scale);\n\t\t// Scale the context to bring back coordinate system in CSS pixels\n\t\tthis.context.setTransform(1, 0, 0, 1, 0, 0);\n\t\tthis.context.scale(scale, scale);\n\n\t\tthis.redraw();\n\t}\n\n\tprivate handlePointerDown(evt: PointerEvent) {\n\t\t// We will accept pen down or mouse left down as the start of a stroke.\n\t\tif (evt.pointerType === \"pen\" || (evt.pointerType === \"mouse\" && evt.button === 0)) {\n\t\t\tconst strokeId = this.model.createStroke(this.currentPen).id;\n\t\t\tthis.localActiveStrokeMap.set(evt.pointerId, strokeId);\n\n\t\t\tthis.appendPointerEventToStroke(evt);\n\n\t\t\tevt.preventDefault();\n\t\t}\n\t}\n\n\tprivate handlePointerMove(evt: PointerEvent) {\n\t\tif (this.localActiveStrokeMap.has(evt.pointerId)) {\n\t\t\tconst evts = (evt as any)?.getCoalescedEvents() ?? ([evt] as PointerEvent[]);\n\t\t\tfor (const e of evts) {\n\t\t\t\tthis.appendPointerEventToStroke(e);\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate handlePointerUp(evt: PointerEvent) {\n\t\tif (this.localActiveStrokeMap.has(evt.pointerId)) {\n\t\t\tthis.appendPointerEventToStroke(evt);\n\t\t\tthis.localActiveStrokeMap.delete(evt.pointerId);\n\t\t}\n\t}\n\n\tprivate appendPointerEventToStroke(evt: PointerEvent) {\n\t\tconst strokeId = this.localActiveStrokeMap.get(evt.pointerId);\n\t\tif (strokeId === undefined) {\n\t\t\tthrow new Error(\"Unexpected pointer ID trying to append to stroke\");\n\t\t}\n\t\tconst inkPt = {\n\t\t\tx: evt.offsetX,\n\t\t\ty: evt.offsetY,\n\t\t\ttime: Date.now(),\n\t\t\tpressure: evt.pressure,\n\t\t};\n\t\tthis.model.appendPointToStroke(inkPt, strokeId);\n\t}\n\n\tprivate animateStroke(stroke: IInkStroke, operationIndex: number, startTime: number) {\n\t\tif (operationIndex >= stroke.points.length) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Draw the requested stroke\n\t\tconst current = stroke.points[operationIndex];\n\t\tconst previous = stroke.points[Math.max(0, operationIndex - 1)];\n\t\tconst time =\n\t\t\toperationIndex === 0 ? current.time - startTime : current.time - previous.time;\n\n\t\tsetTimeout(() => {\n\t\t\tthis.drawStrokeSegment(stroke.pen, current, previous);\n\t\t\tthis.animateStroke(stroke, operationIndex + 1, startTime);\n\t\t}, time);\n\t}\n\n\t/**\n\t * Clears the canvas\n\t */\n\tprivate clearCanvas() {\n\t\tthis.context.clearRect(0, 0, this.canvas.width, this.canvas.height);\n\t}\n\n\tprivate redraw() {\n\t\tthis.clearCanvas();\n\n\t\tconst strokes = this.model.getStrokes();\n\t\tfor (const stroke of strokes) {\n\t\t\tlet previous = stroke.points[0];\n\t\t\tfor (const current of stroke.points) {\n\t\t\t\t// For the down, current === previous === stroke.operations[0]\n\t\t\t\tthis.drawStrokeSegment(stroke.pen, current, previous);\n\t\t\t\tprevious = current;\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate drawStrokeSegment(pen: IPen, current: IInkPoint, previous: IInkPoint) {\n\t\t// TODO Consider save/restore context\n\t\t// TODO Consider half-pixel offset\n\t\tthis.context.fillStyle = `rgb(${pen.color.r}, ${pen.color.g}, ${pen.color.b})`;\n\t\tdrawShapes(this.context, previous, current, pen);\n\t}\n\n\tprivate handleStylus(operation: IStylusOperation) {\n\t\t// Render the dirty stroke\n\t\tconst dirtyStrokeId = operation.id;\n\t\tconst stroke = this.model.getStroke(dirtyStrokeId);\n\t\t// If this is the only point in the stroke, we'll use it for both the start and end of the segment\n\t\tconst prevPoint =\n\t\t\tstroke.points[stroke.points.length - (stroke.points.length >= 2 ? 2 : 1)];\n\t\tthis.drawStrokeSegment(stroke.pen, prevPoint, operation.point);\n\t}\n}\n"]}
1
+ {"version":3,"file":"inkCanvas.js","sourceRoot":"","sources":["../src/inkCanvas.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AASH,MAAM,MAAM;IACX;;OAEG;IACI,MAAM,CAAC,MAAM,CAAC,MAAc,EAAE,KAAa;QACjD,OAAO,IAAI,MAAM,CAChB,MAAM,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG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E;QACpE,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAE,CAAC;QAC5E,IAAI,CAAC,iBAAiB,CAAC,MAAM,CAAC,GAAG,EAAE,SAAS,EAAE,SAAS,CAAC,KAAK,CAAC,CAAC;IAChE,CAAC;CACD;AA5KD,8BA4KC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { IColor, IInk, IInkPoint, IInkStroke, IPen, IStylusOperation } from \"./interfaces.js\";\n\ninterface IPoint {\n\tx: number;\n\ty: number;\n}\n\nclass Vector {\n\t/**\n\t * Returns the vector resulting from rotating vector by angle\n\t */\n\tpublic static rotate(vector: Vector, angle: number): Vector {\n\t\treturn new Vector(\n\t\t\tvector.x * Math.cos(angle) - vector.y * Math.sin(angle),\n\t\t\tvector.x * Math.sin(angle) + vector.y * Math.cos(angle),\n\t\t);\n\t}\n\n\t/**\n\t * Returns the normalized form of the given vector\n\t */\n\tpublic static normalize(vector: Vector): Vector {\n\t\tconst length = vector.length();\n\t\treturn new Vector(vector.x / length, vector.y / length);\n\t}\n\n\tconstructor(\n\t\tpublic x: number,\n\t\tpublic y: number,\n\t) {}\n\n\tpublic length(): number {\n\t\treturn Math.sqrt(this.x * this.x + this.y * this.y);\n\t}\n}\n\nfunction drawPolygon(context: CanvasRenderingContext2D, points: IPoint[]) {\n\tconst firstPoint = points[0];\n\tif (firstPoint === undefined) {\n\t\treturn;\n\t}\n\n\tcontext.beginPath();\n\t// Move to the first point\n\tcontext.moveTo(firstPoint.x, firstPoint.y);\n\n\t// Draw the rest of the segments\n\tfor (let i = 1; i < points.length; i++) {\n\t\t// Non null asserting, this must exist because the we are iterating through the length of points\n\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\tcontext.lineTo(points[i]!.x, points[i]!.y);\n\t}\n\n\t// And then close the shape\n\tcontext.lineTo(firstPoint.x, firstPoint.y);\n\tcontext.closePath();\n\tcontext.fill();\n}\n\nfunction drawCircle(context: CanvasRenderingContext2D, center: IPoint, radius: number) {\n\tcontext.beginPath();\n\tcontext.moveTo(center.x, center.y);\n\tcontext.arc(center.x, center.y, radius, 0, Math.PI * 2);\n\tcontext.closePath();\n\tcontext.fill();\n}\n\nfunction drawShapes(\n\tcontext: CanvasRenderingContext2D,\n\tstartPoint: IInkPoint,\n\tendPoint: IInkPoint,\n\tpen: IPen,\n): void {\n\tconst dirVector = new Vector(endPoint.x - startPoint.x, endPoint.y - startPoint.y);\n\tconst len = dirVector.length();\n\n\tconst widthAtStart = pen.thickness * startPoint.pressure;\n\tconst widthAtEnd = pen.thickness * endPoint.pressure;\n\n\tif (len + Math.min(widthAtStart, widthAtEnd) > Math.max(widthAtStart, widthAtEnd)) {\n\t\t// Circles don't completely overlap, need a trapezoid\n\t\tconst normalizedLateralVector = new Vector(-dirVector.y / len, dirVector.x / len);\n\n\t\tconst trapezoidP0 = {\n\t\t\tx: startPoint.x + widthAtStart * normalizedLateralVector.x,\n\t\t\ty: startPoint.y + widthAtStart * normalizedLateralVector.y,\n\t\t};\n\t\tconst trapezoidP1 = {\n\t\t\tx: startPoint.x - widthAtStart * normalizedLateralVector.x,\n\t\t\ty: startPoint.y - widthAtStart * normalizedLateralVector.y,\n\t\t};\n\t\tconst trapezoidP2 = {\n\t\t\tx: endPoint.x - widthAtEnd * normalizedLateralVector.x,\n\t\t\ty: endPoint.y - widthAtEnd * normalizedLateralVector.y,\n\t\t};\n\t\tconst trapezoidP3 = {\n\t\t\tx: endPoint.x + widthAtEnd * normalizedLateralVector.x,\n\t\t\ty: endPoint.y + widthAtEnd * normalizedLateralVector.y,\n\t\t};\n\n\t\tdrawPolygon(context, [trapezoidP0, trapezoidP1, trapezoidP2, trapezoidP3]);\n\t}\n\n\t// End circle\n\t// TODO should only draw if not eclipsed by the previous circle, be careful about single-point\n\tdrawCircle(context, { x: endPoint.x, y: endPoint.y }, widthAtEnd);\n}\n\n/**\n * @internal\n */\nexport class InkCanvas {\n\tprivate readonly context: CanvasRenderingContext2D;\n\tprivate readonly localActiveStrokeMap: Map<number, string> = new Map();\n\tprivate readonly currentPen: IPen;\n\n\tconstructor(\n\t\tprivate readonly canvas: HTMLCanvasElement,\n\t\tprivate readonly model: IInk,\n\t) {\n\t\tthis.model.on(\"clear\", this.redraw.bind(this));\n\t\tthis.model.on(\"stylus\", this.handleStylus.bind(this));\n\t\tthis.canvas.style.touchAction = \"none\";\n\n\t\tthis.canvas.addEventListener(\"pointerdown\", this.handlePointerDown.bind(this));\n\t\tthis.canvas.addEventListener(\"pointermove\", this.handlePointerMove.bind(this));\n\t\tthis.canvas.addEventListener(\"pointerup\", this.handlePointerUp.bind(this));\n\n\t\tconst context = this.canvas.getContext(\"2d\");\n\t\tif (context === null) {\n\t\t\tthrow new Error(\"InkCanvas requires a canvas with 2d rendering context\");\n\t\t}\n\t\tthis.context = context;\n\n\t\tthis.currentPen = {\n\t\t\tcolor: { r: 0, g: 161, b: 241, a: 0 },\n\t\t\tthickness: 7,\n\t\t};\n\n\t\tthis.sizeCanvasBackingStore();\n\t}\n\n\tpublic setPenColor(color: IColor) {\n\t\tthis.currentPen.color = color;\n\t}\n\n\tpublic replay() {\n\t\tthis.clearCanvas();\n\n\t\tconst strokes = this.model.getStrokes();\n\n\t\t// Time of the first operation in stroke 0 is our starting time\n\t\tconst startTime = strokes[0]?.points[0]?.time;\n\t\tif (startTime === undefined) {\n\t\t\tthrow new Error(\"Couldn't get start time\");\n\t\t}\n\t\tfor (const stroke of strokes) {\n\t\t\tthis.animateStroke(stroke, 0, startTime);\n\t\t}\n\t}\n\n\tpublic clear() {\n\t\tthis.model.clear();\n\t\tthis.redraw();\n\t}\n\n\tpublic sizeCanvasBackingStore() {\n\t\tconst canvasBoundingClientRect = this.canvas.getBoundingClientRect();\n\t\t// Scale the canvas size to match the physical pixel to avoid blurriness\n\t\tconst scale = window.devicePixelRatio;\n\t\tthis.canvas.width = Math.floor(canvasBoundingClientRect.width * scale);\n\t\tthis.canvas.height = Math.floor(canvasBoundingClientRect.height * scale);\n\t\t// Scale the context to bring back coordinate system in CSS pixels\n\t\tthis.context.setTransform(1, 0, 0, 1, 0, 0);\n\t\tthis.context.scale(scale, scale);\n\n\t\tthis.redraw();\n\t}\n\n\tprivate handlePointerDown(evt: PointerEvent) {\n\t\t// We will accept pen down or mouse left down as the start of a stroke.\n\t\tif (evt.pointerType === \"pen\" || (evt.pointerType === \"mouse\" && evt.button === 0)) {\n\t\t\tconst strokeId = this.model.createStroke(this.currentPen).id;\n\t\t\tthis.localActiveStrokeMap.set(evt.pointerId, strokeId);\n\n\t\t\tthis.appendPointerEventToStroke(evt);\n\n\t\t\tevt.preventDefault();\n\t\t}\n\t}\n\n\tprivate handlePointerMove(evt: PointerEvent) {\n\t\tif (this.localActiveStrokeMap.has(evt.pointerId)) {\n\t\t\tconst evts = (evt as any)?.getCoalescedEvents() ?? ([evt] as PointerEvent[]);\n\t\t\tfor (const e of evts) {\n\t\t\t\tthis.appendPointerEventToStroke(e);\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate handlePointerUp(evt: PointerEvent) {\n\t\tif (this.localActiveStrokeMap.has(evt.pointerId)) {\n\t\t\tthis.appendPointerEventToStroke(evt);\n\t\t\tthis.localActiveStrokeMap.delete(evt.pointerId);\n\t\t}\n\t}\n\n\tprivate appendPointerEventToStroke(evt: PointerEvent) {\n\t\tconst strokeId = this.localActiveStrokeMap.get(evt.pointerId);\n\t\tif (strokeId === undefined) {\n\t\t\tthrow new Error(\"Unexpected pointer ID trying to append to stroke\");\n\t\t}\n\t\tconst inkPt = {\n\t\t\tx: evt.offsetX,\n\t\t\ty: evt.offsetY,\n\t\t\ttime: Date.now(),\n\t\t\tpressure: evt.pressure,\n\t\t};\n\t\tthis.model.appendPointToStroke(inkPt, strokeId);\n\t}\n\n\tprivate animateStroke(stroke: IInkStroke, operationIndex: number, startTime: number) {\n\t\tif (operationIndex >= stroke.points.length) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Draw the requested stroke\n\t\t// Non null asserting, this must exist because the operationIndex must be less than the length of stroke.points\n\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\tconst current = stroke.points[operationIndex]!;\n\t\t// Non null asserting, this must exist because its either the first index or operationIndex minus one which is less than the length of stroke.points\n\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\tconst previous = stroke.points[Math.max(0, operationIndex - 1)]!;\n\t\tconst time =\n\t\t\toperationIndex === 0 ? current.time - startTime : current.time - previous.time;\n\n\t\tsetTimeout(() => {\n\t\t\tthis.drawStrokeSegment(stroke.pen, current, previous);\n\t\t\tthis.animateStroke(stroke, operationIndex + 1, startTime);\n\t\t}, time);\n\t}\n\n\t/**\n\t * Clears the canvas\n\t */\n\tprivate clearCanvas() {\n\t\tthis.context.clearRect(0, 0, this.canvas.width, this.canvas.height);\n\t}\n\n\tprivate redraw() {\n\t\tthis.clearCanvas();\n\n\t\tconst strokes = this.model.getStrokes();\n\t\tfor (const stroke of strokes) {\n\t\t\t// Non null asserting since previous would not be used if stroke.points is empty\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\t\tlet previous = stroke.points[0]!;\n\t\t\tfor (const current of stroke.points) {\n\t\t\t\t// For the down, current === previous === stroke.operations[0]\n\t\t\t\tthis.drawStrokeSegment(stroke.pen, current, previous);\n\t\t\t\tprevious = current;\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate drawStrokeSegment(pen: IPen, current: IInkPoint, previous: IInkPoint) {\n\t\t// TODO Consider save/restore context\n\t\t// TODO Consider half-pixel offset\n\t\tthis.context.fillStyle = `rgb(${pen.color.r}, ${pen.color.g}, ${pen.color.b})`;\n\t\tdrawShapes(this.context, previous, current, pen);\n\t}\n\n\tprivate handleStylus(operation: IStylusOperation) {\n\t\t// Render the dirty stroke\n\t\tconst dirtyStrokeId = operation.id;\n\t\tconst stroke = this.model.getStroke(dirtyStrokeId);\n\t\t// If this is the only point in the stroke, we'll use it for both the start and end of the segment\n\t\tconst prevPoint =\n\t\t\t// Non null asserting, this must exist its within the length of stroke.points\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\t\tstroke.points[stroke.points.length - (stroke.points.length >= 2 ? 2 : 1)]!;\n\t\tthis.drawStrokeSegment(stroke.pen, prevPoint, operation.point);\n\t}\n}\n"]}
@@ -5,5 +5,5 @@
5
5
  * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY
6
6
  */
7
7
  export declare const pkgName = "@fluid-experimental/ink";
8
- export declare const pkgVersion = "2.1.0-276985";
8
+ export declare const pkgVersion = "2.1.0-281041";
9
9
  //# sourceMappingURL=packageVersion.d.ts.map
@@ -8,5 +8,5 @@
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.pkgVersion = exports.pkgName = void 0;
10
10
  exports.pkgName = "@fluid-experimental/ink";
11
- exports.pkgVersion = "2.1.0-276985";
11
+ exports.pkgVersion = "2.1.0-281041";
12
12
  //# sourceMappingURL=packageVersion.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"packageVersion.js","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEU,QAAA,OAAO,GAAG,yBAAyB,CAAC;AACpC,QAAA,UAAU,GAAG,cAAc,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n *\n * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY\n */\n\nexport const pkgName = \"@fluid-experimental/ink\";\nexport const pkgVersion = \"2.1.0-276985\";\n"]}
1
+ {"version":3,"file":"packageVersion.js","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEU,QAAA,OAAO,GAAG,yBAAyB,CAAC;AACpC,QAAA,UAAU,GAAG,cAAc,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n *\n * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY\n */\n\nexport const pkgName = \"@fluid-experimental/ink\";\nexport const pkgVersion = \"2.1.0-281041\";\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"snapshot.d.ts","sourceRoot":"","sources":["../src/snapshot.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAChC;;OAEG;IACH,OAAO,EAAE,UAAU,EAAE,CAAC;IAEtB;;;OAGG;IACH,WAAW,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC;CACvC;AAED;;GAEG;AACH,qBAAa,OAAO;IACnB;;OAEG;IACH,OAAO,CAAC,OAAO,CAAe;IAE9B;;OAEG;IACH,OAAO,CAAC,WAAW,CAA4B;IAE/C;;;OAGG;gBACS,QAAQ,CAAC,EAAE,gBAAgB;IAKvC;;OAEG;IACI,UAAU,IAAI,UAAU,EAAE;IAIjC;;OAEG;IACI,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU;IAIzC;;OAEG;IACI,KAAK,IAAI,IAAI;IAKpB;;;OAGG;IACI,SAAS,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI;IAK1C;;;OAGG;IACI,eAAe,IAAI,gBAAgB;CAM1C"}
1
+ {"version":3,"file":"snapshot.d.ts","sourceRoot":"","sources":["../src/snapshot.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAChC;;OAEG;IACH,OAAO,EAAE,UAAU,EAAE,CAAC;IAEtB;;;OAGG;IACH,WAAW,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC;CACvC;AAED;;GAEG;AACH,qBAAa,OAAO;IACnB;;OAEG;IACH,OAAO,CAAC,OAAO,CAAe;IAE9B;;OAEG;IACH,OAAO,CAAC,WAAW,CAA4B;IAE/C;;;OAGG;gBACS,QAAQ,CAAC,EAAE,gBAAgB;IAKvC;;OAEG;IACI,UAAU,IAAI,UAAU,EAAE;IAIjC;;OAEG;IACI,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU;IAMzC;;OAEG;IACI,KAAK,IAAI,IAAI;IAKpB;;;OAGG;IACI,SAAS,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI;IAK1C;;;OAGG;IACI,eAAe,IAAI,gBAAgB;CAM1C"}
package/dist/snapshot.js CHANGED
@@ -27,6 +27,8 @@ class InkData {
27
27
  * {@inheritDoc IInk.getStroke}
28
28
  */
29
29
  getStroke(key) {
30
+ // Non null asserting, it should be okay that a bad key results in an error being thrown
31
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
30
32
  return this.strokes[this.strokeIndex[key]];
31
33
  }
32
34
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"snapshot.js","sourceRoot":"","sources":["../src/snapshot.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAoBH;;GAEG;AACH,MAAa,OAAO;IAWnB;;;OAGG;IACH,YAAY,QAA2B;QACtC,IAAI,CAAC,OAAO,GAAG,QAAQ,EAAE,OAAO,IAAI,EAAE,CAAC;QACvC,IAAI,CAAC,WAAW,GAAG,QAAQ,EAAE,WAAW,IAAI,EAAE,CAAC;IAChD,CAAC;IAED;;OAEG;IACI,UAAU;QAChB,OAAO,IAAI,CAAC,OAAO,CAAC;IACrB,CAAC;IAED;;OAEG;IACI,SAAS,CAAC,GAAW;QAC3B,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC;IAC5C,CAAC;IAED;;OAEG;IACI,KAAK;QACX,IAAI,CAAC,OAAO,GAAG,EAAE,CAAC;QAClB,IAAI,CAAC,WAAW,GAAG,EAAE,CAAC;IACvB,CAAC;IAED;;;OAGG;IACI,SAAS,CAAC,MAAkB;QAClC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAC1B,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC;IACvD,CAAC;IAED;;;OAGG;IACI,eAAe;QACrB,OAAO;YACN,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,WAAW,EAAE,IAAI,CAAC,WAAW;SAC7B,CAAC;IACH,CAAC;CACD;AA7DD,0BA6DC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { IInkStroke } from \"./interfaces.js\";\n\n/**\n * Ink snapshot interface.\n */\nexport interface ISerializableInk {\n\t/**\n\t * Collection of ink strokes.\n\t */\n\tstrokes: IInkStroke[];\n\n\t/**\n\t * Stores a mapping from the provided key to its index in strokes. Since\n\t * ISerializableInk is serialized we need to use an index.\n\t */\n\tstrokeIndex: { [key: string]: number };\n}\n\n/**\n * Maintains a live record of the data that can be used for snapshotting.\n */\nexport class InkData {\n\t/**\n\t * {@inheritDoc ISerializableInk.strokes}\n\t */\n\tprivate strokes: IInkStroke[];\n\n\t/**\n\t * {@inheritDoc ISerializableInk.strokeIndex}\n\t */\n\tprivate strokeIndex: { [key: string]: number };\n\n\t/**\n\t * Construct a new InkData.\n\t * @param snapshot - Existing data to initialize with\n\t */\n\tconstructor(snapshot?: ISerializableInk) {\n\t\tthis.strokes = snapshot?.strokes ?? [];\n\t\tthis.strokeIndex = snapshot?.strokeIndex ?? {};\n\t}\n\n\t/**\n\t * {@inheritDoc IInk.getStrokes}\n\t */\n\tpublic getStrokes(): IInkStroke[] {\n\t\treturn this.strokes;\n\t}\n\n\t/**\n\t * {@inheritDoc IInk.getStroke}\n\t */\n\tpublic getStroke(key: string): IInkStroke {\n\t\treturn this.strokes[this.strokeIndex[key]];\n\t}\n\n\t/**\n\t * Clear all stored data.\n\t */\n\tpublic clear(): void {\n\t\tthis.strokes = [];\n\t\tthis.strokeIndex = {};\n\t}\n\n\t/**\n\t * Add the given stroke to the stored data.\n\t * @param stroke - The stroke to add\n\t */\n\tpublic addStroke(stroke: IInkStroke): void {\n\t\tthis.strokes.push(stroke);\n\t\tthis.strokeIndex[stroke.id] = this.strokes.length - 1;\n\t}\n\n\t/**\n\t * Get a JSON-compatible representation of the stored data.\n\t * @returns The JSON-compatible object\n\t */\n\tpublic getSerializable(): ISerializableInk {\n\t\treturn {\n\t\t\tstrokes: this.strokes,\n\t\t\tstrokeIndex: this.strokeIndex,\n\t\t};\n\t}\n}\n"]}
1
+ {"version":3,"file":"snapshot.js","sourceRoot":"","sources":["../src/snapshot.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAoBH;;GAEG;AACH,MAAa,OAAO;IAWnB;;;OAGG;IACH,YAAY,QAA2B;QACtC,IAAI,CAAC,OAAO,GAAG,QAAQ,EAAE,OAAO,IAAI,EAAE,CAAC;QACvC,IAAI,CAAC,WAAW,GAAG,QAAQ,EAAE,WAAW,IAAI,EAAE,CAAC;IAChD,CAAC;IAED;;OAEG;IACI,UAAU;QAChB,OAAO,IAAI,CAAC,OAAO,CAAC;IACrB,CAAC;IAED;;OAEG;IACI,SAAS,CAAC,GAAW;QAC3B,wFAAwF;QACxF,oEAAoE;QACpE,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAE,CAAE,CAAC;IAC9C,CAAC;IAED;;OAEG;IACI,KAAK;QACX,IAAI,CAAC,OAAO,GAAG,EAAE,CAAC;QAClB,IAAI,CAAC,WAAW,GAAG,EAAE,CAAC;IACvB,CAAC;IAED;;;OAGG;IACI,SAAS,CAAC,MAAkB;QAClC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAC1B,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC;IACvD,CAAC;IAED;;;OAGG;IACI,eAAe;QACrB,OAAO;YACN,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,WAAW,EAAE,IAAI,CAAC,WAAW;SAC7B,CAAC;IACH,CAAC;CACD;AA/DD,0BA+DC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { IInkStroke } from \"./interfaces.js\";\n\n/**\n * Ink snapshot interface.\n */\nexport interface ISerializableInk {\n\t/**\n\t * Collection of ink strokes.\n\t */\n\tstrokes: IInkStroke[];\n\n\t/**\n\t * Stores a mapping from the provided key to its index in strokes. Since\n\t * ISerializableInk is serialized we need to use an index.\n\t */\n\tstrokeIndex: { [key: string]: number };\n}\n\n/**\n * Maintains a live record of the data that can be used for snapshotting.\n */\nexport class InkData {\n\t/**\n\t * {@inheritDoc ISerializableInk.strokes}\n\t */\n\tprivate strokes: IInkStroke[];\n\n\t/**\n\t * {@inheritDoc ISerializableInk.strokeIndex}\n\t */\n\tprivate strokeIndex: { [key: string]: number };\n\n\t/**\n\t * Construct a new InkData.\n\t * @param snapshot - Existing data to initialize with\n\t */\n\tconstructor(snapshot?: ISerializableInk) {\n\t\tthis.strokes = snapshot?.strokes ?? [];\n\t\tthis.strokeIndex = snapshot?.strokeIndex ?? {};\n\t}\n\n\t/**\n\t * {@inheritDoc IInk.getStrokes}\n\t */\n\tpublic getStrokes(): IInkStroke[] {\n\t\treturn this.strokes;\n\t}\n\n\t/**\n\t * {@inheritDoc IInk.getStroke}\n\t */\n\tpublic getStroke(key: string): IInkStroke {\n\t\t// Non null asserting, it should be okay that a bad key results in an error being thrown\n\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\treturn this.strokes[this.strokeIndex[key]!]!;\n\t}\n\n\t/**\n\t * Clear all stored data.\n\t */\n\tpublic clear(): void {\n\t\tthis.strokes = [];\n\t\tthis.strokeIndex = {};\n\t}\n\n\t/**\n\t * Add the given stroke to the stored data.\n\t * @param stroke - The stroke to add\n\t */\n\tpublic addStroke(stroke: IInkStroke): void {\n\t\tthis.strokes.push(stroke);\n\t\tthis.strokeIndex[stroke.id] = this.strokes.length - 1;\n\t}\n\n\t/**\n\t * Get a JSON-compatible representation of the stored data.\n\t * @returns The JSON-compatible object\n\t */\n\tpublic getSerializable(): ISerializableInk {\n\t\treturn {\n\t\t\tstrokes: this.strokes,\n\t\t\tstrokeIndex: this.strokeIndex,\n\t\t};\n\t}\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"inkCanvas.d.ts","sourceRoot":"","sources":["../src/inkCanvas.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,IAAI,EAAiD,MAAM,iBAAiB,CAAC;AAyG9F;;GAEG;AACH,qBAAa,SAAS;IAMpB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,KAAK;IANvB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA2B;IACnD,OAAO,CAAC,QAAQ,CAAC,oBAAoB,CAAkC;IACvE,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAO;gBAGhB,MAAM,EAAE,iBAAiB,EACzB,KAAK,EAAE,IAAI;IAwBtB,WAAW,CAAC,KAAK,EAAE,MAAM;IAIzB,MAAM;IAYN,KAAK;IAKL,sBAAsB;IAa7B,OAAO,CAAC,iBAAiB;IAYzB,OAAO,CAAC,iBAAiB;IASzB,OAAO,CAAC,eAAe;IAOvB,OAAO,CAAC,0BAA0B;IAclC,OAAO,CAAC,aAAa;IAiBrB;;OAEG;IACH,OAAO,CAAC,WAAW;IAInB,OAAO,CAAC,MAAM;IAcd,OAAO,CAAC,iBAAiB;IAOzB,OAAO,CAAC,YAAY;CASpB"}
1
+ {"version":3,"file":"inkCanvas.d.ts","sourceRoot":"","sources":["../src/inkCanvas.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,IAAI,EAAiD,MAAM,iBAAiB,CAAC;AA4G9F;;GAEG;AACH,qBAAa,SAAS;IAMpB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,KAAK;IANvB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA2B;IACnD,OAAO,CAAC,QAAQ,CAAC,oBAAoB,CAAkC;IACvE,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAO;gBAGhB,MAAM,EAAE,iBAAiB,EACzB,KAAK,EAAE,IAAI;IAwBtB,WAAW,CAAC,KAAK,EAAE,MAAM;IAIzB,MAAM;IAeN,KAAK;IAKL,sBAAsB;IAa7B,OAAO,CAAC,iBAAiB;IAYzB,OAAO,CAAC,iBAAiB;IASzB,OAAO,CAAC,eAAe;IAOvB,OAAO,CAAC,0BAA0B;IAclC,OAAO,CAAC,aAAa;IAqBrB;;OAEG;IACH,OAAO,CAAC,WAAW;IAInB,OAAO,CAAC,MAAM;IAgBd,OAAO,CAAC,iBAAiB;IAOzB,OAAO,CAAC,YAAY;CAWpB"}
package/lib/inkCanvas.js CHANGED
@@ -25,18 +25,21 @@ class Vector {
25
25
  }
26
26
  }
27
27
  function drawPolygon(context, points) {
28
- if (points.length === 0) {
28
+ const firstPoint = points[0];
29
+ if (firstPoint === undefined) {
29
30
  return;
30
31
  }
31
32
  context.beginPath();
32
33
  // Move to the first point
33
- context.moveTo(points[0].x, points[0].y);
34
+ context.moveTo(firstPoint.x, firstPoint.y);
34
35
  // Draw the rest of the segments
35
36
  for (let i = 1; i < points.length; i++) {
37
+ // Non null asserting, this must exist because the we are iterating through the length of points
38
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
36
39
  context.lineTo(points[i].x, points[i].y);
37
40
  }
38
41
  // And then close the shape
39
- context.lineTo(points[0].x, points[0].y);
42
+ context.lineTo(firstPoint.x, firstPoint.y);
40
43
  context.closePath();
41
44
  context.fill();
42
45
  }
@@ -109,7 +112,10 @@ export class InkCanvas {
109
112
  this.clearCanvas();
110
113
  const strokes = this.model.getStrokes();
111
114
  // Time of the first operation in stroke 0 is our starting time
112
- const startTime = strokes[0].points[0].time;
115
+ const startTime = strokes[0]?.points[0]?.time;
116
+ if (startTime === undefined) {
117
+ throw new Error("Couldn't get start time");
118
+ }
113
119
  for (const stroke of strokes) {
114
120
  this.animateStroke(stroke, 0, startTime);
115
121
  }
@@ -170,7 +176,11 @@ export class InkCanvas {
170
176
  return;
171
177
  }
172
178
  // Draw the requested stroke
179
+ // Non null asserting, this must exist because the operationIndex must be less than the length of stroke.points
180
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
173
181
  const current = stroke.points[operationIndex];
182
+ // Non null asserting, this must exist because its either the first index or operationIndex minus one which is less than the length of stroke.points
183
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
174
184
  const previous = stroke.points[Math.max(0, operationIndex - 1)];
175
185
  const time = operationIndex === 0 ? current.time - startTime : current.time - previous.time;
176
186
  setTimeout(() => {
@@ -188,6 +198,8 @@ export class InkCanvas {
188
198
  this.clearCanvas();
189
199
  const strokes = this.model.getStrokes();
190
200
  for (const stroke of strokes) {
201
+ // Non null asserting since previous would not be used if stroke.points is empty
202
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
191
203
  let previous = stroke.points[0];
192
204
  for (const current of stroke.points) {
193
205
  // For the down, current === previous === stroke.operations[0]
@@ -207,7 +219,10 @@ export class InkCanvas {
207
219
  const dirtyStrokeId = operation.id;
208
220
  const stroke = this.model.getStroke(dirtyStrokeId);
209
221
  // If this is the only point in the stroke, we'll use it for both the start and end of the segment
210
- const prevPoint = stroke.points[stroke.points.length - (stroke.points.length >= 2 ? 2 : 1)];
222
+ const prevPoint =
223
+ // Non null asserting, this must exist its within the length of stroke.points
224
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
225
+ stroke.points[stroke.points.length - (stroke.points.length >= 2 ? 2 : 1)];
211
226
  this.drawStrokeSegment(stroke.pen, prevPoint, operation.point);
212
227
  }
213
228
  }
@@ -1 +1 @@
1
- {"version":3,"file":"inkCanvas.js","sourceRoot":"","sources":["../src/inkCanvas.ts"],"names":[],"mappings":"AAAA;;;GAGG;AASH,MAAM,MAAM;IACX;;OAEG;IACI,MAAM,CAAC,MAAM,CAAC,MAAc,EAAE,KAAa;QACjD,OAAO,IAAI,MAAM,CAChB,MAAM,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,EACvD,MAAM,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CACvD,CAAC;IACH,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,SAAS,CAAC,MAAc;QACrC,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC;QAC/B,OAAO,IAAI,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,MAAM,EAAE,MAAM,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC;IACzD,CAAC;IAED,YACQ,CAAS,EACT,CAAS;QADT,MAAC,GAAD,CAAC,CAAQ;QACT,MAAC,GAAD,CAAC,CAAQ;IACd,CAAC;IAEG,MAAM;QACZ,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;IACrD,CAAC;CACD;AAED,SAAS,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AAC,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,CAAC,CAAC;IAClD,CAAC;IAEO,YAAY,CAAC,SAA2B;QAC/C,0BAA0B;QAC1B,MAAM,aAAa,GAAG,SAAS,CAAC,EAAE,CAAC;QACnC,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC;QACnD,kGAAkG;QAClG,MAAM,SAAS,GACd,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QAC3E,IAAI,CAAC,iBAAiB,CAAC,MAAM,CAAC,GAAG,EAAE,SAAS,EAAE,SAAS,CAAC,KAAK,CAAC,CAAC;IAChE,CAAC;CACD","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { IColor, IInk, IInkPoint, IInkStroke, IPen, IStylusOperation } from \"./interfaces.js\";\n\ninterface IPoint {\n\tx: number;\n\ty: number;\n}\n\nclass Vector {\n\t/**\n\t * Returns the vector resulting from rotating vector by angle\n\t */\n\tpublic static rotate(vector: Vector, angle: number): Vector {\n\t\treturn new Vector(\n\t\t\tvector.x * Math.cos(angle) - vector.y * Math.sin(angle),\n\t\t\tvector.x * Math.sin(angle) + vector.y * Math.cos(angle),\n\t\t);\n\t}\n\n\t/**\n\t * Returns the normalized form of the given vector\n\t */\n\tpublic static normalize(vector: Vector): Vector {\n\t\tconst length = vector.length();\n\t\treturn new Vector(vector.x / length, vector.y / length);\n\t}\n\n\tconstructor(\n\t\tpublic x: number,\n\t\tpublic y: number,\n\t) {}\n\n\tpublic length(): number {\n\t\treturn Math.sqrt(this.x * this.x + this.y * this.y);\n\t}\n}\n\nfunction drawPolygon(context: CanvasRenderingContext2D, points: IPoint[]) {\n\tif (points.length === 0) {\n\t\treturn;\n\t}\n\n\tcontext.beginPath();\n\t// Move to the first point\n\tcontext.moveTo(points[0].x, points[0].y);\n\n\t// Draw the rest of the segments\n\tfor (let i = 1; i < points.length; i++) {\n\t\tcontext.lineTo(points[i].x, points[i].y);\n\t}\n\n\t// And then close the shape\n\tcontext.lineTo(points[0].x, points[0].y);\n\tcontext.closePath();\n\tcontext.fill();\n}\n\nfunction drawCircle(context: CanvasRenderingContext2D, center: IPoint, radius: number) {\n\tcontext.beginPath();\n\tcontext.moveTo(center.x, center.y);\n\tcontext.arc(center.x, center.y, radius, 0, Math.PI * 2);\n\tcontext.closePath();\n\tcontext.fill();\n}\n\nfunction drawShapes(\n\tcontext: CanvasRenderingContext2D,\n\tstartPoint: IInkPoint,\n\tendPoint: IInkPoint,\n\tpen: IPen,\n): void {\n\tconst dirVector = new Vector(endPoint.x - startPoint.x, endPoint.y - startPoint.y);\n\tconst len = dirVector.length();\n\n\tconst widthAtStart = pen.thickness * startPoint.pressure;\n\tconst widthAtEnd = pen.thickness * endPoint.pressure;\n\n\tif (len + Math.min(widthAtStart, widthAtEnd) > Math.max(widthAtStart, widthAtEnd)) {\n\t\t// Circles don't completely overlap, need a trapezoid\n\t\tconst normalizedLateralVector = new Vector(-dirVector.y / len, dirVector.x / len);\n\n\t\tconst trapezoidP0 = {\n\t\t\tx: startPoint.x + widthAtStart * normalizedLateralVector.x,\n\t\t\ty: startPoint.y + widthAtStart * normalizedLateralVector.y,\n\t\t};\n\t\tconst trapezoidP1 = {\n\t\t\tx: startPoint.x - widthAtStart * normalizedLateralVector.x,\n\t\t\ty: startPoint.y - widthAtStart * normalizedLateralVector.y,\n\t\t};\n\t\tconst trapezoidP2 = {\n\t\t\tx: endPoint.x - widthAtEnd * normalizedLateralVector.x,\n\t\t\ty: endPoint.y - widthAtEnd * normalizedLateralVector.y,\n\t\t};\n\t\tconst trapezoidP3 = {\n\t\t\tx: endPoint.x + widthAtEnd * normalizedLateralVector.x,\n\t\t\ty: endPoint.y + widthAtEnd * normalizedLateralVector.y,\n\t\t};\n\n\t\tdrawPolygon(context, [trapezoidP0, trapezoidP1, trapezoidP2, trapezoidP3]);\n\t}\n\n\t// End circle\n\t// TODO should only draw if not eclipsed by the previous circle, be careful about single-point\n\tdrawCircle(context, { x: endPoint.x, y: endPoint.y }, widthAtEnd);\n}\n\n/**\n * @internal\n */\nexport class InkCanvas {\n\tprivate readonly context: CanvasRenderingContext2D;\n\tprivate readonly localActiveStrokeMap: Map<number, string> = new Map();\n\tprivate readonly currentPen: IPen;\n\n\tconstructor(\n\t\tprivate readonly canvas: HTMLCanvasElement,\n\t\tprivate readonly model: IInk,\n\t) {\n\t\tthis.model.on(\"clear\", this.redraw.bind(this));\n\t\tthis.model.on(\"stylus\", this.handleStylus.bind(this));\n\t\tthis.canvas.style.touchAction = \"none\";\n\n\t\tthis.canvas.addEventListener(\"pointerdown\", this.handlePointerDown.bind(this));\n\t\tthis.canvas.addEventListener(\"pointermove\", this.handlePointerMove.bind(this));\n\t\tthis.canvas.addEventListener(\"pointerup\", this.handlePointerUp.bind(this));\n\n\t\tconst context = this.canvas.getContext(\"2d\");\n\t\tif (context === null) {\n\t\t\tthrow new Error(\"InkCanvas requires a canvas with 2d rendering context\");\n\t\t}\n\t\tthis.context = context;\n\n\t\tthis.currentPen = {\n\t\t\tcolor: { r: 0, g: 161, b: 241, a: 0 },\n\t\t\tthickness: 7,\n\t\t};\n\n\t\tthis.sizeCanvasBackingStore();\n\t}\n\n\tpublic setPenColor(color: IColor) {\n\t\tthis.currentPen.color = color;\n\t}\n\n\tpublic replay() {\n\t\tthis.clearCanvas();\n\n\t\tconst strokes = this.model.getStrokes();\n\n\t\t// Time of the first operation in stroke 0 is our starting time\n\t\tconst startTime = strokes[0].points[0].time;\n\t\tfor (const stroke of strokes) {\n\t\t\tthis.animateStroke(stroke, 0, startTime);\n\t\t}\n\t}\n\n\tpublic clear() {\n\t\tthis.model.clear();\n\t\tthis.redraw();\n\t}\n\n\tpublic sizeCanvasBackingStore() {\n\t\tconst canvasBoundingClientRect = this.canvas.getBoundingClientRect();\n\t\t// Scale the canvas size to match the physical pixel to avoid blurriness\n\t\tconst scale = window.devicePixelRatio;\n\t\tthis.canvas.width = Math.floor(canvasBoundingClientRect.width * scale);\n\t\tthis.canvas.height = Math.floor(canvasBoundingClientRect.height * scale);\n\t\t// Scale the context to bring back coordinate system in CSS pixels\n\t\tthis.context.setTransform(1, 0, 0, 1, 0, 0);\n\t\tthis.context.scale(scale, scale);\n\n\t\tthis.redraw();\n\t}\n\n\tprivate handlePointerDown(evt: PointerEvent) {\n\t\t// We will accept pen down or mouse left down as the start of a stroke.\n\t\tif (evt.pointerType === \"pen\" || (evt.pointerType === \"mouse\" && evt.button === 0)) {\n\t\t\tconst strokeId = this.model.createStroke(this.currentPen).id;\n\t\t\tthis.localActiveStrokeMap.set(evt.pointerId, strokeId);\n\n\t\t\tthis.appendPointerEventToStroke(evt);\n\n\t\t\tevt.preventDefault();\n\t\t}\n\t}\n\n\tprivate handlePointerMove(evt: PointerEvent) {\n\t\tif (this.localActiveStrokeMap.has(evt.pointerId)) {\n\t\t\tconst evts = (evt as any)?.getCoalescedEvents() ?? ([evt] as PointerEvent[]);\n\t\t\tfor (const e of evts) {\n\t\t\t\tthis.appendPointerEventToStroke(e);\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate handlePointerUp(evt: PointerEvent) {\n\t\tif (this.localActiveStrokeMap.has(evt.pointerId)) {\n\t\t\tthis.appendPointerEventToStroke(evt);\n\t\t\tthis.localActiveStrokeMap.delete(evt.pointerId);\n\t\t}\n\t}\n\n\tprivate appendPointerEventToStroke(evt: PointerEvent) {\n\t\tconst strokeId = this.localActiveStrokeMap.get(evt.pointerId);\n\t\tif (strokeId === undefined) {\n\t\t\tthrow new Error(\"Unexpected pointer ID trying to append to stroke\");\n\t\t}\n\t\tconst inkPt = {\n\t\t\tx: evt.offsetX,\n\t\t\ty: evt.offsetY,\n\t\t\ttime: Date.now(),\n\t\t\tpressure: evt.pressure,\n\t\t};\n\t\tthis.model.appendPointToStroke(inkPt, strokeId);\n\t}\n\n\tprivate animateStroke(stroke: IInkStroke, operationIndex: number, startTime: number) {\n\t\tif (operationIndex >= stroke.points.length) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Draw the requested stroke\n\t\tconst current = stroke.points[operationIndex];\n\t\tconst previous = stroke.points[Math.max(0, operationIndex - 1)];\n\t\tconst time =\n\t\t\toperationIndex === 0 ? current.time - startTime : current.time - previous.time;\n\n\t\tsetTimeout(() => {\n\t\t\tthis.drawStrokeSegment(stroke.pen, current, previous);\n\t\t\tthis.animateStroke(stroke, operationIndex + 1, startTime);\n\t\t}, time);\n\t}\n\n\t/**\n\t * Clears the canvas\n\t */\n\tprivate clearCanvas() {\n\t\tthis.context.clearRect(0, 0, this.canvas.width, this.canvas.height);\n\t}\n\n\tprivate redraw() {\n\t\tthis.clearCanvas();\n\n\t\tconst strokes = this.model.getStrokes();\n\t\tfor (const stroke of strokes) {\n\t\t\tlet previous = stroke.points[0];\n\t\t\tfor (const current of stroke.points) {\n\t\t\t\t// For the down, current === previous === stroke.operations[0]\n\t\t\t\tthis.drawStrokeSegment(stroke.pen, current, previous);\n\t\t\t\tprevious = current;\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate drawStrokeSegment(pen: IPen, current: IInkPoint, previous: IInkPoint) {\n\t\t// TODO Consider save/restore context\n\t\t// TODO Consider half-pixel offset\n\t\tthis.context.fillStyle = `rgb(${pen.color.r}, ${pen.color.g}, ${pen.color.b})`;\n\t\tdrawShapes(this.context, previous, current, pen);\n\t}\n\n\tprivate handleStylus(operation: IStylusOperation) {\n\t\t// Render the dirty stroke\n\t\tconst dirtyStrokeId = operation.id;\n\t\tconst stroke = this.model.getStroke(dirtyStrokeId);\n\t\t// If this is the only point in the stroke, we'll use it for both the start and end of the segment\n\t\tconst prevPoint =\n\t\t\tstroke.points[stroke.points.length - (stroke.points.length >= 2 ? 2 : 1)];\n\t\tthis.drawStrokeSegment(stroke.pen, prevPoint, operation.point);\n\t}\n}\n"]}
1
+ {"version":3,"file":"inkCanvas.js","sourceRoot":"","sources":["../src/inkCanvas.ts"],"names":[],"mappings":"AAAA;;;GAGG;AASH,MAAM,MAAM;IACX;;OAEG;IACI,MAAM,CAAC,MAAM,CAAC,MAAc,EAAE,KAAa;QACjD,OAAO,IAAI,MAAM,CAChB,MAAM,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,EACvD,MAAM,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CACvD,CAAC;IACH,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,SAAS,CAAC,MAAc;QACrC,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC;QAC/B,OAAO,IAAI,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,MAAM,EAAE,MAAM,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC;IACzD,CAAC;IAED,YACQ,CAAS,EACT,CAAS;QADT,MAAC,GAAD,CAAC,CAAQ;QACT,MAAC,GAAD,CAAC,CAAQ;IACd,CAAC;IAEG,MAAM;QACZ,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;IACrD,CAAC;CACD;AAED,SAAS,WAAW,CAAC,OAAiC,EAAE,MAAgB;IACvE,MAAM,UAAU,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;IAC7B,IAAI,UAAU,KAAK,SAAS,EAAE,CAAC;QAC9B,OAAO;IACR,CAAC;IAED,OAAO,CAAC,SAAS,EAAE,CAAC;IACp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AAI,CAAC,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,CAAC,CAAC;IAClD,CAAC;IAEO,YAAY,CAAC,SAA2B;QAC/C,0BAA0B;QAC1B,MAAM,aAAa,GAAG,SAAS,CAAC,EAAE,CAAC;QACnC,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC;QACnD,kGAAkG;QAClG,MAAM,SAAS;QACd,6EAA6E;QAC7E,oEAAoE;QACpE,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAE,CAAC;QAC5E,IAAI,CAAC,iBAAiB,CAAC,MAAM,CAAC,GAAG,EAAE,SAAS,EAAE,SAAS,CAAC,KAAK,CAAC,CAAC;IAChE,CAAC;CACD","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { IColor, IInk, IInkPoint, IInkStroke, IPen, IStylusOperation } from \"./interfaces.js\";\n\ninterface IPoint {\n\tx: number;\n\ty: number;\n}\n\nclass Vector {\n\t/**\n\t * Returns the vector resulting from rotating vector by angle\n\t */\n\tpublic static rotate(vector: Vector, angle: number): Vector {\n\t\treturn new Vector(\n\t\t\tvector.x * Math.cos(angle) - vector.y * Math.sin(angle),\n\t\t\tvector.x * Math.sin(angle) + vector.y * Math.cos(angle),\n\t\t);\n\t}\n\n\t/**\n\t * Returns the normalized form of the given vector\n\t */\n\tpublic static normalize(vector: Vector): Vector {\n\t\tconst length = vector.length();\n\t\treturn new Vector(vector.x / length, vector.y / length);\n\t}\n\n\tconstructor(\n\t\tpublic x: number,\n\t\tpublic y: number,\n\t) {}\n\n\tpublic length(): number {\n\t\treturn Math.sqrt(this.x * this.x + this.y * this.y);\n\t}\n}\n\nfunction drawPolygon(context: CanvasRenderingContext2D, points: IPoint[]) {\n\tconst firstPoint = points[0];\n\tif (firstPoint === undefined) {\n\t\treturn;\n\t}\n\n\tcontext.beginPath();\n\t// Move to the first point\n\tcontext.moveTo(firstPoint.x, firstPoint.y);\n\n\t// Draw the rest of the segments\n\tfor (let i = 1; i < points.length; i++) {\n\t\t// Non null asserting, this must exist because the we are iterating through the length of points\n\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\tcontext.lineTo(points[i]!.x, points[i]!.y);\n\t}\n\n\t// And then close the shape\n\tcontext.lineTo(firstPoint.x, firstPoint.y);\n\tcontext.closePath();\n\tcontext.fill();\n}\n\nfunction drawCircle(context: CanvasRenderingContext2D, center: IPoint, radius: number) {\n\tcontext.beginPath();\n\tcontext.moveTo(center.x, center.y);\n\tcontext.arc(center.x, center.y, radius, 0, Math.PI * 2);\n\tcontext.closePath();\n\tcontext.fill();\n}\n\nfunction drawShapes(\n\tcontext: CanvasRenderingContext2D,\n\tstartPoint: IInkPoint,\n\tendPoint: IInkPoint,\n\tpen: IPen,\n): void {\n\tconst dirVector = new Vector(endPoint.x - startPoint.x, endPoint.y - startPoint.y);\n\tconst len = dirVector.length();\n\n\tconst widthAtStart = pen.thickness * startPoint.pressure;\n\tconst widthAtEnd = pen.thickness * endPoint.pressure;\n\n\tif (len + Math.min(widthAtStart, widthAtEnd) > Math.max(widthAtStart, widthAtEnd)) {\n\t\t// Circles don't completely overlap, need a trapezoid\n\t\tconst normalizedLateralVector = new Vector(-dirVector.y / len, dirVector.x / len);\n\n\t\tconst trapezoidP0 = {\n\t\t\tx: startPoint.x + widthAtStart * normalizedLateralVector.x,\n\t\t\ty: startPoint.y + widthAtStart * normalizedLateralVector.y,\n\t\t};\n\t\tconst trapezoidP1 = {\n\t\t\tx: startPoint.x - widthAtStart * normalizedLateralVector.x,\n\t\t\ty: startPoint.y - widthAtStart * normalizedLateralVector.y,\n\t\t};\n\t\tconst trapezoidP2 = {\n\t\t\tx: endPoint.x - widthAtEnd * normalizedLateralVector.x,\n\t\t\ty: endPoint.y - widthAtEnd * normalizedLateralVector.y,\n\t\t};\n\t\tconst trapezoidP3 = {\n\t\t\tx: endPoint.x + widthAtEnd * normalizedLateralVector.x,\n\t\t\ty: endPoint.y + widthAtEnd * normalizedLateralVector.y,\n\t\t};\n\n\t\tdrawPolygon(context, [trapezoidP0, trapezoidP1, trapezoidP2, trapezoidP3]);\n\t}\n\n\t// End circle\n\t// TODO should only draw if not eclipsed by the previous circle, be careful about single-point\n\tdrawCircle(context, { x: endPoint.x, y: endPoint.y }, widthAtEnd);\n}\n\n/**\n * @internal\n */\nexport class InkCanvas {\n\tprivate readonly context: CanvasRenderingContext2D;\n\tprivate readonly localActiveStrokeMap: Map<number, string> = new Map();\n\tprivate readonly currentPen: IPen;\n\n\tconstructor(\n\t\tprivate readonly canvas: HTMLCanvasElement,\n\t\tprivate readonly model: IInk,\n\t) {\n\t\tthis.model.on(\"clear\", this.redraw.bind(this));\n\t\tthis.model.on(\"stylus\", this.handleStylus.bind(this));\n\t\tthis.canvas.style.touchAction = \"none\";\n\n\t\tthis.canvas.addEventListener(\"pointerdown\", this.handlePointerDown.bind(this));\n\t\tthis.canvas.addEventListener(\"pointermove\", this.handlePointerMove.bind(this));\n\t\tthis.canvas.addEventListener(\"pointerup\", this.handlePointerUp.bind(this));\n\n\t\tconst context = this.canvas.getContext(\"2d\");\n\t\tif (context === null) {\n\t\t\tthrow new Error(\"InkCanvas requires a canvas with 2d rendering context\");\n\t\t}\n\t\tthis.context = context;\n\n\t\tthis.currentPen = {\n\t\t\tcolor: { r: 0, g: 161, b: 241, a: 0 },\n\t\t\tthickness: 7,\n\t\t};\n\n\t\tthis.sizeCanvasBackingStore();\n\t}\n\n\tpublic setPenColor(color: IColor) {\n\t\tthis.currentPen.color = color;\n\t}\n\n\tpublic replay() {\n\t\tthis.clearCanvas();\n\n\t\tconst strokes = this.model.getStrokes();\n\n\t\t// Time of the first operation in stroke 0 is our starting time\n\t\tconst startTime = strokes[0]?.points[0]?.time;\n\t\tif (startTime === undefined) {\n\t\t\tthrow new Error(\"Couldn't get start time\");\n\t\t}\n\t\tfor (const stroke of strokes) {\n\t\t\tthis.animateStroke(stroke, 0, startTime);\n\t\t}\n\t}\n\n\tpublic clear() {\n\t\tthis.model.clear();\n\t\tthis.redraw();\n\t}\n\n\tpublic sizeCanvasBackingStore() {\n\t\tconst canvasBoundingClientRect = this.canvas.getBoundingClientRect();\n\t\t// Scale the canvas size to match the physical pixel to avoid blurriness\n\t\tconst scale = window.devicePixelRatio;\n\t\tthis.canvas.width = Math.floor(canvasBoundingClientRect.width * scale);\n\t\tthis.canvas.height = Math.floor(canvasBoundingClientRect.height * scale);\n\t\t// Scale the context to bring back coordinate system in CSS pixels\n\t\tthis.context.setTransform(1, 0, 0, 1, 0, 0);\n\t\tthis.context.scale(scale, scale);\n\n\t\tthis.redraw();\n\t}\n\n\tprivate handlePointerDown(evt: PointerEvent) {\n\t\t// We will accept pen down or mouse left down as the start of a stroke.\n\t\tif (evt.pointerType === \"pen\" || (evt.pointerType === \"mouse\" && evt.button === 0)) {\n\t\t\tconst strokeId = this.model.createStroke(this.currentPen).id;\n\t\t\tthis.localActiveStrokeMap.set(evt.pointerId, strokeId);\n\n\t\t\tthis.appendPointerEventToStroke(evt);\n\n\t\t\tevt.preventDefault();\n\t\t}\n\t}\n\n\tprivate handlePointerMove(evt: PointerEvent) {\n\t\tif (this.localActiveStrokeMap.has(evt.pointerId)) {\n\t\t\tconst evts = (evt as any)?.getCoalescedEvents() ?? ([evt] as PointerEvent[]);\n\t\t\tfor (const e of evts) {\n\t\t\t\tthis.appendPointerEventToStroke(e);\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate handlePointerUp(evt: PointerEvent) {\n\t\tif (this.localActiveStrokeMap.has(evt.pointerId)) {\n\t\t\tthis.appendPointerEventToStroke(evt);\n\t\t\tthis.localActiveStrokeMap.delete(evt.pointerId);\n\t\t}\n\t}\n\n\tprivate appendPointerEventToStroke(evt: PointerEvent) {\n\t\tconst strokeId = this.localActiveStrokeMap.get(evt.pointerId);\n\t\tif (strokeId === undefined) {\n\t\t\tthrow new Error(\"Unexpected pointer ID trying to append to stroke\");\n\t\t}\n\t\tconst inkPt = {\n\t\t\tx: evt.offsetX,\n\t\t\ty: evt.offsetY,\n\t\t\ttime: Date.now(),\n\t\t\tpressure: evt.pressure,\n\t\t};\n\t\tthis.model.appendPointToStroke(inkPt, strokeId);\n\t}\n\n\tprivate animateStroke(stroke: IInkStroke, operationIndex: number, startTime: number) {\n\t\tif (operationIndex >= stroke.points.length) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Draw the requested stroke\n\t\t// Non null asserting, this must exist because the operationIndex must be less than the length of stroke.points\n\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\tconst current = stroke.points[operationIndex]!;\n\t\t// Non null asserting, this must exist because its either the first index or operationIndex minus one which is less than the length of stroke.points\n\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\tconst previous = stroke.points[Math.max(0, operationIndex - 1)]!;\n\t\tconst time =\n\t\t\toperationIndex === 0 ? current.time - startTime : current.time - previous.time;\n\n\t\tsetTimeout(() => {\n\t\t\tthis.drawStrokeSegment(stroke.pen, current, previous);\n\t\t\tthis.animateStroke(stroke, operationIndex + 1, startTime);\n\t\t}, time);\n\t}\n\n\t/**\n\t * Clears the canvas\n\t */\n\tprivate clearCanvas() {\n\t\tthis.context.clearRect(0, 0, this.canvas.width, this.canvas.height);\n\t}\n\n\tprivate redraw() {\n\t\tthis.clearCanvas();\n\n\t\tconst strokes = this.model.getStrokes();\n\t\tfor (const stroke of strokes) {\n\t\t\t// Non null asserting since previous would not be used if stroke.points is empty\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\t\tlet previous = stroke.points[0]!;\n\t\t\tfor (const current of stroke.points) {\n\t\t\t\t// For the down, current === previous === stroke.operations[0]\n\t\t\t\tthis.drawStrokeSegment(stroke.pen, current, previous);\n\t\t\t\tprevious = current;\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate drawStrokeSegment(pen: IPen, current: IInkPoint, previous: IInkPoint) {\n\t\t// TODO Consider save/restore context\n\t\t// TODO Consider half-pixel offset\n\t\tthis.context.fillStyle = `rgb(${pen.color.r}, ${pen.color.g}, ${pen.color.b})`;\n\t\tdrawShapes(this.context, previous, current, pen);\n\t}\n\n\tprivate handleStylus(operation: IStylusOperation) {\n\t\t// Render the dirty stroke\n\t\tconst dirtyStrokeId = operation.id;\n\t\tconst stroke = this.model.getStroke(dirtyStrokeId);\n\t\t// If this is the only point in the stroke, we'll use it for both the start and end of the segment\n\t\tconst prevPoint =\n\t\t\t// Non null asserting, this must exist its within the length of stroke.points\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\t\tstroke.points[stroke.points.length - (stroke.points.length >= 2 ? 2 : 1)]!;\n\t\tthis.drawStrokeSegment(stroke.pen, prevPoint, operation.point);\n\t}\n}\n"]}
@@ -5,5 +5,5 @@
5
5
  * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY
6
6
  */
7
7
  export declare const pkgName = "@fluid-experimental/ink";
8
- export declare const pkgVersion = "2.1.0-276985";
8
+ export declare const pkgVersion = "2.1.0-281041";
9
9
  //# sourceMappingURL=packageVersion.d.ts.map
@@ -5,5 +5,5 @@
5
5
  * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY
6
6
  */
7
7
  export const pkgName = "@fluid-experimental/ink";
8
- export const pkgVersion = "2.1.0-276985";
8
+ export const pkgVersion = "2.1.0-281041";
9
9
  //# sourceMappingURL=packageVersion.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"packageVersion.js","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,CAAC,MAAM,OAAO,GAAG,yBAAyB,CAAC;AACjD,MAAM,CAAC,MAAM,UAAU,GAAG,cAAc,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n *\n * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY\n */\n\nexport const pkgName = \"@fluid-experimental/ink\";\nexport const pkgVersion = \"2.1.0-276985\";\n"]}
1
+ {"version":3,"file":"packageVersion.js","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,CAAC,MAAM,OAAO,GAAG,yBAAyB,CAAC;AACjD,MAAM,CAAC,MAAM,UAAU,GAAG,cAAc,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n *\n * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY\n */\n\nexport const pkgName = \"@fluid-experimental/ink\";\nexport const pkgVersion = \"2.1.0-281041\";\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"snapshot.d.ts","sourceRoot":"","sources":["../src/snapshot.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAChC;;OAEG;IACH,OAAO,EAAE,UAAU,EAAE,CAAC;IAEtB;;;OAGG;IACH,WAAW,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC;CACvC;AAED;;GAEG;AACH,qBAAa,OAAO;IACnB;;OAEG;IACH,OAAO,CAAC,OAAO,CAAe;IAE9B;;OAEG;IACH,OAAO,CAAC,WAAW,CAA4B;IAE/C;;;OAGG;gBACS,QAAQ,CAAC,EAAE,gBAAgB;IAKvC;;OAEG;IACI,UAAU,IAAI,UAAU,EAAE;IAIjC;;OAEG;IACI,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU;IAIzC;;OAEG;IACI,KAAK,IAAI,IAAI;IAKpB;;;OAGG;IACI,SAAS,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI;IAK1C;;;OAGG;IACI,eAAe,IAAI,gBAAgB;CAM1C"}
1
+ {"version":3,"file":"snapshot.d.ts","sourceRoot":"","sources":["../src/snapshot.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAChC;;OAEG;IACH,OAAO,EAAE,UAAU,EAAE,CAAC;IAEtB;;;OAGG;IACH,WAAW,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC;CACvC;AAED;;GAEG;AACH,qBAAa,OAAO;IACnB;;OAEG;IACH,OAAO,CAAC,OAAO,CAAe;IAE9B;;OAEG;IACH,OAAO,CAAC,WAAW,CAA4B;IAE/C;;;OAGG;gBACS,QAAQ,CAAC,EAAE,gBAAgB;IAKvC;;OAEG;IACI,UAAU,IAAI,UAAU,EAAE;IAIjC;;OAEG;IACI,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU;IAMzC;;OAEG;IACI,KAAK,IAAI,IAAI;IAKpB;;;OAGG;IACI,SAAS,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI;IAK1C;;;OAGG;IACI,eAAe,IAAI,gBAAgB;CAM1C"}
package/lib/snapshot.js CHANGED
@@ -24,6 +24,8 @@ export class InkData {
24
24
  * {@inheritDoc IInk.getStroke}
25
25
  */
26
26
  getStroke(key) {
27
+ // Non null asserting, it should be okay that a bad key results in an error being thrown
28
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
27
29
  return this.strokes[this.strokeIndex[key]];
28
30
  }
29
31
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"snapshot.js","sourceRoot":"","sources":["../src/snapshot.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAoBH;;GAEG;AACH,MAAM,OAAO,OAAO;IAWnB;;;OAGG;IACH,YAAY,QAA2B;QACtC,IAAI,CAAC,OAAO,GAAG,QAAQ,EAAE,OAAO,IAAI,EAAE,CAAC;QACvC,IAAI,CAAC,WAAW,GAAG,QAAQ,EAAE,WAAW,IAAI,EAAE,CAAC;IAChD,CAAC;IAED;;OAEG;IACI,UAAU;QAChB,OAAO,IAAI,CAAC,OAAO,CAAC;IACrB,CAAC;IAED;;OAEG;IACI,SAAS,CAAC,GAAW;QAC3B,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC;IAC5C,CAAC;IAED;;OAEG;IACI,KAAK;QACX,IAAI,CAAC,OAAO,GAAG,EAAE,CAAC;QAClB,IAAI,CAAC,WAAW,GAAG,EAAE,CAAC;IACvB,CAAC;IAED;;;OAGG;IACI,SAAS,CAAC,MAAkB;QAClC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAC1B,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC;IACvD,CAAC;IAED;;;OAGG;IACI,eAAe;QACrB,OAAO;YACN,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,WAAW,EAAE,IAAI,CAAC,WAAW;SAC7B,CAAC;IACH,CAAC;CACD","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { IInkStroke } from \"./interfaces.js\";\n\n/**\n * Ink snapshot interface.\n */\nexport interface ISerializableInk {\n\t/**\n\t * Collection of ink strokes.\n\t */\n\tstrokes: IInkStroke[];\n\n\t/**\n\t * Stores a mapping from the provided key to its index in strokes. Since\n\t * ISerializableInk is serialized we need to use an index.\n\t */\n\tstrokeIndex: { [key: string]: number };\n}\n\n/**\n * Maintains a live record of the data that can be used for snapshotting.\n */\nexport class InkData {\n\t/**\n\t * {@inheritDoc ISerializableInk.strokes}\n\t */\n\tprivate strokes: IInkStroke[];\n\n\t/**\n\t * {@inheritDoc ISerializableInk.strokeIndex}\n\t */\n\tprivate strokeIndex: { [key: string]: number };\n\n\t/**\n\t * Construct a new InkData.\n\t * @param snapshot - Existing data to initialize with\n\t */\n\tconstructor(snapshot?: ISerializableInk) {\n\t\tthis.strokes = snapshot?.strokes ?? [];\n\t\tthis.strokeIndex = snapshot?.strokeIndex ?? {};\n\t}\n\n\t/**\n\t * {@inheritDoc IInk.getStrokes}\n\t */\n\tpublic getStrokes(): IInkStroke[] {\n\t\treturn this.strokes;\n\t}\n\n\t/**\n\t * {@inheritDoc IInk.getStroke}\n\t */\n\tpublic getStroke(key: string): IInkStroke {\n\t\treturn this.strokes[this.strokeIndex[key]];\n\t}\n\n\t/**\n\t * Clear all stored data.\n\t */\n\tpublic clear(): void {\n\t\tthis.strokes = [];\n\t\tthis.strokeIndex = {};\n\t}\n\n\t/**\n\t * Add the given stroke to the stored data.\n\t * @param stroke - The stroke to add\n\t */\n\tpublic addStroke(stroke: IInkStroke): void {\n\t\tthis.strokes.push(stroke);\n\t\tthis.strokeIndex[stroke.id] = this.strokes.length - 1;\n\t}\n\n\t/**\n\t * Get a JSON-compatible representation of the stored data.\n\t * @returns The JSON-compatible object\n\t */\n\tpublic getSerializable(): ISerializableInk {\n\t\treturn {\n\t\t\tstrokes: this.strokes,\n\t\t\tstrokeIndex: this.strokeIndex,\n\t\t};\n\t}\n}\n"]}
1
+ {"version":3,"file":"snapshot.js","sourceRoot":"","sources":["../src/snapshot.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAoBH;;GAEG;AACH,MAAM,OAAO,OAAO;IAWnB;;;OAGG;IACH,YAAY,QAA2B;QACtC,IAAI,CAAC,OAAO,GAAG,QAAQ,EAAE,OAAO,IAAI,EAAE,CAAC;QACvC,IAAI,CAAC,WAAW,GAAG,QAAQ,EAAE,WAAW,IAAI,EAAE,CAAC;IAChD,CAAC;IAED;;OAEG;IACI,UAAU;QAChB,OAAO,IAAI,CAAC,OAAO,CAAC;IACrB,CAAC;IAED;;OAEG;IACI,SAAS,CAAC,GAAW;QAC3B,wFAAwF;QACxF,oEAAoE;QACpE,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAE,CAAE,CAAC;IAC9C,CAAC;IAED;;OAEG;IACI,KAAK;QACX,IAAI,CAAC,OAAO,GAAG,EAAE,CAAC;QAClB,IAAI,CAAC,WAAW,GAAG,EAAE,CAAC;IACvB,CAAC;IAED;;;OAGG;IACI,SAAS,CAAC,MAAkB;QAClC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAC1B,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC;IACvD,CAAC;IAED;;;OAGG;IACI,eAAe;QACrB,OAAO;YACN,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,WAAW,EAAE,IAAI,CAAC,WAAW;SAC7B,CAAC;IACH,CAAC;CACD","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { IInkStroke } from \"./interfaces.js\";\n\n/**\n * Ink snapshot interface.\n */\nexport interface ISerializableInk {\n\t/**\n\t * Collection of ink strokes.\n\t */\n\tstrokes: IInkStroke[];\n\n\t/**\n\t * Stores a mapping from the provided key to its index in strokes. Since\n\t * ISerializableInk is serialized we need to use an index.\n\t */\n\tstrokeIndex: { [key: string]: number };\n}\n\n/**\n * Maintains a live record of the data that can be used for snapshotting.\n */\nexport class InkData {\n\t/**\n\t * {@inheritDoc ISerializableInk.strokes}\n\t */\n\tprivate strokes: IInkStroke[];\n\n\t/**\n\t * {@inheritDoc ISerializableInk.strokeIndex}\n\t */\n\tprivate strokeIndex: { [key: string]: number };\n\n\t/**\n\t * Construct a new InkData.\n\t * @param snapshot - Existing data to initialize with\n\t */\n\tconstructor(snapshot?: ISerializableInk) {\n\t\tthis.strokes = snapshot?.strokes ?? [];\n\t\tthis.strokeIndex = snapshot?.strokeIndex ?? {};\n\t}\n\n\t/**\n\t * {@inheritDoc IInk.getStrokes}\n\t */\n\tpublic getStrokes(): IInkStroke[] {\n\t\treturn this.strokes;\n\t}\n\n\t/**\n\t * {@inheritDoc IInk.getStroke}\n\t */\n\tpublic getStroke(key: string): IInkStroke {\n\t\t// Non null asserting, it should be okay that a bad key results in an error being thrown\n\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\treturn this.strokes[this.strokeIndex[key]!]!;\n\t}\n\n\t/**\n\t * Clear all stored data.\n\t */\n\tpublic clear(): void {\n\t\tthis.strokes = [];\n\t\tthis.strokeIndex = {};\n\t}\n\n\t/**\n\t * Add the given stroke to the stored data.\n\t * @param stroke - The stroke to add\n\t */\n\tpublic addStroke(stroke: IInkStroke): void {\n\t\tthis.strokes.push(stroke);\n\t\tthis.strokeIndex[stroke.id] = this.strokes.length - 1;\n\t}\n\n\t/**\n\t * Get a JSON-compatible representation of the stored data.\n\t * @returns The JSON-compatible object\n\t */\n\tpublic getSerializable(): ISerializableInk {\n\t\treturn {\n\t\t\tstrokes: this.strokes,\n\t\t\tstrokeIndex: this.strokeIndex,\n\t\t};\n\t}\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fluid-experimental/ink",
3
- "version": "2.1.0-276985",
3
+ "version": "2.1.0-281041",
4
4
  "description": "Ink DDS",
5
5
  "homepage": "https://fluidframework.com",
6
6
  "repository": {
@@ -47,24 +47,24 @@
47
47
  "temp-directory": "nyc/.nyc_output"
48
48
  },
49
49
  "dependencies": {
50
- "@fluidframework/core-interfaces": "2.1.0-276985",
51
- "@fluidframework/datastore-definitions": "2.1.0-276985",
52
- "@fluidframework/driver-definitions": "2.1.0-276985",
53
- "@fluidframework/driver-utils": "2.1.0-276985",
54
- "@fluidframework/runtime-definitions": "2.1.0-276985",
55
- "@fluidframework/shared-object-base": "2.1.0-276985",
50
+ "@fluidframework/core-interfaces": "2.1.0-281041",
51
+ "@fluidframework/datastore-definitions": "2.1.0-281041",
52
+ "@fluidframework/driver-definitions": "2.1.0-281041",
53
+ "@fluidframework/driver-utils": "2.1.0-281041",
54
+ "@fluidframework/runtime-definitions": "2.1.0-281041",
55
+ "@fluidframework/shared-object-base": "2.1.0-281041",
56
56
  "uuid": "^9.0.0"
57
57
  },
58
58
  "devDependencies": {
59
59
  "@arethetypeswrong/cli": "^0.15.2",
60
- "@biomejs/biome": "^1.7.3",
61
- "@fluid-internal/mocha-test-setup": "2.1.0-276985",
62
- "@fluid-tools/build-cli": "^0.39.0",
60
+ "@biomejs/biome": "~1.8.3",
61
+ "@fluid-internal/mocha-test-setup": "2.1.0-281041",
62
+ "@fluid-tools/build-cli": "^0.40.0",
63
63
  "@fluidframework/build-common": "^2.0.3",
64
- "@fluidframework/build-tools": "^0.39.0",
65
- "@fluidframework/container-definitions": "2.1.0-276985",
64
+ "@fluidframework/build-tools": "^0.40.0",
65
+ "@fluidframework/container-definitions": "2.1.0-281041",
66
66
  "@fluidframework/eslint-config-fluid": "^5.3.0",
67
- "@fluidframework/test-runtime-utils": "2.1.0-276985",
67
+ "@fluidframework/test-runtime-utils": "2.1.0-281041",
68
68
  "@microsoft/api-extractor": "^7.45.1",
69
69
  "@types/mocha": "^9.1.1",
70
70
  "@types/node": "^18.19.0",
@@ -98,7 +98,7 @@
98
98
  "build:test:cjs": "fluid-tsc commonjs --project ./src/test/tsconfig.cjs.json",
99
99
  "build:test:esm": "tsc --project ./src/test/tsconfig.json",
100
100
  "check:are-the-types-wrong": "attw --pack .",
101
- "check:biome": "biome check . --formatter-enabled=true",
101
+ "check:biome": "biome check .",
102
102
  "check:exports": "concurrently \"npm:check:exports:*\"",
103
103
  "check:exports:bundle-release-tags": "api-extractor run --config api-extractor/api-extractor-lint-bundle.json",
104
104
  "check:exports:cjs:index": "api-extractor run --config api-extractor/api-extractor-lint-index.cjs.json",
@@ -110,7 +110,7 @@
110
110
  "eslint": "eslint --format stylish src",
111
111
  "eslint:fix": "eslint --format stylish src --fix --fix-type problem,suggestion,layout",
112
112
  "format": "npm run format:biome",
113
- "format:biome": "biome check . --formatter-enabled=true --apply",
113
+ "format:biome": "biome check . --write",
114
114
  "format:prettier": "prettier --write . --cache --ignore-path ../../../.prettierignore",
115
115
  "lint": "fluid-build . --task lint",
116
116
  "lint:fix": "fluid-build . --task eslint:fix --task format",
package/src/inkCanvas.ts CHANGED
@@ -40,21 +40,24 @@ class Vector {
40
40
  }
41
41
 
42
42
  function drawPolygon(context: CanvasRenderingContext2D, points: IPoint[]) {
43
- if (points.length === 0) {
43
+ const firstPoint = points[0];
44
+ if (firstPoint === undefined) {
44
45
  return;
45
46
  }
46
47
 
47
48
  context.beginPath();
48
49
  // Move to the first point
49
- context.moveTo(points[0].x, points[0].y);
50
+ context.moveTo(firstPoint.x, firstPoint.y);
50
51
 
51
52
  // Draw the rest of the segments
52
53
  for (let i = 1; i < points.length; i++) {
53
- context.lineTo(points[i].x, points[i].y);
54
+ // Non null asserting, this must exist because the we are iterating through the length of points
55
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
56
+ context.lineTo(points[i]!.x, points[i]!.y);
54
57
  }
55
58
 
56
59
  // And then close the shape
57
- context.lineTo(points[0].x, points[0].y);
60
+ context.lineTo(firstPoint.x, firstPoint.y);
58
61
  context.closePath();
59
62
  context.fill();
60
63
  }
@@ -152,7 +155,10 @@ export class InkCanvas {
152
155
  const strokes = this.model.getStrokes();
153
156
 
154
157
  // Time of the first operation in stroke 0 is our starting time
155
- const startTime = strokes[0].points[0].time;
158
+ const startTime = strokes[0]?.points[0]?.time;
159
+ if (startTime === undefined) {
160
+ throw new Error("Couldn't get start time");
161
+ }
156
162
  for (const stroke of strokes) {
157
163
  this.animateStroke(stroke, 0, startTime);
158
164
  }
@@ -224,8 +230,12 @@ export class InkCanvas {
224
230
  }
225
231
 
226
232
  // Draw the requested stroke
227
- const current = stroke.points[operationIndex];
228
- const previous = stroke.points[Math.max(0, operationIndex - 1)];
233
+ // Non null asserting, this must exist because the operationIndex must be less than the length of stroke.points
234
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
235
+ const current = stroke.points[operationIndex]!;
236
+ // Non null asserting, this must exist because its either the first index or operationIndex minus one which is less than the length of stroke.points
237
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
238
+ const previous = stroke.points[Math.max(0, operationIndex - 1)]!;
229
239
  const time =
230
240
  operationIndex === 0 ? current.time - startTime : current.time - previous.time;
231
241
 
@@ -247,7 +257,9 @@ export class InkCanvas {
247
257
 
248
258
  const strokes = this.model.getStrokes();
249
259
  for (const stroke of strokes) {
250
- let previous = stroke.points[0];
260
+ // Non null asserting since previous would not be used if stroke.points is empty
261
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
262
+ let previous = stroke.points[0]!;
251
263
  for (const current of stroke.points) {
252
264
  // For the down, current === previous === stroke.operations[0]
253
265
  this.drawStrokeSegment(stroke.pen, current, previous);
@@ -269,7 +281,9 @@ export class InkCanvas {
269
281
  const stroke = this.model.getStroke(dirtyStrokeId);
270
282
  // If this is the only point in the stroke, we'll use it for both the start and end of the segment
271
283
  const prevPoint =
272
- stroke.points[stroke.points.length - (stroke.points.length >= 2 ? 2 : 1)];
284
+ // Non null asserting, this must exist its within the length of stroke.points
285
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
286
+ stroke.points[stroke.points.length - (stroke.points.length >= 2 ? 2 : 1)]!;
273
287
  this.drawStrokeSegment(stroke.pen, prevPoint, operation.point);
274
288
  }
275
289
  }
@@ -6,4 +6,4 @@
6
6
  */
7
7
 
8
8
  export const pkgName = "@fluid-experimental/ink";
9
- export const pkgVersion = "2.1.0-276985";
9
+ export const pkgVersion = "2.1.0-281041";
package/src/snapshot.ts CHANGED
@@ -55,7 +55,9 @@ export class InkData {
55
55
  * {@inheritDoc IInk.getStroke}
56
56
  */
57
57
  public getStroke(key: string): IInkStroke {
58
- return this.strokes[this.strokeIndex[key]];
58
+ // Non null asserting, it should be okay that a bad key results in an error being thrown
59
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
60
+ return this.strokes[this.strokeIndex[key]!]!;
59
61
  }
60
62
 
61
63
  /**
package/tsconfig.json CHANGED
@@ -5,7 +5,6 @@
5
5
  "compilerOptions": {
6
6
  "rootDir": "./src",
7
7
  "outDir": "./lib",
8
- "noUncheckedIndexedAccess": false,
9
8
  "exactOptionalPropertyTypes": false,
10
9
  },
11
10
  }