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 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:
|
<<: 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
Problem Description Recently, a host reported the...
Docker has many log plug-ins. The default is to u...
Preface: MySQL master-slave architecture should b...
Our bank's MGR will be launched at the end of...
I have read countless my.cnf configurations on th...
This article describes how to use Docker's mu...
Table of contents 2. Comma operator 3. JavaScript...
1. Why do packaging? Facilitates overall code cal...
Make an animation of the eight planets in the sol...
1. Grammar: <meta name="name" content...
LocalStorage stores Boolean values Today, when I ...
Normally, when you run a command in the terminal,...
Table of contents What is insert buffer? What are...
I'm working on electronic archives recently, ...
1. Replace your .js library file address with the...