简体   繁体   中英

Clean tainted canvas

Is it possible to clean a tainted canvas?

I have an Openlayers map where user can add layer with CORS restriction. I can't bypass the cors by my proxy config and crossOrigin='anonymus' is not an option.

What I need, it's to be able to remove the " bad " layer and let the user produces the map image.

So I try to:

  • Remove the layer who can't be printed
  • Try to sync the map
  • Try to print

But I always get the cors error. How can I make my map great again without refreshing the browser?

No there is no way to clean a tainted canvas.
Once tainted, it will remain tainted for its whole life.

Not only this, but also drawing this canvas on a second canvas will taint that second canvas too. Even if that canvas has been completely cleared.

So, the only way for you, is to start again from a new canvas element, and to filter out the bad layers before they are drawn.

There is a property crossOrigin in the source of added layer so you can give it the value crossOrigin: "Anonymous" as the following sample

var basemap = new ol.layer.Tile({
        source: new ol.source.TileArcGISRest({
            ratio: 1,
            params: {},
            crossOrigin: "Anonymous",
            url: "http://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer"
        })
   })

I hope this will fit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM