convert image to base64 in javascript
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-05-16
* @modified
*
* @description convert image to base64 in javascript
* @augments
* @example
* @link
*
*/
const log = console.log;
function getDataUrl(img) {
// Create canvas
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
// Set width and height
canvas.width = img.width;
canvas.height = img.height;
// Draw the image
ctx.drawImage(img, 0, 0);
return canvas.toDataURL('image/jpeg');
}
// Select the image
// const img = document.querySelector('#my-image');
// img.addEventListener('load', function (event) {
// const dataUrl = getDataUrl(event.currentTarget);
// console.log(dataUrl);
// });
demo
const log = console.log;
function getDataUrl(img) {
// Create canvas
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
// Set width and height
canvas.width = img.width;
canvas.height = img.height;
// Draw the image
ctx.drawImage(img, 0, 0);
return canvas.toDataURL('image/png');
// return canvas.toDataURL('image/webp');
// return canvas.toDataURL('image/jpeg');
}
img = document.querySelectorAll(`img`)[37];
base64Result = ``;
img.addEventListener('click', function (event) {
console.log(`event`, event)
console.log(`event.currentTarget`, event.currentTarget)
const dataUrl = getDataUrl(event.currentTarget);
console.log(dataUrl);
try {
// window.copy(dataUrl);
base64Result = dataUrl;
log(`auto copy success!`)
} catch (err) {
log(err)
}
});
copy(base64Result);
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAT7klEQVR4Xu2dCXQUVbrH/70mnUCAAIJEkVWGPaAoDMI46giCw+AcRRBBZRxHDoODiAZGGUSDOnHB5fmUJw+BJzKogGzuAyiKsiVAQsAQyAIRZElI0llId1e989XSqa6uunU7JhAmfTk59HL7VvX91bffum0TRVFEtDWaGbBFgTQaFtKJRIE0Lh5RII2MRxRIFEhjm4FGdj5RGxIF0shmoJGdTlRCLkkgmwDs4jjzrQD6AGjD6HsSwCEAN1qMR2O5AAzl6DcAQAtGv2MAjnAe0wPgeo7v+rRFnwIA73KMQ11GAhgs9+WTEDr4fI7BbQB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75QaMH4C+oxofkCEHhkQExEw6SF3pgfw/idc2SgYaD7gAAAAASUVORK5CYII=
©xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!