text
stringlengths
1
372
advantage of browser caching, and built-in image
optimization and memory management.
they allow you to safely display images from arbitrary sources
(more on than in the CORS section below).
drawImage is great when the image must fit within
other content rendered using the <canvas> element.
you also gain control over image sizing and,
when the CORS policy allows it, read the pixels
of the image back for further processing.
finally, WebGL gives you the highest degree of
control over the image. not only can you read the pixels and
apply custom image algorithms, but you can also use GLSL for
hardware-acceleration.
<topic_end>
<topic_start>
Cross-Origin resource sharing (cors)
CORS is a mechanism that browsers use to control
how one site accesses the resources of another site.
it is designed such that, by default, one web-site
is not allowed to make HTTP requests to another site
using XHR or fetch.
this prevents scripts on another site from acting on behalf
of the user and from gaining access to another
site’s resources without permission.
when using <img>, <picture>, or <canvas>,
the browser automatically blocks access to pixels
when it knows that an image is coming from another site
and the CORS policy disallows access to data.
WebGL requires access to the image data in order
to be able to render the image. therefore,
images to be rendered using WebGL must only come from servers
that have a CORS policy configured to work with
the domain that serves your application.
<topic_end>
<topic_start>
flutter renderers on the web
flutter offers a choice of two renderers on the web:
because the HTML renderer uses the <img>
element it can display images from
arbitrary sources. however,
this places the following limitations on what you
can do with them:
the CanvasKit renderer implements flutter’s image API fully.
however, it requires access to image pixels to do so,
and is therefore subject to the CORS policy.
<topic_end>
<topic_start>
solutions
<topic_end>
<topic_start>
in-memory, asset, and same-origin network images
if the app has the bytes of the encoded image in memory,
provided as an asset, or stored on the
same server that serves the application
(also known as same-origin), no extra effort is necessary.
the image can be displayed using
image.memory, image.asset, and image.network
in both HTML and CanvasKit modes.
<topic_end>
<topic_start>
cross-origin images
the HTML renderer can load cross-origin images
without extra configuration.
CanvasKit requires that the app gets the bytes of the encoded image.
there are several ways to do this, discussed below.
<topic_end>
<topic_start>
host your images in a CORS-enabled CDN.
typically, content delivery networks (cdn)
can be configured to customize what domains
are allowed to access your content.
for example, firebase site hosting allows
specifying a custom Access-Control-Allow-Origin
header in the firebase.json file.
<topic_end>
<topic_start>
lack control over the image server? use a CORS proxy.
if the image server cannot be configured to allow CORS
requests from your application,
you might still be able to load images by proxying
the requests through another server. this requires that the
intermediate server has sufficient access to load the images.
this method can be used in situations when the original
image server serves images publicly,
but is not configured with the correct CORS headers.
examples:
<topic_end>
<topic_start>
use <img> in a platform view.
flutter supports embedding HTML inside the app using
HtmlElementView. use it to create an <img>
element to render the image from another domain.
however, do keep in mind that this comes with the
limitations explained in the section
“flutter renderers on the web” above.
as of today, using too many HTML elements
with the CanvasKit renderer might hurt performance.
if images interleave non-image content flutter needs to
create extra WebGL contexts between the <img> elements.
if your application needs to display a lot of images