-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
154 lines (137 loc) · 3.91 KB
/
index.html
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
<!doctype html>
<html>
<head>
<title></title>
<meta lang="zh">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<style type="text/css">
header{
margin-bottom: 20px;
}
header span{
display: inline-block;
vertical-align: middle;
}
header #color{
width: 100px;
height: 100px;
margin-right: 50px;
}
header #filedrag{
width: 400px;
height: 100px;
border: 4px dotted gray;
text-align: center;
line-height: 100px;
color: gray;
font-size: 30px;
}
#collector{
width: 600px;
height: 50px;
}
#collector .item{
display: inline-block;
margin-right: 5px;
width: 50px;
height: 50px;
}
#collector .item span{
background-color: #fff;
}
.hover {
background-color: gray;
}
</style>
</head>
<body>
<header>
<span id="color"></span>
<span id="filedrag">拖拽上传图片</span>
</header>
<canvas id="myCanvas"></canvas>
<div id="collector"></div>
<div style="display:none"><img id="image" src="./img/photo1.jpg"></div>
<script type="text/javascript">
var color = $id("color");
var canvas = $id("myCanvas");
var context = canvas.getContext("2d");
var image = $id('image');
var collectors = $id('collector');
var filedrag = $id('filedrag');
function $id(id) {
return document.getElementById(id);
}
function fileDragHover(e) {
e.stopPropagation();
e.preventDefault();
e.target.className = (e.type == 'dragover' ? "hover" : "");
}
function fileSelectHandler(e) {
fileDragHover(e);
var files = e.target.files || e.dataTransfer.files;
for(var i = 0, f; f = files[i]; i++) {
parseFile(f);
}
}
function parseFile(f) {
var reader = new FileReader();
var _image;
reader.onload = (function (file) {
return function(e) {
_image = new Image();
_image.onload = function () {
context.drawImage(_image, 0, 0, _image.width, _image.height);
}
_image.src = e.target.result;
// var span = document.createElement('span');
// span.innerHTML = ['<img class="thumb" src="', e.target.result,
// '" title="', escape(file.name), '"/>'].join('');
// filedrag.appendChild(span);
}
})(f);
reader.readAsDataURL(f);
}
window.onload = function() {
console.log(image.width);
console.log(image.height);
canvas.width = image.width;
canvas.height = image.height;
context.drawImage(image, 0,0,image.width,image.height);
filedrag.addEventListener('dragover', fileDragHover, false);
filedrag.addEventListener('dragleave', fileDragHover, false);
filedrag.addEventListener('drop', fileSelectHandler, false);
canvas.onmousemove = function(e) {
// not so sure about these... might need to offset them or so
var x = e.offsetX;
var y = e.offsetY;
// set color now
var canvasColor = context.getImageData(x, y, 1, 1).data; // rgba e [0,255]
var r = canvasColor[0];
var g = canvasColor[1];
var b = canvasColor[2];
color.style.backgroundColor = 'rgb(' + r + ',' + g + ',' + b + ')';
}
canvas.onclick = function(e) {
var x = e.offsetX;
var y = e.offsetY;
var canvasColor = context.getImageData(x, y, 1, 1).data; // rgba e [0,255]
var r = canvasColor[0];
var g = canvasColor[1];
var b = canvasColor[2];
var item = document.createElement('span');
item.className = 'item';
item.style.fontSize = '12px';
item.style.backgroundColor = 'rgb(' + r + ',' + g + ',' + b + ')';
collectors.appendChild(item);
var colorText = document.createElement('span');
colorText.innerText = rgbToHex(r,g,b);
item.appendChild(colorText);
}
}
function rgbToHex(r,g,b) {
return '#' + parseInt(r, 10).toString(16) + parseInt(g, 10).toString(16) + parseInt(b, 10).toString(16);
}
</script>
</body>
</html>