Table of Contents
Canvas cross-domain
Home Web Front-end H5 Tutorial Introduction to Canvas cross-domain solution

Introduction to Canvas cross-domain solution

Nov 16, 2018 pm 05:22 PM
canvas html5 javascript

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')
Copy after login

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()
}
Copy after login

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)
Copy after login

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.
Copy after login
Copy after login

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.
Copy after login
Copy after login

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.
Copy after login
Copy after login

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.
Copy after login
Copy after login

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
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

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

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

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.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

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

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

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

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

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

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

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

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

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

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

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

See all articles