The HTML specification document introduces the crossorigin attribute for images. By setting the appropriate header information CORS, img images can be loaded from other sites and used in canvas as if they were downloaded directly from the current site (current origin). For details on using the crossorigin attribute, see CORS settings attributes. What is “tainted” canvas? Although it is possible to use images in canvas without CORS authorization, doing so will taint the canvas. Once the canvas is polluted, you can no longer extract data from the canvas, which means you can no longer call methods such as toBlob(), toDataURL(), and getImageData(), otherwise a security error will be thrown. This is actually to protect the user's personal information and avoid loading the user's image information from remote web sites without permission, which would cause privacy leakage.
Example: Save image from other site First, the image server must set the corresponding <IfModule mod_setenvif.c> <IfModule mod_headers.c> <FilesMatch "\.(cur|gif|ico|jpe?g|png|svgz?|webp)$"> SetEnvIf Origin ":" IS_CORS Header set Access-Control-Allow-Origin "*" env=IS_CORS </FilesMatch> </IfModule> </IfModule> With these settings in effect, you can save images from other sites to DOM storage (or elsewhere) just like your own resources. var img = new Image, canvas = document.createElement("canvas"), ctx = canvas.getContext("2d"), src = "http://example.com/image"; // Specific image address img.crossOrigin = "Anonymous"; img.onload = function() { canvas.width = img.width; canvas.height = img.height; ctx.drawImage( img, 0, 0 ); localStorage.setItem( "savedImageData", canvas.toDataURL("image/png") ); } img.src = src; // Ensure that cached images also trigger the load event if ( img.complete || img.complete === undefined ) { img.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw=="; img.src = src; } Browser compatibility Desktop
Mobile
See also Chrome: Using cross-origin images in WebGL HTML Specification - crossorigin Attribute The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. |
<<: Dynamic starry sky background implemented with CSS3
>>: Detailed example of inserting custom HTML records in Quill editor
First, your container must be running You can vie...
1. Problem The project developed using Eclipse on...
1. Grammar location [=|~|~*|^~|@] /uri/ { ... } 2...
The following questions are all based on the Inno...
Docker includes three basic concepts: Image: A Do...
In our daily business, form validation is a very ...
If the words in the sql statement conflict with t...
Table of contents 1. Display and hide by default ...
In Docker's design, a container runs only one...
After adding –subnet to Docker network Create, us...
Page domain relationship: The main page a.html bel...
Transactional Characteristics 1. Atomicity: After...
Table of contents Preface What is data binding? T...
Preface In addition to the default built-in direc...
Table of contents 1. Vue2 syntax 2. Use of Vue3 1...