-
Notifications
You must be signed in to change notification settings - Fork 0
/
qpainter.js
57 lines (49 loc) · 1.69 KB
/
qpainter.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
export default class QPainter {
constructor(painterUuid = '', selector, dimensions, properties) {
this.selector = selector;
this.dimensions = dimensions;
this.properties = properties;
this.paint = null;
if (painterUuid === '') {
this.uuid = this.randomLetters();
} else {
this.uuid = painterUuid;
}
this.init();
}
randomLetters() {
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
let randomLetters = '';
for (let i = 0; i < 12; i++) {
randomLetters += alphabet.charAt(Math.floor(Math.random() * alphabet.length));
}
return randomLetters;
}
init() {}
renderQPainter() {
if ('paintWorklet' in CSS) {
var qPainter = this;
const workletCode = `
class ${this.uuid} {
static get inputProperties() { return ${JSON.stringify(this.properties)}; }
paint(ctx, size, properties) {
${this.paint}
}
}
registerPaint('${this.uuid}', ${this.uuid});
`;
const blob = new Blob([workletCode], {
type: 'application/javascript'
});
const blobURL = URL.createObjectURL(blob);
CSS.paintWorklet.addModule(blobURL)
/* TO DO
Add this.properties contents to the rendered snippet */
document.head.insertAdjacentHTML('beforeend', `
<style>
${qPainter.selector} { background-image: paint(${this.uuid}); }
</style>
`);
}
}
}