js uses Canvas to merge multiple pictures into one implementation code

js uses Canvas to merge multiple pictures into one implementation code

Solution

function mergeImgs(list) {
 const imgDom = document.createElement('img')
 document.body.appendChild(imgDom)

 const canvas = document.createElement('canvas')
 canvas.width = 500
 canvas.height = 500 * list.length
 const context = canvas.getContext('2d')

 list.map((item, index) => {
 const img = new Image()
 img.src = item
 // Cross-domain img.crossOrigin = 'Anonymous'
 
 img.onload = () => {
  context.drawImage(img, 0, 500 * index, 500, 500)
  const base64 = canvas.toDataURL('image/png')
  imgDom.setAttribute('src', base64)
  // console.log(baseList)
 }
 })
}

const urlList = ['./img/timg%20(1).jpg', './img/timg.jpg']
mergeImgs(urlList )

Optimize the code slightly and change it to a public method

/**
 * Merge multiple images and return a new image * @param {Array} list Image url array * @param {Number} cwith Canvas width defaults to 500
 * @param {Number} cheight The default height of the canvas is 500
 */
function mergeImgs(list, cwith = 500, cheight = 500) {
 return new Promise((resolve, reject) => {
 const baseList = []

 const canvas = document.createElement('canvas')
 canvas.width = cwith
 canvas.height = cheight * list.length
 const context = canvas.getContext('2d')

 list.map((item, index) => {
  const img = new Image()
  img.src = item
  // Cross-domain img.crossOrigin = 'Anonymous'

  img.onload = () => {
  context.drawImage(img, 0, cheight * index, cwith, cheight)
  const base64 = canvas.toDataURL('image/png')
  baseList.push(base64)

  if (baseList[list.length - 1]) {
   console.log(baseList)
   // Return the new image resolve(baseList[list.length - 1])
  }
  }
 })
 })
}

const urlList = ['./img/timg%20(1).jpg', './img/timg.jpg']
mergeImgs(urlList ).then(base64 => {
	const imgDom = document.createElement('img')
	imgDom.src = base64
	document.body.appendChild(imgDom)
})

Effect

insert image description here

This is the end of this article about the implementation code of js using Canvas to merge multiple images into one. For more relevant js canvas image merging content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • js+canvas realizes the method of merging two pictures into one picture
  • js saves the image generated by canvas, or directly saves an image
  • js implements canvas method to save pictures in png format and download them locally
  • JS mobile/H5 select multiple images to upload at the same time and use canvas to compress the images
  • How to draw pictures on canvas using js+html5

<<:  How to quickly install and deploy MySQL in Windows system (green free installation version)

>>:  Basic knowledge of load balancing and a simple example of load balancing using nginx

Recommend

Getting Started with CSS3 Animation in 10 Minutes

Introduction Animation allows you to easily imple...

MySQL reports an error: Can't find file: './mysql/plugin.frm' solution

Find the problem Recently, I found a problem at w...

Use Shell scripts to batch start and stop Docker services

Table of contents Start Docker Stop Docker Python...

Summary of Vue's cross-domain problem handling and solutions

When you send a network request, the following sa...

Vue front-end development auxiliary function state management detailed example

Table of contents mapState mapGetters mapMutation...

Introduction to JavaScript array deduplication and flattening functions

Table of contents 1. Array flattening (also known...

How to implement responsive layout in vue-cli

When we are doing front-end development, we will ...

Solution to JS out-of-precision number problem

The most understandable explanation of the accura...

Solve the problem of using less in Vue

1. Install less dependency: npm install less less...

Reasons why MySQL cancelled Query Cache

MySQL previously had a query cache, Query Cache. ...

Example of how to set up a third-level domain name in nginx

Problem Description By configuring nginx, you can...

Vue implements a scroll bar style

At first, I wanted to modify the browser scroll b...

Press Enter to automatically submit the form. Unexpected discovery

Copy code The code is as follows: <!DOCTYPE ht...

Vue implements small search function

This article example shares the specific code of ...