Potential perf improvements for canvas

pull/450/head
Lim Chee Aun 2024-03-10 23:25:07 +08:00
rodzic f5ea96a093
commit d63e6c87c4
3 zmienionych plików z 9 dodań i 2 usunięć

Wyświetl plik

@ -453,12 +453,15 @@ function AccountInfo({
e.target.classList.add('loaded'); e.target.classList.add('loaded');
try { try {
// Get color from four corners of image // Get color from four corners of image
const canvas = document.createElement('canvas'); const canvas = window.OffscreenCanvas
? new OffscreenCanvas(1, 1)
: document.createElement('canvas');
const ctx = canvas.getContext('2d', { const ctx = canvas.getContext('2d', {
willReadFrequently: true, willReadFrequently: true,
}); });
canvas.width = e.target.width; canvas.width = e.target.width;
canvas.height = e.target.height; canvas.height = e.target.height;
ctx.imageSmoothingEnabled = false;
ctx.drawImage(e.target, 0, 0); ctx.drawImage(e.target, 0, 0);
// const colors = [ // const colors = [
// ctx.getImageData(0, 0, 1, 1).data, // ctx.getImageData(0, 0, 1, 1).data,

Wyświetl plik

@ -21,6 +21,7 @@ const canvas = window.OffscreenCanvas
const ctx = canvas.getContext('2d', { const ctx = canvas.getContext('2d', {
willReadFrequently: true, willReadFrequently: true,
}); });
ctx.imageSmoothingEnabled = false;
function Avatar({ url, size, alt = '', squircle, ...props }) { function Avatar({ url, size, alt = '', squircle, ...props }) {
size = SIZES[size] || size || SIZES.m; size = SIZES[size] || size || SIZES.m;

Wyświetl plik

@ -2167,10 +2167,13 @@ function Card({ card, selfReferential, instance }) {
const w = 44; const w = 44;
const h = 44; const h = 44;
const blurhashPixels = decodeBlurHash(blurhash, w, h); const blurhashPixels = decodeBlurHash(blurhash, w, h);
const canvas = document.createElement('canvas'); const canvas = window.OffscreenCanvas
? new OffscreenCanvas(1, 1)
: document.createElement('canvas');
canvas.width = w; canvas.width = w;
canvas.height = h; canvas.height = h;
const ctx = canvas.getContext('2d'); const ctx = canvas.getContext('2d');
ctx.imageSmoothingEnabled = false;
const imageData = ctx.createImageData(w, h); const imageData = ctx.createImageData(w, h);
imageData.data.set(blurhashPixels); imageData.data.set(blurhashPixels);
ctx.putImageData(imageData, 0, 0); ctx.putImageData(imageData, 0, 0);