Skip to content

Commit

Permalink
getTile support cssFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
deyihu committed Oct 15, 2024
1 parent b48b9f5 commit 68594cb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/cavnas.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,19 @@ export function toBlobURL(imagebitmap) {
ctx.drawImage(imagebitmap, 0, 0);
return canvas.convertToBlob();
}

export function imageFilter(canvas, imagebitmap, filter) {
if (!filter) {
return imagebitmap;
}
canvas.width = imagebitmap.width;
canvas.height = imagebitmap.height;
const ctx = getCanvasContext(canvas);
clearCanvas(ctx);
ctx.save();
ctx.filter = filter;
ctx.drawImage(imagebitmap, 0, 0);
ctx.restore();
const bitImage = canvas.transferToImageBitmap();
return bitImage;
}
14 changes: 13 additions & 1 deletion src/tileget.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import { getCanvas, imageFilter } from './cavnas';

export function getTile(url, options = {}) {
return new Promise((resolve, reject) => {
if (!url) {
reject(new Error('url is null'));
return;
}
fetch(url).then(res => res.blob()).then(blob => createImageBitmap(blob)).then(imagebit => {
resolve(imagebit);
const filter = options.filter;
if (filter) {
const canvas = getCanvas();
if (!canvas) {
reject(new Error('not find canvas.The current environment does not support OffscreenCanvas'));
} else {
resolve(imageFilter(canvas, imagebit, filter));
}
} else {
resolve(imagebit);
}
}).catch(error => {
reject(error);
});
Expand Down

0 comments on commit 68594cb

Please sign in to comment.