I have an SVG filter file in /filters/filter.svg that contains a SVG filter with ID filter-id.
I apply this filter in an HTML5 canvas by using context.filter = "url(/filters/filter.svg#filter-id)"; and it works.
However, the filter gets loaded every time it is used, which happens a lot since I have to constantly animate many individually filtered images in the canvas.
What is the best way, using Javascript only, to cache the filter file once, then retrieve the filter from cache every time it is used, instead of having it loaded again?
It would be preferable that the filter not be embedded or even declared in the HTML file that loads the canvas, since there are actually many filters stored in the filters folder and used simultaneously, to keep the whole process cleaner and more dynamic.
Some basic sample code showing how to cache and retrieve would be highly appreciated.
I have an SVG filter file in /filters/filter.svg that contains a SVG filter with ID filter-id.
I apply this filter in an HTML5 canvas by using context.filter = "url(/filters/filter.svg#filter-id)"; and it works.
However, the filter gets loaded every time it is used, which happens a lot since I have to constantly animate many individually filtered images in the canvas.
What is the best way, using Javascript only, to cache the filter file once, then retrieve the filter from cache every time it is used, instead of having it loaded again?
It would be preferable that the filter not be embedded or even declared in the HTML file that loads the canvas, since there are actually many filters stored in the filters folder and used simultaneously, to keep the whole process cleaner and more dynamic.
Some basic sample code showing how to cache and retrieve would be highly appreciated.
Share Improve this question edited Mar 30 at 16:45 jmdecombe asked Mar 30 at 16:06 jmdecombejmdecombe 8027 silver badges16 bronze badges 3 |1 Answer
Reset to default 0Modern browsers support writing File
objects, among other objects such as Uint8Array
, to the "Origin Private File System". The data is written to the browser configuration folder, referencing the origin where the data was written.
Something like
let dir = await navigator.storage.getDirectory();
let handle = await dir.getFileHandle("filter", { create: true });
let writable = await handle.createWritable();
await writable.write(data);
await writable.close();
to open the OPFS
/filters/filter.svg
resource? – Bergi Commented Mar 30 at 20:39