-
Notifications
You must be signed in to change notification settings - Fork 1.7k
improve: add blur and pixelation mask effects #2012
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6883643
709faee
a8c91b1
d91baac
0d9d34e
81afd5b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { | ||
| defaultMaskSegment, | ||
| encodeMaskEffect, | ||
| getMaskEffect, | ||
| getMaskEffectAmount, | ||
| } from "./masks"; | ||
|
|
||
| describe("mask effects", () => { | ||
| it("defaults new sensitive masks to a fully obscuring blur", () => { | ||
| const segment = defaultMaskSegment(0, 1); | ||
|
|
||
| expect(getMaskEffect(segment)).toBe("blur"); | ||
| expect(getMaskEffectAmount(segment)).toBe(16); | ||
| expect(segment.opacity).toBe(1); | ||
| }); | ||
|
|
||
| it("preserves legacy pixelation values", () => { | ||
| const segment = defaultMaskSegment(0, 1); | ||
| segment.pixelation = 18; | ||
|
|
||
| expect(getMaskEffect(segment)).toBe("pixelate"); | ||
| expect(getMaskEffectAmount(segment)).toBe(18); | ||
| }); | ||
|
|
||
| it("encodes blur as a privacy-safe legacy pixelation fallback", () => { | ||
| const segment = defaultMaskSegment(0, 1); | ||
| segment.pixelation = encodeMaskEffect("blur", 24); | ||
|
|
||
| expect(segment.pixelation).toBe(1024); | ||
| expect(getMaskEffect(segment)).toBe("blur"); | ||
| expect(getMaskEffectAmount(segment)).toBe(24); | ||
| }); | ||
|
|
||
| it("keeps effect amounts within the supported range", () => { | ||
| const segment = defaultMaskSegment(0, 1); | ||
| segment.pixelation = encodeMaskEffect("pixelate", Number.NaN); | ||
|
|
||
| expect(getMaskEffectAmount(segment)).toBe(16); | ||
| expect(encodeMaskEffect("pixelate", 1)).toBe(4); | ||
| expect(encodeMaskEffect("blur", 100)).toBe(1080); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,16 @@ | ||
| import type { XY } from "~/utils/tauri"; | ||
| import maskEffectContract from "../../../../../crates/project/mask-effects.json"; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Heads up: importing |
||
|
|
||
| export type MaskKind = "sensitive" | "highlight"; | ||
| export type MaskEffect = "blur" | "pixelate"; | ||
|
|
||
| // Older versions interpret encoded blur as strong pixelation, keeping masked content private. | ||
| const { | ||
| blurEncodingOffset: MASK_BLUR_ENCODING_OFFSET, | ||
| defaultAmount: DEFAULT_MASK_EFFECT_AMOUNT, | ||
| minAmount: MIN_MASK_EFFECT_AMOUNT, | ||
| maxAmount: MAX_MASK_EFFECT_AMOUNT, | ||
| } = maskEffectContract; | ||
|
|
||
| export type MaskScalarKeyframe = { | ||
| time: number; | ||
|
|
@@ -38,7 +48,37 @@ export type MaskSegment = { | |
| export type MaskState = { | ||
| position: XY<number>; | ||
| size: XY<number>; | ||
| intensity: number; | ||
| }; | ||
|
|
||
| const normalizeMaskEffectAmount = (amount: number) => { | ||
| if (!Number.isFinite(amount) || amount <= 0) { | ||
| return DEFAULT_MASK_EFFECT_AMOUNT; | ||
| } | ||
| return Math.min( | ||
| Math.max(amount, MIN_MASK_EFFECT_AMOUNT), | ||
| MAX_MASK_EFFECT_AMOUNT, | ||
| ); | ||
| }; | ||
|
|
||
| export const encodeMaskEffect = (effect: MaskEffect, amount: number) => { | ||
| const normalizedAmount = normalizeMaskEffectAmount(amount); | ||
| return effect === "blur" | ||
| ? MASK_BLUR_ENCODING_OFFSET + normalizedAmount | ||
| : normalizedAmount; | ||
| }; | ||
|
|
||
| export const getMaskEffect = (segment: MaskSegment): MaskEffect => | ||
| segment.pixelation >= MASK_BLUR_ENCODING_OFFSET ? "blur" : "pixelate"; | ||
|
|
||
| export const getMaskEffectAmount = (segment: MaskSegment) => { | ||
| const storedAmount = Number.isFinite(segment.pixelation) | ||
| ? segment.pixelation | ||
| : DEFAULT_MASK_EFFECT_AMOUNT; | ||
| const decodedAmount = | ||
| getMaskEffect(segment) === "blur" | ||
| ? storedAmount - MASK_BLUR_ENCODING_OFFSET | ||
| : storedAmount; | ||
| return normalizeMaskEffectAmount(decodedAmount); | ||
| }; | ||
|
|
||
| export const defaultMaskSegment = ( | ||
|
|
@@ -54,7 +94,7 @@ export const defaultMaskSegment = ( | |
| size: { x: 0.35, y: 0.35 }, | ||
| feather: 0.1, | ||
| opacity: 1, | ||
| pixelation: 18, | ||
| pixelation: encodeMaskEffect("blur", DEFAULT_MASK_EFFECT_AMOUNT), | ||
| darkness: 0.5, | ||
| fadeDuration: 0, | ||
| keyframes: { position: [], size: [], intensity: [] }, | ||
|
|
@@ -72,9 +112,7 @@ export const evaluateMask = ( | |
| x: Math.min(Math.max(segment.size.x, 0.01), 2), | ||
| y: Math.min(Math.max(segment.size.y, 0.01), 2), | ||
| }; | ||
| const intensity = Math.min(Math.max(segment.opacity, 0), 1); | ||
|
|
||
| return { position, size, intensity }; | ||
| return { position, size }; | ||
| }; | ||
|
|
||
| const sortByTime = <T extends { time: number }>(items: T[]) => | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "blurEncodingOffset": 1000, | ||
| "defaultAmount": 16, | ||
| "minAmount": 4, | ||
| "maxAmount": 80 | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor type-safety: it’d be nice to avoid the
as MaskEffectcast here sinceRadioGroupcan usually emit arbitrary strings.