forked from AkashP4769/Figma-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.html
168 lines (140 loc) · 3.9 KB
/
ui.html
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<style>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
background-color: var(--figma-color-bg);
color: var(--figma-color-text);
font-family: Inter;
padding: 20px;
}
.nav {
margin-top: 20px;
display: flex;
border-bottom: 1px solid var(--figma-color-border);
}
.nav li {
font-weight: 500;
list-style: none;
padding: 10px;
color: var(--figma-color-text-disabled);
}
.nav li:hover {
cursor: pointer;
}
li.active {
color: var(--figma-color-text-brand);
border-bottom: 1px solid var(--figma-color-text-brand);
}
.container {
margin-top: 20px;
display: flex;
flex-direction: column;
gap: 10px;
}
pre {
margin-top: 10px;
background-color: black;
border-radius: 10px;
}
button {
background-color: var(--figma-color-bg-brand);
border-radius: 5px;
padding: 10px 15px;
color: white;
border: none;
}
button:hover {
background-color: var(--figma-color-bg-brand-hover);
cursor: pointer;
}
</style>
<h2>Apply filters</h2>
<div class="container">
<button id="invert">Invert</button>
<button id="vignette">Grayscale</button>
</div>
<script>
function convertTo2DArray(imageArray, height, width) {
const resultArray = new Array(height).fill(0).map(() => new Array(width).fill(0));
for (let i = 0; i < height; i++) {
for (let j = 0; j < width; j++) {
const index = i * width + j;
resultArray[i][j] = imageArray[index];
}
}
console.log("returned");
return resultArray;
}
function convertTo1DArray(arr2D) {
const height = arr2D.length;
const width = arr2D[0].length;
const resultArray = new Array(height * width);
let index = 0;
for (let i = 0; i < height; i++) {
for (let j = 0; j < width; j++) {
resultArray[index] = arr2D[i][j];
index++;
}
}
return resultArray;
}
async function encode(canvas, ctx, imageData) {
ctx.putImageData(imageData, 0, 0)
return await new Promise((resolve, reject) => {
canvas.toBlob(blob => {
const reader = new FileReader()
reader.onload = () => resolve(new Uint8Array(reader.result))
reader.onerror = () => reject(new Error('Could not read from blob'))
reader.readAsArrayBuffer(blob)
})
})
}
async function decode(canvas, ctx, bytes) {
const url = URL.createObjectURL(new Blob([bytes]))
const image = await new Promise((resolve, reject) => {
const img = new Image()
img.onload = () => resolve(img)
img.onerror = () => reject()
img.src = url
})
canvas.width = image.width
canvas.height = image.height
ctx.drawImage(image, 0, 0)
const imageData = ctx.getImageData(0, 0, image.width, image.height)
return imageData
}
window.onmessage = async (event) => {
const bytes = event.data.pluginMessage.data
const canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d')
const imageData = await decode(canvas, ctx, bytes)
let pixels = imageData.data
if (event.data.pluginMessage.mode === 'invert') {
console.log("invert");
for (let i = 0; i < pixels.length; i += 4) {
pixels[i + 0] = 255 - pixels[i + 0]
pixels[i + 1] = 255 - pixels[i + 1]
pixels[i + 2] = 255 - pixels[i + 2]
}
}
else if (event.data.pluginMessage.mode === 'vignette') {
for (let i = 0; i < pixels.length; i += 4) {
const avg = (pixels[i + 0] + pixels[i + 1] + pixels[i + 2])/3;
pixels[i + 0] = avg
pixels[i + 1] = avg
pixels[i + 2] = avg
}
}
const newBytes = await encode(canvas, ctx, imageData)
window.parent.postMessage({pluginMessage: newBytes}, '*')
}
document.getElementById('invert').onclick = () => {
parent.postMessage({ pluginMessage: { type: 'invert' } }, '*')
}
document.getElementById('vignette').onclick = () => {
parent.postMessage({ pluginMessage: { type: 'vignette' }}, '*')
}
</script>