Solution to using html2canvas to process Dom elements with Baidu map into images

Solution to using html2canvas to process Dom elements with Baidu map into images

Problem 1: Baidu Map uses tiled images (the map is made up of pictures). When html2canvas is used to process images that are not in the same domain name, the browser will display a cross-domain error. It cannot be solved by using a reverse proxy because the domain name of the tile image is uncertain and proxy_pass cannot be specified.

Solution: Use Baidu Map static image processing (http://lbsyun.baidu.com/index.php?title=static). At this time, the domain name is determined (http://api.map.baidu.com), and reverse proxy can be used to solve cross-domain

<!--html-->
<el-image
:src="`/baidu-static/staticimage/v2?ak=yourak&width=1024&height=400¢er=${center.lng},${center.lat}&zoom=16`"
>
<div
  slot="placeholder"
  class="image-slot"
>
  Loading...
</div>
</el-image>

<!--nginx-->
location ^~ /baidu-static/ {
add_header 'Access-Control-Allow-Origin' "$http_origin" always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-
Since,Keep-Alive,Origin,User-Agent,X-Requested-With' always;
proxy_pass http://api.map.baidu.com/;
}

Question 2: How do I display the overlay on the map?

Solution: I looked at the Baidu Map static image API and found that it does not support custom overlay styles very well. It can only allow you to specify a custom image (not a local image). I tried many methods and thought that using openLayers.Map was a feasible method. However, the workload of code modification was too large, so I gave up decisively. Later, I thought of using div to simulate the overlay directly, and setting it a little higher than the static layer level would solve the problem.

Question 3: I drew a dotted circle using CSS style. After processing the generated image with html2canvas, I found that the dotted line turned into a solid line.

Solution: Use canvas to draw circles

Question 4: An icon is absolutely positioned, but the icon is not displayed in the generated image after being processed by html2canvas

Solution: Set the z-index of the icon to be greater than the Baidu static image level (PS: The style of the static image also uses absolute positioning)

Question 5: The image generated after html2canvas processing has a black background color

Solution: Change image/png to image/jpg

try {
  html2canvas(sharePage, {
    useCORS: true
  }).then((canvas) => {
    const imgBase64 = canvas.toDataURL('image/jpg')
    this.data64 = imgBase64
    })
  } catch (err) {
}

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.

<<:  Responsive layout summary (recommended)

>>:  Example of using CSS3 to achieve shiny font effect when unlocking an Apple phone

Recommend

CSS new feature contain controls page redrawing and rearrangement issues

Before introducing the new CSS property contain, ...

FlashFXP ftp client software registration cracking method

The download address of FlashFXP is: https://www....

How to change the website accessed by http to https in nginx

Table of contents 1. Background 2. Prerequisites ...

MySQL Community Server compressed package installation and configuration method

Today, because I wanted to install MySQL, I went ...

Repair solution for inconsistent MySQL GTID master and slave

Table of contents Solution 1: Rebuild Replicas Pr...

How to connect XShell and network configuration in CentOS7

1. Linux network configuration Before configuring...

Examples of common Nginx misconfigurations

Table of contents Missing root location Off-By-Sl...

How to visualize sketched charts in Vue.js using RoughViz

introduce A chart is a graphical representation o...

Javascript implements simple navigation bar

This article shares the specific code of Javascri...

How to operate the check box in HTML page

Checkboxes are very common on web pages. Whether ...

mysql5.7.20 installation and configuration method graphic tutorial (mac)

MySQL 5.7.20 installation and configuration metho...

Docker beginners' first exploration of common commands practice records

Before officially using Docker, let's first f...

Vue commonly used high-order functions and comprehensive examples

1. Commonly used high-order functions of arrays S...

How to use the dig/nslookup command to view DNS resolution steps

dig - DNS lookup utility When a domain name acces...