@kedao/finder
Version:
Media manager for Kedao Editor.
41 lines • 2 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
export const compressImage = (url, width = 1280, height = 800) => __awaiter(void 0, void 0, void 0, function* () {
return yield new Promise((resolve, reject) => {
const image = new Image();
image.src = url;
image.onerror = function (error) {
reject(error);
};
image.onload = function () {
try {
const compressCanvas = document.createElement('canvas');
const scale = this.width > width || this.height > height
? this.width > this.height
? width / this.width
: height / this.height
: 1;
compressCanvas.width = this.width * scale;
compressCanvas.height = this.height * scale;
const canvasContext = compressCanvas.getContext('2d');
canvasContext.drawImage(this, 0, 0, compressCanvas.width, compressCanvas.height);
resolve({
url: compressCanvas.toDataURL('image/png', 1),
width: compressCanvas.width,
height: compressCanvas.height
});
}
catch (error) {
reject(error);
}
};
});
});
//# sourceMappingURL=image.js.map