-
Notifications
You must be signed in to change notification settings - Fork 608
/
FeGaussianBlurElement.ts
59 lines (48 loc) · 1.12 KB
/
FeGaussianBlurElement.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import {
canvasRGBA
} from 'stackblur-canvas';
import {
RenderingContext2D
} from '../types';
import Document from './Document';
import Element from './Element';
export default class FeGaussianBlurElement extends Element {
type = 'feGaussianBlur';
readonly extraFilterDistance: number;
protected readonly blurRadius: number;
constructor(
document: Document,
node: HTMLElement,
captureTextNodes?: boolean
) {
super(document, node, captureTextNodes);
this.blurRadius = Math.floor(this.getAttribute('stdDeviation').getNumber());
this.extraFilterDistance = this.blurRadius;
}
apply(
ctx: RenderingContext2D,
x: number,
y: number,
width: number,
height: number
) {
const {
document,
blurRadius
} = this;
const body = document.window
? document.window.document.body
: null;
const canvas = ctx.canvas as HTMLCanvasElement;
// StackBlur requires canvas be on document
canvas.id = document.getUniqueId();
if (body) {
canvas.style.display = 'none';
body.appendChild(canvas);
}
canvasRGBA(canvas, x, y, width, height, blurRadius);
if (body) {
body.removeChild(canvas);
}
}
}