admin管理员组文章数量:1356595
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
本文标签: javascriptHow to properly cache SVG filters for constant reuse in a canvas elementStack Overflow
版权声明:本文标题:javascript - How to properly cache SVG filters for constant reuse in a canvas element? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743982790a2571150.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
/filters/filter.svg
resource? – Bergi Commented Mar 30 at 20:39