-
Notifications
You must be signed in to change notification settings - Fork 1
/
create-background-canvas.js
42 lines (32 loc) · 1.21 KB
/
create-background-canvas.js
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
define(function () {
'use strict';
const heightMargin = 4;
const makeCanvas = (left, top, width, height) => {
let canvas = document.createElement('canvas');
canvas.style.position = 'absolute';
canvas.height = height;
canvas.width = width;
canvas.style.left = left + 'px';
canvas.style.top = top + 'px';
return canvas;
};
const isPositioned = node => node.style.position !== '' && node.style.position !== 'static';
const positionElement = node => {
if (!isPositioned(node)) {
node.style.position = 'relative';
}
};
/**
* Create a new canvas element and position it behind the given target element.
*/
return targetElement => {
positionElement(targetElement.parentNode);
const {offsetWidth, offsetHeight, scrollWidth, scrollHeight, offsetLeft, offsetTop} = targetElement;
const width = scrollWidth;
const height = scrollHeight + heightMargin;
const canvas = makeCanvas(offsetLeft, offsetTop, width, height);
canvas.style.zIndex = targetElement.style.zIndex - 1;
targetElement.after(canvas);
return canvas;
};
});