MCPcopy
hub / github.com/zxlie/FeHelper / loadImage

Function loadImage

apps/qr-code/content-script.js:5–39  ·  view source on GitHub ↗
(src)

Source from the content-addressed store, hash-verified

3 let decode = function (imgUrl) {
4
5 function loadImage(src) {
6 return new Promise(resolve => {
7 let image = new Image();
8 image.setAttribute('crossOrigin', 'Anonymous');
9 image.src = src;
10 image.onload = function () {
11 let canvas;
12 try {
13 let width = this.naturalWidth;
14 let height = this.naturalHeight;
15 canvas = document.createElement('canvas');
16 canvas.style.cssText = 'position:absolute;top:-10000px;left:-10000px';
17 document.body.appendChild(canvas);
18 canvas.setAttribute('id', 'qr-canvas');
19 canvas.height = height + 100;
20 canvas.width = width + 100;
21 let context = canvas.getContext('2d');
22 context.fillStyle = 'rgb(255,255,255)';
23 context.fillRect(0, 0, canvas.width, canvas.height);
24 context.imageSmoothingEnabled = false;
25 context.drawImage(image, 0, 0, width, height, 50, 50, width, height);
26 resolve(canvas.toDataURL());
27 } catch (err) {
28 resolve(src);
29 } finally {
30 if (canvas && canvas.parentNode) {
31 canvas.parentNode.removeChild(canvas);
32 }
33 }
34 };
35 image.onerror = function () {
36 resolve(src);
37 };
38 });
39 }
40
41 loadImage(imgUrl).then(dataUrl => {
42

Callers 1

decodeFunction · 0.85

Calls 2

resolveFunction · 0.50
getContextMethod · 0.45

Tested by

no test coverage detected