Introduction to Canvas cross-domain solution
This article brings you an introduction to the Canvas cross-domain solution. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
How to solve Canvas cross-domain problem? Here we record the cross-domain problems and solutions encountered during drawing using Canvas.
Let’s first look at the implementation method.
Implementation method
The target image is generally composed of image text. Whether it is pictures of various sizes or unpredictable texts, it can be accomplished using the canvas api drawImage and fillText methods.
The basic process is as follows:
Get the canvas context--ctx
const canvas = document.querySelector(selector) const ctx = canvas.getContext('2d')
Drawing
Ignore the content on the picture and use drawImage to draw it directly Just draw on the canvas.
const image = new Image() image.src = src image.onload = () => { ctx.save() // 这里我们采用以下参数调用 this.ctx.drawImage(image, dx, dy, dWidth, dHeight) this.ctx.restore() }
drawImage has 3 ways to use parameters. For specific usage, please see the MDN documentation.
Get image data
Call the toBlob(), toDataURL() or getImageData() method provided by the HTMLCanvasElement DOM object.
canvas.toBlob(blob => { // 你要的 blob }, mimeType, encoderOptions)
The default value of mimeType here is image/png. encoderOptions specifies the image quality and can be used for compression, but the mimeType format needs to be image/jpeg or image/webp.
Canvas cross-domain
Under normal circumstances, if we need to output the drawn image, we can call the toBlob(), toDataURL() or getImageData() method of canvas to obtain the image data . However, it is a bit embarrassing when encountering cross-domain images. The following error may be reported:
Failed to execute 'toBlob' on 'HTMLCanvasElement': Tainted canvases may not be exported.
or
Access to image at 'https://your.image.src' from origin 'https://your.website' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Let’s look at the second situation first.
Access-Control-Allow-Origin
If you use certain image resources across domains and the service does not respond correctly to the Access-Control-Allow-Origin header information, the following error message will be reported:
Access to image at 'https://your.image.src' from origin 'https://your.website' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
It means that cross-domain access is not allowed, then you can try to let the background modify the value of Access-Control-Allow-Origin to * or your.website, or use the same domain resource instead (think about it?).
Next, let’s solve the first situation.
img.crossOrigin = 'Anonymous'
In order to avoid user privacy leakage caused by pulling remote website information without permission (such as GPS and other information, you can search Exif for details), when calling canvas's toBlob( ), toDataURL() or getImageData() will throw a security error:
Failed to execute 'toBlob' on 'HTMLCanvasElement': Tainted canvases may not be exported.
If your image service allows cross-domain use (if not, see the previous article), then you should consider adding On the crossOrigin attribute, that is:
const image = new Image() image.crossOrigin = 'Anonymous' image.src = src
In this way, you can get the image data. If you can’t find it, try changing resources from the same domain~
The above is the detailed content of Introduction to Canvas cross-domain solution. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
