@altnovatrail/canvas 0.3.24 → 0.3.25
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/lib/element/index.d.ts +13 -0
- package/lib/element/index.js +38 -0
- package/package.json +1 -1
package/lib/element/index.d.ts
CHANGED
|
@@ -211,6 +211,19 @@ export declare class NovatrailCanvas extends HTMLElement implements CanvasElemen
|
|
|
211
211
|
* worked example, whose sheet the learner does not write on (R42).
|
|
212
212
|
*/
|
|
213
213
|
exportWork(): Promise<ExportedWork | null>;
|
|
214
|
+
/**
|
|
215
|
+
* The picture of the work, for the `picture` of a stored work that app puts on the server
|
|
216
|
+
* (contracts/README.md spec, section 15; R83): the PNG of the strokes the surface shows, with no
|
|
217
|
+
* condition — the whole sheet, or the workbook's whole page with its draft, one pixel a unit,
|
|
218
|
+
* 1000 × 1400, whatever is filled and whether the work was sent to be checked or not; an empty
|
|
219
|
+
* surface, and a worked example's, is white. The strokes are those `exportWork()` takes, as the
|
|
220
|
+
* history has them: not the stroke under way, but those under an eraser not yet lifted. Each is
|
|
221
|
+
* the outline the screen fills, black on white with no theme and no template, and the pixels go
|
|
222
|
+
* through the crop's encoder, grey a byte each: so the picture looks as the sheet does, and holds
|
|
223
|
+
* at most 1,576,640 bytes, for the encoder spends at most 9 bits on a byte of a row, under the
|
|
224
|
+
* 4 MiB of a stored work.
|
|
225
|
+
*/
|
|
226
|
+
exportPicture(): Promise<Blob>;
|
|
214
227
|
disconnectedCallback(): void;
|
|
215
228
|
}
|
|
216
229
|
declare global {
|
package/lib/element/index.js
CHANGED
|
@@ -50,6 +50,7 @@ import { NO_MANIFEST, settingsOf } from "../core/manifest.js";
|
|
|
50
50
|
import { marksOf, placesOf, shownOf } from "../core/marks.js";
|
|
51
51
|
import { INK_REACH, outline } from "../core/outline.js";
|
|
52
52
|
import { isBox, LAYOUT, pieceWidth, } from "../core/page.js";
|
|
53
|
+
import { encodePNG } from "../core/png.js";
|
|
53
54
|
import { Recorder } from "../core/recorder.js";
|
|
54
55
|
import { fitScale, K_MAX, scaleFor, toSheet } from "../core/screen.js";
|
|
55
56
|
import { SHEET } from "../core/sheet.js";
|
|
@@ -1005,6 +1006,43 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1005
1006
|
return null;
|
|
1006
1007
|
return Object.freeze({ png: blobOf(work.png), submission: work.submission });
|
|
1007
1008
|
}
|
|
1009
|
+
/**
|
|
1010
|
+
* The picture of the work, for the `picture` of a stored work that app puts on the server
|
|
1011
|
+
* (contracts/README.md spec, section 15; R83): the PNG of the strokes the surface shows, with no
|
|
1012
|
+
* condition — the whole sheet, or the workbook's whole page with its draft, one pixel a unit,
|
|
1013
|
+
* 1000 × 1400, whatever is filled and whether the work was sent to be checked or not; an empty
|
|
1014
|
+
* surface, and a worked example's, is white. The strokes are those `exportWork()` takes, as the
|
|
1015
|
+
* history has them: not the stroke under way, but those under an eraser not yet lifted. Each is
|
|
1016
|
+
* the outline the screen fills, black on white with no theme and no template, and the pixels go
|
|
1017
|
+
* through the crop's encoder, grey a byte each: so the picture looks as the sheet does, and holds
|
|
1018
|
+
* at most 1,576,640 bytes, for the encoder spends at most 9 bits on a byte of a row, under the
|
|
1019
|
+
* 4 MiB of a stored work.
|
|
1020
|
+
*/
|
|
1021
|
+
async exportPicture() {
|
|
1022
|
+
// A pixel a unit: the ink is 3 px wide, as on a screen of a pixel a unit.
|
|
1023
|
+
const { width, height } = SHEET;
|
|
1024
|
+
const canvas = document.createElement("canvas");
|
|
1025
|
+
canvas.width = width;
|
|
1026
|
+
canvas.height = height;
|
|
1027
|
+
const context = canvas.getContext("2d", { willReadFrequently: true });
|
|
1028
|
+
if (context === null)
|
|
1029
|
+
throw new Error(`<${TAG}> got no 2D context to draw its picture on`);
|
|
1030
|
+
context.fillStyle = "#fff";
|
|
1031
|
+
context.fillRect(0, 0, width, height);
|
|
1032
|
+
context.fillStyle = "#000";
|
|
1033
|
+
for (const stroke of this.#work.strokes)
|
|
1034
|
+
context.fill(this.#finished(stroke).path);
|
|
1035
|
+
const { data } = context.getImageData(0, 0, width, height);
|
|
1036
|
+
// The canvas's memory goes at once, not at a collection: WebKit caps what all canvases hold.
|
|
1037
|
+
canvas.width = 0;
|
|
1038
|
+
canvas.height = 0;
|
|
1039
|
+
// Black on white: the red of a pixel is its grey. Every index is within the pixels' four bytes
|
|
1040
|
+
// a pixel, so none reads past them.
|
|
1041
|
+
const gray = new Uint8Array(width * height);
|
|
1042
|
+
for (let at = 0; at < gray.length; at++)
|
|
1043
|
+
gray[at] = data[4 * at];
|
|
1044
|
+
return blobOf(encodePNG(gray, width, height));
|
|
1045
|
+
}
|
|
1008
1046
|
disconnectedCallback() {
|
|
1009
1047
|
// A stroke or an erasing under way ends where it is: its pointerup will not reach a sheet off
|
|
1010
1048
|
// the page. Its historychange reaches only the element's own listeners, for the element is in
|