|
| 1 | +import type { |
| 2 | + DrawingPoint, |
| 3 | + ImageAnnotation, |
| 4 | + InkAnnotation, |
| 5 | + Instance, |
| 6 | + List, |
| 7 | +} from "@nutrient-sdk/viewer"; |
| 8 | +import { baseOptions } from "../../shared/base-options"; |
| 9 | + |
| 10 | +const scalingFactorTypeSignature = 0.2; |
| 11 | +const scalingFactorDrawSignature = 0.2; |
| 12 | + |
| 13 | +window.NutrientViewer.load({ |
| 14 | + ...baseOptions, |
| 15 | + theme: window.NutrientViewer.Theme.DARK, |
| 16 | +}).then((instance: Instance) => { |
| 17 | + instance.addEventListener( |
| 18 | + "annotations.create", |
| 19 | + async (createdAnnotations) => { |
| 20 | + const typeSignatures = createdAnnotations.filter( |
| 21 | + (annotation): annotation is ImageAnnotation => |
| 22 | + annotation instanceof |
| 23 | + window.NutrientViewer.Annotations.ImageAnnotation, |
| 24 | + ); |
| 25 | + |
| 26 | + const firstTypeSignature = typeSignatures.first() as |
| 27 | + | ImageAnnotation |
| 28 | + | undefined; |
| 29 | + |
| 30 | + if (firstTypeSignature?.isSignature) { |
| 31 | + const boundingBox = firstTypeSignature.boundingBox; |
| 32 | + const newWidth = scalingFactorTypeSignature * boundingBox.width; |
| 33 | + const newHeight = scalingFactorTypeSignature * boundingBox.height; |
| 34 | + const newLeft = boundingBox.left + (boundingBox.width - newWidth) / 2; |
| 35 | + const newTop = boundingBox.top + (boundingBox.height - newHeight) / 2; |
| 36 | + |
| 37 | + const newBoundingBox = new window.NutrientViewer.Geometry.Rect({ |
| 38 | + left: newLeft, |
| 39 | + top: newTop, |
| 40 | + width: newWidth, |
| 41 | + height: newHeight, |
| 42 | + }); |
| 43 | + |
| 44 | + const newAnnotation = firstTypeSignature.set( |
| 45 | + "boundingBox", |
| 46 | + newBoundingBox, |
| 47 | + ); |
| 48 | + await instance.update(newAnnotation); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + const inkSignatures = createdAnnotations.filter( |
| 53 | + (annotation): annotation is InkAnnotation => |
| 54 | + annotation instanceof |
| 55 | + window.NutrientViewer.Annotations.InkAnnotation && |
| 56 | + annotation.isSignature, |
| 57 | + ); |
| 58 | + |
| 59 | + if (inkSignatures.size > 0) { |
| 60 | + const scaledAnnotations = inkSignatures.map((annotation) => { |
| 61 | + const boundingBox = annotation.boundingBox; |
| 62 | + if (!boundingBox) return annotation; |
| 63 | + |
| 64 | + const scaledBoundingBox = boundingBox |
| 65 | + .scale(scalingFactorDrawSignature) |
| 66 | + .merge({ |
| 67 | + left: boundingBox.left + boundingBox.width / 4, |
| 68 | + top: boundingBox.top + boundingBox.height / 4, |
| 69 | + }); |
| 70 | + |
| 71 | + const scaledLines = annotation.lines.map((line: List<DrawingPoint>) => |
| 72 | + line.map((point: DrawingPoint) => |
| 73 | + point |
| 74 | + .translate({ |
| 75 | + x: -boundingBox.left, |
| 76 | + y: -boundingBox.top, |
| 77 | + }) |
| 78 | + .scale(scalingFactorDrawSignature) |
| 79 | + .translate({ |
| 80 | + x: boundingBox.left + boundingBox.width / 4, |
| 81 | + y: boundingBox.top + boundingBox.height / 4, |
| 82 | + }), |
| 83 | + ), |
| 84 | + ); |
| 85 | + |
| 86 | + return annotation |
| 87 | + .set("boundingBox", scaledBoundingBox) |
| 88 | + .set("lines", scaledLines); |
| 89 | + }); |
| 90 | + |
| 91 | + await instance.update(scaledAnnotations); |
| 92 | + } |
| 93 | + }, |
| 94 | + ); |
| 95 | +}); |
0 commit comments