-
Notifications
You must be signed in to change notification settings - Fork 76
/
index.html
339 lines (280 loc) · 9.65 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<!doctype html>
<meta charset="utf-8">
<title>Dimensions Browser Extension</title>
<link rel="stylesheet" href="css/style.css">
<link rel="shortcut icon" href="favicon.ico">
<link rel="chrome-webstore-item" href="https://chrome.google.com/webstore/detail/baocaagndhipibgklemoalmkljaimfdj">
<header>
<h1><img src="extension/images/icon48.png" srcset="extension/images/icon128.png 2x" width="48" height="48"><span class="h1">Dimensions</span></h1>
<h2><span class="h2">Chrome Browser Extension</span></h2>
<a href="https://chrome.google.com/webstore/detail/dimensions/baocaagndhipibgklemoalmkljaimfdj" class="button invisible"><img src="img/chrome-icon.png" srcset="img/chrome-icon@2x.png" width="20" height="20"> Install from Chrome Webstore</a>
</header>
<div class="main container">
<div class="columns">
<div class="column">
<img src="img/search.svg">
<h2>Images & HTML Elements</h2>
<p>Measure between the following elements: images, input-fields, buttons, videos, gifs, text, icons. You can measure everything you see in the browser.</p>
</div>
<div class="column">
<img src="img/files.svg">
<h2>Mockups</h2>
<p>Your designer handed you mockups as PNGs or JPEGs? Just drop them into Chrome, activate Dimensions and start measuring.</p>
</div>
<div class="column">
<img src="img/keyboard-shortcut.svg">
<h2>Keyboard Shortcut</h2>
<p>For the best experience set a keyboard shortcut in the chrome setting at the end of the extensions list to quickly enable and disable Dimensions.</p>
</div>
<div class="column">
<img src="img/area.svg">
<h2>Area Boundaries</h2>
<p>Wanna get the radius of a circle? Is text standing in your way? Press <kbd>Alt</kbd> to measure the dimensions of a connected area. Try it out!</p>
</div>
<div class="column">
<img src="img/open-source.svg">
<h2>Open Source</h2>
<p>Dimensions is Open Source and hosted on Github. Want to port it to Firefox? Go ahead and fork it. Pull requests are welcomed!</p>
</div>
</div>
</div>
<footer>
<div class="container">
<div>
Made in Berlin in 2014-2016 by Felix Niklas <a href="http://www.twitter.com/mrflix" target="_blank">@mrflix</a>
</div>
<ul>
<li><a href="https://github.com/mrflix/dimensions">Github Project</a>
<li><a href="https://github.com/mrflix/dimensions/issues">Issues</a>
<li><a href="https://chrome.google.com/webstore/detail/dimensions/baocaagndhipibgklemoalmkljaimfdj">Chrome Webstore</a>
</ul>
</div>
</footer>
<script type="text/javascript">
var _paq = _paq || [];
_paq.push(["trackPageView"]);
_paq.push(["enableLinkTracking"]);
(function() {
var u=(("https:" == document.location.protocol) ? "https" : "http") + "://felixniklas.de/piwik/";
_paq.push(["setTrackerUrl", u+"piwik.php"]);
_paq.push(["setSiteId", "1"]);
var d=document, g=d.createElement("script"), s=d.getElementsByTagName("script")[0]; g.type="text/javascript";
g.defer=true; g.async=true; g.src=u+"piwik.js"; s.parentNode.insertBefore(g,s);
})();
</script>
<script>
var favicon = document.querySelector('link[rel="shortcut icon"]');
var holder = document.querySelector('header');
var threshold = 6;
var data, width, height;
var is_firefox = /firefox/i.test(navigator.userAgent);
var virgin = true;
var worker;
var isTouch = 'ontouchstart' in window;
var canvas, ctx;
var inputX, inputY;
var altKeyWasPressed = false;
if(isTouch)
holder.classList.add('touch');
var images = [];
var loadedImages;
// prefetch header images for the Dimensions canvas
function render(){
loadedImages = 0;
var sources = ['img/header.svg'];
var imgs = holder.querySelectorAll('img:not(.invisible)');
for(var i=0; i<imgs.length; i++)
sources.push(imgs[i].src);
sources.forEach(function(src, i){
var image = new Image();
image.onload = function(){
images[i] = this;
if(++loadedImages == sources.length)
drawCanvas();
};
image.src = src;
});
}
function storeInputPos(event){
event.preventDefault();
if(virgin){
virgin = false;
favicon.href = 'favicon_active.ico';
}
if(event.touches){
inputX = event.touches[0].pageX;
inputY = event.touches[0].pageY;
} else {
inputX = event.pageX;
inputY = event.pageY;
}
sendToWorker(event);
}
function sendToWorker(event){
worker.postMessage({
type: event.altKey ? 'area' : 'position',
x: inputX,
y: inputY
});
}
function removeDimensions(){
var dimensions = holder.querySelector('.dimensions');
dimensions && holder.removeChild(dimensions);
}
//
// showDistances
// =============
//
// renders the visualisation of the measured distances
//
function showDistances(distances){
removeDimensions();
if(!distances)
return;
var measureWidth = distances.left + distances.right;
var measureHeight = distances.top + distances.bottom;
var textWidth = 6 + 7 * measureWidth.toString().length;
var textHeight = 20;
var xTextStart = 14;
var yTextStart = 9;
var dimensions = document.createElement('div');
dimensions.className = 'dimensions';
dimensions.style.left = distances.x + "px";
dimensions.style.top = distances.y + "px";
var xAxis = document.createElement('div');
xAxis.className = 'x axis';
xAxis.style.left = -distances.left + "px";
xAxis.style.width = (distances.left + distances.right) + "px";
dimensions.appendChild(xAxis);
var yAxis = document.createElement('div');
yAxis.className = 'y axis';
yAxis.style.top = -distances.top + "px";
yAxis.style.height = (distances.top + distances.bottom) + "px";
dimensions.appendChild(yAxis);
var tooltip = document.createElement('div');
tooltip.className = 'tooltip';
// tooltip.style.width = textWidth + "px";
tooltip.textContent = (measureWidth+1) +" x "+ (measureHeight+1);
dimensions.appendChild(tooltip);
var tooltipY = document.createElement('div');
tooltipY.className = 'tooltip y';
tooltipY.textContent = measureHeight;
holder.appendChild(dimensions);
}
//
// drawCanvas
// ----------
//
// responsible to draw the objects onto the canvas
//
function drawCanvas(){
canvas = document.createElement('canvas');
ctx = canvas.getContext('2d');
// adjust the canvas size to the image size
width = canvas.width = holder.clientWidth;
height = canvas.height = holder.clientHeight;
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, width, height);
// draw the background
var background = images.shift();
var sx = Math.round((background.width - width)/2);
var sy = Math.round((background.height - height)/2);
ctx.drawImage(background, sx, sy, width, height, 0, 0, width, height);
// draw images
for(var index in images){
var src = images[index].src.slice( images[index].src.lastIndexOf('/') + 1, images[index].src.lastIndexOf('.') );
var el = holder.querySelector('img[src*='+ src +']');
var pos = el.getBoundingClientRect();
ctx.drawImage(images[index], pos.left, scrollY+pos.top, el.clientWidth, el.clientHeight);
}
// draw install button
var installBtn = holder.querySelector('.button');
var btnPos = installBtn.getBoundingClientRect();
ctx.fillStyle = '#444';
roundedRect(ctx, btnPos.left, scrollY+btnPos.top, installBtn.offsetWidth, installBtn.offsetHeight, 3);
// place the headlines
['.h1', '.h2'].forEach(function(selector){
var el = holder.querySelector(selector);
var pos = el.getBoundingClientRect();
var style = getComputedStyle(el);
ctx.font = style.fontWeight +" "+ style.fontSize +" "+ style.fontFamily;
ctx.textBaseline = 'top';
ctx.fillStyle = style.color;
var top = scrollY + pos.top;
if(is_firefox)
top += 0.4*parseInt(style.fontSize);
ctx.fillText(el.textContent, pos.left, top);
});
// read out the image data from the canvas
var imgData = ctx.getImageData(0, 0, width, height).data;
worker = new Worker("js/worker.js");
worker.onmessage = receiveMessage;
worker.postMessage({
type: 'imgData',
imgData: imgData.buffer,
width: width,
height: height,
threshold: threshold
}, [imgData.buffer]);
holder.onmousemove = storeInputPos;
holder.ontouchmove = storeInputPos;
window.addEventListener('keydown', detectAltKeyPress);
window.addEventListener('keyup', detectAltKeyRelease);
holder.onmouseleave = removeDimensions;
// worker.postMessage({ type: 'debug' });
worker.postMessage({
type: 'position',
x: holder.clientWidth/2 - 210,
y: holder.clientHeight/2 - 150
});
}
function detectAltKeyPress(event){
if(event.altKey){
altKeyWasPressed = true;
sendToWorker(event);
}
}
function detectAltKeyRelease(event){
if(altKeyWasPressed){
altKeyWasPressed = false;
sendToWorker(event);
}
}
function receiveMessage(event){
switch(event.data.type){
case "distances":
showDistances(event.data.distances);
break;
case "debug":
debugCanvas(event.data.data);
break;
}
}
function debugCanvas(binaries){
var imgData = ctx.createImageData(width, height);
for(var i=0, x=0, l=imgData.data.length; i<l; i+=4, x++){
var gray = binaries[x];
imgData.data[i] = gray;
imgData.data[i+1] = gray;
imgData.data[i+2] = gray;
imgData.data[i+3] = 255;
}
ctx.putImageData(imgData, 0, 0);
holder.appendChild(canvas);
}
// by visionmedia (TJ Holowaychuk)
// https://github.com/component/rounded-rect/blob/master/index.js
function roundedRect(ctx, x, y, w, h, r) {
if (w < 2 * r) r = w / 2;
if (h < 2 * r) r = h / 2;
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + w, y, x + w, y + h, r);
ctx.arcTo(x + w, y + h, x, y + h, r);
ctx.arcTo(x, y + h, x, y, r);
ctx.arcTo(x, y, x + w, y, r);
ctx.fill();
};
render();
window.onresize = render;
</script>