forked from karbassi/imageinfo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrc.js
More file actions
161 lines (130 loc) · 3.68 KB
/
src.js
File metadata and controls
161 lines (130 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const $canvas = document.getElementById('canvas');
const context = $canvas.getContext('2d');
const $hiddenImage = document.createElement('img');
const $hex = document.querySelector('.hex');
const $rgb = document.querySelector('.rgb');
const $color = document.querySelector('.color');
const $x = document.querySelector('.x');
const $y = document.querySelector('.y');
const $error = document.querySelector('.error');
let pos = {
x: 0,
y: 0
};
let isLocked = false;
function rgbToHex(r, g, b) {
return '#' + ((r << 16) | (g << 8) | b).toString(16);
}
function makeCursor(color) {
const $cursor = document.createElement('canvas');
const ctx = $cursor.getContext('2d');
$cursor.width = 41;
$cursor.height = 41;
// Crosshair
ctx.beginPath();
ctx.moveTo(0, 6);
ctx.lineTo(12, 6);
ctx.moveTo(6, 0);
ctx.lineTo(6, 12);
ctx.lineWidth = 1;
ctx.lineCap = 'butt';
ctx.shadowColor = '#FFF';
ctx.shadowBlur = 2;
ctx.strokeStyle = '#000';
ctx.stroke();
// Color circle
ctx.beginPath();
ctx.arc(25, 25, 14, 0, 2 * Math.PI, false);
ctx.lineWidth = 2;
ctx.strokeStyle = '#000';
ctx.stroke();
ctx.fillStyle = color;
ctx.fill();
$canvas.style.cursor = 'crosshair';
$canvas.style.cursor = 'url(' + $cursor.toDataURL() + ') 6 6, crosshair';
}
function loadImage(file) {
if (typeof (file) === 'string' && file.indexOf('http') === 0) {
fetch(file)
.then(response => response.blob())
.then(blob => {
document.body.className = 'active';
$hiddenImage.src = URL.createObjectURL(blob);
})
.catch(() => {
$error.textContent = 'This file can\'t be loaded. Try another URL.';
});
return;
}
if (typeof FileReader !== 'undefined' && file.type.indexOf('image') !== -1) {
const reader = new FileReader();
reader.onload = event => {
$hiddenImage.src = event.target.result;
};
reader.readAsDataURL(file);
}
}
$hiddenImage.addEventListener('load', () => {
$canvas.width = $hiddenImage.width;
$canvas.height = $hiddenImage.height;
context.clearRect(0, 0, $canvas.width, $canvas.height);
context.drawImage($hiddenImage, 0, 0);
}, false);
$canvas.addEventListener('mousemove', function (event) {
if (isLocked) {
return;
}
pos = {
x: event.offsetX,
y: event.offsetY
};
if (pos.x > $canvas.width || pos.y > $canvas.height || pos.x < 1 || pos.y < 1) {
return;
}
const ctx = this.getContext('2d');
const p = ctx.getImageData(pos.x, pos.y, 1, 1).data;
const hex = rgbToHex(p[0], p[1], p[2]);
const rgb = 'rgb(' + p[0] + ',' + p[1] + ',' + p[2] + ')';
$hex.textContent = hex.toUpperCase();
$rgb.textContent = rgb;
$x.textContent = pos.x;
$y.textContent = pos.y;
$color.style.backgroundColor = hex;
makeCursor(hex);
});
$canvas.addEventListener('click', event => {
if (!isLocked) {
console.log(pos.x, pos.y);
}
isLocked = !isLocked;
event.preventDefault();
});
document.addEventListener('drop', event => {
document.body.className = 'active';
const files = event.dataTransfer.files;
if (files.length > 0) {
loadImage(files[0]);
}
event.preventDefault();
});
document.addEventListener('dragover', event => {
document.body.className = 'hover';
event.preventDefault();
});
document.addEventListener('dragenter', event => {
document.body.className = 'hover';
event.preventDefault();
});
document.addEventListener('dragleave', event => {
document.body.className = '';
event.preventDefault();
});
document.addEventListener('dragend', event => {
document.body.className = '';
event.preventDefault();
});
const searchParams = new URLSearchParams(window.location.search); // ?src=123
const src = searchParams.get('src');
if (src) {
loadImage(src);
}